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.Test;
24 import org.opendaylight.mdsal.binding.api.DataBroker;
25 import org.opendaylight.mdsal.binding.api.WriteTransaction;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.transportpce.test.DataStoreContext;
28 import org.opendaylight.transportpce.test.DataStoreContextImpl;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.network.Nodes;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.network.NodesBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.network.NodesKey;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.network.nodes.Mapping;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.network.nodes.MappingBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.network.nodes.MappingKey;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.network.nodes.NodeInfo;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210310.network.nodes.NodeInfoBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 public class PortMappingImplTest {
42 DataBroker dataBroker = null;
43 private PortMappingVersion710 portMappingVersion710;
44 private PortMappingVersion221 portMappingVersion221;
45 private PortMappingVersion121 portMappingVersion121;
46 private PortMapping portMapping;
49 public void setUp() throws Exception {
50 DataStoreContext dataStoreContext = new DataStoreContextImpl();
51 dataBroker = dataStoreContext.getDataBroker();
52 portMappingVersion710 = mock(PortMappingVersion710.class);
53 portMappingVersion221 = mock(PortMappingVersion221.class);
54 portMappingVersion121 = mock(PortMappingVersion121.class);
55 portMapping = new PortMappingImpl(dataBroker, portMappingVersion710,
56 portMappingVersion221, portMappingVersion121);
60 public void createMappingDataTest() {
61 //test create mapping version 1
62 when(portMappingVersion121.createMappingData("node")).thenReturn(true);
63 assertTrue(portMapping.createMappingData("node", OPENROADM_DEVICE_VERSION_1_2_1));
65 //test create mapping version 2
66 when(portMappingVersion221.createMappingData("node")).thenReturn(true);
67 assertTrue(portMapping.createMappingData("node", OPENROADM_DEVICE_VERSION_2_2_1));
69 //test create mapping version with wrong value
70 assertFalse(portMapping.createMappingData("node", "test"));
75 public void updateMappingTest() throws ExecutionException, InterruptedException {
76 Mapping mapping = new MappingBuilder().setLogicalConnectionPoint("logicalConnectionPoint")
77 .setPortDirection("1").setConnectionMapLcp("1").setPartnerLcp("1")
78 .setPortQual("1").setSupportingCircuitPackName("1").setSupportingOms("1")
79 .setSupportingOts("1").setSupportingPort("1").build();
80 InstanceIdentifier<Mapping> portMappingIID = InstanceIdentifier.builder(Network.class)
81 .child(Nodes.class, new NodesKey("node"))
82 .child(Mapping.class, new MappingKey("logicalConnectionPoint"))
84 InstanceIdentifier<NodeInfo> nodeInfoIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
85 new NodesKey("node")).child(NodeInfo.class).build();
86 final NodeInfo nodeInfo = new NodeInfoBuilder().setOpenroadmVersion(NodeInfo.OpenroadmVersion._221).build();
87 final NodeInfo nodeInfo2 = new NodeInfoBuilder().setOpenroadmVersion(NodeInfo.OpenroadmVersion._121).build();
88 Nodes nodes = new NodesBuilder().setNodeId("node").setNodeInfo(nodeInfo).build();
89 InstanceIdentifier<Nodes> nodeIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
90 new NodesKey("node")).build();
91 //create node with portmapping and nodeifno version 2
92 WriteTransaction wr = dataBroker.newWriteOnlyTransaction();
93 wr.merge(LogicalDatastoreType.CONFIGURATION, nodeIID, nodes);
94 wr.merge(LogicalDatastoreType.CONFIGURATION, portMappingIID, mapping);
95 wr.merge(LogicalDatastoreType.CONFIGURATION, nodeInfoIID, nodeInfo);
97 //test update port mapping version 2
98 when(portMappingVersion221.updateMapping("node", mapping)).thenReturn(true);
99 assertTrue("Update sould be ok", portMapping.updateMapping("node", mapping));
101 //replace node nodefino version 1 instead of version 2
102 WriteTransaction wr2 = dataBroker.newWriteOnlyTransaction();
103 wr2.merge(LogicalDatastoreType.CONFIGURATION, nodeInfoIID, nodeInfo2);
106 //test update portmapping version 1
107 when(portMappingVersion121.updateMapping("node", mapping)).thenReturn(true);
108 assertTrue(portMapping.updateMapping("node", mapping));
110 //test get node that exists
111 assertNotNull(portMapping.getNode("node"));
113 //test get node that doesn't exist
114 assertNull(portMapping.getNode("node2"));
116 //test get portmapping for existing node
117 assertEquals(portMapping
118 .getMapping("node", "logicalConnectionPoint"), mapping);
120 //test delete portmapping for existing node
121 portMapping.deleteMappingData("node");
123 //test get portmapping that was deleted above and doesn't exist anymore
124 assertNull(portMapping.getMapping("node", "logicalConnectionPoint"));