From af2f4a8296e6e825b7129eda5f8016ab755afbb3 Mon Sep 17 00:00:00 2001 From: "tobias.pobocik" Date: Tue, 12 Sep 2023 12:40:56 +0200 Subject: [PATCH] Refactor code to improve readability and maintainability This commit was made as a response to bugs reported by sonarcloud. https://sonarcloud.io/project/issues?resolved=false&sinceLeakPeriod=true&types=BUG&id=PantheonTechnologies_lighty-core&open=AYpqn0fygwnrORD6WXZp JIRA: LIGHTY-235 Signed-off-by: tobias.pobocik --- .../springboot/rest/NetconfDeviceRestService.java | 6 ++---- .../springboot/rest/TopologyRestService.java | 14 +++++++------- .../codecs/GetResponseToNormalizedNodeCodec.java | 7 ++----- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lighty-examples/lighty-controller-springboot-netconf/src/main/java/io/lighty/core/controller/springboot/rest/NetconfDeviceRestService.java b/lighty-examples/lighty-controller-springboot-netconf/src/main/java/io/lighty/core/controller/springboot/rest/NetconfDeviceRestService.java index 4bf8982bdd..d5ad389cd1 100644 --- a/lighty-examples/lighty-controller-springboot-netconf/src/main/java/io/lighty/core/controller/springboot/rest/NetconfDeviceRestService.java +++ b/lighty-examples/lighty-controller-springboot-netconf/src/main/java/io/lighty/core/controller/springboot/rest/NetconfDeviceRestService.java @@ -87,10 +87,8 @@ public ResponseEntity getNetconfDevicesIds(Authentication authentication) throws .get(TIMEOUT, TimeUnit.SECONDS); } - if (netconfTopoOptional.isPresent()) { - return ResponseEntity.ok(getNetconfDevices(netconfTopoOptional.get())); - } - return ResponseEntity.ok(Collections.emptyList()); + return netconfTopoOptional.isPresent() ? ResponseEntity.ok(getNetconfDevices(netconfTopoOptional.get())) : + ResponseEntity.ok(Collections.emptyList()); } private List getNetconfDevices(final Topology netconfTopology) diff --git a/lighty-examples/lighty-controller-springboot-netconf/src/main/java/io/lighty/core/controller/springboot/rest/TopologyRestService.java b/lighty-examples/lighty-controller-springboot-netconf/src/main/java/io/lighty/core/controller/springboot/rest/TopologyRestService.java index 2a6d007ab3..effd94ae1a 100644 --- a/lighty-examples/lighty-controller-springboot-netconf/src/main/java/io/lighty/core/controller/springboot/rest/TopologyRestService.java +++ b/lighty-examples/lighty-controller-springboot-netconf/src/main/java/io/lighty/core/controller/springboot/rest/TopologyRestService.java @@ -64,14 +64,14 @@ public ResponseEntity getAllTopologyIdsOperational(Authentication authentication final Optional readData = tx.read(LogicalDatastoreType.OPERATIONAL, iid).get(TIMEOUT, TimeUnit.SECONDS); - if (readData.isPresent() && readData.get().getTopology() != null) { - final List topology = readData.get().getTopology().values().stream() + return ResponseEntity.ok( + readData.map(NetworkTopology::getTopology) + .map(topologyMap -> topologyMap.values() + .stream() .map(topology1 -> topology1.getTopologyId().getValue()) - .collect(Collectors.toList()); - return ResponseEntity.ok(topology); - } else { - return ResponseEntity.ok(Collections.emptyList()); - } + .collect(Collectors.toList())) + .orElse(Collections.emptyList()) + ); } } diff --git a/lighty-modules/lighty-gnmi/lighty-gnmi-sb/src/main/java/io/lighty/gnmi/southbound/mountpoint/codecs/GetResponseToNormalizedNodeCodec.java b/lighty-modules/lighty-gnmi/lighty-gnmi-sb/src/main/java/io/lighty/gnmi/southbound/mountpoint/codecs/GetResponseToNormalizedNodeCodec.java index b29bb86f48..db23f96f97 100644 --- a/lighty-modules/lighty-gnmi/lighty-gnmi-sb/src/main/java/io/lighty/gnmi/southbound/mountpoint/codecs/GetResponseToNormalizedNodeCodec.java +++ b/lighty-modules/lighty-gnmi/lighty-gnmi-sb/src/main/java/io/lighty/gnmi/southbound/mountpoint/codecs/GetResponseToNormalizedNodeCodec.java @@ -56,16 +56,13 @@ public GetResponseToNormalizedNodeCodec(final SchemaContextProvider schemaContex public Optional apply(Gnmi.GetResponse response, YangInstanceIdentifier identifier) throws GnmiCodecException { for (Gnmi.Notification notification : response.getNotificationList()) { - for (Gnmi.Update update : notification.getUpdateList()) { - // Json to NormalizedNode - final NormalizedNode codecResult = updateToNormalizedNode(update, identifier); /* If the serialized normalized node is of type AugmentationNode we need to return the child because the AugmentationNode has no QName so later post processing (for example restconf) can correctly deal with that. */ - return Optional.of(codecResult); - } + return Optional.of(updateToNormalizedNode( + notification.getUpdateList().get(0), identifier)); } return Optional.empty(); }