Bump upstream dependencies to Ca
[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 static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.when;
15
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Disabled;
21 import org.junit.jupiter.api.Test;
22 import org.junit.jupiter.api.extension.ExtendWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.jupiter.MockitoExtension;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
27 import org.opendaylight.mdsal.binding.api.RpcService;
28 import org.opendaylight.transportpce.common.ResponseCodes;
29 import org.opendaylight.transportpce.common.StringConstants;
30 import org.opendaylight.transportpce.common.mapping.PortMapping;
31 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
32 import org.opendaylight.transportpce.renderer.provisiondevice.notification.NotificationSender;
33 import org.opendaylight.transportpce.renderer.utils.NotificationPublishServiceMock;
34 import org.opendaylight.transportpce.renderer.utils.ServiceDataUtils;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.RendererRollbackInputBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.RendererRollbackOutputBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathOutputBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPm;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInput;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInputBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutput;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutputBuilder;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetup;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutputBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndown;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutputBuilder;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.get.pm.output.Measurements;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.get.pm.output.MeasurementsBuilder;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestInput;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestOutput;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.types.rev161014.ResourceTypeEnum;
52 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.PmGranularity;
53 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.olm.get.pm.input.ResourceIdentifierBuilder;
54 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
55
56 @ExtendWith(MockitoExtension.class)
57 public class RendererServiceOperationsImplTest {
58
59     @Mock
60     private DeviceRendererService deviceRenderer;
61     @Mock
62     private OtnDeviceRendererService otnDeviceRendererService;
63     @Mock
64     private DataBroker dataBroker;
65     @Mock
66     private PortMapping portMapping;
67     @Mock
68     private RpcService rpcService;
69     @Mock
70     private ServicePowerSetup servicePowerSetup;
71     @Mock
72     private ServicePowerTurndown servicePowerTurndown;
73     @Mock
74     private GetPm getPm;
75     private RendererServiceOperationsImpl rendererServiceOperations;
76
77
78     @BeforeEach
79     void setUp() throws OpenRoadmInterfaceException {
80         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
81         this.rendererServiceOperations = new RendererServiceOperationsImpl(deviceRenderer, otnDeviceRendererService,
82                 dataBroker, new NotificationSender(notificationPublishService), portMapping, rpcService);
83     }
84
85     @Test
86     void serviceImplementationTerminationPointAsResourceTtp() throws InterruptedException, ExecutionException {
87         ServiceImplementationRequestInput input = ServiceDataUtils
88                 .buildServiceImplementationRequestInputTerminationPointResource(StringConstants.TTP_TOKEN);
89         when(deviceRenderer.setupServicePath(any(), any(), any()))
90                 .thenReturn(new ServicePathOutputBuilder().setResult("success").setSuccess(true).build());
91
92         when(rpcService.getRpc(ServicePowerSetup.class)).thenReturn(servicePowerSetup);
93         when(rpcService.getRpc(ServicePowerTurndown.class)).thenReturn(servicePowerTurndown);
94         when(rpcService.getRpc(GetPm.class)).thenReturn(getPm);
95         doReturn(RpcResultBuilder
96                 .success(new ServicePowerSetupOutputBuilder().setResult(ResponseCodes.SUCCESS_RESULT).build())
97                 .buildFuture()).when(servicePowerSetup).invoke(any());
98         doReturn(RpcResultBuilder.success(new GetPmOutputBuilder().setNodeId("node id").build()).buildFuture())
99                 .when(getPm).invoke(any());
100         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input, false)
101                 .get();
102         assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
103     }
104
105     @Test
106     void serviceImplementationTerminationPointAsResourceTtp2() throws InterruptedException, ExecutionException {
107         ServiceImplementationRequestInput input = ServiceDataUtils
108                 .buildServiceImplementationRequestInputTerminationPointResource(StringConstants.TTP_TOKEN);
109         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
110                 .setSuccess(true);
111         doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(any(), any(), any());
112         when(rpcService.getRpc(ServicePowerSetup.class)).thenReturn(servicePowerSetup);
113         doReturn(RpcResultBuilder.failed().buildFuture()).when(servicePowerSetup).invoke(any());
114         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input, false)
115                 .get();
116         assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
117     }
118
119     @Test
120     void serviceImplementationTerminationPointAsResourceNoMapping() throws InterruptedException, ExecutionException {
121         // when no mapping available, 100GE between transponders must be implemented
122
123         when(deviceRenderer.setupServicePath(any(), any(), any()))
124                 .thenReturn(new ServicePathOutputBuilder().setResult("success").setSuccess(true).build());
125
126         when(rpcService.getRpc(ServicePowerSetup.class)).thenReturn(servicePowerSetup);
127         when(rpcService.getRpc(ServicePowerTurndown.class)).thenReturn(servicePowerTurndown);
128         when(rpcService.getRpc(GetPm.class)).thenReturn(getPm);
129         doReturn(RpcResultBuilder
130                 .success(new ServicePowerSetupOutputBuilder().setResult(ResponseCodes.SUCCESS_RESULT).build())
131                 .buildFuture()).when(servicePowerSetup).invoke(any());
132         doReturn(RpcResultBuilder.success(new GetPmOutputBuilder().setNodeId("node id").build()).buildFuture())
133                 .when(getPm).invoke(any());
134
135         String[] interfaceTokens = { StringConstants.NETWORK_TOKEN, StringConstants.CLIENT_TOKEN,
136             StringConstants.TTP_TOKEN, StringConstants.PP_TOKEN };
137         for (String tpToken : interfaceTokens) {
138             ServiceImplementationRequestInput input = ServiceDataUtils
139                     .buildServiceImplementationRequestInputTerminationPointResource(tpToken);
140             ServiceImplementationRequestOutput result = this.rendererServiceOperations
141                     .serviceImplementation(input, false).get();
142             assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
143         }
144     }
145
146     @Test
147     void serviceImplementationRollbackAllNecessary() throws InterruptedException, ExecutionException {
148         ServiceImplementationRequestInput input = ServiceDataUtils
149                 .buildServiceImplementationRequestInputTerminationPointResource(StringConstants.NETWORK_TOKEN);
150         when(deviceRenderer.setupServicePath(any(), any()))
151                 .thenReturn(new ServicePathOutputBuilder().setResult("success").setSuccess(true).build());
152         when(rpcService.getRpc(ServicePowerSetup.class)).thenReturn(servicePowerSetup);
153         when(rpcService.getRpc(ServicePowerTurndown.class)).thenReturn(servicePowerTurndown);
154         doReturn(RpcResultBuilder
155                 .success(new ServicePowerSetupOutputBuilder().setResult(ResponseCodes.RESPONSE_FAILED).build())
156                 .buildFuture()).when(servicePowerSetup).invoke(any());
157         doReturn(RpcResultBuilder
158                 .success(new ServicePowerTurndownOutputBuilder().setResult(ResponseCodes.SUCCESS_RESULT).build())
159                 .buildFuture()).when(servicePowerTurndown).invoke(any());
160         when(deviceRenderer.rendererRollback(new RendererRollbackInputBuilder().build()))
161                 .thenReturn(new RendererRollbackOutputBuilder().setSuccess(true).build());
162         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input, false)
163                 .get();
164         assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
165     }
166
167     @Disabled("Disabled until we understand the author objective...")
168     @Test
169     void serviceImplementationServiceInActive() throws InterruptedException, ExecutionException {
170         ServiceImplementationRequestInput input = ServiceDataUtils
171                 .buildServiceImplementationRequestInputTerminationPointResource(StringConstants.NETWORK_TOKEN);
172         List<Measurements> measurementsList = new ArrayList<Measurements>();
173         measurementsList.add(new MeasurementsBuilder().setPmparameterName("FECUncorrectableBlocks")
174                 .setPmparameterValue("1").build());
175         GetPmOutput getPmOutput = new GetPmOutputBuilder().setNodeId("node1").setMeasurements(measurementsList).build();
176         doReturn(RpcResultBuilder.success(getPmOutput).buildFuture()).when(getPm).invoke(any());
177         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input, false)
178                 .get();
179         assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
180     }
181
182     @Disabled("Disabled until we understand the author objective...")
183     @Test
184     void serviceImplementationServiceInActive3() throws InterruptedException, ExecutionException {
185         when(rpcService.getRpc(ServicePowerSetup.class)).thenReturn(servicePowerSetup);
186         List<Measurements> measurementsList = new ArrayList<Measurements>();
187         measurementsList.add(new MeasurementsBuilder().setPmparameterName("FECUncorrectableBlocks")
188                 .setPmparameterValue("1").build());
189         GetPmOutput getPmOutput = new GetPmOutputBuilder().setNodeId("node1").setMeasurements(measurementsList).build();
190         GetPmOutput getPmOutput2 = new GetPmOutputBuilder().setNodeId("node1").setMeasurements(new ArrayList<>())
191                 .build();
192
193         GetPmInput getPmInputZ = createGetPmInput("XPONDER-2-3", StringConstants.NETWORK_TOKEN);
194         GetPmInput getPmInputA = createGetPmInput("XPONDER-1-2", StringConstants.NETWORK_TOKEN);
195
196         when(getPm.invoke(eq(getPmInputZ))).thenReturn(RpcResultBuilder.success(getPmOutput2).buildFuture());
197         when(getPm.invoke(eq(getPmInputA))).thenReturn(RpcResultBuilder.success(getPmOutput).buildFuture());
198         ServicePathOutputBuilder mockOutputBuilder = new ServicePathOutputBuilder().setResult("success")
199                 .setSuccess(true);
200         doReturn(mockOutputBuilder.build()).when(this.deviceRenderer).setupServicePath(any(), any(), any());
201         ServiceImplementationRequestInput input = ServiceDataUtils
202                 .buildServiceImplementationRequestInputTerminationPointResource(StringConstants.NETWORK_TOKEN);
203         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input, false)
204                 .get();
205         assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
206     }
207
208     @Test
209     void serviceImplementationServiceActive() throws InterruptedException, ExecutionException {
210         ServiceImplementationRequestInput input = ServiceDataUtils
211                 .buildServiceImplementationRequestInputTerminationPointResource(StringConstants.NETWORK_TOKEN);
212         when(deviceRenderer.setupServicePath(any(), any(), any()))
213                 .thenReturn(new ServicePathOutputBuilder().setResult("success").setSuccess(true).build());
214
215         when(rpcService.getRpc(ServicePowerSetup.class)).thenReturn(servicePowerSetup);
216         when(rpcService.getRpc(ServicePowerTurndown.class)).thenReturn(servicePowerTurndown);
217         when(rpcService.getRpc(GetPm.class)).thenReturn(getPm);
218         doReturn(RpcResultBuilder
219                 .success(new ServicePowerSetupOutputBuilder().setResult(ResponseCodes.SUCCESS_RESULT).build())
220                 .buildFuture()).when(servicePowerSetup).invoke(any());
221         GetPmOutput getPmOutput1 = null;
222         when(getPm.invoke(any())).thenReturn(RpcResultBuilder.success(getPmOutput1).buildFuture());
223         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input, false)
224                 .get();
225         assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
226     }
227
228     @Test
229     void serviceImplementationServiceInActive4() throws InterruptedException, ExecutionException {
230         when(deviceRenderer.setupServicePath(any(), any(), any()))
231                 .thenReturn(new ServicePathOutputBuilder().setResult("success").setSuccess(true).build());
232
233         when(rpcService.getRpc(ServicePowerSetup.class)).thenReturn(servicePowerSetup);
234         when(rpcService.getRpc(ServicePowerTurndown.class)).thenReturn(servicePowerTurndown);
235         when(rpcService.getRpc(GetPm.class)).thenReturn(getPm);
236         doReturn(RpcResultBuilder
237                 .success(new ServicePowerSetupOutputBuilder().setResult(ResponseCodes.SUCCESS_RESULT).build())
238                 .buildFuture()).when(servicePowerSetup).invoke(any());
239
240         List<Measurements> measurementsList = new ArrayList<Measurements>();
241         measurementsList.add(
242                 new MeasurementsBuilder().setPmparameterName("preFECCorrectedErrors").setPmparameterValue("1").build());
243         GetPmOutput getPmOutput = new GetPmOutputBuilder().setNodeId("node1").setMeasurements(measurementsList).build();
244         when(getPm.invoke(any())).thenReturn(RpcResultBuilder.success(getPmOutput).buildFuture());
245
246         ServiceImplementationRequestInput input = ServiceDataUtils
247                 .buildServiceImplementationRequestInputTerminationPointResource(StringConstants.NETWORK_TOKEN);
248         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input, false)
249                 .get();
250         assertEquals(ResponseCodes.RESPONSE_OK, result.getConfigurationResponseCommon().getResponseCode());
251     }
252
253     @Test
254     void serviceImplementationServiceInActive5() throws InterruptedException, ExecutionException {
255         when(deviceRenderer.setupServicePath(any(), any(), any()))
256                 .thenReturn(new ServicePathOutputBuilder().setResult("success").setSuccess(true).build());
257         when(rpcService.getRpc(ServicePowerSetup.class)).thenReturn(servicePowerSetup);
258         when(rpcService.getRpc(ServicePowerTurndown.class)).thenReturn(servicePowerTurndown);
259         when(rpcService.getRpc(GetPm.class)).thenReturn(getPm);
260         doReturn(RpcResultBuilder
261                 .success(new ServicePowerSetupOutputBuilder().setResult(ResponseCodes.SUCCESS_RESULT).build())
262                 .buildFuture()).when(servicePowerSetup).invoke(any());
263
264         List<Measurements> measurementsList = new ArrayList<Measurements>();
265         measurementsList.add(new MeasurementsBuilder().setPmparameterName("preFECCorrectedErrors")
266                 .setPmparameterValue("112000000000d").build());
267         GetPmOutput getPmOutput = new GetPmOutputBuilder().setNodeId("node1").setMeasurements(measurementsList).build();
268         when(getPm.invoke(any())).thenReturn(RpcResultBuilder.success(getPmOutput).buildFuture());
269
270         ServiceImplementationRequestInput input = ServiceDataUtils
271                 .buildServiceImplementationRequestInputTerminationPointResource(StringConstants.NETWORK_TOKEN);
272         ServiceImplementationRequestOutput result = this.rendererServiceOperations.serviceImplementation(input, false)
273                 .get();
274         assertEquals(ResponseCodes.RESPONSE_FAILED, result.getConfigurationResponseCommon().getResponseCode());
275     }
276
277     private GetPmInput createGetPmInput(String nodeId, String tp) {
278         return new GetPmInputBuilder().setNodeId(nodeId).setGranularity(PmGranularity._15min)
279                 .setResourceIdentifier(new ResourceIdentifierBuilder().setResourceName(tp + "-OTU").build())
280                 .setResourceType(ResourceTypeEnum.Interface).build();
281     }
282 }