TapiLink creation refactoring
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / topology / TapiTopologyImpl.java
index 618ccf77cf84d9d97bcbae2d4b18838b6df34b9e..754ef9322d60f79d1c9d6cb8f389ccc669ca147e 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.transportpce.tapi.topology;
 import com.google.common.util.concurrent.FluentFuture;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -27,11 +28,13 @@ import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.transportpce.common.InstanceIdentifiers;
 import org.opendaylight.transportpce.common.NetworkUtils;
+import org.opendaylight.transportpce.tapi.TapiStringConstants;
 import org.opendaylight.transportpce.tapi.utils.TapiContext;
-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.mapping.MappingKey;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.network.Nodes;
-import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.network.NodesKey;
+import org.opendaylight.transportpce.tapi.utils.TapiLink;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mapping.Mapping;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.mapping.MappingKey;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.Nodes;
+import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.network.NodesKey;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Link1;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.TerminationPoint1;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.network.types.rev200529.OpenroadmLinkType;
@@ -116,22 +119,23 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
     private final DataBroker dataBroker;
     private final TapiContext tapiContext;
     private final TopologyUtils topologyUtils;
+    private final TapiLink tapiLink;
 
-    public TapiTopologyImpl(DataBroker dataBroker, TapiContext tapiContext, TopologyUtils topologyUtils) {
+    public TapiTopologyImpl(DataBroker dataBroker, TapiContext tapiContext, TopologyUtils topologyUtils,
+                            TapiLink tapiLink) {
         this.dataBroker = dataBroker;
         this.tapiContext = tapiContext;
         this.topologyUtils = topologyUtils;
+        this.tapiLink = tapiLink;
     }
 
     @Override
     public ListenableFuture<RpcResult<GetNodeDetailsOutput>> getNodeDetails(GetNodeDetailsInput input) {
         // TODO Auto-generated method stub
         // TODO -> maybe we get errors when having CEPs?
-        Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(input.getTopologyIdOrName().getBytes(Charset.forName("UTF-8")))
-                .toString());
+        Uuid topoUuid = getUuidFromIput(input.getTopologyIdOrName());
         // Node id: if roadm -> ROADM+PHOTONIC_MEDIA. if xpdr -> XPDR-XPDR+DSR/OTSi
-        Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes(input.getNodeIdOrName().getBytes(Charset.forName("UTF-8")))
-                .toString());
+        Uuid nodeUuid = getUuidFromIput(input.getNodeIdOrName());
         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node node = this.tapiContext
                 .getTapiNode(topoUuid, nodeUuid);
         if (node == null) {
@@ -148,9 +152,9 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
     @Override
     public ListenableFuture<RpcResult<GetTopologyDetailsOutput>> getTopologyDetails(GetTopologyDetailsInput input) {
         // TODO -> Add check for Full T0 Multilayer
-        if (!TopologyUtils.T0_MULTILAYER.equals(input.getTopologyIdOrName())
-            && !TopologyUtils.TPDR_100G.equals(input.getTopologyIdOrName())) {
-            if (TopologyUtils.T0_FULL_MULTILAYER.equals(input.getTopologyIdOrName())) {
+        if (!TapiStringConstants.T0_MULTILAYER.equals(input.getTopologyIdOrName())
+            && !TapiStringConstants.TPDR_100G.equals(input.getTopologyIdOrName())) {
+            if (TapiStringConstants.T0_FULL_MULTILAYER.equals(input.getTopologyIdOrName())) {
                 Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(input.getTopologyIdOrName()
                     .getBytes(Charset.forName("UTF-8"))).toString());
                 Context context = this.tapiContext.getTapiContext();
@@ -176,7 +180,7 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
         try {
             LOG.info("Building TAPI Topology abstraction for {}", input.getTopologyIdOrName());
             Topology topology = createAbstractedOtnTopology();
-            if (TopologyUtils.TPDR_100G.equals(input.getTopologyIdOrName())) {
+            if (TapiStringConstants.TPDR_100G.equals(input.getTopologyIdOrName())) {
                 topology = createAbstracted100GTpdrTopology(topology);
             }
             return RpcResultBuilder.success(new GetTopologyDetailsOutputBuilder().setTopology(topology).build())
@@ -199,9 +203,10 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
                 .filter(nep -> nep.getName().containsKey(new NameKey("100G-tpdr"))).collect(Collectors.toList());
             nep100GTpdrList.addAll(nepList);
         }
-        Name topoName = new NameBuilder().setValue(TopologyUtils.TPDR_100G).setValueName("TAPI Topology Name").build();
-        Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(TopologyUtils.TPDR_100G.getBytes(Charset.forName("UTF-8")))
-            .toString());
+        Name topoName = new NameBuilder().setValue(TapiStringConstants.TPDR_100G)
+            .setValueName("TAPI Topology Name").build();
+        Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(
+            TapiStringConstants.TPDR_100G.getBytes(Charset.forName("UTF-8"))).toString());
         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node node =
             createTapiNode(nep100GTpdrList, topoUuid);
         return new TopologyBuilder()
@@ -270,9 +275,9 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
             tapiNodeList = new HashMap<>();
         Map<LinkKey, org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Link>
             tapiLinkList = new HashMap<>();
-        Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(TopologyUtils.T0_MULTILAYER.getBytes(Charset.forName("UTF-8")))
-            .toString());
-        ConvertORTopoToTapiTopo tapiFactory = new ConvertORTopoToTapiTopo(topoUuid);
+        Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(TapiStringConstants.T0_MULTILAYER
+            .getBytes(Charset.forName("UTF-8"))).toString());
+        ConvertORTopoToTapiTopo tapiFactory = new ConvertORTopoToTapiTopo(topoUuid, this.tapiLink);
         Iterator<Entry<String, List<String>>> it = networkPortMap.entrySet().iterator();
         while (it.hasNext()) {
             String nodeId = it.next().getKey();
@@ -282,7 +287,7 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
         }
         if (openroadmTopo.nonnullNode().values().stream().filter(nt ->
                 nt.augmentation(org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev200529.Node1.class)
-                .getNodeType().equals(OpenroadmNodeType.SRG)).count() > 0) {
+                    .getNodeType().equals(OpenroadmNodeType.SRG)).count() > 0) {
             tapiFactory.convertRoadmInfrastructure();
             tapiNodeList.putAll(tapiFactory.getTapiNodes());
             tapiLinkList.putAll(tapiFactory.getTapiLinks());
@@ -296,7 +301,10 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
             tapiFactory.convertLinks(otnLinkMap);
             tapiLinkList.putAll(tapiFactory.getTapiLinks());
         }
-        Name name = new NameBuilder().setValue(TopologyUtils.T0_MULTILAYER).setValueName("TAPI Topology Name").build();
+        Name name = new NameBuilder()
+            .setValue(TapiStringConstants.T0_MULTILAYER)
+            .setValueName("TAPI Topology Name")
+            .build();
         return new TopologyBuilder()
                 .setName(Map.of(name.key(), name))
                 .setUuid(topoUuid)
@@ -309,15 +317,12 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
             GetNodeEdgePointDetailsInput input) {
         // TODO Auto-generated method stub
         // TODO -> maybe we get errors when having CEPs?
-        Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(input.getTopologyIdOrName().getBytes(Charset.forName("UTF-8")))
-                .toString());
+        Uuid topoUuid = getUuidFromIput(input.getTopologyIdOrName());
         // Node id: if roadm -> ROADMid+PHOTONIC_MEDIA. if xpdr -> XPDRid-XPDRnbr+DSR/OTSi
