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