Merge "Debug tool for openconfig proprietary extensions"
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / topology / TapiPortMappingListener.java
index d824693c7a56b4ec886e312c1b1cb9af7c4232d5..a25858a702cf843ccd76d8c92ed7ba18af8de8f6 100644 (file)
@@ -7,14 +7,14 @@
  */
 package org.opendaylight.transportpce.tapi.topology;
 
-import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
 import org.opendaylight.mdsal.binding.api.DataTreeModification;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mapping.Mapping;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.mapping.MappingKey;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210927.network.Nodes;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.mapping.MappingKey;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev231221.network.Nodes;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -29,13 +29,13 @@ public class TapiPortMappingListener implements DataTreeChangeListener<Nodes> {
     }
 
     @Override
-    public void onDataTreeChanged(@NonNull Collection<DataTreeModification<Nodes>> changes) {
+    public void onDataTreeChanged(@NonNull List<DataTreeModification<Nodes>> changes) {
         for (DataTreeModification<Nodes> change : changes) {
             LOG.debug("TAPI module: Change in Node = {}", change.getRootNode());
             // Data before needs to be not null
-            if (change.getRootNode().getDataAfter() != null && change.getRootNode().getDataBefore() != null) {
-                Nodes nodesAft = change.getRootNode().getDataAfter();
-                Nodes nodesBef = change.getRootNode().getDataBefore();
+            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();
@@ -43,16 +43,34 @@ public class TapiPortMappingListener implements DataTreeChangeListener<Nodes> {
                 Map<MappingKey, Mapping> mappingBef = nodesBef.getMapping();
                 LOG.info("Change in node {} with OR version = {}", nodeId,
                     nodesAft.getNodeInfo().getOpenroadmVersion().getName());
-                if (mappingAft != null && mappingBef == null) {
+                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);
                 } else {
-                    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);
+                    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());
+                        }
+                    }
                 }
+            } 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);
             }
         }
     }