Add node-info in portmapping structure
[transportpce.git] / renderer / src / test / java / org / opendaylight / transportpce / renderer / provisiondevice / DeviceRendererServiceImplSetupTest.java
1 /*
2  * Copyright © 2018 Orange Systems, 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
9 package org.opendaylight.transportpce.renderer.provisiondevice;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.junit.Assert;
15 import org.junit.Ignore;
16 import org.junit.Test;
17 import org.mockito.Mockito;
18 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
19 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
20 import org.opendaylight.transportpce.common.StringConstants;
21 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
22 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
23 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
24 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl221;
25 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
26 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
27 import org.opendaylight.transportpce.common.fixedflex.FixedFlexImpl;
28 import org.opendaylight.transportpce.common.fixedflex.FixedFlexInterface;
29 import org.opendaylight.transportpce.common.mapping.MappingUtils;
30 import org.opendaylight.transportpce.common.mapping.PortMapping;
31 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
32 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
33 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
34 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
35 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
36 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
37 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
38 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
39 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterface121;
40 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterface221;
41 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaceFactory;
42 import org.opendaylight.transportpce.renderer.provisiondevice.servicepath.ServicePathDirection;
43 import org.opendaylight.transportpce.renderer.stub.MountPointServiceStub;
44 import org.opendaylight.transportpce.renderer.utils.MountPointUtils;
45 import org.opendaylight.transportpce.renderer.utils.ServiceImplementationDataUtils;
46 import org.opendaylight.transportpce.test.AbstractTest;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev170228.ServicePathInput;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev170228.ServicePathOutput;
49 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.olm.renderer.input.Nodes;
50
51 public class DeviceRendererServiceImplSetupTest extends AbstractTest {
52
53     private DeviceTransactionManager deviceTransactionManager;
54
55     private DeviceRendererService deviceRendererService;
56     private CrossConnect crossConnect;
57     private PortMapping portMapping;
58     private OpenRoadmInterfaces openRoadmInterfaces;
59     private MappingUtils mappingUtils;
60     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
61     private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl221;
62     private PortMappingVersion221 portMappingVersion22;
63     private PortMappingVersion121 portMappingVersion121;
64     private CrossConnectImpl121 crossConnectImpl121;
65     private CrossConnectImpl221 crossConnectImpl221;
66
67
68     private void setMountPoint(MountPoint mountPoint) {
69         MountPointService mountPointService = new MountPointServiceStub(mountPoint);
70         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
71         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
72         this.openRoadmInterfacesImpl221 = new OpenRoadmInterfacesImpl221(deviceTransactionManager);
73         this.mappingUtils = Mockito.spy(MappingUtils.class);
74
75         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
76             .getOpenRoadmVersion(Mockito.anyString());
77
78         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl(deviceTransactionManager, mappingUtils,
79             openRoadmInterfacesImpl121, openRoadmInterfacesImpl221);
80         this.openRoadmInterfaces = Mockito.spy(this.openRoadmInterfaces);
81         this.portMappingVersion22 =
82             new PortMappingVersion221(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
83         this.portMappingVersion121 =
84             new PortMappingVersion121(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
85         this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion22,
86             this.portMappingVersion121);
87         this.portMapping = Mockito.spy(this.portMapping);
88         this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
89         this.crossConnectImpl221 = new CrossConnectImpl221(deviceTransactionManager);
90         this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
91             this.crossConnectImpl221);
92         this.crossConnect = Mockito.spy(this.crossConnect);
93
94
95         FixedFlexInterface fixedFlexInterface = new FixedFlexImpl();
96         OpenRoadmInterface121 openRoadmInterface121 = new OpenRoadmInterface121(portMapping,openRoadmInterfaces);
97         OpenRoadmInterface221 openRoadmInterface221 = new OpenRoadmInterface221(portMapping,openRoadmInterfaces,
98             fixedFlexInterface);
99         OpenRoadmInterfaceFactory openRoadmInterfaceFactory = new OpenRoadmInterfaceFactory(this.mappingUtils,
100             openRoadmInterface121,openRoadmInterface221);
101
102         this.deviceRendererService = new DeviceRendererServiceImpl(this.getDataBroker(),
103             this.deviceTransactionManager, openRoadmInterfaceFactory, openRoadmInterfaces, crossConnect,
104             portMapping);
105     }
106
107     @Test
108     public void testSetupServiceWhenDeviceIsNotMounted() {
109         setMountPoint(null);
110         ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs();
111         for (ServicePathDirection servicePathDirection : ServicePathDirection.values()) {
112             ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
113                 servicePathDirection);
114             Assert.assertFalse(servicePathOutput.isSuccess());
115             Assert.assertEquals("node1 is not mounted on the controller",
116                 servicePathOutput.getResult());
117         }
118     }
119
120     @Test
121     public void testSetupServicemptyPorts() {
122         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
123         String nodeId = "node1";
124         String srcTP = StringConstants.TTP_TOKEN;
125         String dstTp = StringConstants.PP_TOKEN;
126         List<Nodes> nodes = new ArrayList<>();
127         nodes.add(ServiceImplementationDataUtils.createNode(nodeId, srcTP, dstTp));
128         ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
129         for (ServicePathDirection servicePathDirection : ServicePathDirection.values()) {
130             ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
131                 servicePathDirection);
132             Assert.assertFalse(servicePathOutput.isSuccess());
133         }
134     }
135
136     @Test
137     public void testSetupServiceCannotCrossConnect() {
138         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
139         String nodeId = "node1";
140         String srcTP = StringConstants.TTP_TOKEN;
141         String dstTp = StringConstants.PP_TOKEN;
142         MountPointUtils.writeMapping(nodeId, srcTP, this.deviceTransactionManager);
143         MountPointUtils.writeMapping(nodeId, dstTp, this.deviceTransactionManager);
144         List<Nodes> nodes = new ArrayList<>();
145         nodes.add(ServiceImplementationDataUtils.createNode(nodeId, srcTP, dstTp));
146         ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
147         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(this.mappingUtils)
148             .getOpenRoadmVersion("node1");
149         Mockito.doReturn(java.util.Optional.empty()).when(this.crossConnect).postCrossConnect(nodeId, 20L, srcTP,
150             dstTp);
151         ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
152             ServicePathDirection.A_TO_Z);
153         Assert.assertFalse(servicePathOutput.isSuccess());
154     }
155
156     @Ignore("need to be reviewed")
157     @Test
158     public void testSetupService() throws OpenRoadmInterfaceException {
159         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
160         Mockito.doNothing().when(this.openRoadmInterfaces).postEquipmentState(Mockito.anyString(),
161             Mockito.anyString(), Mockito.anyBoolean());
162         String [] interfaceTokens = {
163             StringConstants.NETWORK_TOKEN,
164             StringConstants.CLIENT_TOKEN,
165             StringConstants.TTP_TOKEN,
166             StringConstants.PP_TOKEN
167         };
168
169         String nodeId = "node1";
170
171         for (String srcToken : interfaceTokens) {
172             String srcTP = "src-" + srcToken;
173             MountPointUtils.writeMapping(nodeId, srcTP, this.deviceTransactionManager);
174             for (String dstToken : interfaceTokens) {
175                 String dstTp = "dst-" + dstToken;
176                 MountPointUtils.writeMapping(nodeId, dstTp, this.deviceTransactionManager);
177
178                 boolean connectingUsingCrossConnect = true;
179                 if (StringConstants.NETWORK_TOKEN.equals(srcToken)
180                     || StringConstants.CLIENT_TOKEN.equals(srcToken)) {
181                     connectingUsingCrossConnect = false;
182                 }
183                 if (StringConstants.NETWORK_TOKEN.equals(dstToken)
184                     || StringConstants.CLIENT_TOKEN.equals(dstToken)) {
185                     connectingUsingCrossConnect = false;
186                 }
187
188                 List<Nodes> nodes = new ArrayList<>();
189                 nodes.add(ServiceImplementationDataUtils.createNode(nodeId, srcTP, dstTp));
190                 ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
191                 Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(this.mappingUtils)
192                     .getOpenRoadmVersion("node1");
193                 for (ServicePathDirection servicePathDirection : ServicePathDirection.values()) {
194                     ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
195                         servicePathDirection);
196                     Assert.assertTrue(servicePathOutput.isSuccess());
197                     String expectedResult = "Roadm-connection successfully created for nodes: ";
198                     if (connectingUsingCrossConnect) {
199                         expectedResult = expectedResult + nodeId;
200                     }
201                     Assert.assertEquals(expectedResult, servicePathOutput.getResult());
202                     Assert.assertEquals(1, servicePathOutput.getNodeInterface().size());
203                     Assert.assertEquals(nodeId, servicePathOutput.getNodeInterface().get(0).getNodeId());
204                     if (!connectingUsingCrossConnect) { // No need to try both directions if not cross connect
205                         break;
206                     }
207                 }
208             }
209         }
210     }
211
212     @Test
213     public void testSetupServiceNulls() throws OpenRoadmInterfaceException {
214         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
215         String nodeId = "node1";
216         String srcTP = null;
217         String dstTp = null;
218         boolean connectingUsingCrossConnect = true;
219
220         List<Nodes> nodes = new ArrayList<>();
221         nodes.add(ServiceImplementationDataUtils.createNode(nodeId, srcTP, dstTp));
222         ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
223         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(this.mappingUtils)
224             .getOpenRoadmVersion("node1");
225         for (ServicePathDirection servicePathDirection : ServicePathDirection.values()) {
226             ServicePathOutput servicePathOutput = deviceRendererService.setupServicePath(servicePathInput,
227                 servicePathDirection);
228             Assert.assertTrue(servicePathOutput.isSuccess());
229             String expectedResult = "Roadm-connection successfully created for nodes: ";
230             expectedResult = expectedResult + nodeId;
231             Assert.assertEquals(expectedResult, servicePathOutput.getResult());
232             Assert.assertEquals(1, servicePathOutput.getNodeInterface().size());
233             Assert.assertEquals(nodeId, servicePathOutput.getNodeInterface().get(0).getNodeId());
234         }
235     }
236 }