Update node-tp and link states on topologies
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / service / NetworkModelServiceImpl.java
index ce7a69884b0e3c37b1ec27ee5ae24798415359cd..66e5460d1936f8150104e17c68b8ae29da813212 100644 (file)
@@ -18,7 +18,9 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.transportpce.common.InstanceIdentifiers;
 import org.opendaylight.transportpce.common.NetworkUtils;
 import org.opendaylight.transportpce.common.mapping.PortMapping;
 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
@@ -29,8 +31,10 @@ import org.opendaylight.transportpce.networkmodel.util.LinkIdUtil;
 import org.opendaylight.transportpce.networkmodel.util.OpenRoadmNetwork;
 import org.opendaylight.transportpce.networkmodel.util.OpenRoadmOtnTopology;
 import org.opendaylight.transportpce.networkmodel.util.OpenRoadmTopology;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.NodeInfo;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev201012.network.nodes.NodeInfo.OpenroadmVersion;
+import org.opendaylight.transportpce.networkmodel.util.TopologyUtils;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.OpenroadmNodeVersion;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.network.nodes.NodeInfo;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.types.rev191129.NodeTypes;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev200529.Link1;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.network.topology.rev200529.TerminationPoint1;
@@ -57,7 +61,6 @@ import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 public class NetworkModelServiceImpl implements NetworkModelService {
 
     private static final Logger LOG = LoggerFactory.getLogger(NetworkModelServiceImpl.class);
@@ -69,7 +72,8 @@ public class NetworkModelServiceImpl implements NetworkModelService {
     private Map<String, TopologyShard> otnTopologyShardMountedDevice;
 
     public NetworkModelServiceImpl(final NetworkTransactionService networkTransactionService,
-        final R2RLinkDiscovery linkDiscovery, PortMapping portMapping) {
+            final R2RLinkDiscovery linkDiscovery, PortMapping portMapping,
+            final NotificationPublishService notificationPublishService) {
 
         this.networkTransactionService = networkTransactionService;
         this.linkDiscovery = linkDiscovery;
@@ -95,9 +99,6 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                 return;
             }
             NodeInfo nodeInfo = portMapping.getNode(nodeId).getNodeInfo();
-            if (nodeInfo.getNodeType().getIntValue() == 1) {
-                this.linkDiscovery.readLLDP(new NodeId(nodeId), openRoadmVersion);
-            }
             // node creation in clli-network
             Node clliNode = ClliNetwork.createNode(nodeId, nodeInfo);
             InstanceIdentifier<Node> iiClliNode = InstanceIdentifier.builder(Networks.class)
@@ -145,12 +146,15 @@ public class NetworkModelServiceImpl implements NetworkModelService {
             } else {
                 LOG.error("Unable to create openroadm-topology shard for node {}!", nodeId);
             }
-
             // nodes/links creation in otn-topology
             if (nodeInfo.getNodeType().getIntValue() == 2 && (nodeInfo.getOpenroadmVersion().getIntValue() != 1)) {
                 createOpenRoadmOtnNode(nodeId);
             }
             networkTransactionService.commit().get();
+            // neighbour links through LLDP
+            if (nodeInfo.getNodeType().getIntValue() == 1) {
+                this.linkDiscovery.readLLDP(new NodeId(nodeId), openRoadmVersion);
+            }
             LOG.info("all nodes and links created");
         } catch (InterruptedException | ExecutionException e) {
             LOG.error("ERROR: ", e);
@@ -209,7 +213,7 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                 LOG.warn("TopologyShard for node '{}' is not present", nodeId);
             }
             @Nullable
-            OpenroadmVersion deviceVersion = this.portMapping.getNode(nodeId).getNodeInfo().getOpenroadmVersion();
+            OpenroadmNodeVersion deviceVersion = this.portMapping.getNode(nodeId).getNodeInfo().getOpenroadmVersion();
             @Nullable
             NodeTypes nodeType = this.portMapping.getNode(nodeId).getNodeInfo().getNodeType();
             if (nodeType.getIntValue() == 2 && deviceVersion.getIntValue() != 1) {
@@ -248,6 +252,101 @@ public class NetworkModelServiceImpl implements NetworkModelService {
         }
     }
 
+    @Override
+    public void updateOpenRoadmTopologies(String nodeId, Mapping mapping) {
+        LOG.info("update OpenRoadm topologies after change update from: {} ", nodeId);
+        Network openroadmTopology = null;
+        Network otnTopology = null;
+        Map<LinkKey, Link> openroadmTopologyLinks = null;
+        Map<LinkKey, Link> otnTopologyLinks = null;
+        try {
+            openroadmTopology = this.networkTransactionService
+                .read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifiers.OVERLAY_NETWORK_II)
+                .get().get();
+            if (openroadmTopology.augmentation(Network1.class) != null) {
+                openroadmTopologyLinks = openroadmTopology.augmentation(Network1.class).getLink();
+            }
+            otnTopology = this.networkTransactionService
+                .read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifiers.OTN_NETWORK_II)
+                .get().get();
+            if (otnTopology.augmentation(Network1.class) != null) {
+                otnTopologyLinks = otnTopology.augmentation(Network1.class).getLink();
+            }
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.error("Error when trying to update node : {}", nodeId, e);
+        }
+        if (openroadmTopology == null || otnTopology == null) {
+            LOG.warn("Error getting topologies from datastore");
+            return;
+        }
+        String abstractNodeid = String.join("-", nodeId, mapping.getLogicalConnectionPoint().split("-")[0]);
+        // nodes/links update in openroadm-topology
+        if (openroadmTopology.getNode() != null) {
+            TopologyShard topologyShard = TopologyUtils.updateTopologyShard(abstractNodeid, mapping,
+                openroadmTopology.getNode(), openroadmTopologyLinks);
+            if (topologyShard.getLinks() != null) {
+                for (Link link : topologyShard.getLinks()) {
+                    LOG.info("updating links {} in {}", link.getLinkId().getValue(),
+                        NetworkUtils.OVERLAY_NETWORK_ID);
+                    InstanceIdentifier<Link> iiTopologyLink = InstanceIdentifier.builder(Networks.class)
+                        .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID)))
+                        .augmentation(Network1.class)
+                        .child(Link.class, link.key())
+                        .build();
+                    networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiTopologyLink, link);
+                }
+            }
+            if (topologyShard.getTps() != null) {
+                for (TerminationPoint tp : topologyShard.getTps()) {
+                    LOG.info("updating TP {} in openroadm-topology", tp.getTpId().getValue());
+                    InstanceIdentifier<TerminationPoint> iiTopologyTp = InstanceIdentifier.builder(Networks.class)
+                        .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID)))
+                        .child(Node.class, new NodeKey(new NodeId(abstractNodeid)))
+                        .augmentation(Node1.class)
+                        .child(TerminationPoint.class, new TerminationPointKey(tp.getTpId()))
+                        .build();
+                    networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiTopologyTp, tp);
+                }
+            }
+        }
+        // nodes/links update in otn-topology
+        if (otnTopology.getNode() != null
+            && otnTopology.getNode().containsKey(new NodeKey(new NodeId(abstractNodeid)))) {
+            TopologyShard otnShard = TopologyUtils.updateTopologyShard(abstractNodeid, mapping,
+                otnTopology.getNode(), otnTopologyLinks);
+            if (otnShard.getLinks() != null) {
+                for (Link link : otnShard.getLinks()) {
+                    LOG.info("updating links {} in {}", link.getLinkId().getValue(),
+                        NetworkUtils.OVERLAY_NETWORK_ID);
+                    InstanceIdentifier<Link> iiTopologyLink = InstanceIdentifier.builder(Networks.class)
+                        .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID)))
+                        .augmentation(Network1.class)
+                        .child(Link.class, link.key())
+                        .build();
+                    networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiTopologyLink, link);
+                }
+            }
+            if (otnShard.getTps() != null) {
+                for (TerminationPoint tp : otnShard.getTps()) {
+                    LOG.info("updating TP {} in otn-topology", tp.getTpId().getValue());
+                    InstanceIdentifier<TerminationPoint> iiTopologyTp = InstanceIdentifier.builder(Networks.class)
+                        .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID)))
+                        .child(Node.class, new NodeKey(new NodeId(abstractNodeid)))
+                        .augmentation(Node1.class)
+                        .child(TerminationPoint.class, new TerminationPointKey(tp.getTpId()))
+                        .build();
+                    networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiTopologyTp, tp);
+                }
+            }
+        }
+        // commit datastore updates
+        try {
+            networkTransactionService.commit().get();
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.error("Error updating openroadm-topology", e);
+        }
+    }
+
     @Override
     public void createOtnLinks(String nodeA, String tpA, String nodeZ, String tpZ, OtnLinkType linkType) {
         TopologyShard otnTopologyShard;
@@ -279,15 +378,14 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                     .augmentation(Network1.class)
                     .child(Link.class, otnTopologyLink.key())
                     .build();
-                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyLink,
-                    otnTopologyLink);
+                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyLink, otnTopologyLink);
             }
         }
         if (otnTopologyShard.getTps() != null) {
             for (TerminationPoint otnTopologyTp : otnTopologyShard.getTps()) {
                 LOG.info("updating otn nodes TP {} in otn-topology", otnTopologyTp.getTpId().getValue());
                 List<SupportingTerminationPoint> supportingTerminationPoint =
-                        new ArrayList<>(otnTopologyTp.nonnullSupportingTerminationPoint().values());
+                    new ArrayList<>(otnTopologyTp.nonnullSupportingTerminationPoint().values());
                 InstanceIdentifier<TerminationPoint> iiOtnTopologyTp = InstanceIdentifier.builder(Networks.class)
                     .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID)))
                     .child(Node.class, new NodeKey(supportingTerminationPoint.get(0).getNodeRef()))
