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