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