From: manuedelf Date: Tue, 2 Jun 2020 22:11:07 +0000 (+0200) Subject: Fix tapi execution X-Git-Tag: 2.0.0~109 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=transportpce.git;a=commitdiff_plain;h=9860391e05b7bc0972bdd3c1deceff0662fa8320 Fix tapi execution - Fix NPE in TapiTopologyImpl - Fix Junit for TapiTopoogyImpl JIRA: TRNSPRTPCE-275 Change-Id: Ia2323068a5d39ca91fcbd05d19659a6f748e0759 --- diff --git a/tapi/src/main/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImpl.java b/tapi/src/main/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImpl.java index 7dcb1f5e8..5fd5c4d43 100644 --- a/tapi/src/main/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImpl.java +++ b/tapi/src/main/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImpl.java @@ -94,7 +94,7 @@ public class TapiTopologyImpl implements TapiTopologyService { @Override public ListenableFuture> 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> 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 nodeList = openroadmTopo.getNode(); - @Nullable - List linkList = openroadmTopo.augmentation(Network1.class).getLink(); - List xponderOutLinkList = linkList.stream().filter(lk -> lk.augmentation(Link1.class).getLinkType() - .equals(OpenroadmLinkType.XPONDEROUTPUT)).collect(Collectors.toList()); - List xponderInLinkList = linkList.stream().filter(lk -> lk.augmentation(Link1.class).getLinkType() - .equals(OpenroadmLinkType.XPONDERINPUT)).collect(Collectors.toList()); + FluentFuture> openroadmTopoOpt = dataBroker.newReadOnlyTransaction() + .read(LogicalDatastoreType.CONFIGURATION, InstanceIdentifiers.OVERLAY_NETWORK_II); + if (!openroadmTopoOpt.isDone()) { + LOG.warn("Cannot get openroadm topology, returning null"); + return null; + } + Optional 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 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> clientPortMap = new HashMap<>(); - for (Node node : xpdrNodeList) { - String nodeId = node.getSupportingNode().get(0).getNodeRef().getValue(); - List 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 nodeList = openroadmTopo.getNode(); + List linkList = null; + if (openroadmTopo.augmentation(Network1.class) != null) { + linkList = openroadmTopo.augmentation(Network1.class).getLink(); + } else { + linkList = new ArrayList<>(); + } + List xponderOutLinkList = linkList.stream() + .filter(lk -> lk.augmentation(Link1.class).getLinkType().equals(OpenroadmLinkType.XPONDEROUTPUT)) + .collect(Collectors.toList()); + List xponderInLinkList = linkList.stream() + .filter(lk -> lk.augmentation(Link1.class).getLinkType().equals(OpenroadmLinkType.XPONDERINPUT)) + .collect(Collectors.toList()); - List goodTpList = new ArrayList<>(); - for (Map.Entry> entry : clientPortMap.entrySet()) { - String key = entry.getKey(); - List value = entry.getValue(); - for (String tpid : value) { - goodTpList.add(key + "--" + tpid); + List 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> clientPortMap = new HashMap<>(); + for (Node node : xpdrNodeList) { + String nodeId = node.getSupportingNode().get(0).getNodeRef().getValue(); + List 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 goodTpList = extractGoodTpList(clientPortMap); + // tapi topology creation + List names = new ArrayList(); + names.add(new NameBuilder().setValue("topo ethernet").setValueName("Topo Name").build()); + Uuid uuid = new Uuid(UUID.randomUUID().toString()); + List + tapiNodeList = new ArrayList<>(); + tapiNodeList.add(createTapiNode(goodTpList)); + Topology topology = new TopologyBuilder().setName(names).setUuid(uuid).setNode(tapiNodeList).build(); + return topology; - // tapi topology creation - List names = new ArrayList(); - names.add(new NameBuilder().setValue("topo ethernet").setValueName("Topo Name").build()); - Uuid uuid = new Uuid(UUID.randomUUID().toString()); - List 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 extractGoodTpList(Map> clientPortMap) { + List goodTpList = new ArrayList<>(); + for (Map.Entry> entry : clientPortMap.entrySet()) { + String key = entry.getKey(); + List 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 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; diff --git a/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java b/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java index 517f34e71..eec94e321 100644 --- a/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java +++ b/tapi/src/test/java/org/opendaylight/transportpce/tapi/topology/TapiTopologyImplTest.java @@ -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> result = tapiTopoImpl.getTopologyDetails(input); - result.addListener(new Runnable() { @Override public void run() { - callbackRan = true; endSignal.countDown(); } }, executorService); - endSignal.await(); - RpcResult 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()); } } diff --git a/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java b/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java index 90c857999..226b57d4f 100644 --- a/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java +++ b/tapi/src/test/java/org/opendaylight/transportpce/tapi/utils/TopologyDataUtils.java @@ -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(); }