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