-        Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes(input.getNodeIdOrName().getBytes(Charset.forName("UTF-8")))
-                .toString());
+        Uuid nodeUuid = getUuidFromIput(input.getNodeIdOrName());
         // NEP id: if roadm -> ROADMid+PHOTONIC_MEDIA/MC/OTSiMC+TPid.
         // if xpdr -> XPDRid-XPDRnbr+DSR/eODU/iODU/iOTSi/eOTSi/PHOTONIC_MEDIA+TPid
-        Uuid nepUuid = new Uuid(UUID.nameUUIDFromBytes(input.getEpIdOrName().getBytes(Charset.forName("UTF-8")))
-                .toString());
+        Uuid nepUuid = getUuidFromIput(input.getEpIdOrName());
         OwnedNodeEdgePoint nep = this.tapiContext.getTapiNEP(topoUuid, nodeUuid, nepUuid);
         if (nep == null) {
             LOG.error("Invalid TAPI nep name");
@@ -331,11 +336,9 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
     @Override
     public ListenableFuture<RpcResult<GetLinkDetailsOutput>> getLinkDetails(GetLinkDetailsInput input) {
         // TODO Auto-generated method stub
-        Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(input.getTopologyIdOrName().getBytes(Charset.forName("UTF-8")))
-                .toString());
+        Uuid topoUuid = getUuidFromIput(input.getTopologyIdOrName());
         // Link id: same as OR link id
-        Uuid linkUuid = new Uuid(UUID.nameUUIDFromBytes(input.getLinkIdOrName().getBytes(Charset.forName("UTF-8")))
-                .toString());
+        Uuid linkUuid = getUuidFromIput(input.getLinkIdOrName());
         org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Link link = this.tapiContext
                 .getTapiLink(topoUuid, linkUuid);
         if (link == null) {
@@ -398,14 +401,14 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
         String networkLcp;
         if (tp.augmentation(TerminationPoint1.class).getTpType().equals(OpenroadmTpType.XPONDERCLIENT)) {
             networkLcp = tp.augmentation(
-                org.opendaylight.yang.gen.v1.http.transportpce.topology.rev201019.TerminationPoint1.class)
+                org.opendaylight.yang.gen.v1.http.transportpce.topology.rev210511.TerminationPoint1.class)
                 .getAssociatedConnectionMapPort();
         } else {
             networkLcp = tp.getTpId().getValue();
         }
         @NonNull
         KeyedInstanceIdentifier<Mapping, MappingKey> pmIID = InstanceIdentifier.create(
-            org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.Network.class)
+            org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210426.Network.class)
             .child(Nodes.class, new NodesKey(nodeIdPortMap)).child(Mapping.class, new MappingKey(networkLcp));
         @NonNull
         FluentFuture<Optional<Mapping>> mappingOpt = dataBroker.newReadOnlyTransaction().read(
@@ -428,9 +431,9 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
         switch (networkPortDirection) {
             case "bidirectional":
                 count += xpdOut.stream().filter(lk -> lk.getSource().getSourceNode().getValue().equals(nodeIdTopo)
-                    && lk.getSource().getSourceTp().equals(networkLcp)).count();
+                    && lk.getSource().getSourceTp().getValue().equals(networkLcp)).count();
                 count += xpdIn.stream().filter(lk -> lk.getDestination().getDestNode().getValue().equals(nodeIdTopo)
-                    && lk.getDestination().getDestTp().equals(networkLcp)).count();
+                    && lk.getDestination().getDestTp().getValue().equals(networkLcp)).count();
                 return (count == 2);
             case "tx":
             case "rx":
