TAPI topology consolidation
[transportpce.git] / tapi / src / test / java / org / opendaylight / transportpce / tapi / topology / TapiTopologyImplTest.java
1 /*
2  * Copyright © 2019 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.tapi.topology;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.either;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.ListeningExecutorService;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import java.nio.charset.Charset;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.UUID;
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Executors;
27 import java.util.stream.Collectors;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.opendaylight.transportpce.common.InstanceIdentifiers;
32 import org.opendaylight.transportpce.common.NetworkUtils;
33 import org.opendaylight.transportpce.tapi.utils.TopologyDataUtils;
34 import org.opendaylight.transportpce.test.AbstractTest;
35 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.AdministrativeState;
36 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.CapacityUnit;
37 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.ForwardingDirection;
38 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.LayerProtocolName;
39 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.OperationalState;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Uuid;
41 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.Name;
42 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.NameKey;
43 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
44 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsOutput;
45 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.get.topology.details.output.Topology;
46 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Link;
47 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Node;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.common.Uint64;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class TapiTopologyImplTest extends AbstractTest {
54     private static final Logger LOG = LoggerFactory.getLogger(TapiTopologyImplTest.class);
55
56     private static ListeningExecutorService executorService;
57     private static CountDownLatch endSignal;
58     private static final int NUM_THREADS = 3;
59
60     @BeforeClass
61     public static void setUp() throws InterruptedException, ExecutionException {
62         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
63         endSignal = new CountDownLatch(1);
64         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
65             TopologyDataUtils.OPENROADM_TOPOLOGY_FILE, InstanceIdentifiers.OVERLAY_NETWORK_II);
66         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
67             TopologyDataUtils.OTN_TOPOLOGY_WITH_OTN_LINKS_FILE, InstanceIdentifiers.OTN_NETWORK_II);
68         TopologyDataUtils.writePortmappingFromFileToDatastore(getDataStoreContextUtil());
69         LOG.info("setup done");
70     }
71
72     @Test
73     public void getTopologyDetailsForOpenroadmTopologyWhenSuccessful() throws ExecutionException, InterruptedException {
74         GetTopologyDetailsInput input = TopologyDataUtils.buildGetTopologyDetailsInput(NetworkUtils.OVERLAY_NETWORK_ID);
75         TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(getDataBroker());
76         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
77         result.addListener(new Runnable() {
78             @Override
79             public void run() {
80                 endSignal.countDown();
81             }
82         }, executorService);
83         endSignal.await();
84         RpcResult<GetTopologyDetailsOutput> rpcResult = result.get();
85         @Nullable
86         Topology topology = rpcResult.getResult().getTopology();
87         assertNotNull("Topology should not be null", topology);
88         assertEquals("Nodes list size should be 1", 1, topology.getNode().size());
89         List<Node> topologyNodeList = new ArrayList<>(topology.nonnullNode().values());
90         List<Node> nodeList = new ArrayList<>(topologyNodeList);
91         List<Name> nameList = new ArrayList<>(nodeList.get(0).nonnullName().values());
92         assertEquals("Node name should be TAPI Ethernet Node",
93             "TAPI Ethernet Node", nameList.get(0).getValue());
94         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes("Ethernet Topology".getBytes()).toString());
95         Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes("TAPI Ethernet Node".getBytes()).toString());
96         assertEquals("incorrect topology uuid", topoUuid, topology.getUuid());
97         assertEquals("incorrect node uuid", nodeUuid, topologyNodeList.get(0).getUuid());
98         assertNull("TAPI Ethernet Node should have no nep", topologyNodeList.get(0).getOwnedNodeEdgePoint());
99     }
100
101
102     @Test
103     public void getTopologyDetailsForOtnTopologyWithOtnLinksWhenSuccessful()
104         throws ExecutionException, InterruptedException {
105         GetTopologyDetailsInput input = TopologyDataUtils.buildGetTopologyDetailsInput(NetworkUtils.OTN_NETWORK_ID);
106         TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(getDataBroker());
107         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
108         result.addListener(new Runnable() {
109             @Override
110             public void run() {
111                 endSignal.countDown();
112             }
113         }, executorService);
114         endSignal.await();
115         RpcResult<GetTopologyDetailsOutput> rpcResult = result.get();
116         @Nullable
117         Topology topology = rpcResult.getResult().getTopology();
118         assertNotNull("Topology should not be null", topology);
119         assertEquals("Node list size should be 9", 9, topology.getNode().size());
120         assertEquals("Link list size should be 14", 14, topology.getLink().size());
121         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes("T0 - Multi-layer topology".getBytes()).toString());
122         assertEquals("incorrect topology uuid", topoUuid, topology.getUuid());
123         assertEquals("topology name should be T0 - Multi-layer topology",
124             "T0 - Multi-layer topology",
125             topology.nonnullName().values().stream().findFirst().get().getValue());
126
127         long nbDsrOduNodes = topology.nonnullNode().values().stream()
128             .filter(n -> n.getName().containsKey(new NameKey("dsr/odu node name"))).count();
129         long nbPhotonicNodes = topology.nonnullNode().values().stream()
130             .filter(n -> n.getName().containsKey(new NameKey("otsi node name"))).count();
131         assertEquals("Node list should contain 4 DSR-ODU nodes", 4, nbDsrOduNodes);
132         assertEquals("Node list should contain 5 Photonics nodes", 5, nbPhotonicNodes);
133         long nbTransititionalLinks = topology.getLink().values().stream()
134             .filter(l -> l.getName().containsKey(new NameKey("transitional link name"))).count();
135         long nbOmsLinks = topology.getLink().values().stream()
136             .filter(l -> l.getName().containsKey(new NameKey("OMS link name"))).count();
137         long nbOtnLinks = topology.getLink().values().stream()
138             .filter(l -> l.getName().containsKey(new NameKey("otn link name"))).count();
139         assertEquals("Link list should contain 6 transitional links", 6, nbTransititionalLinks);
140         assertEquals("Link list should contain 6 transitional links", 6, nbOmsLinks);
141         assertEquals("Link list should contain 2 OTN links", 2, nbOtnLinks);
142
143         Uuid node1Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SA1-XPDR1+DSR".getBytes(Charset.forName("UTF-8")))
144             .toString());
145         Uuid node2Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SC1-XPDR1+DSR".getBytes(Charset.forName("UTF-8")))
146             .toString());
147         Uuid node3Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SA1-XPDR1+OTSi".getBytes(Charset.forName("UTF-8")))
148             .toString());
149         Uuid node4Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SC1-XPDR1+OTSi".getBytes(Charset.forName("UTF-8")))
150             .toString());
151         Uuid tp1Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SA1-XPDR1+DSR+XPDR1-NETWORK1"
152             .getBytes(Charset.forName("UTF-8"))).toString());
153         Uuid tp2Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SC1-XPDR1+DSR+XPDR1-NETWORK1"
154             .getBytes(Charset.forName("UTF-8"))).toString());
155         Uuid tp3Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SA1-XPDR1+iOTSi+XPDR1-NETWORK1"
156             .getBytes(Charset.forName("UTF-8"))).toString());
157         Uuid tp4Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SC1-XPDR1+iOTSi+XPDR1-NETWORK1"
158             .getBytes(Charset.forName("UTF-8"))).toString());
159         Uuid link1Uuid =
160             new Uuid(UUID.nameUUIDFromBytes("ODU4-SPDR-SA1-XPDR1-XPDR1-NETWORK1toSPDR-SC1-XPDR1-XPDR1-NETWORK1"
161                 .getBytes(Charset.forName("UTF-8"))).toString());
162         Uuid link2Uuid =
163             new Uuid(UUID.nameUUIDFromBytes("OTU4-SPDR-SA1-XPDR1-XPDR1-NETWORK1toSPDR-SC1-XPDR1-XPDR1-NETWORK1"
164                 .getBytes(Charset.forName("UTF-8"))).toString());
165
166         List<Link> links = topology.nonnullLink().values().stream()
167             .sorted((l1, l2) -> l1.getUuid().getValue().compareTo(l2.getUuid().getValue()))
168             .collect(Collectors.toList());
169         checkOtnLink(links.get(4), topoUuid, node1Uuid, node2Uuid, tp1Uuid, tp2Uuid, link1Uuid,
170             "ODU4-SPDR-SA1-XPDR1-XPDR1-NETWORK1toSPDR-SC1-XPDR1-XPDR1-NETWORK1");
171         checkOtnLink(links.get(11), topoUuid, node3Uuid, node4Uuid, tp3Uuid, tp4Uuid, link2Uuid,
172             "OTU4-SPDR-SA1-XPDR1-XPDR1-NETWORK1toSPDR-SC1-XPDR1-XPDR1-NETWORK1");
173     }
174
175     private void checkOtnLink(Link link, Uuid topoUuid, Uuid node1Uuid, Uuid node2Uuid, Uuid tp1Uuid, Uuid tp2Uuid,
176         Uuid linkUuid, String linkName) {
177         assertEquals("bad name for the link", linkName, link.getName().get(new NameKey("otn link name")).getValue());
178         assertEquals("bad uuid for link", linkUuid, link.getUuid());
179         assertEquals("Available capacity unit should be MBPS",
180             CapacityUnit.MBPS, link.getAvailableCapacity().getTotalSize().getUnit());
181         String prefix = linkName.split("-")[0];
182         if ("OTU4".equals(prefix)) {
183             assertEquals("Available capacity -total size value should be 0",
184                 Uint64.valueOf(0), link.getAvailableCapacity().getTotalSize().getValue());
185         } else if ("ODU4".equals(prefix)) {
186             assertEquals("Available capacity -total size value should be 100 000",
187                 Uint64.valueOf(100000), link.getAvailableCapacity().getTotalSize().getValue());
188         }
189         assertEquals("Total capacity unit should be GBPS",
190             CapacityUnit.GBPS, link.getTotalPotentialCapacity().getTotalSize().getUnit());
191         assertEquals("Total capacity -total size value should be 100",
192             Uint64.valueOf(100), link.getTotalPotentialCapacity().getTotalSize().getValue());
193         if ("OTU4".equals(prefix)) {
194             assertEquals("otn link should be between 2 nodes of protocol layers PHOTONIC_MEDIA",
195                 LayerProtocolName.PHOTONICMEDIA.getName(), link.getLayerProtocolName().get(0).getName());
196         } else if ("ODU4".equals(prefix)) {
197             assertEquals("otn link should be between 2 nodes of protocol layers ODU",
198                 LayerProtocolName.ODU.getName(), link.getLayerProtocolName().get(0).getName());
199         }
200         assertEquals("transitional link should be BIDIRECTIONAL",
201             ForwardingDirection.BIDIRECTIONAL, link.getDirection());
202         List<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210
203             .link.NodeEdgePoint> nodeEdgePointList = new ArrayList<>(link.nonnullNodeEdgePoint().values());
204         assertEquals("topology uuid should be the same for the two termination point of the link",
205             topoUuid, nodeEdgePointList.get(0).getTopologyUuid());
206         assertEquals("topology uuid should be the same for the two termination point of the link",
207             topoUuid, nodeEdgePointList.get(1).getTopologyUuid());
208         assertThat("otn links should terminate on two distinct nodes",
209             nodeEdgePointList.get(0).getNodeUuid().getValue(),
210             either(containsString(node1Uuid.getValue())).or(containsString(node2Uuid.getValue())));
211         assertThat("otn links should terminate on two distinct nodes",
212             nodeEdgePointList.get(1).getNodeUuid().getValue(),
213             either(containsString(node1Uuid.getValue())).or(containsString(node2Uuid.getValue())));
214         assertThat("otn links should terminate on two distinct tps",
215             nodeEdgePointList.get(0).getNodeEdgePointUuid().getValue(),
216             either(containsString(tp1Uuid.getValue())).or(containsString(tp2Uuid.getValue())));
217         assertThat("otn links should terminate on two distinct tps",
218             nodeEdgePointList.get(1).getNodeEdgePointUuid().getValue(),
219             either(containsString(tp1Uuid.getValue())).or(containsString(tp2Uuid.getValue())));
220         assertEquals("operational state should be ENABLED",
221             OperationalState.ENABLED, link.getOperationalState());
222         assertEquals("administrative state should be UNLOCKED",
223             AdministrativeState.UNLOCKED, link.getAdministrativeState());
224     }
225 }