TAPI topology consolidation - step3
[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
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.nio.charset.Charset;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.UUID;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Executors;
26 import java.util.stream.Collectors;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.transportpce.common.InstanceIdentifiers;
31 import org.opendaylight.transportpce.tapi.utils.TopologyDataUtils;
32 import org.opendaylight.transportpce.test.AbstractTest;
33 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.AdministrativeState;
34 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.CapacityUnit;
35 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.ForwardingDirection;
36 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.LayerProtocolName;
37 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.OperationalState;
38 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Uuid;
39 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.Name;
40 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.global._class.NameKey;
41 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.ForwardingRule;
42 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
43 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsOutput;
44 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.RuleType;
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.node.NodeRuleGroup;
47 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.NodeEdgePoint;
48 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.node.rule.group.Rule;
49 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.Link;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.Uint64;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class TapiTopologyImplTest extends AbstractTest {
56     private static final Logger LOG = LoggerFactory.getLogger(TapiTopologyImplTest.class);
57
58     private static ListeningExecutorService executorService;
59     private static CountDownLatch endSignal;
60     private static final int NUM_THREADS = 3;
61
62     @BeforeClass
63     public static void setUp() throws InterruptedException, ExecutionException {
64         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
65         endSignal = new CountDownLatch(1);
66         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
67             TopologyDataUtils.OPENROADM_TOPOLOGY_FILE, InstanceIdentifiers.OVERLAY_NETWORK_II);
68         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
69             TopologyDataUtils.OTN_TOPOLOGY_FILE, InstanceIdentifiers.OTN_NETWORK_II);
70         TopologyDataUtils.writePortmappingFromFileToDatastore(getDataStoreContextUtil());
71         LOG.info("setup done");
72     }
73
74     @Test
75     public void getTopologyDetailsForTransponder100GTopologyWhenSuccessful()
76             throws ExecutionException, InterruptedException {
77         GetTopologyDetailsInput input = TopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.TPDR_100G);
78         TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(getDataBroker());
79         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
80         result.addListener(new Runnable() {
81             @Override
82             public void run() {
83                 endSignal.countDown();
84             }
85         }, executorService);
86         endSignal.await();
87         RpcResult<GetTopologyDetailsOutput> rpcResult = result.get();
88         @Nullable
89         Topology topology = rpcResult.getResult().getTopology();
90         assertNotNull("Topology should not be null", topology);
91         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes(TopologyUtils.TPDR_100G.getBytes(Charset.forName("UTF-8")))
92             .toString());
93         assertEquals("incorrect topology uuid", topoUuid, topology.getUuid());
94         assertEquals("Node list size should be 1", 1, topology.getNode().size());
95         Name nodeName = topology.getNode().values().stream().findFirst().get().getName()
96             .get(new NameKey("Tpdr100g node name"));
97         assertEquals("Node name should be 'Tpdr100g over WDM node'", "Tpdr100g over WDM node", nodeName.getValue());
98         Uuid nodeUuid = new Uuid(UUID.nameUUIDFromBytes(nodeName.getValue().getBytes(Charset.forName("UTF-8")))
99             .toString());
100         assertEquals("incorrect node uuid", nodeUuid, topology.getNode().values().stream().findFirst().get().getUuid());
101         long nb = topology.getNode().values().stream().findFirst().get().getOwnedNodeEdgePoint().size();
102         assertEquals("'Transponder 100GE' node should have 2 neps", 2, nb);
103         List<NodeRuleGroup> nrgList = topology.getNode().values().stream().findFirst().get().nonnullNodeRuleGroup()
104             .values().stream().sorted((nrg1, nrg2) -> nrg1.getUuid().getValue().compareTo(nrg2.getUuid().getValue()))
105             .collect(Collectors.toList());
106         assertEquals("'Transponder 100GE' node should contain a single node rule groups", 1, nrgList.size());
107         List<NodeEdgePoint> nodeEdgePointList = new ArrayList<>(nrgList.get(0).getNodeEdgePoint().values());
108         assertEquals("'Transponder 100GE' node -rule-group should contain 2 NEPs", nb, nodeEdgePointList.size());
109         List<Rule> ruleList = new ArrayList<>(nrgList.get(0).nonnullRule().values());
110         assertEquals("node-rule-group should contain a single rule", 1, ruleList.size());
111         assertEquals("local-id of the rule should be 'forward'",
112             "forward", ruleList.get(0).getLocalId());
113         assertEquals("the forwarding rule should be 'MAYFORWARDACROSSGROUP'",
114             ForwardingRule.MAYFORWARDACROSSGROUP, ruleList.get(0).getForwardingRule());
115         assertEquals("the rule type should be 'FORWARDING'",
116             RuleType.FORWARDING, ruleList.get(0).getRuleType());
117     }
118
119     @Test
120     public void getTopologyDetailsForOtnTopologyWithOtnLinksWhenSuccessful()
121         throws ExecutionException, InterruptedException {
122         GetTopologyDetailsInput input = TopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.T0_MULTILAYER);
123         TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(getDataBroker());
124         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
125         result.addListener(new Runnable() {
126             @Override
127             public void run() {
128                 endSignal.countDown();
129             }
130         }, executorService);
131         endSignal.await();
132         RpcResult<GetTopologyDetailsOutput> rpcResult = result.get();
133         @Nullable
134         Topology topology = rpcResult.getResult().getTopology();
135         assertNotNull("Topology should not be null", topology);
136         assertEquals("Node list size should be 13", 13, topology.getNode().size());
137         long nb1 = topology.getNode().values().stream()
138             .filter(node -> node.getLayerProtocolName().contains(LayerProtocolName.DSR))
139             .filter(node -> node.getName().values().stream().findFirst().get().getValue().equals("XPDR-A1-XPDR1"))
140             .flatMap(node -> node.getOwnedNodeEdgePoint().values().stream()
141                 .filter(nep -> nep.getName().containsKey(new NameKey("100G-tpdr"))))
142             .count();
143         assertEquals("XPDR-A1-XPDR1 should only have one client nep", 1, nb1);
144         long nb2 = topology.getNode().values().stream()
145             .filter(node -> node.getLayerProtocolName().contains(LayerProtocolName.DSR))
146             .filter(node -> node.getName().values().stream().findFirst().get().getValue().equals("SPDR-SA1-XPDR1"))
147             .flatMap(node -> node.getOwnedNodeEdgePoint().values().stream()
148                 .filter(nep -> nep.getName().containsKey(new NameKey("NodeEdgePoint_C"))))
149             .count();
150         assertEquals("SPDR-SA1-XPDR1 (mux) should have 4 client neps", 4, nb2);
151         long nb3 = topology.getNode().values().stream()
152             .filter(node -> node.getLayerProtocolName().contains(LayerProtocolName.DSR))
153             .filter(node -> node.getName().values().stream().findFirst().get().getValue().equals("SPDR-SA1-XPDR1"))
154             .flatMap(node -> node.getOwnedNodeEdgePoint().values().stream()
155                 .filter(nep -> nep.getName().containsKey(new NameKey("NodeEdgePoint_N"))))
156             .count();
157         assertEquals("SPDR-SA1-XPDR1 (mux) should have a single network nep", 1, nb3);
158         long nb4 = topology.getNode().values().stream()
159             .filter(node -> node.getLayerProtocolName().contains(LayerProtocolName.DSR))
160             .filter(node -> node.getName().values().stream().findFirst().get().getValue().equals("SPDR-SA1-XPDR2"))
161             .flatMap(node -> node.getOwnedNodeEdgePoint().values().stream()
162                 .filter(nep -> nep.getName().containsKey(new NameKey("NodeEdgePoint_C"))))
163             .count();
164         assertEquals("SPDR-SA1-XPDR2 (switch) should have 4 client neps", 4, nb4);
165         long nb5 = topology.getNode().values().stream()
166             .filter(node -> node.getLayerProtocolName().contains(LayerProtocolName.DSR))
167             .filter(node -> node.getName().values().stream().findFirst().get().getValue().equals("SPDR-SA1-XPDR2"))
168             .flatMap(node -> node.getOwnedNodeEdgePoint().values().stream()
169                 .filter(nep -> nep.getName().containsKey(new NameKey("NodeEdgePoint_N"))))
170             .count();
171         assertEquals("SPDR-SA1-XPDR2 (switch) should have 2 network neps", 2, nb5);
172         assertEquals("Link list size should be 18", 18, topology.getLink().size());
173         Uuid topoUuid = new Uuid(UUID.nameUUIDFromBytes("T0 - Multi-layer topology".getBytes()).toString());
174         assertEquals("incorrect topology uuid", topoUuid, topology.getUuid());
175         assertEquals("topology name should be T0 - Multi-layer topology",
176             "T0 - Multi-layer topology",
177             topology.nonnullName().values().stream().findFirst().get().getValue());
178
179         long nbDsrOduNodes = topology.nonnullNode().values().stream()
180             .filter(n -> n.getName().containsKey(new NameKey("dsr/odu node name"))).count();
181         long nbPhotonicNodes = topology.nonnullNode().values().stream()
182             .filter(n -> n.getName().containsKey(new NameKey("otsi node name"))).count();
183         assertEquals("Node list should contain 6 DSR-ODU nodes", 6, nbDsrOduNodes);
184         assertEquals("Node list should contain 7 Photonics nodes", 7, nbPhotonicNodes);
185         long nbTransititionalLinks = topology.getLink().values().stream()
186             .filter(l -> l.getName().containsKey(new NameKey("transitional link name"))).count();
187         long nbOmsLinks = topology.getLink().values().stream()
188             .filter(l -> l.getName().containsKey(new NameKey("OMS link name"))).count();
189         long nbOtnLinks = topology.getLink().values().stream()
190             .filter(l -> l.getName().containsKey(new NameKey("otn link name"))).count();
191         assertEquals("Link list should contain 8 transitional links", 8, nbTransititionalLinks);
192         assertEquals("Link list should contain 8 transitional links", 8, nbOmsLinks);
193         assertEquals("Link list should contain 2 OTN links", 2, nbOtnLinks);
194
195         Uuid node1Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SA1-XPDR1+DSR".getBytes(Charset.forName("UTF-8")))
196             .toString());
197         Uuid node2Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SC1-XPDR1+DSR".getBytes(Charset.forName("UTF-8")))
198             .toString());
199         Uuid node3Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SA1-XPDR1+OTSi".getBytes(Charset.forName("UTF-8")))
200             .toString());
201         Uuid node4Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SC1-XPDR1+OTSi".getBytes(Charset.forName("UTF-8")))
202             .toString());
203         Uuid tp1Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SA1-XPDR1+DSR+XPDR1-NETWORK1"
204             .getBytes(Charset.forName("UTF-8"))).toString());
205         Uuid tp2Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SC1-XPDR1+DSR+XPDR1-NETWORK1"
206             .getBytes(Charset.forName("UTF-8"))).toString());
207         Uuid tp3Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SA1-XPDR1+iOTSi+XPDR1-NETWORK1"
208             .getBytes(Charset.forName("UTF-8"))).toString());
209         Uuid tp4Uuid = new Uuid(UUID.nameUUIDFromBytes("SPDR-SC1-XPDR1+iOTSi+XPDR1-NETWORK1"
210             .getBytes(Charset.forName("UTF-8"))).toString());
211         Uuid link1Uuid =
212             new Uuid(UUID.nameUUIDFromBytes("ODU4-SPDR-SA1-XPDR1-XPDR1-NETWORK1toSPDR-SC1-XPDR1-XPDR1-NETWORK1"
213                 .getBytes(Charset.forName("UTF-8"))).toString());
214         Uuid link2Uuid =
215             new Uuid(UUID.nameUUIDFromBytes("OTU4-SPDR-SA1-XPDR1-XPDR1-NETWORK1toSPDR-SC1-XPDR1-XPDR1-NETWORK1"
216                 .getBytes(Charset.forName("UTF-8"))).toString());
217
218         List<Link> links = topology.nonnullLink().values().stream()
219             .filter(l -> l.getName().containsKey(new NameKey("otn link name")))
220             .sorted((l1, l2) -> l1.getUuid().getValue().compareTo(l2.getUuid().getValue()))
221             .collect(Collectors.toList());
222         checkOtnLink(links.get(0), topoUuid, node1Uuid, node2Uuid, tp1Uuid, tp2Uuid, link1Uuid,
223             "ODU4-SPDR-SA1-XPDR1-XPDR1-NETWORK1toSPDR-SC1-XPDR1-XPDR1-NETWORK1");
224         checkOtnLink(links.get(1), topoUuid, node3Uuid, node4Uuid, tp3Uuid, tp4Uuid, link2Uuid,
225             "OTU4-SPDR-SA1-XPDR1-XPDR1-NETWORK1toSPDR-SC1-XPDR1-XPDR1-NETWORK1");
226     }
227
228     private void checkOtnLink(Link link, Uuid topoUuid, Uuid node1Uuid, Uuid node2Uuid, Uuid tp1Uuid, Uuid tp2Uuid,
229         Uuid linkUuid, String linkName) {
230         assertEquals("bad name for the link", linkName, link.getName().get(new NameKey("otn link name")).getValue());
231         assertEquals("bad uuid for link", linkUuid, link.getUuid());
232         assertEquals("Available capacity unit should be MBPS",
233             CapacityUnit.MBPS, link.getAvailableCapacity().getTotalSize().getUnit());
234         String prefix = linkName.split("-")[0];
235         if ("OTU4".equals(prefix)) {
236             assertEquals("Available capacity -total size value should be 0",
237                 Uint64.valueOf(0), link.getAvailableCapacity().getTotalSize().getValue());
238         } else if ("ODU4".equals(prefix)) {
239             assertEquals("Available capacity -total size value should be 100 000",
240                 Uint64.valueOf(100000), link.getAvailableCapacity().getTotalSize().getValue());
241         }
242         assertEquals("Total capacity unit should be GBPS",
243             CapacityUnit.GBPS, link.getTotalPotentialCapacity().getTotalSize().getUnit());
244         assertEquals("Total capacity -total size value should be 100",
245             Uint64.valueOf(100), link.getTotalPotentialCapacity().getTotalSize().getValue());
246         if ("OTU4".equals(prefix)) {
247             assertEquals("otn link should be between 2 nodes of protocol layers PHOTONIC_MEDIA",
248                 LayerProtocolName.PHOTONICMEDIA.getName(), link.getLayerProtocolName().get(0).getName());
249         } else if ("ODU4".equals(prefix)) {
250             assertEquals("otn link should be between 2 nodes of protocol layers ODU",
251                 LayerProtocolName.ODU.getName(), link.getLayerProtocolName().get(0).getName());
252         }
253         assertEquals("transitional link should be BIDIRECTIONAL",
254             ForwardingDirection.BIDIRECTIONAL, link.getDirection());
255         List<org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210
256             .link.NodeEdgePoint> nodeEdgePointList = new ArrayList<>(link.nonnullNodeEdgePoint().values());
257         assertEquals("topology uuid should be the same for the two termination point of the link",
258             topoUuid, nodeEdgePointList.get(0).getTopologyUuid());
259         assertEquals("topology uuid should be the same for the two termination point of the link",
260             topoUuid, nodeEdgePointList.get(1).getTopologyUuid());
261         assertThat("otn links should terminate on two distinct nodes",
262             nodeEdgePointList.get(0).getNodeUuid().getValue(),
263             either(containsString(node1Uuid.getValue())).or(containsString(node2Uuid.getValue())));
264         assertThat("otn links should terminate on two distinct nodes",
265             nodeEdgePointList.get(1).getNodeUuid().getValue(),
266             either(containsString(node1Uuid.getValue())).or(containsString(node2Uuid.getValue())));
267         assertThat("otn links should terminate on two distinct tps",
268             nodeEdgePointList.get(0).getNodeEdgePointUuid().getValue(),
269             either(containsString(tp1Uuid.getValue())).or(containsString(tp2Uuid.getValue())));
270         assertThat("otn links should terminate on two distinct tps",
271             nodeEdgePointList.get(1).getNodeEdgePointUuid().getValue(),
272             either(containsString(tp1Uuid.getValue())).or(containsString(tp2Uuid.getValue())));
273         assertEquals("operational state should be ENABLED",
274             OperationalState.ENABLED, link.getOperationalState());
275         assertEquals("administrative state should be UNLOCKED",
276             AdministrativeState.UNLOCKED, link.getAdministrativeState());
277     }
278 }