@@ -438,15 +441,15 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
                 String partnerLcp = mapping.getPartnerLcp();
                 if (mapping.getPortQual().equals("tx")) {
                     count += xpdOut.stream().filter(lk -> lk.getSource().getSourceNode().getValue().equals(nodeIdTopo)
-                        && lk.getSource().getSourceTp().equals(networkLcp)).count();
+                        && lk.getSource().getSourceTp().getValue().equals(networkLcp)).count();
                     count += xpdIn.stream().filter(lk -> lk.getDestination().getDestNode().getValue().equals(nodeIdTopo)
-                        && lk.getDestination().getDestTp().equals(partnerLcp)).count();
+                        && lk.getDestination().getDestTp().getValue().equals(partnerLcp)).count();
                 }
                 if (mapping.getPortQual().equals("rx")) {
                     count += xpdIn.stream().filter(lk -> lk.getDestination().getDestNode().getValue().equals(nodeIdTopo)
-                        && lk.getDestination().getDestTp().equals(networkLcp)).count();
+                        && lk.getDestination().getDestTp().getValue().equals(networkLcp)).count();
                     count += xpdOut.stream().filter(lk -> lk.getSource().getSourceNode().getValue().equals(nodeIdTopo)
-                        && lk.getSource().getSourceTp().equals(partnerLcp)).count();
+                        && lk.getSource().getSourceTp().getValue().equals(partnerLcp)).count();
                 }
                 return (count == 2);
             default:
@@ -492,7 +495,7 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
     @Override
     public ListenableFuture<RpcResult<GetServiceInterfacePointDetailsOutput>>
             getServiceInterfacePointDetails(GetServiceInterfacePointDetailsInput input) {
-        Uuid sipUuid = new Uuid(input.getSipIdOrName());
+        Uuid sipUuid = getUuidFromIput(input.getSipIdOrName());
         Map<ServiceInterfacePointKey, ServiceInterfacePoint> sips =
             this.tapiContext.getTapiContext().getServiceInterfacePoint();
         if (sips == null || sips.isEmpty()) {
@@ -537,4 +540,15 @@ public class TapiTopologyImpl implements TapiTopologyService, TapiCommonService
         // TODO --> not yet implemented
         return null;
     }
+
+    private Uuid getUuidFromIput(String serviceIdOrName) {
+        try {
+            UUID.fromString(serviceIdOrName);
+            LOG.info("Given attribute {} is a UUID", serviceIdOrName);
+            return new Uuid(serviceIdOrName);
+        } catch (IllegalArgumentException e) {
+            LOG.info("Given attribute {} is not a UUID", serviceIdOrName);
+            return new Uuid(UUID.nameUUIDFromBytes(serviceIdOrName.getBytes(StandardCharsets.UTF_8)).toString());
+        }
+    }
 }