@@ -354,15 +452,14 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                     .augmentation(Network1.class)
                     .child(Link.class, otnTopologyLink.key())
                     .build();
-                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION,
-                        iiOtnTopologyLink, otnTopologyLink);
+                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyLink, otnTopologyLink);
             }
         }
         if (otnTopologyShard.getTps() != null) {
             for (TerminationPoint otnTopologyTp : otnTopologyShard.getTps()) {
                 LOG.info("updating otn nodes TP {} in otn-topology", otnTopologyTp.getTpId().getValue());
                 List<SupportingTerminationPoint> supportingTerminationPoint =
-                        new ArrayList<>(otnTopologyTp.nonnullSupportingTerminationPoint().values());
+                    new ArrayList<>(otnTopologyTp.nonnullSupportingTerminationPoint().values());
                 InstanceIdentifier<TerminationPoint> iiOtnTopologyTp = InstanceIdentifier.builder(Networks.class)
                     .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID)))
                     .child(Node.class, new NodeKey(supportingTerminationPoint.get(0).getNodeRef()))
@@ -382,7 +479,7 @@ public class NetworkModelServiceImpl implements NetworkModelService {
 
     @Override
     public void updateOtnLinks(List<String> nodeTps, String serviceRate, Short tribPortNb, Short tribSoltNb,
-        boolean isDeletion) {
+            boolean isDeletion) {
         List<Link> supportedOdu4Links = getSupportingOdu4Links(nodeTps);
         List<TerminationPoint> tps = getOtnNodeTps(nodeTps);
         TopologyShard otnTopologyShard;
@@ -397,21 +494,20 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                     .augmentation(Network1.class)
                     .child(Link.class, new LinkKey(new LinkId(otnTopologyLink.getLinkId().getValue())))
                     .build();
-                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyLink,
-                    otnTopologyLink);
+                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyLink, otnTopologyLink);
             }
         }
         if (otnTopologyShard.getTps() != null) {
             for (TerminationPoint otnTopologyTp : otnTopologyShard.getTps()) {
                 LOG.info("updating otn nodes TP {} in otn-topology", otnTopologyTp.getTpId().getValue());
                 List<SupportingTerminationPoint> supportingTerminationPoint =
-                        new ArrayList<>(otnTopologyTp.nonnullSupportingTerminationPoint().values());
+                    new ArrayList<>(otnTopologyTp.nonnullSupportingTerminationPoint().values());
                 InstanceIdentifier<TerminationPoint> iiOtnTopologyTp = InstanceIdentifier.builder(Networks.class)
                     .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID)))
                     .child(Node.class, new NodeKey(supportingTerminationPoint.get(0).getNodeRef()))
                     .augmentation(Node1.class)
