fix import extra separations
[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.Test;
20 import org.mockito.Mockito;
21 import org.opendaylight.mdsal.binding.api.MountPoint;
22 import org.opendaylight.mdsal.binding.api.MountPointService;
23 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.transportpce.common.ResponseCodes;
26 import org.opendaylight.transportpce.common.StringConstants;
27 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
28 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
29 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
30 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl221;
31 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
32 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
33 import org.opendaylight.transportpce.common.fixedflex.FixedFlexImpl;
34 import org.opendaylight.transportpce.common.fixedflex.FixedFlexInterface;
35 import org.opendaylight.transportpce.common.mapping.MappingUtils;
36 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
37 import org.opendaylight.transportpce.common.mapping.PortMapping;
38 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
39 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
40 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
41 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
42 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
43 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
44 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
45 import org.opendaylight.transportpce.renderer.NetworkModelWavelengthService;
46 import org.opendaylight.transportpce.renderer.NetworkModelWavelengthServiceImpl;
47 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterface121;
48 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterface221;
49 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaceFactory;
50 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmOtnInterface221;
51 import org.opendaylight.transportpce.renderer.stub.MountPointServiceStub;
52 import org.opendaylight.transportpce.renderer.stub.MountPointStub;
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.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutputBuilder;
59 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.TransportpceOlmService;
60 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteInputBuilder;
61 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutput;
62 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.service.handler.header.ServiceHandlerHeaderBuilder;
63 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
64 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
65 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsBuilder;
66 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
69
70 public class RendererServiceOperationsImplDeleteTest extends AbstractTest {
71
72     private static final int NUMBER_OF_THREADS = 4;
73     private DeviceTransactionManager deviceTransactionManager;
74     private RendererServiceOperationsImpl rendererServiceOperations;
75     private OpenRoadmInterfaces openRoadmInterfaces;
76     private DeviceRendererService deviceRenderer;
77     private PortMapping portMapping;
78     private CrossConnect crossConnect;
79     private NetworkModelWavelengthService networkModelWavelengthService;
80     private TransportpceOlmService olmService;
81     private MappingUtils mappingUtils;
82     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
83     private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl221;
84     private PortMappingVersion221 portMappingVersion22;
85     private PortMappingVersion121 portMappingVersion121;
86     private CrossConnectImpl121 crossConnectImpl121;
87     private CrossConnectImpl221 crossConnectImpl221;
88
89     private void setMountPoint(MountPoint mountPoint) {
90         MountPointService mountPointService = new MountPointServiceStub(mountPoint);
91         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
92         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
93         this.openRoadmInterfacesImpl221 = new OpenRoadmInterfacesImpl221(deviceTransactionManager);
94         this.mappingUtils = new MappingUtilsImpl(getDataBroker());
95         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl(deviceTransactionManager, mappingUtils,
96             openRoadmInterfacesImpl121, openRoadmInterfacesImpl221);
97         this.openRoadmInterfaces = Mockito.spy(this.openRoadmInterfaces);
98         this.portMappingVersion22 =
99             new PortMappingVersion221(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
100         this.portMappingVersion121 =
101             new PortMappingVersion121(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
102         this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion22,
103             this.portMappingVersion121);
104         this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
105         this.crossConnectImpl221 = new CrossConnectImpl221(deviceTransactionManager);
106         this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
107             this.crossConnectImpl221);
108         this.crossConnect = Mockito.spy(crossConnect);
109         FixedFlexInterface fixedFlexInterface = new FixedFlexImpl();
110         OpenRoadmInterface121 openRoadmInterface121 = new OpenRoadmInterface121(portMapping,openRoadmInterfaces);
111         OpenRoadmInterface221 openRoadmInterface221 = new OpenRoadmInterface221(portMapping,openRoadmInterfaces,
112             fixedFlexInterface);
113         OpenRoadmOtnInterface221 openRoadmOTNInterface = new OpenRoadmOtnInterface221(portMapping, openRoadmInterfaces);
114         OpenRoadmInterfaceFactory openRoadmInterfaceFactory = new OpenRoadmInterfaceFactory(this.mappingUtils,
115              openRoadmInterface121, openRoadmInterface221, openRoadmOTNInterface);
116
117         this.deviceRenderer = new DeviceRendererServiceImpl(this.getDataBroker(),
118             this.deviceTransactionManager, openRoadmInterfaceFactory, openRoadmInterfaces, crossConnect,
119             this.portMapping, null);
120     }
121
122     @Before
123     public void setUp() {
124         setMountPoint(new MountPointStub(getDataBroker()));
125         this.olmService = new OlmServiceStub();
126         this.olmService = Mockito.spy(this.olmService);
127         ListeningExecutorService executor =
128             MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUMBER_OF_THREADS));
129         this.networkModelWavelengthService = new NetworkModelWavelengthServiceImpl(getDataBroker());
130         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
131         this.rendererServiceOperations =  new RendererServiceOperationsImpl(this.deviceRenderer, olmService,
132                 getDataBroker(), this.networkModelWavelengthService, notificationPublishService);
133
134     }
135
136
137     @Test
138     public void serviceDeleteOperationPp() throws ExecutionException, InterruptedException {
139         writePathDescription();
140         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
141         serviceDeleteInputBuilder.setServiceName("service 1");
142         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
143             .setRequestId("request1").build());
144         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
145             Mockito.anyString(), Mockito.eq(false));
146         ServiceDeleteOutput serviceDeleteOutput
147                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build()).get();
148         Assert.assertEquals(ResponseCodes.RESPONSE_OK,
149             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
150         Mockito.verify(this.crossConnect, Mockito.times(2))
151                 .deleteCrossConnect(Mockito.any(), Mockito.any(), Mockito.eq(false));
152     }
153
154     @Test
155     public void serviceDeleteOperationNoDescription() throws InterruptedException, ExecutionException {
156         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
157         serviceDeleteInputBuilder.setServiceName("service 1");
158         ServiceDeleteOutput serviceDeleteOutput
159                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build()).get();
160         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
161             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
162         Mockito.verify(this.crossConnect, Mockito.times(0))
163                 .deleteCrossConnect(Mockito.any(), Mockito.any(), Mockito.eq(false));
164     }
165
166     @Test
167     public void serviceDeleteOperationTearDownFailedAtoZ() throws ExecutionException, InterruptedException {
168         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
169             Mockito.anyString(), Mockito.eq(false));
170         Mockito.doReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
171             .setResult("Failed").build()).buildFuture()).when(this.olmService).servicePowerTurndown(Mockito.any());
172
173         writePathDescription();
174         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
175         serviceDeleteInputBuilder.setServiceName("service 1");
176         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
177                 .setRequestId("request1").build());
178         ListenableFuture<ServiceDeleteOutput> serviceDeleteOutput
179                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build());
180         ServiceDeleteOutput output = serviceDeleteOutput.get();
181         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
182                 output.getConfigurationResponseCommon().getResponseCode());
183         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node1"), Mockito.any(),
184             Mockito.eq(false));
185         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node2"), Mockito.any(),
186             Mockito.eq(false));
187     }
188
189     @Test
190     public void serviceDeleteOperationTearDownFailedZtoA() throws ExecutionException, InterruptedException {
191         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
192             Mockito.anyString(), Mockito.eq(false));
193         Mockito.when(this.olmService.servicePowerTurndown(Mockito.any()))
194             .thenReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
195                 .setResult("Success").build()).buildFuture())
196             .thenReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
197                 .setResult("Failed").build()).buildFuture());
198
199         writePathDescription();
200         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
201         serviceDeleteInputBuilder.setServiceName("service 1");
202         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
203             .setRequestId("request1").build());
204         ServiceDeleteOutput serviceDeleteOutput =
205                 this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build()).get();
206         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
207             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
208         Mockito.verify(this.olmService, Mockito.times(2)).servicePowerTurndown(Mockito.any());
209         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node1"), Mockito.any(),
210             Mockito.eq(false));
211         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node2"), Mockito.any(),
212             Mockito.eq(false));
213     }
214
215     private void writePathDescription() throws ExecutionException, InterruptedException {
216         ServicePathsBuilder servicePathsBuilder = new ServicePathsBuilder();
217         servicePathsBuilder.setPathDescription(ServiceDeleteDataUtils
218             .createTransactionPathDescription(StringConstants.PP_TOKEN));
219         servicePathsBuilder.setServiceAEnd(ServiceDeleteDataUtils.getServiceAEndBuild().build())
220             .setServiceZEnd(ServiceDeleteDataUtils.getServiceZEndBuild().build());
221         servicePathsBuilder.withKey(new ServicePathsKey("service 1"));
222         servicePathsBuilder.setServiceHandlerHeader(new ServiceHandlerHeaderBuilder().setRequestId("Request 1")
223             .build());
224         InstanceIdentifier<ServicePaths> servicePathsInstanceIdentifier = InstanceIdentifier.create(
225             ServicePathList.class).child(ServicePaths.class, new ServicePathsKey("service 1"));
226         TransactionUtils.writeTransaction(
227             this.deviceTransactionManager,
228             "node1" + StringConstants.PP_TOKEN,
229             LogicalDatastoreType.OPERATIONAL,
230             servicePathsInstanceIdentifier,
231             servicePathsBuilder.build());
232     }
233 }