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