SH-Renderer API code to handle OTN Step 1
[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.rev200520.ServiceDeleteInputBuilder;
61 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.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     private OtnDeviceRendererService otnDeviceRendererService;
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, null);
121
122         this.otnDeviceRendererService = new OtnDeviceRendererServiceImpl(openRoadmInterfaceFactory, crossConnect,
123             openRoadmInterfaces, this.deviceTransactionManager, null);
124
125     }
126
127     @Before
128     public void setUp() {
129         setMountPoint(new MountPointStub(getDataBroker()));
130         this.olmService = new OlmServiceStub();
131         this.olmService = Mockito.spy(this.olmService);
132         ListeningExecutorService executor =
133             MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUMBER_OF_THREADS));
134         this.networkModelWavelengthService = new NetworkModelWavelengthServiceImpl(getDataBroker());
135         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
136         this.rendererServiceOperations =  new RendererServiceOperationsImpl(this.deviceRenderer,
137             this.otnDeviceRendererService, olmService, getDataBroker(), this.networkModelWavelengthService,
138             notificationPublishService);
139
140     }
141
142
143     @Test
144     public void serviceDeleteOperationPp() throws ExecutionException, InterruptedException {
145         writePathDescription();
146         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
147         serviceDeleteInputBuilder.setServiceName("service 1");
148         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
149             .setRequestId("request1").build());
150         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
151             Mockito.anyString(), Mockito.eq(false));
152         ServiceDeleteOutput serviceDeleteOutput
153                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build()).get();
154         Assert.assertEquals(ResponseCodes.RESPONSE_OK,
155             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
156         Mockito.verify(this.crossConnect, Mockito.times(2))
157                 .deleteCrossConnect(Mockito.any(), Mockito.any(), Mockito.eq(false));
158     }
159
160     @Test
161     public void serviceDeleteOperationNoDescription() throws InterruptedException, ExecutionException {
162         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
163         serviceDeleteInputBuilder.setServiceName("service 1");
164         ServiceDeleteOutput serviceDeleteOutput
165                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build()).get();
166         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
167             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
168         Mockito.verify(this.crossConnect, Mockito.times(0))
169                 .deleteCrossConnect(Mockito.any(), Mockito.any(), Mockito.eq(false));
170     }
171
172     @Test
173     public void serviceDeleteOperationTearDownFailedAtoZ() throws ExecutionException, InterruptedException {
174         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
175             Mockito.anyString(), Mockito.eq(false));
176         Mockito.doReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
177             .setResult("Failed").build()).buildFuture()).when(this.olmService).servicePowerTurndown(Mockito.any());
178
179         writePathDescription();
180         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
181         serviceDeleteInputBuilder.setServiceName("service 1");
182         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
183                 .setRequestId("request1").build());
184         ListenableFuture<ServiceDeleteOutput> serviceDeleteOutput
185                 = this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build());
186         ServiceDeleteOutput output = serviceDeleteOutput.get();
187         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
188                 output.getConfigurationResponseCommon().getResponseCode());
189         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node1"), Mockito.any(),
190             Mockito.eq(false));
191         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node2"), Mockito.any(),
192             Mockito.eq(false));
193     }
194
195     @Test
196     public void serviceDeleteOperationTearDownFailedZtoA() throws ExecutionException, InterruptedException {
197         Mockito.doReturn(Collections.emptyList()).when(this.crossConnect).deleteCrossConnect(Mockito.anyString(),
198             Mockito.anyString(), Mockito.eq(false));
199         Mockito.when(this.olmService.servicePowerTurndown(Mockito.any()))
200             .thenReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
201                 .setResult("Success").build()).buildFuture())
202             .thenReturn(RpcResultBuilder.success((new ServicePowerTurndownOutputBuilder())
203                 .setResult("Failed").build()).buildFuture());
204
205         writePathDescription();
206         ServiceDeleteInputBuilder serviceDeleteInputBuilder = new ServiceDeleteInputBuilder();
207         serviceDeleteInputBuilder.setServiceName("service 1");
208         serviceDeleteInputBuilder.setServiceHandlerHeader((new ServiceHandlerHeaderBuilder())
209             .setRequestId("request1").build());
210         ServiceDeleteOutput serviceDeleteOutput =
211                 this.rendererServiceOperations.serviceDelete(serviceDeleteInputBuilder.build()).get();
212         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
213             serviceDeleteOutput.getConfigurationResponseCommon().getResponseCode());
214         Mockito.verify(this.olmService, Mockito.times(2)).servicePowerTurndown(Mockito.any());
215         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node1"), Mockito.any(),
216             Mockito.eq(false));
217         Mockito.verify(this.crossConnect, Mockito.times(0)).deleteCrossConnect(Mockito.eq("node2"), Mockito.any(),
218             Mockito.eq(false));
219     }
220
221     private void writePathDescription() throws ExecutionException, InterruptedException {
222         ServicePathsBuilder servicePathsBuilder = new ServicePathsBuilder();
223         servicePathsBuilder.setPathDescription(ServiceDeleteDataUtils
224             .createTransactionPathDescription(StringConstants.PP_TOKEN));
225         servicePathsBuilder.setServiceAEnd(ServiceDeleteDataUtils.getServiceAEndBuild().build())
226             .setServiceZEnd(ServiceDeleteDataUtils.getServiceZEndBuild().build());
227         servicePathsBuilder.withKey(new ServicePathsKey("service 1"));
228         servicePathsBuilder.setServiceHandlerHeader(new ServiceHandlerHeaderBuilder().setRequestId("Request 1")
229             .build());
230         InstanceIdentifier<ServicePaths> servicePathsInstanceIdentifier = InstanceIdentifier.create(
231             ServicePathList.class).child(ServicePaths.class, new ServicePathsKey("service 1"));
232         TransactionUtils.writeTransaction(
233             this.deviceTransactionManager,
234             "node1" + StringConstants.PP_TOKEN,
235             LogicalDatastoreType.OPERATIONAL,
236             servicePathsInstanceIdentifier,
237             servicePathsBuilder.build());
238     }
239 }