Merge "fix netconf testtool portmapping"
[transportpce.git] / renderer / src / test / java / org / opendaylight / transportpce / renderer / provisiondevice / RendererServiceOperationsImplTest.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 package org.opendaylight.transportpce.renderer.provisiondevice;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mockito;
16 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
17 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
18 import org.opendaylight.transportpce.common.ResponseCodes;
19 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
20 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
21 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
22 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
23 import org.opendaylight.transportpce.common.mapping.PortMapping;
24 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
25 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
26 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
27 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
28 import org.opendaylight.transportpce.renderer.NetworkModelWavelengthService;
29 import org.opendaylight.transportpce.renderer.NetworkModelWavelengthServiceImpl;
30 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaceFactory;
31 import org.opendaylight.transportpce.renderer.stub.MountPointServiceStub;
32 import org.opendaylight.transportpce.renderer.stub.MountPointStub;
33 import org.opendaylight.transportpce.renderer.stub.OlmServiceStub;
34 import org.opendaylight.transportpce.renderer.utils.MountPointUtils;
35 import org.opendaylight.transportpce.renderer.utils.ServiceDataUtils;
36 import org.opendaylight.transportpce.test.AbstractTest;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev161014.PmGranularity;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.types.rev161014.ResourceTypeEnum;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.ServiceImplementationRequestInput;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.ServiceImplementationRequestOutput;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.olm.get.pm.input.ResourceIdentifierBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.GetPmOutputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.OlmService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm.output.Measurements;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm.output.MeasurementsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.ServicePathOutputBuilder;
50 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
51
52 public class RendererServiceOperationsImplTest extends AbstractTest {
53
54     private MountPointService mountPointService;
55     private DeviceTransactionManager deviceTransactionManager;
56     private RendererServiceOperationsImpl rendererServiceOperations;
57     private OpenRoadmInterfaces openRoadmInterfaces;
58     private DeviceRendererService deviceRenderer;
59     private PortMapping portMapping;
60     private OpenRoadmInterfaceFactory openRoadmInterfaceFactory;
61     private CrossConnect crossConnect;
62     private OlmService olmService;
63     private NetworkModelWavelengthService networkModelWavelengthService;
64
65     private void setMountPoint(MountPoint mountPoint) {
66         this.mountPointService = new MountPointServiceStub(mountPoint);
67         this.deviceTransactionManager = new DeviceTransactionManagerImpl(this.mountPointService, 3000);
68         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl(this.deviceTransactionManager);
69         this.openRoadmInterfaces = Mockito.spy(this.openRoadmInterfaces);
70         this.portMapping = new PortMappingImpl(this.getDataBroker(), this.deviceTransactionManager,
71             openRoadmInterfaces);
72         this.openRoadmInterfaceFactory = new OpenRoadmInterfaceFactory(portMapping,
73             openRoadmInterfaces);
74         this.crossConnect = new CrossConnectImpl(this.deviceTransactionManager);
75     }
76
77     @Before
78     public void setUp() throws OpenRoadmInterfaceException {
79         setMountPoint(new MountPointStub(getDataBroker()));
80         this.olmService = new OlmServiceStub();
81         this.networkModelWavelengthService = new NetworkModelWavelengthServiceImpl(getDataBroker());
82         this.deviceRenderer = new DeviceRendererServiceImpl(this.getDataBroker(),
83             this.deviceTransactionManager, openRoadmInterfaceFactory, openRoadmInterfaces, crossConnect, portMapping);
84         Mockito.doNothing().when(this.openRoadmInterfaces).postEquipmentState(Mockito.anyString(),
85             Mockito.anyString(), Mockito.anyBoolean());
86
87         this.olmService = Mockito.spy(this.olmService);
88         this.deviceRenderer = Mockito.spy(this.deviceRenderer);
89         this.rendererServiceOperations =  new RendererServiceOperationsImpl(this.deviceRenderer, this.olmService,
90             getDataBroker(), this.networkModelWavelengthService);
91
92         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
93             .setSuccess(true);
94         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
95             Mockito.any());
96     }
97
98     @Test
99     public void serviceImplementationTerminationPointAsResourceTtp() {
100
101         ServiceImplementationRequestInput input = ServiceDataUtils
102             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.TTP_TOKEN);
103         writePortMapping(input, OpenRoadmInterfacesImpl.TTP_TOKEN);
104         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
105             .setSuccess(true);
106         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
107             Mockito.any());
108         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
109         Assert.assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
110
111     }
112
113     @Test
114     public void serviceImplementationTerminationPointAsResourceTtp2() {
115
116         ServiceImplementationRequestInput input = ServiceDataUtils
117             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.TTP_TOKEN);
118         writePortMapping(input, OpenRoadmInterfacesImpl.TTP_TOKEN);
119         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
120             .setSuccess(true);
121         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
122             Mockito.any());
123         Mockito.doReturn(RpcResultBuilder.failed().buildFuture()).when(this.olmService)
124             .servicePowerSetup(Mockito.any());
125         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
126         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
127
128     }
129
130     @Test
131     public void serviceImplementationTerminationPointAsResourcePp() {
132
133         ServiceImplementationRequestInput input = ServiceDataUtils
134             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.PP_TOKEN);
135         writePortMapping(input, OpenRoadmInterfacesImpl.PP_TOKEN);
136         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
137             .setSuccess(true);
138         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
139             Mockito.any());
140         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
141         Assert.assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
142
143     }
144
145     @Test
146     public void serviceImplementationTerminationPointAsResourceNetwork() {
147
148         ServiceImplementationRequestInput input = ServiceDataUtils
149             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
150         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
151         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
152             .setSuccess(true);
153         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
154             Mockito.any());
155         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
156         Assert.assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
157
158     }
159
160     @Test
161     public void serviceImplementationTerminationPointAsResourceClient() {
162
163         ServiceImplementationRequestInput input = ServiceDataUtils
164             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.CLIENT_TOKEN);
165         writePortMapping(input, OpenRoadmInterfacesImpl.CLIENT_TOKEN);
166         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
167         Assert.assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
168     }
169
170     @Test
171     public void serviceImplementationTerminationPointAsResourceNoMapping() {
172
173         String[] interfaceTokens = {
174             OpenRoadmInterfacesImpl.NETWORK_TOKEN,
175             OpenRoadmInterfacesImpl.CLIENT_TOKEN,
176             OpenRoadmInterfacesImpl.TTP_TOKEN,
177             OpenRoadmInterfacesImpl.PP_TOKEN
178         };
179
180         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("failed")
181             .setSuccess(false);
182         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
183             Mockito.any());
184
185         for (String tpToken : interfaceTokens) {
186             ServiceImplementationRequestInput input = ServiceDataUtils
187                 .buildServiceImplementationRequestInputTerminationPointResource(tpToken);
188             ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
189             Assert.assertEquals(ResponseCodes.RESPONSE_FAILED,
190                     result.getConfigurationResponseCommon().getResponseCode());
191         }
192     }
193
194     private void writePortMapping(ServiceImplementationRequestInput input, String tpToken) {
195         MountPointUtils.writeMapping(
196             input.getServiceAEnd().getNodeId(),
197             input.getServiceAEnd().getNodeId() + "-" + tpToken,
198             this.deviceTransactionManager
199         );
200         MountPointUtils.writeMapping(
201                 input.getServiceZEnd().getNodeId(),
202                 input.getServiceZEnd().getNodeId() + "-"
203                     + input.getServiceAEnd().getNodeId() + "-" + tpToken,
204                 this.deviceTransactionManager
205         );
206         MountPointUtils.writeMapping(
207                 input.getServiceAEnd().getNodeId(),
208                 input.getServiceAEnd().getNodeId() + "-"
209                     + input.getServiceZEnd().getNodeId() + "-" + tpToken,
210                 this.deviceTransactionManager
211         );
212         MountPointUtils.writeMapping(
213             input.getServiceZEnd().getNodeId(),
214             input.getServiceZEnd().getNodeId() + "-" + tpToken,
215             this.deviceTransactionManager
216         );
217     }
218
219     @Test
220     public void serviceImplementationRollbackAllNecessary() {
221
222         ServiceImplementationRequestInput input = ServiceDataUtils
223             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
224         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
225         Mockito.doReturn(RpcResultBuilder.failed().buildFuture()).when(this.olmService)
226             .servicePowerSetup(Mockito.any());
227         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
228         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
229
230     }
231
232     private GetPmInput createGetPmInput(String nodeId, String tp) {
233         GetPmInputBuilder getPmIpBldr = new GetPmInputBuilder();
234         getPmIpBldr.setNodeId(nodeId);
235         getPmIpBldr.setGranularity(PmGranularity._15min);
236         ResourceIdentifierBuilder rsrcBldr = new ResourceIdentifierBuilder();
237         rsrcBldr.setResourceName(tp + "-OTU");
238         getPmIpBldr.setResourceIdentifier(rsrcBldr.build());
239         getPmIpBldr.setResourceType(ResourceTypeEnum.Interface);
240         return getPmIpBldr.build();
241     }
242
243     @Test
244     public void serviceImplementationServiceInActive() {
245
246         ServiceImplementationRequestInput input = ServiceDataUtils
247             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
248         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
249         Measurements measurements = new MeasurementsBuilder().setPmparameterName("FECUncorrectableBlocks")
250             .setPmparameterValue("1").build();
251         List<Measurements> measurementsList = new ArrayList<Measurements>();
252         measurementsList.add(measurements);
253         GetPmOutput getPmOutput = new GetPmOutputBuilder()
254             .setNodeId("node1").setMeasurements(measurementsList).build();
255         Mockito.doReturn(RpcResultBuilder.success(getPmOutput).buildFuture()).when(this.olmService)
256             .getPm(Mockito.any());
257         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
258         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
259
260     }
261
262     @Test
263     public void serviceImplementationServiceInActive2() {
264
265         ServiceImplementationRequestInput input = ServiceDataUtils
266             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
267         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
268         Measurements measurements = new MeasurementsBuilder().setPmparameterName("FECUncorrectableBlocks")
269             .setPmparameterValue("1").build();
270         List<Measurements> measurementsList = new ArrayList<Measurements>();
271         measurementsList.add(measurements);
272         GetPmOutput getPmOutput = new GetPmOutputBuilder()
273             .setNodeId("node1").setMeasurements(measurementsList).build();
274         GetPmOutput getPmOutput2 = new GetPmOutputBuilder()
275             .setNodeId("node1").setMeasurements(new ArrayList<>()).build();
276
277         Mockito.when(this.olmService.getPm(Mockito.any()))
278             .thenReturn(RpcResultBuilder.success(getPmOutput).buildFuture());
279         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
280         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
281
282     }
283
284     @Test
285     public void serviceImplementationServiceInActive3() {
286
287         ServiceImplementationRequestInput input = ServiceDataUtils
288             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
289         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
290         Measurements measurements = new MeasurementsBuilder().setPmparameterName("FECUncorrectableBlocks")
291             .setPmparameterValue("1").build();
292         List<Measurements> measurementsList = new ArrayList<Measurements>();
293         measurementsList.add(measurements);
294         GetPmOutput getPmOutput = new GetPmOutputBuilder()
295                 .setNodeId("node1").setMeasurements(measurementsList).build();
296         GetPmOutput getPmOutput2 = new GetPmOutputBuilder()
297                 .setNodeId("node1").setMeasurements(new ArrayList<>()).build();
298
299         GetPmInput getPmInputZ = createGetPmInput("XPONDER-2-3",
300             OpenRoadmInterfacesImpl.NETWORK_TOKEN);
301         GetPmInput getPmInputA = createGetPmInput("XPONDER-1-2",
302             OpenRoadmInterfacesImpl.NETWORK_TOKEN);
303
304         Mockito.when(this.olmService.getPm(Mockito.eq(getPmInputZ)))
305             .thenReturn(RpcResultBuilder.success(getPmOutput2).buildFuture());
306         Mockito.when(this.olmService.getPm(Mockito.eq(getPmInputA)))
307             .thenReturn(RpcResultBuilder.success(getPmOutput).buildFuture());
308         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
309         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
310
311     }
312
313     @Test
314     public void serviceImplementationServiceActive() {
315
316         ServiceImplementationRequestInput input = ServiceDataUtils
317             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
318         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
319         GetPmOutput getPmOutput = new GetPmOutputBuilder()
320             .setNodeId("node1").setMeasurements(new ArrayList<>()).build();
321         GetPmOutput getPmOutput1 = null;
322         Mockito.when(this.olmService.getPm(Mockito.any())).thenReturn(RpcResultBuilder.success(getPmOutput1)
323             .buildFuture());
324         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
325             .setSuccess(true);
326         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
327             Mockito.any());
328         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
329         Assert.assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
330
331     }
332
333     @Test
334     public void serviceImplementationServiceActive2() {
335
336         ServiceImplementationRequestInput input = ServiceDataUtils
337             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
338         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
339         GetPmOutput getPmOutput = new GetPmOutputBuilder().setMeasurements(new ArrayList<>()).build();
340         Mockito.when(this.olmService.getPm(Mockito.any())).thenReturn(RpcResultBuilder.success(getPmOutput)
341             .buildFuture());
342         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
343             .setSuccess(true);
344         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
345             Mockito.any());
346         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
347         Assert.assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
348
349     }
350
351     @Test
352     public void serviceImplementationServiceInActive4() {
353
354         ServiceImplementationRequestInput input = ServiceDataUtils
355             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
356         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
357         Measurements measurements = new MeasurementsBuilder().setPmparameterName("preFECCorrectedErrors")
358             .setPmparameterValue("1").build();
359         List<Measurements> measurementsList = new ArrayList<Measurements>();
360         measurementsList.add(measurements);
361         GetPmOutput getPmOutput = new GetPmOutputBuilder()
362             .setNodeId("node1").setMeasurements(measurementsList).build();
363
364         Mockito.doReturn(RpcResultBuilder.success(getPmOutput).buildFuture()).when(this.olmService)
365             .getPm(Mockito.any());
366         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
367             .setSuccess(true);
368         Mockito.doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(Mockito.any(),
369             Mockito.any());
370         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
371         Assert.assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
372
373     }
374
375     @Test
376     public void serviceImplementationServiceInActive5() {
377
378         ServiceImplementationRequestInput input = ServiceDataUtils
379             .buildServiceImplementationRequestInputTerminationPointResource(OpenRoadmInterfacesImpl.NETWORK_TOKEN);
380         writePortMapping(input, OpenRoadmInterfacesImpl.NETWORK_TOKEN);
381         Measurements measurements = new MeasurementsBuilder().setPmparameterName("preFECCorrectedErrors")
382             .setPmparameterValue("112000000000d").build();
383         List<Measurements> measurementsList = new ArrayList<Measurements>();
384         measurementsList.add(measurements);
385         GetPmOutput getPmOutput = new GetPmOutputBuilder()
386             .setNodeId("node1").setMeasurements(measurementsList).build();
387
388         Mockito.doReturn(RpcResultBuilder.success(getPmOutput).buildFuture()).when(this.olmService)
389             .getPm(Mockito.any());
390         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input);
391         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
392
393     }
394 }