Refactor TAPI topology TapiPortMappingListener 12/111112/2
authorguillaume.lambert <guillaume.lambert@orange.com>
Wed, 27 Mar 2024 15:33:22 +0000 (16:33 +0100)
committerGuillaume Lambert <guillaume.lambert@orange.com>
Wed, 27 Mar 2024 15:34:31 +0000 (15:34 +0000)
Signed-off-by: guillaume.lambert <guillaume.lambert@orange.com>
Change-Id: I3c0937ed3552ba3192644fc490730c18c334a64a

tapi/src/main/java/org/opendaylight/transportpce/tapi/topology/TapiPortMappingListener.java

index a25858a702cf843ccd76d8c92ed7ba18af8de8f6..e90efcdd3144f989fd8c135ed6dc8b7d9157140d 100644 (file)
@@ -31,46 +31,50 @@ public class TapiPortMappingListener implements DataTreeChangeListener<Nodes> {
     @Override
     public void onDataTreeChanged(@NonNull List<DataTreeModification<Nodes>> changes) {
         for (DataTreeModification<Nodes> change : changes) {
     @Override
     public void onDataTreeChanged(@NonNull List<DataTreeModification<Nodes>> changes) {
         for (DataTreeModification<Nodes> change : changes) {
-            LOG.debug("TAPI module: Change in Node = {}", change.getRootNode());
+            var rootNode = change.getRootNode();
+            LOG.debug("TAPI module: Change in Node = {}", rootNode);
             // Data before needs to be not null
             // Data before needs to be not null
-            if (change.getRootNode().dataAfter() != null && change.getRootNode().dataBefore() != null) {
-                Nodes nodesAft = change.getRootNode().dataAfter();
-                Nodes nodesBef = change.getRootNode().dataBefore();
-                // TODO -> need to filter out the ones that are not after creation.
-                //  (Mapping before = null & Mapping after != null) is the rule for a first time connected device
-                String nodeId = nodesAft.getNodeId();
-                Map<MappingKey, Mapping> mappingAft = nodesAft.getMapping();
-                Map<MappingKey, Mapping> mappingBef = nodesBef.getMapping();
-                LOG.info("Change in node {} with OR version = {}", nodeId,
-                    nodesAft.getNodeInfo().getOpenroadmVersion().getName());
-                if (mappingAft == null) {
-                    LOG.warn("Mapping already existed in the datastore, which means that node {} already existed "
-                        + "in TAPI topology. The action to take will be different", nodeId);
+            Nodes nodesAft = rootNode.dataAfter();
+            if (nodesAft == null) {
+                continue;
+            }
+            Nodes nodesBef = rootNode.dataBefore();
+            if (nodesBef == null) {
+                this.tapiNetworkModelService.createTapiNode(
+                    nodesAft.getNodeId(), nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
+                continue;
+            }
+            // TODO -> need to filter out the ones that are not after creation.
+            //  (Mapping before = null & Mapping after != null) is the rule for a first time connected device
+            String nodeId = nodesAft.getNodeId();
+            Map<MappingKey, Mapping> mappingAft = nodesAft.getMapping();
+            Map<MappingKey, Mapping> mappingBef = nodesBef.getMapping();
+            LOG.info("Change in node {} with OR version = {}",
+                nodeId, nodesAft.getNodeInfo().getOpenroadmVersion().getName());
+            //TODO avoid long message and concatenation in following LOG messages
+            if (mappingAft == null) {
+                LOG.warn("Mapping already existed in the datastore, which means that node {} already existed "
+                    + "in TAPI topology. The action to take will be different", nodeId);
+                continue;
+            }
+            if (mappingBef == null) {
+                LOG.info("New mapping for node {} = {}", nodeId, mappingAft);
+                LOG.info("As the mapping is now created for the first time, "
+                    + "we can proceed with the creation of the node {} in the TAPI topology", nodeId);
+                this.tapiNetworkModelService.createTapiNode(
+                    nodeId, nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
+                continue;
+            }
+            for (Map.Entry<MappingKey, Mapping> entry : mappingAft.entrySet()) {
+                Mapping oldMapping = mappingBef.get(entry.getKey());
+                Mapping newMapping = mappingAft.get(entry.getKey());
+                if (oldMapping == null || newMapping == null) {
                     continue;
                 }
                     continue;
                 }
-                if (mappingBef == null) {
-                    LOG.info("New mapping for node {} = {}", nodeId, mappingAft);
-                    LOG.info("As the mapping is now created for the first time, "
-                        + "we can proceed with the creation of the node {} in the TAPI topology", nodeId);
-                    this.tapiNetworkModelService.createTapiNode(nodeId,
-                        nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
-                } else {
-                    for (Map.Entry<MappingKey, Mapping> entry : mappingAft.entrySet()) {
-                        Mapping oldMapping = mappingBef.get(entry.getKey());
-                        Mapping newMapping = mappingAft.get(entry.getKey());
-                        if (oldMapping == null || newMapping == null) {
-                            continue;
-                        }
-                        if (!oldMapping.getPortAdminState().equals(newMapping.getPortAdminState())
-                                || !oldMapping.getPortOperState().equals(newMapping.getPortOperState())) {
-                            this.tapiNetworkModelService.updateTapiTopology(nodeId, entry.getValue());
-                        }
-                    }
+                if (!oldMapping.getPortAdminState().equals(newMapping.getPortAdminState())
+                        || !oldMapping.getPortOperState().equals(newMapping.getPortOperState())) {
+                    this.tapiNetworkModelService.updateTapiTopology(nodeId, entry.getValue());
                 }
                 }
-            } else if (change.getRootNode().getDataAfter() != null && change.getRootNode().getDataBefore() == null) {
-                Nodes nodesAft = change.getRootNode().getDataAfter();
-                this.tapiNetworkModelService.createTapiNode(nodesAft.getNodeId(),
-                    nodesAft.getNodeInfo().getOpenroadmVersion().getIntValue(), nodesAft);
             }
         }
     }
             }
         }
     }