c8dd03dcc6748cbbe2e1609208813ba32f1cd29a
[transportpce.git] / common / src / test / java / org / opendaylight / transportpce / common / mapping / PortMappingImplTest.java
1 /*
2  * Copyright © 2020 Orange.  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
9 package org.opendaylight.transportpce.common.mapping;
10
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;
20
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.common.DataStoreContext;
28 import org.opendaylight.transportpce.common.DataStoreContextImpl;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.Network;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.Nodes;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.NodesKey;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.Mapping;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.MappingBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.MappingKey;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.NodeInfo;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev200429.network.nodes.NodeInfoBuilder;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38
39
40 public class PortMappingImplTest {
41
42     DataBroker dataBroker = null;
43     private PortMappingVersion221 portMappingVersion221;
44     private PortMappingVersion121 portMappingVersion121;
45     private PortMapping portMapping;
46
47     @Before
48     public void setUp() throws Exception {
49         DataStoreContext dataStoreContext = new DataStoreContextImpl();
50         dataBroker = dataStoreContext.getDataBroker();
51         portMappingVersion221 = mock(PortMappingVersion221.class);
52         portMappingVersion121 = mock(PortMappingVersion121.class);
53         portMapping = new PortMappingImpl(dataBroker, portMappingVersion221, portMappingVersion121);
54     }
55
56     @Test
57     public void createMappingDataTest() {
58         //test create mapping version 1
59         when(portMappingVersion121.createMappingData("node")).thenReturn(true);
60         assertTrue(portMapping.createMappingData("node", OPENROADM_DEVICE_VERSION_1_2_1));
61
62         //test create mapping version 2
63         when(portMappingVersion221.createMappingData("node")).thenReturn(true);
64         assertTrue(portMapping.createMappingData("node", OPENROADM_DEVICE_VERSION_2_2_1));
65
66         //test create mapping version with wrong value
67         assertFalse(portMapping.createMappingData("node", "test"));
68     }
69
70
71     @Test
72     public void updateMappingTest() throws ExecutionException, InterruptedException {
73         Mapping mapping = new MappingBuilder().setLogicalConnectionPoint("logicalConnectionPoint")
74                 .setPortDirection("1").setConnectionMapLcp("1").setPartnerLcp("1")
75                 .setPortQual("1").setSupportingCircuitPackName("1").setSupportingOms("1")
76                 .setSupportingOts("1").setSupportingPort("1").build();
77         InstanceIdentifier<Mapping> portMappingIID = InstanceIdentifier.builder(Network.class)
78                 .child(Nodes.class, new NodesKey("node"))
79                 .child(Mapping.class, new MappingKey("logicalConnectionPoint"))
80                 .build();
81         InstanceIdentifier<NodeInfo> nodeInfoIID = InstanceIdentifier.builder(Network.class).child(Nodes.class,
82                 new NodesKey("node")).child(NodeInfo.class).build();
83         final NodeInfo nodeInfo = new NodeInfoBuilder().setOpenroadmVersion(NodeInfo.OpenroadmVersion._221).build();
84         final NodeInfo nodeInfo2 = new NodeInfoBuilder().setOpenroadmVersion(NodeInfo.OpenroadmVersion._121).build();
85
86         //create node with portmapping and nodeifno version 2
87         WriteTransaction wr = dataBroker.newWriteOnlyTransaction();
88         wr.merge(LogicalDatastoreType.CONFIGURATION, portMappingIID, mapping, true);
89         wr.merge(LogicalDatastoreType.CONFIGURATION, nodeInfoIID, nodeInfo, true);
90         wr.commit().get();
91         //test update port mapping version 2
92         when(portMappingVersion221.updateMapping("node", mapping)).thenReturn(true);
93         assertEquals(portMapping.updateMapping("node", mapping), true);
94
95         //replace node nodefino version 1 instead of version 2
96         WriteTransaction wr2 = dataBroker.newWriteOnlyTransaction();
97         wr2.put(LogicalDatastoreType.CONFIGURATION, nodeInfoIID, nodeInfo2, true);
98         wr2.commit().get();
99
100         //test update portmapping version 1
101         when(portMappingVersion121.updateMapping("node", mapping)).thenReturn(true);
102         assertTrue(portMapping.updateMapping("node", mapping));
103
104         //test get node that exists
105         assertNotNull(portMapping.getNode("node"));
106
107         //test get node that doesn't exist
108         assertNull(portMapping.getNode("node2"));
109
110         //test get portmapping for existing node
111         assertEquals(portMapping
112                 .getMapping("node", "logicalConnectionPoint"), mapping);
113
114         //test delete portmapping for existing node
115         portMapping.deleteMappingData("node");
116
117         //test get portmapping that was deleted above and doesn't exist anymore
118         assertNull(portMapping.getMapping("node", "logicalConnectionPoint"));
119
120     }
121
122 }