Update Renderer Module
[transportpce.git] / renderer / src / test / java / org / opendaylight / transportpce / renderer / provisiondevice / DeviceRendererServiceImplDeleteTest.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 java.util.concurrent.ExecutionException;
14
15 import org.junit.Assert;
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.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.transportpce.common.StringConstants;
22 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
23 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
24 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
25 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl22;
26 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
27 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
28 import org.opendaylight.transportpce.common.fixedflex.FixedFlexImpl;
29 import org.opendaylight.transportpce.common.fixedflex.FixedFlexInterface;
30 import org.opendaylight.transportpce.common.mapping.MappingUtils;
31 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
32 import org.opendaylight.transportpce.common.mapping.PortMapping;
33 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
34 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
35 import org.opendaylight.transportpce.common.mapping.PortMappingVersion22;
36 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
37 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
38 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
39 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
40 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl22;
41 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterface121;
42 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterface22;
43 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaceFactory;
44 import org.opendaylight.transportpce.renderer.stub.MountPointServiceStub;
45 import org.opendaylight.transportpce.renderer.utils.MountPointUtils;
46 import org.opendaylight.transportpce.renderer.utils.ServiceImplementationDataUtils;
47 import org.opendaylight.transportpce.renderer.utils.TransactionUtils;
48 import org.opendaylight.transportpce.test.AbstractTest;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev170228.ServicePathInput;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev170228.ServicePathOutput;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.DestinationBuilder;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.connection.SourceBuilder;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsBuilder;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
57 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.olm.renderer.input.Nodes;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59
60 public class DeviceRendererServiceImplDeleteTest extends AbstractTest {
61
62     private DeviceRendererService deviceRendererService;
63     private CrossConnect crossConnect;
64     private OpenRoadmInterfaces openRoadmInterfaces;
65     private OpenRoadmInterfaceFactory openRoadmInterfaceFactory;
66     private DeviceTransactionManager deviceTransactionManager;
67     private MappingUtils mappingUtils;
68     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
69     private OpenRoadmInterfacesImpl22 openRoadmInterfacesImpl22;
70     private PortMappingVersion22 portMappingVersion22;
71     private PortMappingVersion121 portMappingVersion121;
72     private CrossConnectImpl121 crossConnectImpl121;
73     private CrossConnectImpl22 crossConnectImpl22;
74
75     private void setMountPoint(MountPoint mountPoint) {
76         MountPointService mountPointService = new MountPointServiceStub(mountPoint);
77         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
78         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(this.deviceTransactionManager);
79         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl22(this.deviceTransactionManager);
80         this.mappingUtils = new MappingUtilsImpl(getDataBroker());
81         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl(deviceTransactionManager, mappingUtils,
82             openRoadmInterfacesImpl121, openRoadmInterfacesImpl22);
83         this.openRoadmInterfaces = Mockito.spy(this.openRoadmInterfaces);
84         this.portMappingVersion22 = new PortMappingVersion22(getDataBroker(), this.deviceTransactionManager,
85             this.openRoadmInterfaces);
86         this.portMappingVersion121 = new PortMappingVersion121(getDataBroker(), this.deviceTransactionManager,
87             this.openRoadmInterfaces);
88         PortMapping portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion22, this.mappingUtils,
89             this.portMappingVersion121);
90         FixedFlexInterface fixedFlexInterface = new FixedFlexImpl();
91         OpenRoadmInterface121 openRoadmInterface121 = new OpenRoadmInterface121(portMapping,openRoadmInterfaces);
92         OpenRoadmInterface22 openRoadmInterface22 = new OpenRoadmInterface22(portMapping,openRoadmInterfaces,
93             fixedFlexInterface);
94         this.openRoadmInterfaceFactory = new OpenRoadmInterfaceFactory(this.mappingUtils,openRoadmInterface121,
95             openRoadmInterface22);
96
97         this.crossConnectImpl121 = new CrossConnectImpl121(this.deviceTransactionManager);
98         this.crossConnectImpl22 = new CrossConnectImpl22(this.deviceTransactionManager);
99         this.crossConnect = new CrossConnectImpl(this.deviceTransactionManager, this.mappingUtils,
100             this.crossConnectImpl121, this.crossConnectImpl22);
101         this.crossConnect = Mockito.spy(this.crossConnect);
102         this.deviceRendererService = new DeviceRendererServiceImpl(this.getDataBroker(),
103             this.deviceTransactionManager, this.openRoadmInterfaceFactory, this.openRoadmInterfaces,
104             this.crossConnect, portMapping);
105     }
106
107     @Test
108     public void testSetupServiceWhenDeviceIsNotMounted() {
109         setMountPoint(null);
110         ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs();
111         ServicePathOutput servicePathOutput = deviceRendererService.deleteServicePath(servicePathInput);
112         Assert.assertFalse(servicePathOutput.isSuccess());
113         Assert.assertEquals("node1 is not mounted on the controller",
114             servicePathOutput.getResult());
115     }
116
117     @Test
118     public void testDeleteServiceSuccess() throws OpenRoadmInterfaceException {
119         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
120         String [] interfaceTokens = {
121             StringConstants.NETWORK_TOKEN,
122             StringConstants.CLIENT_TOKEN,
123             StringConstants.TTP_TOKEN,
124             StringConstants.PP_TOKEN
125         };
126
127         String nodeId = "node1";
128         Mockito.doReturn(true).when(this.crossConnect).deleteCrossConnect(Mockito.eq(nodeId), Mockito.anyString());
129         Mockito.doNothing().when(this.openRoadmInterfaces).deleteInterface(Mockito.eq(nodeId), Mockito.anyString());
130
131         for (String srcToken : interfaceTokens) {
132             String srcTP = "src-" + srcToken;
133             for (String dstToken : interfaceTokens) {
134                 String dstTp = "dst-" + dstToken;
135
136                 List<Nodes> nodes = new ArrayList<>();
137                 nodes.add(ServiceImplementationDataUtils.createNode(nodeId, srcTP, dstTp));
138                 ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
139
140                 ServicePathOutput servicePathOutput = deviceRendererService.deleteServicePath(servicePathInput);
141                 Assert.assertTrue(servicePathOutput.isSuccess());
142                 Assert.assertEquals("Request processed", servicePathOutput.getResult());
143             }
144         }
145     }
146
147     @Test
148     public void testDeleteServiceFailure() throws OpenRoadmInterfaceException {
149         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
150         String [] interfaceTokens = {
151             StringConstants.NETWORK_TOKEN,
152             StringConstants.CLIENT_TOKEN,
153             StringConstants.TTP_TOKEN,
154             StringConstants.PP_TOKEN };
155         String nodeId = "node1";
156         Mockito.doReturn(true).when(this.crossConnect).deleteCrossConnect(Mockito.eq(nodeId), Mockito.anyString());
157         Mockito.doThrow(OpenRoadmInterfaceException.class).when(this.openRoadmInterfaces)
158             .deleteInterface(Mockito.eq(nodeId), Mockito.anyString());
159
160         for (String srcToken : interfaceTokens) {
161             String srcTP = "src-" + srcToken;
162             for (String dstToken : interfaceTokens) {
163                 String dstTp = "dst-" + dstToken;
164
165                 List<Nodes> nodes = new ArrayList<>();
166                 nodes.add(ServiceImplementationDataUtils.createNode(nodeId, srcTP, dstTp));
167                 ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
168
169                 ServicePathOutput servicePathOutput = deviceRendererService.deleteServicePath(servicePathInput);
170                 Assert.assertFalse(servicePathOutput.isSuccess());
171                 Assert.assertNotEquals("Request processed", servicePathOutput.getResult());
172             }
173         }
174     }
175
176     @Test
177     public void testDeleteServiceNulls() throws OpenRoadmInterfaceException {
178         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
179
180         String nodeId = "node1";
181         Mockito.doReturn(false).when(this.crossConnect).deleteCrossConnect(Mockito.eq(nodeId), Mockito.anyString());
182         Mockito.doThrow(OpenRoadmInterfaceException.class).when(this.openRoadmInterfaces)
183             .deleteInterface(Mockito.eq(nodeId), Mockito.anyString());
184
185         List<Nodes> nodes = new ArrayList<>();
186         nodes.add(ServiceImplementationDataUtils.createNode(nodeId, null, null));
187         nodes.add(ServiceImplementationDataUtils.createNode(nodeId, "src-" + StringConstants.PP_TOKEN, null));
188         ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
189
190         ServicePathOutput servicePathOutput = deviceRendererService.deleteServicePath(servicePathInput);
191         Assert.assertTrue(servicePathOutput.isSuccess());
192         Assert.assertEquals("Request processed", servicePathOutput.getResult());
193     }
194
195     @Test
196     public void testDeleteServiceFailedCrossConnect() throws OpenRoadmInterfaceException {
197         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
198
199         String nodeId = "node1";
200
201         Mockito.doReturn(false).when(this.crossConnect).deleteCrossConnect(Mockito.eq(nodeId), Mockito.anyString());
202         Mockito.doNothing().when(this.openRoadmInterfaces).deleteInterface(Mockito.eq(nodeId), Mockito.anyString());
203
204         String srcTP = "src-" + StringConstants.TTP_TOKEN;
205         String dstTp = "dst-" + StringConstants.PP_TOKEN;
206
207         List<Nodes> nodes = new ArrayList<>();
208         nodes.add(ServiceImplementationDataUtils.createNode(nodeId, srcTP, dstTp));
209         ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
210
211         ServicePathOutput servicePathOutput = deviceRendererService.deleteServicePath(servicePathInput);
212         Assert.assertTrue(servicePathOutput.isSuccess());
213         Assert.assertEquals("Request processed", servicePathOutput.getResult());
214     }
215
216     @Test
217     public void testDeleteServiceInterfacesUsedByXc() throws OpenRoadmInterfaceException, ExecutionException,
218         InterruptedException {
219         setMountPoint(MountPointUtils.getMountPoint(new ArrayList<>(), getDataBroker()));
220
221         String nodeId = "node1";
222
223         Mockito.doReturn(true).when(this.crossConnect).deleteCrossConnect(Mockito.eq(nodeId), Mockito.anyString());
224         Mockito.doThrow(OpenRoadmInterfaceException.class).when(this.openRoadmInterfaces)
225             .deleteInterface(Mockito.eq(nodeId), Mockito.anyString());
226
227         String srcTp = "src-" + StringConstants.PP_TOKEN;
228         String dstTp = "dst-" + StringConstants.TTP_TOKEN;
229         Long waveNumber = 20L;
230
231         String connectionNumber = dstTp + "-" + srcTp + "-" + waveNumber;
232         RoadmConnectionsBuilder roadmConnectionsBuilder = new RoadmConnectionsBuilder();
233         roadmConnectionsBuilder.setConnectionNumber(connectionNumber)
234             .withKey(new RoadmConnectionsKey(connectionNumber));
235         String interfaceName = this.openRoadmInterfaceFactory.createOpenRoadmOchInterfaceName(srcTp, waveNumber);
236         roadmConnectionsBuilder.setSource((new SourceBuilder()).setSrcIf(interfaceName).build());
237         interfaceName = this.openRoadmInterfaceFactory.createOpenRoadmOchInterfaceName(dstTp, waveNumber);
238         roadmConnectionsBuilder.setWavelengthNumber(20L);
239         roadmConnectionsBuilder.setDestination((new DestinationBuilder()).setDstIf(interfaceName).build());
240         InstanceIdentifier<RoadmConnections> xciid = InstanceIdentifier.create(OrgOpenroadmDevice.class)
241             .child(RoadmConnections.class, new RoadmConnectionsKey(connectionNumber));
242         TransactionUtils.writeTransaction(this.deviceTransactionManager, nodeId, LogicalDatastoreType.CONFIGURATION,
243             xciid, roadmConnectionsBuilder.build());
244
245         List<Nodes> nodes = new ArrayList<>();
246         nodes.add(ServiceImplementationDataUtils.createNode(nodeId, srcTp, dstTp));
247         ServicePathInput servicePathInput = ServiceImplementationDataUtils.buildServicePathInputs(nodes);
248
249         ServicePathOutput servicePathOutput = deviceRendererService.deleteServicePath(servicePathInput);
250         Assert.assertTrue(servicePathOutput.isSuccess());
251         Assert.assertEquals("Request processed", servicePathOutput.getResult());
252     }
253 }