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