Minimal refactor of renderer to prepare flexgrid
[transportpce.git] / renderer / src / test / java / org / opendaylight / transportpce / renderer / provisiondevice / RendererServiceOperationsImplDeleteTest.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 com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.ListeningExecutorService;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.Collections;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Executors;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Ignore;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.mdsal.binding.api.MountPoint;
23 import org.opendaylight.mdsal.binding.api.MountPointService;
24 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.transportpce.common.ResponseCodes;
27 import org.opendaylight.transportpce.common.StringConstants;
28 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
29 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
30 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
31 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl221;
32 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
33 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
34 import org.opendaylight.transportpce.common.fixedflex.FixedFlexImpl;
35 import org.opendaylight.transportpce.common.fixedflex.FixedFlexInterface;
36 import org.opendaylight.transportpce.common.fixedflex.FlexGridImpl;
37 import org.opendaylight.transportpce.common.mapping.MappingUtils;
38 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
39 import org.opendaylight.transportpce.common.mapping.PortMapping;
40 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
41 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
42 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
43 import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
44 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
45 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
46 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
47 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
48 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl710;
49 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterface121;
50 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterface221;
51 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaceFactory;
52 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmOtnInterface221;
53 import org.opendaylight.transportpce.renderer.stub.OlmServiceStub;
54 import org.opendaylight.transportpce.renderer.utils.NotificationPublishServiceMock;
55 import org.opendaylight.transportpce.renderer.utils.ServiceDeleteDataUtils;
56 import org.opendaylight.transportpce.renderer.utils.TransactionUtils;
57 import org.opendaylight.transportpce.test.AbstractTest;
58 import org.opendaylight.transportpce.test.stub.MountPointServiceStub;
59 import org.opendaylight.transportpce.test.stub.MountPointStub;
60 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutputBuilder;
61 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.TransportpceOlmService;
62 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.ServiceDeleteInputBuilder;
63 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.ServiceDeleteOutput;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.ConnectionType;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceAEnd;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.service.ServiceAEndBuilder;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.format.rev190531.ServiceFormat;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.ServicesBuilder;
70 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.service.handler.header.ServiceHandlerHeaderBuilder;
71 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
72 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
73 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsBuilder;
74 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
77 import org.opendaylight.yangtools.yang.common.Uint32;
78
79 @Ignore
80 public class RendererServiceOperationsImplDeleteTest extends AbstractTest {
81
82     private static final int NUMBER_OF_THREADS = 4;
83     private DeviceTransactionManager deviceTransactionManager;
84     private RendererServiceOperationsImpl rendererServiceOperations;
85     private OpenRoadmInterfaces openRoadmInterfaces;
86     private DeviceRendererService deviceRenderer;
87     private PortMapping portMapping;
88     private CrossConnect crossConnect;
89     private TransportpceOlmService olmService;
90     private MappingUtils mappingUtils;
91     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
92     private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl221;
93     private OpenRoadmInterfacesImpl710 openRoadmInterfacesImpl710;
94     private PortMappingVersion710 portMappingVersion710;
95     private PortMappingVersion221 portMappingVersion22;
96     private PortMappingVersion121 portMappingVersion121;
97     private CrossConnectImpl121 crossConnectImpl121;
98     private CrossConnectImpl221 crossConnectImpl221;
99     private OtnDeviceRendererService otnDeviceRendererService;
100
101     private void setMountPoint(MountPoint mountPoint) {
102         MountPointService mountPointService = new MountPointServiceStub(mountPoint);
103         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
104         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
105         this.openRoadmInterfacesImpl221 = new OpenRoadmInterfacesImpl221(deviceTransactionManager);
106         this.openRoadmInterfacesImpl710 = new OpenRoadmInterfacesImpl710(deviceTransactionManager);
107         this.mappingUtils = new MappingUtilsImpl(getDataBroker());
108         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl(deviceTransactionManager, mappingUtils,
109             openRoadmInterfacesImpl121, openRoadmInterfacesImpl221, openRoadmInterfacesImpl710);
110         this.openRoadmInterfaces = Mockito.spy(this.openRoadmInterfaces);
111         this.portMappingVersion710 =
112             new PortMappingVersion710(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
113         this.portMappingVersion22 =
114             new PortMappingVersion221(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
115         this.portMappingVersion121 =
116             new PortMappingVersion121(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
117         this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion710, this.portMappingVersion22,
118             this.portMappingVersion121);
119         this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
120         this.crossConnectImpl221 = new CrossConnectImpl221(deviceTransactionManager);
121         this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
122             this.crossConnectImpl221);
123         this.crossConnect = Mockito.spy(crossConnect);
124         FixedFlexInterface fixedFlexInterface = new FixedFlexImpl();
125         FlexGridImpl flexGrid = new FlexGridImpl();
126         OpenRoadmInterface121 openRoadmInterface121 = new OpenRoadmInterface121(portMapping,openRoadmInterfaces);
127         OpenRoadmInterface221 openRoadmInterface221 = new OpenRoadmInterface221(portMapping,openRoadmInterfaces,
128             fixedFlexInterface, flexGrid);
129         OpenRoadmOtnInterface221 openRoadmOTNInterface = new OpenRoadmOtnInterface221(portMapping, openRoadmInterfaces);
130         OpenRoadmInterfaceFactory openRoadmInterfaceFactory = new OpenRoadmInterfaceFactory(this.mappingUtils,
131              openRoadmInterface121, openRoadmInterface221, openRoadmOTNInterface);
132
133         this.deviceRenderer = new DeviceRendererServiceImpl(this.getDataBroker(),
134             this.deviceTransactionManager, openRoadmInterfaceFactory, openRoadmInterfaces, crossConnect,
135             this.portMapping, null);
136
137         this.otnDeviceRendererService = new OtnDeviceRendererServiceImpl(openRoadmInterfaceFactory, crossConnect,
138             openRoadmInterfaces, this.deviceTransactionManager, null);
139
140     }
141
142     @Before
143     public void setUp() {
144         setMountPoint(new MountPointStub(getDataBroker()));
145         this.olmService = new OlmServiceStub();
146         this.olmService = Mockito.spy(this.olmService);
147         ListeningExecutorService executor =
148             MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUMBER_OF_THREADS));
149         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
150         this.rendererServiceOperations =  new RendererServiceOperationsImpl(this.deviceRenderer,
151             this.otnDeviceRendererService, olmService, getDataBroker(), notificationPublishService);
152
153     }
154
155
156     @Test
157     public void serviceDeleteOperationPp() throws ExecutionException, InterruptedException {
158         writePathDescription();
159         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
160         serviceDeleteInputBuilder.setServiceName("service 1");
161         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
162             .setRequestId("request1").build());
163         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
164             Mockito.anyString(), Mockito.eq(false));
165         ServiceAEnd serviceAEnd = new ServiceAEndBuilder()
166             .setServiceFormat(ServiceFormat.Ethernet)
167             .setServiceRate(Uint32.valueOf("100"))
168             .build();
169         Services service = new ServicesBuilder()
170             .setConnectionType(ConnectionType.Service)
171             .setServiceAEnd(serviceAEnd)
172             .build();
173         ServiceDeleteOutput serviceDeleteOutput
174                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build(), service).get();
175         Assert.assertEquals(ResponseCodes.RESPONSE_OK,
176             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
177         Mockito.verify(this.crossConnect, Mockito.times(2))
178                 .deleteCrossConnect(Mockito.any(), Mockito.any(), Mockito.eq(false));
179     }
180
181     @Test
182     public void serviceDeleteOperationNoDescription() throws InterruptedException, ExecutionException {
183         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
184         serviceDeleteInputBuilder.setServiceName("service 1");
185         ServiceDeleteOutput serviceDeleteOutput
186                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build(), null).get();
187         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
188             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
189         Mockito.verify(this.crossConnect, Mockito.times(0))
190                 .deleteCrossConnect(Mockito.any(), Mockito.any(), Mockito.eq(false));
191     }
192
193     @Test
194     public void serviceDeleteOperationTearDownFailedAtoZ() throws ExecutionException, InterruptedException {
195         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
196             Mockito.anyString(), Mockito.eq(false));
197         Mockito.doReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
198             .setResult("Failed").build()).buildFuture()).when(this.olmService).servicePowerTurndown(Mockito.any());
199
200         writePathDescription();
201         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
202         serviceDeleteInputBuilder.setServiceName("service 1");
203         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
204                 .setRequestId("request1").build());
205         ServiceAEnd serviceAEnd = new ServiceAEndBuilder()
206             .setServiceFormat(ServiceFormat.Ethernet)
207             .setServiceRate(Uint32.valueOf("100"))
208             .build();
209         Services service = new ServicesBuilder()
210             .setConnectionType(ConnectionType.Service)
211             .setServiceAEnd(serviceAEnd)
212             .build();
213         ListenableFuture<ServiceDeleteOutput> serviceDeleteOutput
214                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build(), service);
215         ServiceDeleteOutput output = serviceDeleteOutput.get();
216         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
217                 output.getConfigurationResponseCommon().getResponseCode());
218         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node1"), Mockito.any(),
219             Mockito.eq(false));
220         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node2"), Mockito.any(),
221             Mockito.eq(false));
222     }
223
224     @Test
225     public void serviceDeleteOperationTearDownFailedZtoA() throws ExecutionException, InterruptedException {
226         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
227             Mockito.anyString(), Mockito.eq(false));
228         Mockito.when(this.olmService.servicePowerTurndown(Mockito.any()))
229             .thenReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
230                 .setResult("Success").build()).buildFuture())
231             .thenReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
232                 .setResult("Failed").build()).buildFuture());
233
234         writePathDescription();
235         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
236         serviceDeleteInputBuilder.setServiceName("service 1");
237         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
238             .setRequestId("request1").build());
239         ServiceAEnd serviceAEnd = new ServiceAEndBuilder()
240             .setServiceFormat(ServiceFormat.Ethernet)
241             .setServiceRate(Uint32.valueOf("100"))
242             .build();
243         Services service = new ServicesBuilder()
244             .setConnectionType(ConnectionType.Service)
245             .setServiceAEnd(serviceAEnd)
246             .build();
247         ServiceDeleteOutput serviceDeleteOutput =
248                 this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build(), service).get();
249         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
250             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
251         Mockito.verify(this.olmService, Mockito.times(2)).servicePowerTurndown(Mockito.any());
252         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node1"), Mockito.any(),
253             Mockito.eq(false));
254         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node2"), Mockito.any(),
255             Mockito.eq(false));
256     }
257
258     private void writePathDescription() throws ExecutionException, InterruptedException {
259         ServicePathsBuilder servicePathsBuilder = new ServicePathsBuilder();
260         servicePathsBuilder.setPathDescription(ServiceDeleteDataUtils
261             .createTransactionPathDescription(StringConstants.PP_TOKEN));
262         servicePathsBuilder.setServiceAEnd(ServiceDeleteDataUtils.getServiceAEndBuild().build())
263             .setServiceZEnd(ServiceDeleteDataUtils.getServiceZEndBuild().build());
264         servicePathsBuilder.withKey(new ServicePathsKey("service 1"));
265         servicePathsBuilder.setServiceHandlerHeader(new ServiceHandlerHeaderBuilder().setRequestId("Request 1")
266             .build());
267         InstanceIdentifier<ServicePaths> servicePathsInstanceIdentifier = InstanceIdentifier.create(
268             ServicePathList.class).child(ServicePaths.class, new ServicePathsKey("service 1"));
269         TransactionUtils.writeTransaction(
270             this.deviceTransactionManager,
271             "node1" + StringConstants.PP_TOKEN,
272             LogicalDatastoreType.OPERATIONAL,
273             servicePathsInstanceIdentifier,
274             servicePathsBuilder.build());
275     }
276 }