Fix tapi execution 21/90221/5
authormanuedelf <emmanuelle.delfour@gmail.com>
Tue, 2 Jun 2020 22:11:07 +0000 (00:11 +0200)
committerGuillaume Lambert <guillaume.lambert@orange.com>
Thu, 4 Jun 2020 12:09:33 +0000 (14:09 +0200)
- Fix NPE in TapiTopologyImpl
- Fix Junit for TapiTopoogyImpl

JIRA: TRNSPRTPCE-275
Change-Id: Ia2323068a5d39ca91fcbd05d19659a6f748e0759

tapi/src/main/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImpl.java
tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java
tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java

index 7dcb1f5e8d576f43d928b0896918e6f7e53f8f07..5fd5c4d43cb2f93ac7c191d47c102501c61b039b 100644 (file)
@@ -94,7 +94,7 @@ public class TapiTopologyImpl implements TapiTopologyService {
 
     @Override
     public ListenableFuture<RpcResult<GetTopologyDetailsOutput>> getTopologyDetails(GetTopologyDetailsInput input) {
-        LOG.info("Building TAPI Topology absraction from {}", input.getTopologyIdOrName());
+        LOG.info("Building TAPI Topology abstraction from {}", input.getTopologyIdOrName());
         Topology topology = null;
         switch (input.getTopologyIdOrName()) {
             case NetworkUtils.OVERLAY_NETWORK_ID:
@@ -118,64 +118,89 @@ public class TapiTopologyImpl implements TapiTopologyService {
     private Topology createAbstractedOpenroadmTopology() {
         // read openroadm-topology
         @NonNull
-        FluentFuture<Optional<Network>> openroadmTopoOpt = dataBroker.newReadOnlyTransaction().read(
-            LogicalDatastoreType.CONFIGURATION, InstanceIdentifiers.OVERLAY_NETWORK_II);
-        if (openroadmTopoOpt.isDone()) {
-            Network openroadmTopo = null;
-            try {
-                openroadmTopo = openroadmTopoOpt.get().get();
-            } catch (InterruptedException | ExecutionException | NoSuchElementException e) {
-                LOG.error("Impossible to retreive openroadm-topology from mdsal");
-                return null;
-            }
-            List<Node> nodeList = openroadmTopo.getNode();
-            @Nullable
-            List<Link> linkList = openroadmTopo.augmentation(Network1.class).getLink();
-            List<Link> xponderOutLinkList = linkList.stream().filter(lk -> lk.augmentation(Link1.class).getLinkType()
-                .equals(OpenroadmLinkType.XPONDEROUTPUT)).collect(Collectors.toList());
-            List<Link> xponderInLinkList = linkList.stream().filter(lk -> lk.augmentation(Link1.class).getLinkType()
-                .equals(OpenroadmLinkType.XPONDERINPUT)).collect(Collectors.toList());
+        FluentFuture<Optional<Network>> openroadmTopoOpt = dataBroker.newReadOnlyTransaction()
+                .read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifiers.OVERLAY_NETWORK_II);
+        if (!openroadmTopoOpt.isDone()) {
+            LOG.warn("Cannot get openroadm topology, returning null");
+            return null;
+        }
+        Optional<Network> optionalOpenroadmTop = null;
+        try {
+            optionalOpenroadmTop = openroadmTopoOpt.get();
+        } catch (InterruptedException e) {
+            //sonar : "InterruptedException" should not be ignored (java:S2142)
+            //https://www.ibm.com/developerworks/java/library/j-jtp05236/index.html?ca=drs-#2.1
+            Thread.currentThread().interrupt();
+            return null;
+        } catch (ExecutionException | NoSuchElementException e) {
+            LOG.error("Impossible to retrieve openroadm-topology from mdsal", e);
+            return null;
+        }
+        Network openroadmTopo = null;
+        if (optionalOpenroadmTop.isPresent()) {
+            openroadmTopo = optionalOpenroadmTop.get();
+        } else {
+            LOG.warn("Openroadm topology is not present, returning null");
+            return null;
+        }
 
-            List<Node> xpdrNodeList = nodeList.stream().filter(nt -> nt.augmentation(
-                org.opendaylight.yang.gen.v1.http.org.openroadm.common.network.rev181130.Node1.class).getNodeType()
-                .equals(OpenroadmNodeType.XPONDER)).collect(Collectors.toList());
-            Map<String, List<String>> clientPortMap = new HashMap<>();
-            for (Node node : xpdrNodeList) {
-                String nodeId = node.getSupportingNode().get(0).getNodeRef().getValue();
-                List<String> clientPortList = new ArrayList<>();
-                for (TerminationPoint tp : node.augmentation(Node1.class).getTerminationPoint()) {
-                    if (tp.augmentation(TerminationPoint1.class).getTpType().equals(OpenroadmTpType.XPONDERCLIENT)) {
-                        if (checkTp(node.getNodeId().getValue(), nodeId, tp, xponderOutLinkList, xponderInLinkList)) {
-                            clientPortList.add(tp.getTpId().getValue());
-                        }
-                    }
-                }
-                if (!clientPortList.isEmpty()) {
-                    clientPortMap.put(nodeId, clientPortList);
-                }
-            }
+        List<Node> nodeList = openroadmTopo.getNode();
+        List<Link> linkList = null;
+        if (openroadmTopo.augmentation(Network1.class) != null) {
+            linkList = openroadmTopo.augmentation(Network1.class).getLink();
+        } else {
+            linkList = new ArrayList<>();
+        }
+        List<Link> xponderOutLinkList = linkList.stream()
+                .filter(lk -> lk.augmentation(Link1.class).getLinkType().equals(OpenroadmLinkType.XPONDEROUTPUT))
+                .collect(Collectors.toList());
+        List<Link> xponderInLinkList = linkList.stream()
+                .filter(lk -> lk.augmentation(Link1.class).getLinkType().equals(OpenroadmLinkType.XPONDERINPUT))
+                .collect(Collectors.toList());
 
-            List<String> goodTpList = new ArrayList<>();
-            for (Map.Entry<String, List<String>> entry : clientPortMap.entrySet()) {
-                String key = entry.getKey();
-                List<String> value = entry.getValue();
-                for (String tpid : value) {
-                    goodTpList.add(key + "--" + tpid);
+        List<Node> xpdrNodeList = nodeList
+                .stream()
+                .filter(nt -> nt
+                        .augmentation(org.opendaylight.yang.gen.v1
+                                .http.org.openroadm.common.network.rev181130.Node1.class)
+                        .getNodeType().equals(OpenroadmNodeType.XPONDER)).collect(Collectors.toList());
+        Map<String, List<String>> clientPortMap = new HashMap<>();
+        for (Node node : xpdrNodeList) {
+            String nodeId = node.getSupportingNode().get(0).getNodeRef().getValue();
+            List<String> clientPortList = new ArrayList<>();
+            for (TerminationPoint tp : node.augmentation(Node1.class).getTerminationPoint()) {
+                if (tp.augmentation(TerminationPoint1.class).getTpType().equals(OpenroadmTpType.XPONDERCLIENT)
+                        && checkTp(node.getNodeId().getValue(), nodeId, tp, xponderOutLinkList, xponderInLinkList)) {
+                    clientPortList.add(tp.getTpId().getValue());
                 }
             }
+            if (!clientPortList.isEmpty()) {
+                clientPortMap.put(nodeId, clientPortList);
+            }
+        }
+        List<String> goodTpList = extractGoodTpList(clientPortMap);
+        // tapi topology creation
+        List<Name> names = new ArrayList<Name>();
+        names.add(new NameBuilder().setValue("topo ethernet").setValueName("Topo Name").build());
+        Uuid uuid = new Uuid(UUID.randomUUID().toString());
+        List<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node>
+            tapiNodeList = new ArrayList<>();
+        tapiNodeList.add(createTapiNode(goodTpList));
+        Topology topology = new TopologyBuilder().setName(names).setUuid(uuid).setNode(tapiNodeList).build();
+        return topology;
 
-            // tapi topology creation
-            List<Name> names = new ArrayList<Name>();
-            names.add(new NameBuilder().setValue("topo ethernet").setValueName("Topo Name").build());
-            Uuid uuid = new Uuid(UUID.randomUUID().toString());
-            List<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node> tapiNodeList =
-                new ArrayList<>();
-            tapiNodeList.add(createTapiNode(goodTpList));
-            Topology topology = new TopologyBuilder().setName(names).setUuid(uuid).setNode(tapiNodeList).build();
-            return topology;
-        } else {
-            return null;
+    }
+
+    private List<String> extractGoodTpList(Map<String, List<String>> clientPortMap) {
+        List<String> goodTpList = new ArrayList<>();
+        for (Map.Entry<String, List<String>> entry : clientPortMap.entrySet()) {
+            String key = entry.getKey();
+            List<String> value = entry.getValue();
+            for (String tpid : value) {
+                goodTpList.add(key + "--" + tpid);
+            }
         }
+        return goodTpList;
     }
 
     private Topology createAbstractedOtnTopology() {
@@ -188,7 +213,7 @@ public class TapiTopologyImpl implements TapiTopologyService {
             try {
                 otnTopo = otnTopoOpt.get().get();
             } catch (InterruptedException | ExecutionException | NoSuchElementException e) {
-                LOG.error("Impossible to retreive otn-topology from mdsal");
+                LOG.error("Impossible to retreive otn-topology from mdsal",e);
                 return null;
             }
             List<Node> nodeList = otnTopo.getNode();
@@ -295,7 +320,7 @@ public class TapiTopologyImpl implements TapiTopologyService {
             try {
                 mapping = mappingOpt.get().get();
             } catch (InterruptedException | ExecutionException e) {
-                LOG.error("Error getting mapping for {}", networkLcp);
+                LOG.error("Error getting mapping for {}", networkLcp,e);
                 return false;
             }
         } else {
@@ -311,11 +336,7 @@ public class TapiTopologyImpl implements TapiTopologyService {
                     .getSource().getSourceTp().equals(networkLcp)).count();
                 count += xpdIn.stream().filter(lk -> lk.getDestination().getDestNode().getValue().equals(nodeIdTopo)
                     && lk.getDestination().getDestTp().equals(networkLcp)).count();
-                if (count == 2) {
-                    return true;
-                } else {
-                    return false;
-                }
+                return (count == 2);
             case "tx":
             case "rx":
                 @Nullable
@@ -332,11 +353,7 @@ public class TapiTopologyImpl implements TapiTopologyService {
                     count += xpdOut.stream().filter(lk -> lk.getSource().getSourceNode().getValue().equals(nodeIdTopo)
                         && lk.getSource().getSourceTp().equals(partnerLcp)).count();
                 }
-                if (count == 2) {
-                    return true;
-                } else {
-                    return false;
-                }
+                return (count == 2);
             default:
                 LOG.error("Invalid port direction for {}", networkLcp);
                 return false;
index 517f34e71e8a0035ea450d5d707402521da3c692..eec94e321a5f9a7ed8b654a330cb8474990c2f6d 100644 (file)
@@ -7,12 +7,16 @@
  */
 package org.opendaylight.transportpce.tapi.topology;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
+import org.eclipse.jdt.annotation.Nullable;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.transportpce.common.DataStoreContext;
@@ -21,50 +25,52 @@ import org.opendaylight.transportpce.tapi.utils.TopologyDataUtils;
 import org.opendaylight.transportpce.test.AbstractTest;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsOutput;
+import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.get.topology.details.output.Topology;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+
 public class TapiTopologyImplTest extends AbstractTest {
     private static final Logger LOG = LoggerFactory.getLogger(TapiTopologyImplTest.class);
 
     private ListeningExecutorService executorService;
     private CountDownLatch endSignal;
     private static final int NUM_THREADS = 3;
-    private boolean callbackRan;
     private DataStoreContext dataStoreContextUtil;
 
     @Before
     public void setUp() throws InterruptedException {
         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
         endSignal = new CountDownLatch(1);
-        callbackRan = false;
         dataStoreContextUtil = new DataStoreContextImpl();
         TopologyDataUtils.writeTopologyFromFileToDatastore(dataStoreContextUtil);
         TopologyDataUtils.writePortmappingFromFileToDatastore(dataStoreContextUtil);
         LOG.info("setup done");
-        Thread.sleep(1000);
     }
 
     @Test
     public void getTopologyDetailsWhenSuccessful() throws ExecutionException, InterruptedException {
         GetTopologyDetailsInput input = TopologyDataUtils.buildGetTopologyDetailsInput();
         TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(dataStoreContextUtil.getDataBroker());
-
         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
-
         result.addListener(new Runnable() {
             @Override
             public void run() {
-                callbackRan = true;
                 endSignal.countDown();
             }
         }, executorService);
-
         endSignal.await();
-
         RpcResult<GetTopologyDetailsOutput> rpcResult = result.get();
-        LOG.info("topo TAPI returned = {}", rpcResult.getResult().getTopology());
+        @Nullable
+        Topology topology = rpcResult.getResult().getTopology();
+        LOG.info("topo TAPI returned = {}", topology);
+
+        assertNotNull("Topology should not be null", topology);
+        assertEquals("Nodes list size should be 1",
+                1, topology.getNode().size());
+        assertEquals("Node name should be TapiNode1",
+                "TapiNode1", topology.getNode().get(0).getName().get(0).getValue());
     }
 
 }
index 90c857999e750d330c92426357cdb54d638575a1..226b57d4f94ece348bcf02788f5d88c35d4a3f4f 100644 (file)
@@ -38,7 +38,7 @@ public final class TopologyDataUtils {
 
     public static GetTopologyDetailsInput buildGetTopologyDetailsInput() {
         GetTopologyDetailsInputBuilder builtInput = new GetTopologyDetailsInputBuilder();
-        builtInput.setTopologyIdOrName("topo1");
+        builtInput.setTopologyIdOrName("openroadm-topology");
         return builtInput.build();
     }