-                    .child(TerminationPoint.class, new TerminationPointKey(
-                        new TpId(otnTopologyTp.getTpId().getValue())))
+                    .child(TerminationPoint.class, new TerminationPointKey(new TpId(otnTopologyTp.getTpId()
+                        .getValue())))
                     .build();
                 if (isDeletion) {
                     networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyTp, otnTopologyTp);
@@ -453,35 +549,30 @@ public class NetworkModelServiceImpl implements NetworkModelService {
     }
 
     private boolean checkLinks(List<Link> links) {
-        boolean canBeDeleted = true;
         if (links.isEmpty()) {
             return false;
-        } else {
-            for (Link link : links) {
-                if (link.augmentation(Link1.class) != null
+        }
+        for (Link link : links) {
+            if (link.augmentation(Link1.class) != null
                     && !link.augmentation(Link1.class).getUsedBandwidth().equals(Uint32.valueOf(0))) {
-                    canBeDeleted = false;
-                }
+                return false;
             }
         }
-        return canBeDeleted;
+        return true;
     }
 
     private boolean checkTerminationPoints(List<TerminationPoint> tps) {
-        boolean canBeDeleted = true;
         if (tps.isEmpty()) {
             return false;
-        } else {
-            for (TerminationPoint tp : tps) {
-                if (tp.augmentation(TerminationPoint1.class) != null
-                    && tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getTsPool() != null
-                    && tp.augmentation(TerminationPoint1.class).getXpdrTpPortConnectionAttributes().getTsPool()
-                    .size() != 80) {
-                    canBeDeleted = false;
-                }
+        }
+        for (TerminationPoint tp : tps) {
+            if (tp.augmentation(TerminationPoint1.class) != null && tp.augmentation(TerminationPoint1.class)
+                    .getXpdrTpPortConnectionAttributes().getTsPool() != null && tp.augmentation(TerminationPoint1.class)
+                    .getXpdrTpPortConnectionAttributes().getTsPool().size() != 80) {
+                return false;
             }
         }
-        return canBeDeleted;
+        return true;
     }
 
     private List<TerminationPoint> getOtnNodeTps(String nodeTopoA, String tpA, String nodeTopoZ, String tpZ) {
@@ -502,7 +593,7 @@ public class NetworkModelServiceImpl implements NetworkModelService {
         Optional<TerminationPoint> tpZOpt = Optional.empty();
 
         if (networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, iiTpA).isDone()
-            && networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, iiTpZ).isDone()) {
+                && networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, iiTpZ).isDone()) {
             try {
                 tpAOpt = networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, iiTpA).get();
                 tpZOpt = networkTransactionService.read(LogicalDatastoreType.CONFIGURATION, iiTpZ).get();
@@ -591,9 +682,9 @@ public class NetworkModelServiceImpl implements NetworkModelService {
         List<Link> odu4links = null;
         if (netw1Opt.isPresent() && netw1Opt.get().getLink() != null) {
             odu4links = netw1Opt
-                    .get()
-                    .nonnullLink().values()
-                    .stream().filter(lk -> lk.getLinkId().getValue().startsWith("ODU4"))
+                .get()
+                .nonnullLink().values()
+                .stream().filter(lk -> lk.getLinkId().getValue().startsWith("ODU4"))
                 .collect(Collectors.toList());
         }
         List<Link> links = new ArrayList<>();
@@ -604,14 +695,12 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                     String nodeId = nodeAndTp[0];
                     String tp = nodeAndTp[1];
                     Link slink = odu4links.stream().filter(lk -> lk.getSource().getSourceNode().getValue()
-                        .equals(nodeId) && lk.getSource().getSourceTp().toString().equals(tp))
-                        .findFirst().get();
+                        .equals(nodeId) && lk.getSource().getSourceTp().toString().equals(tp)).findFirst().get();
                     if (!links.contains(slink)) {
                         links.add(slink);
                     }
                     Link dlink = odu4links.stream().filter(lk -> lk.getDestination().getDestNode().getValue()
-                        .equals(nodeId) && lk.getDestination().getDestTp().toString().equals(tp))
-                        .findFirst().get();
+                        .equals(nodeId) && lk.getDestination().getDestTp().toString().equals(tp)).findFirst().get();
                     if (!links.contains(dlink)) {
                         links.add(dlink);
                     }
@@ -635,8 +724,7 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                     .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID)))
                     .child(Node.class, otnTopologyNode.key())
                     .build();
-                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyNode,
-                    otnTopologyNode);
+                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyNode, otnTopologyNode);
             }
             for (Link otnTopologyLink : otnTopologyShard.getLinks()) {
                 LOG.info("creating otn link {} in {}", otnTopologyLink.getLinkId().getValue(),
@@ -646,12 +734,10 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                     .augmentation(Network1.class)
                     .child(Link.class, otnTopologyLink.key())
                     .build();
-                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyLink,
-                    otnTopologyLink);
+                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyLink, otnTopologyLink);
             }
         } else {
             LOG.error("Unable to create OTN topology shard for node {}!", nodeId);
         }
-
     }
 }