2 * Copyright © 2020 Orange. All rights reserved.
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
9 package org.opendaylight.transportpce.common.mapping;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_1_2_1;
19 import static org.opendaylight.transportpce.common.StringConstants.OPENROADM_DEVICE_VERSION_2_2_1;
21 import java.util.concurrent.ExecutionException;
22 import org.junit.Before;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.WriteTransaction;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.transportpce.test.DataStoreContext;
29 import org.opendaylight.transportpce.test.DataStoreContextImpl;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.Network;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.Nodes;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.NodesBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.NodesKey;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.Mapping;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.MappingBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.MappingKey;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.NodeInfo;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200827.network.nodes.NodeInfoBuilder;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 public class PortMappingImplTest {
44 DataBroker dataBroker = null;
45 private PortMappingVersion221 portMappingVersion221;
46 private PortMappingVersion121 portMappingVersion121;
47 private PortMapping portMapping;
50 public void setUp() throws Exception {
51 DataStoreContext dataStoreContext = new DataStoreContextImpl();
52 dataBroker = dataStoreContext.getDataBroker();
53 portMappingVersion221 = mock(PortMappingVersion221.class);
54 portMappingVersion121 = mock(PortMappingVersion121.class);
55 portMapping = new PortMappingImpl(dataBroker, portMappingVersion221, portMappingVersion121);
59 public void createMappingDataTest() {
60 //test create mapping version 1
61 when(portMappingVersion121.createMappingData("node")).thenReturn(true);
62 assertTrue(portMapping.createMappingData("node", OPENROADM_DEVICE_VERSION_1_2_1));
64 //test create mapping version 2
65 when(portMappingVersion221.createMappingData("node")).thenReturn(true);
66 assertTrue(portMapping.createMappingData("node", OPENROADM_DEVICE_VERSION_2_2_1));
68 //test create mapping version with wrong value
69 assertFalse(portMapping.createMappingData("node", "test"));
74 public void updateMappingTest() throws ExecutionException, InterruptedException {
75 Mapping mapping = new MappingBuilder().setLogicalConnectionPoint("logicalConnectionPoint")
76 .setPortDirection("1").setConnectionMapLcp("1").setPartnerLcp("1")
77 .setPortQual("1").setSupportingCircuitPackName("1").setSupportingOms("1")
78 .setSupportingOts("1").setSupportingPort("1").build();
79 InstanceIdentifier<Mapping> portMappingIID = InstanceIdentifier.builder(Network.class)
80 .child(Nodes.class, new NodesKey("node"))
81 .child(Mapping.class, new MappingKey("logicalConnectionPoint"))
83 InstanceIdentifier<NodeInfo> nodeInfoIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
84 new NodesKey("node")).child(NodeInfo.class).build();
85 final NodeInfo nodeInfo = new NodeInfoBuilder().setOpenroadmVersion(NodeInfo.OpenroadmVersion._221).build();
86 final NodeInfo nodeInfo2 = new NodeInfoBuilder().setOpenroadmVersion(NodeInfo.OpenroadmVersion._121).build();
87 Nodes nodes = new NodesBuilder().setNodeId("node").setNodeInfo(nodeInfo).build();
88 InstanceIdentifier<Nodes> nodeIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
89 new NodesKey("node")).build();
90 //create node with portmapping and nodeifno version 2
91 WriteTransaction wr = dataBroker.newWriteOnlyTransaction();
92 wr.merge(LogicalDatastoreType.CONFIGURATION, nodeIID, nodes);
93 wr.merge(LogicalDatastoreType.CONFIGURATION, portMappingIID, mapping);
94 wr.merge(LogicalDatastoreType.CONFIGURATION, nodeInfoIID, nodeInfo);
96 //test update port mapping version 2
97 when(portMappingVersion221.updateMapping("node", mapping)).thenReturn(true);
98 assertTrue("Update sould be ok", portMapping.updateMapping("node", mapping));
100 //replace node nodefino version 1 instead of version 2
101 WriteTransaction wr2 = dataBroker.newWriteOnlyTransaction();
102 wr2.merge(LogicalDatastoreType.CONFIGURATION, nodeInfoIID, nodeInfo2);
105 //test update portmapping version 1
106 when(portMappingVersion121.updateMapping("node", mapping)).thenReturn(true);
107 assertTrue(portMapping.updateMapping("node", mapping));
109 //test get node that exists
110 assertNotNull(portMapping.getNode("node"));
112 //test get node that doesn't exist
113 assertNull(portMapping.getNode("node2"));
115 //test get portmapping for existing node
116 assertEquals(portMapping
117 .getMapping("node", "logicalConnectionPoint"), mapping);
119 //test delete portmapping for existing node
120 portMapping.deleteMappingData("node");
122 //test get portmapping that was deleted above and doesn't exist anymore
123 assertNull(portMapping.getMapping("node", "logicalConnectionPoint"));