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