Adapt network module to update OTU links only
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / service / NetworkModelServiceImpl.java
index 867f341e6a0d6aec15f0db94dd71708e0044e10d..74567811e46a2f346f2eaba8fc1721e7960e9eb3 100644 (file)
@@ -191,8 +191,11 @@ public class NetworkModelServiceImpl implements NetworkModelService {
      */
 
     @Override
-    public void deleteOpenRoadmnode(String nodeId) {
+    public boolean deleteOpenRoadmnode(String nodeId) {
         try {
+            if (!this.portMapping.isNodeExist(nodeId)) {
+                return false;
+            }
             NodeKey nodeIdKey = new NodeKey(new NodeId(nodeId));
 
             LOG.info("deleting node in {}", NetworkUtils.UNDERLAY_NETWORK_ID);
@@ -263,7 +266,9 @@ public class NetworkModelServiceImpl implements NetworkModelService {
             LOG.info("all nodes and links deleted ! ");
         } catch (InterruptedException | ExecutionException | TimeoutException e) {
             LOG.error("Error when trying to delete node : {}", nodeId, e);
+            return false;
         }
+        return true;
     }
 
     @Override
@@ -395,8 +400,6 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                 break;
             case ODTU4:
             case ODUC4:
-                String nodeTopoA = new StringBuilder(nodeA).append("-").append(tpA.split("-")[0]).toString();
-                String nodeTopoZ = new StringBuilder(nodeZ).append("-").append(tpZ.split("-")[0]).toString();
                 List<LinkId> linkIdList = new ArrayList<>();
                 String prefix;
                 if (OtnLinkType.ODTU4.equals(linkType)) {
@@ -404,10 +407,13 @@ public class NetworkModelServiceImpl implements NetworkModelService {
                 } else {
                     prefix = OtnLinkType.OTUC4.getName();
                 }
-                linkIdList.add(LinkIdUtil.buildOtnLinkId(nodeTopoA, tpA, nodeTopoZ, tpZ, prefix));
-                linkIdList.add(LinkIdUtil.buildOtnLinkId(nodeTopoZ, tpZ, nodeTopoA, tpA, prefix));
+                linkIdList.add(LinkIdUtil.buildOtnLinkId(convertNetconfNodeIdToTopoNodeId(nodeA, tpA), tpA,
+                    convertNetconfNodeIdToTopoNodeId(nodeZ, tpZ), tpZ, prefix));
+                linkIdList.add(LinkIdUtil.buildOtnLinkId(convertNetconfNodeIdToTopoNodeId(nodeZ, tpZ), tpZ,
+                    convertNetconfNodeIdToTopoNodeId(nodeA, tpA), tpA, prefix));
                 List<Link> supportedOtu4links = getOtnLinks(linkIdList);
-                List<TerminationPoint> tps = getOtnNodeTps(nodeTopoA, tpA, nodeTopoZ, tpZ);
+                List<TerminationPoint> tps = getOtnNodeTps(convertNetconfNodeIdToTopoNodeId(nodeA, tpA), tpA,
+                    convertNetconfNodeIdToTopoNodeId(nodeZ, tpZ), tpZ);
                 otnTopologyShard = OpenRoadmOtnTopology.createOtnLinks(supportedOtu4links, tps, linkType);
                 break;
             default:
@@ -556,7 +562,7 @@ public class NetworkModelServiceImpl implements NetworkModelService {
         linkTerminations.add(atermination);
         linkTerminations.add(ztermination);
 
-        List<Link> supportedOdu4Links = getSupportingOdu4Links(linkTerminations);
+        List<Link> supportedOdu4Links = getSupportingOdu4Links(linkTerminations, serviceRate);
         List<TerminationPoint> tps = getOtnNodeTps(linkTerminations);
         TopologyShard otnTopologyShard;
         otnTopologyShard = OpenRoadmOtnTopology.updateOtnLinks(supportedOdu4Links, tps, serviceRate, tribPortNb,
@@ -599,6 +605,43 @@ public class NetworkModelServiceImpl implements NetworkModelService {
         }
     }
 
+    @Override
+    public void updateOtnLinks(
+        org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210618.renderer.rpc.result.sp.Link
+            notifLink, boolean isDeletion) {
+
+        List<LinkId> linkIdList = new ArrayList<>();
+        String nodeTopoA = convertNetconfNodeIdToTopoNodeId(notifLink.getATermination().getNodeId(),
+            notifLink.getATermination().getTpId());
+        String nodeTopoZ = convertNetconfNodeIdToTopoNodeId(notifLink.getZTermination().getNodeId(),
+            notifLink.getZTermination().getTpId());
+        linkIdList.add(LinkIdUtil.buildOtnLinkId(nodeTopoA, notifLink.getATermination().getTpId(),
+            nodeTopoZ, notifLink.getZTermination().getTpId(), OtnLinkType.OTU4.getName()));
+        linkIdList.add(LinkIdUtil.buildOtnLinkId(nodeTopoZ, notifLink.getZTermination().getTpId(),
+            nodeTopoA, notifLink.getATermination().getTpId(), OtnLinkType.OTU4.getName()));
+        List<Link> supportedOtu4links = getOtnLinks(linkIdList);
+
+        TopologyShard otnTopologyShard = OpenRoadmOtnTopology.updateOtnLinks(supportedOtu4links, isDeletion);
+        if (otnTopologyShard.getLinks() != null) {
+            for (Link otnTopologyLink : otnTopologyShard.getLinks()) {
+                LOG.info("creating and updating otn links {} in {}", otnTopologyLink.getLinkId().getValue(),
+                    NetworkUtils.OVERLAY_NETWORK_ID);
+                InstanceIdentifier<Link> iiOtnTopologyLink = InstanceIdentifier.builder(Networks.class)
+                    .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID)))
+                    .augmentation(Network1.class)
+                    .child(Link.class, otnTopologyLink.key())
+                    .build();
+                networkTransactionService.merge(LogicalDatastoreType.CONFIGURATION, iiOtnTopologyLink, otnTopologyLink);
+            }
+        }
+        try {
+            networkTransactionService.commit().get();
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.error("Error adding OTN links in otn-topology", e);
+        }
+        LOG.info("OTN links updated");
+    }
+
     private List<Link> getOtnLinks(List<LinkId> linkIds) {
         List<Link> links = new ArrayList<>();
         for (LinkId linkId : linkIds) {
@@ -741,7 +784,7 @@ public class NetworkModelServiceImpl implements NetworkModelService {
         }
     }
 
-    private List<Link> getSupportingOdu4Links(List<LinkTp> nodesTopoTps) {
+    private List<Link> getSupportingOdu4Links(List<LinkTp> nodesTopoTps, Uint32 serviceRate) {
         InstanceIdentifier<Network1> iiOtnTopologyLinks = InstanceIdentifier.builder(Networks.class)
             .child(Network.class, new NetworkKey(new NetworkId(NetworkUtils.OTN_NETWORK_ID)))
             .augmentation(Network1.class)
@@ -761,31 +804,31 @@ public class NetworkModelServiceImpl implements NetworkModelService {
             odu4links = netw1Opt
                 .get()
                 .nonnullLink().values()
-                .stream().filter(lk -> lk.getLinkId().getValue().startsWith("ODTU4"))
+                .stream().filter(lk -> lk.getLinkId().getValue()
+                    .startsWith(Uint32.valueOf(100).equals(serviceRate) ? "ODUC4" : "ODTU4"))
                 .collect(Collectors.toList());
         }
+        if (odu4links == null) {
+            return null;
+        }
         List<Link> links = new ArrayList<>();
-        if (odu4links != null) {
-            for (LinkTp linkTp : nodesTopoTps) {
-                String tp = linkTp.getTpId();
-                String nodeId = new StringBuilder(linkTp.getNodeId()).append("-")
-                    .append(tp.split("-")[0]).toString();
-                Link slink = odu4links.stream().filter(lk -> lk.getSource().getSourceNode().getValue()
-                    .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();
-                if (!links.contains(dlink)) {
-                    links.add(dlink);
-                }
+        for (LinkTp linkTp : nodesTopoTps) {
+            String tp = linkTp.getTpId();
+            String nodeId = new StringBuilder(linkTp.getNodeId()).append("-")
+                .append(tp.split("-")[0]).toString();
+            Link slink = odu4links.stream().filter(lk -> lk.getSource().getSourceNode().getValue()
+                .equals(nodeId) && lk.getSource().getSourceTp().getValue().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().getValue().equals(tp)).findFirst().get();
+            if (!links.contains(dlink)) {
+                links.add(dlink);
             }
-            LOG.debug("odu4links = {}", links.toString());
-            return links;
-        } else {
-            return null;
         }
+        LOG.debug("odu4oduC4links = {}", links);
+        return links;
     }
 
     private void createOpenRoadmOtnNode(String nodeId) {
@@ -816,6 +859,10 @@ public class NetworkModelServiceImpl implements NetworkModelService {
         }
     }
 
+    private String convertNetconfNodeIdToTopoNodeId(String nodeId, String tpId) {
+        return new StringBuilder(nodeId).append("-").append(tpId.split("-")[0]).toString();
+    }
+
     @SuppressFBWarnings(
             value = "UPM_UNCALLED_PRIVATE_METHOD",
             justification = "false positive, this method is used by public updateOpenRoadmNetworkTopology")