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