Turn PortMappingImpl into a component
[transportpce.git] / olm / src / test / java / org / opendaylight / transportpce / olm / OlmPowerServiceRpcImplTest.java
1 /*
2  * Copyright © 2018 Orange, 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.olm;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.mdsal.binding.api.MountPoint;
23 import org.opendaylight.mdsal.binding.api.MountPointService;
24 import org.opendaylight.transportpce.common.StringConstants;
25 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
26 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
27 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
28 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl221;
29 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl710;
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.PortMappingVersion221;
38 import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
39 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
40 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
41 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
42 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
43 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl710;
44 import org.opendaylight.transportpce.olm.power.PowerMgmt;
45 import org.opendaylight.transportpce.olm.power.PowerMgmtImpl;
46 import org.opendaylight.transportpce.olm.service.OlmPowerService;
47 import org.opendaylight.transportpce.olm.service.OlmPowerServiceImpl;
48 import org.opendaylight.transportpce.olm.stub.MountPointServiceStub;
49 import org.opendaylight.transportpce.olm.stub.MountPointStub;
50 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
51 import org.opendaylight.transportpce.test.AbstractTest;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInput;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutput;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutputBuilder;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
56 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutput;
57 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutputBuilder;
58 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInput;
59 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutput;
60 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutputBuilder;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlist;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlistBuilder;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.current.pm.LayerRateBuilder;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.current.pm.Measurements;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.current.pm.MeasurementsBuilder;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.current.pm.measurements.MeasurementBuilder;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.currentpmlist.CurrentPm;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.currentpmlist.CurrentPmBuilder;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.currentpmlist.CurrentPmKey;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev161014.PmDataType;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev161014.PmGranularity;
72 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev161014.PmNamesEnum;
73 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev161014.pm.measurement.PmParameterNameBuilder;
74 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.DeviceBuilder;
75 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.ResourceTypeBuilder;
76 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.CircuitPackBuilder;
77 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.types.rev161014.ResourceTypeEnum;
78 import org.opendaylight.yangtools.yang.common.RpcResult;
79 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
80 import org.opendaylight.yangtools.yang.common.Uint64;
81
82 public class OlmPowerServiceRpcImplTest extends AbstractTest {
83
84     private MountPoint mountPoint;
85     private MountPointService mountPointService;
86     private DeviceTransactionManager deviceTransactionManager;
87     private CrossConnect crossConnect;
88     private OpenRoadmInterfaces openRoadmInterfaces;
89     private PortMapping portMapping;
90     private PowerMgmt powerMgmt;
91     private OlmPowerService olmPowerService;
92     private OlmPowerServiceRpcImpl olmPowerServiceRpc;
93     private CrossConnectImpl121 crossConnectImpl121;
94     private CrossConnectImpl221 crossConnectImpl22;
95     private CrossConnectImpl710 crossConnectImpl710;
96     private MappingUtils mappingUtils;
97     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
98     private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl22;
99     private OpenRoadmInterfacesImpl710 openRoadmInterfacesImpl710;
100     private PortMappingVersion710 portMappingVersion710;
101     private PortMappingVersion221 portMappingVersion22;
102     private PortMappingVersion121 portMappingVersion121;
103
104     @Before
105     public void setUp() {
106         this.mountPoint = new MountPointStub(getDataBroker());
107         this.mountPointService = new MountPointServiceStub(mountPoint);
108         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
109         this.mappingUtils = Mockito.spy(new MappingUtilsImpl(getDataBroker()));
110         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
111                 .getOpenRoadmVersion(Mockito.anyString());
112         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
113         this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
114         this.crossConnectImpl22 = new CrossConnectImpl221(deviceTransactionManager);
115         this.crossConnectImpl710 = new CrossConnectImpl710(deviceTransactionManager);
116         this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
117                 this.crossConnectImpl22, this.crossConnectImpl710);
118         this.portMappingVersion710 = new PortMappingVersion710(getDataBroker(), deviceTransactionManager);
119         this.portMappingVersion22 = new PortMappingVersion221(getDataBroker(), deviceTransactionManager);
120         this.portMappingVersion121 = new PortMappingVersion121(getDataBroker(), deviceTransactionManager);
121         this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion710,
122             this.portMappingVersion22, this.portMappingVersion121);
123         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
124         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager, this.portMapping);
125         this.openRoadmInterfacesImpl710 = new OpenRoadmInterfacesImpl710(deviceTransactionManager, this.portMapping);
126         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
127                 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22,
128             this.openRoadmInterfacesImpl710);
129         this.portMapping = Mockito.spy(this.portMapping);
130         this.powerMgmt = new PowerMgmtImpl(getDataBroker(), this.openRoadmInterfaces, this.crossConnect,
131             this.deviceTransactionManager, this.portMapping);
132         this.olmPowerService = new OlmPowerServiceImpl(getDataBroker(), this.powerMgmt,
133             this.deviceTransactionManager, this.portMapping,mappingUtils,openRoadmInterfaces);
134         this.olmPowerServiceRpc = new OlmPowerServiceRpcImpl(this.olmPowerService);
135         //TODO
136         this.olmPowerServiceRpc = Mockito.mock(OlmPowerServiceRpcImpl.class);
137     }
138
139     @Test
140     public void pmIsNotPresentTest() throws ExecutionException, InterruptedException {
141         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
142         //TODO
143 //        ListenableFuture<RpcResult<GetPmOutput>> output = this.olmPowerServiceRpc.getPm(input);
144 //        Assert.assertEquals(new GetPmOutputBuilder().build(), output.get().getResult());
145 //        Assert.assertEquals(null, output.get().getResult().getResourceId());
146 //        Assert.assertEquals(null, output.get().getResult().getMeasurements());
147 //        Assert.assertEquals(null, output.get().getResult().getGranularity());
148 //        Assert.assertEquals(null, output.get().getResult().getNodeId());
149 //        Assert.assertEquals(null, output.get().getResult().getResourceIdentifier());
150 //        Assert.assertEquals(null, output.get().getResult().getResourceType());
151     }
152
153     @Test
154     public void testGetPm1() throws ExecutionException, InterruptedException {
155         Measurements measurements = new MeasurementsBuilder().setMeasurement(
156             new MeasurementBuilder()
157                 .setPmParameterUnit("unit")
158                 .setPmParameterName(new PmParameterNameBuilder()
159                 .setExtension("123")
160                 .setType(PmNamesEnum.DefectSeconds).build())
161                 .setPmParameterValue(new PmDataType(Uint64.valueOf(1234))).build())
162             .build();
163         List<Measurements> measurementsList = new ArrayList<Measurements>();
164         measurementsList.add(measurements);
165
166         org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.Resource resource =
167             new org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.ResourceBuilder()
168                 .setResource(new CircuitPackBuilder().setCircuitPackName("circuit pack name").build()).build();
169
170         org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.current.pm.Resource resource2 =
171             new org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.current.pm.ResourceBuilder()
172                 .setDevice(new DeviceBuilder().setNodeId("node 1").build())
173                 .setResourceType(new ResourceTypeBuilder()
174                     .setExtension("123")
175                     .setType(ResourceTypeEnum.Device).build())
176                 .setResource(resource).build();
177         CurrentPm currentPm = new CurrentPmBuilder()
178             .setGranularity(PmGranularity._15min)
179             .setId("id")
180             .setLayerRate(new LayerRateBuilder().build())
181             .setMeasurements(measurementsList)
182             .setResource(resource2)
183             .setRetrievalTime(new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715
184                 .DateAndTime("2018-11-01T12:00:31.456449+06:00")).build();
185
186         Map<CurrentPmKey, CurrentPm> currentPmList = new HashMap<>();
187         currentPmList.put(currentPm.key(),currentPm);
188
189         Optional<CurrentPmlist> currentPmlistOptional = Optional.of(new CurrentPmlistBuilder()
190             .setCurrentPm(currentPmList).build());
191
192         org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.get.pm.output.Measurements
193             measurements1 = new org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.get.pm
194                 .output.MeasurementsBuilder().setPmparameterName("name").setPmparameterValue("1234").build();
195
196
197         List<org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.get.pm.output
198             .Measurements> measurementsList1 = new ArrayList<>();
199         measurementsList1.add(measurements1);
200
201         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
202         GetPmOutputBuilder pmOutputBuilder = new GetPmOutputBuilder().setNodeId(input.getNodeId())
203             .setResourceType(input.getResourceType())
204             .setResourceIdentifier(input.getResourceIdentifier())
205             .setGranularity(input.getGranularity())
206             .setMeasurements(measurementsList1);
207
208
209         ListenableFuture<RpcResult<GetPmOutput>> output = this.olmPowerServiceRpc.getPm(input);
210         //TODO
211 //        Assert.assertEquals(new GetPmOutputBuilder().build(), output.get().getResult());
212 //        Assert.assertEquals(null, output.get().getResult().getResourceId());
213     }
214
215 //    @Test
216 //    public void testGetPm2() throws ExecutionException, InterruptedException {
217 //        this.olmPowerService = Mockito.spy(this.olmPowerService);
218 //        GetPmInput input = new GetPmInputBuilder().setGranularity(PmGranularity._15min).setNodeId("node1")
219 //            .setResourceIdentifier(new ResourceIdentifierBuilder().setCircuitPackName("circuit pack name")
220 //                .setResourceName("resource name").build()).setResourceType(ResourceTypeEnum.Device).build();
221 //        org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm.output.Measurements
222 //        measurements1 = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm
223 //            .output.MeasurementsBuilder().setPmparameterName("name").setPmparameterValue("1234").build();
224 //
225 //
226 //        List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.olm.rev170418.get.pm.output
227 //            .Measurements> measurementsList1 = new ArrayList<>();
228 //        measurementsList1.add(measurements1);
229 //
230 //        GetPmOutputBuilder pmOutputBuilder = new GetPmOutputBuilder().setNodeId(input.getNodeId())
231 //            .setResourceType(input.getResourceType())
232 //            .setResourceIdentifier(input.getResourceIdentifier())
233 //            .setGranularity(input.getGranularity())
234 //            .setMeasurements(measurementsList1);
235 //
236 //        Mockito.doReturn(pmOutputBuilder.build()).when(this.olmPowerService).getPm(Mockito.any());
237 //        ListenableFuture<RpcResult<GetPmOutput>> output = this.olmPowerServiceRpc.getPm(input);
238 //        Assert.assertEquals(pmOutputBuilder.build(), output.get().getResult());
239 //        Assert.assertEquals(true, output.get().isSuccessful());
240 //    }
241
242     @Test
243     public void testGetPm3() throws ExecutionException, InterruptedException {
244         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
245         ListenableFuture<RpcResult<GetPmOutput>> output = this.olmPowerServiceRpc.getPm(input);
246         //TODO
247 //        Assert.assertEquals(new GetPmOutputBuilder().build(), output.get().getResult());
248 //        Assert.assertEquals(null, output.get().getResult().getResourceId());
249 //        Assert.assertEquals(null, output.get().getResult().getMeasurements());
250 //        Assert.assertEquals(null, output.get().getResult().getGranularity());
251 //        Assert.assertEquals(null, output.get().getResult().getNodeId());
252 //        Assert.assertEquals(null, output.get().getResult().getResourceIdentifier());
253 //        Assert.assertEquals(null, output.get().getResult().getResourceType());
254     }
255
256     @Test
257     public void testServicePowerSetup1() throws ExecutionException, InterruptedException {
258         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
259         //TODO
260         Mockito.when(this.olmPowerServiceRpc.servicePowerSetup(Mockito.any()))
261                 .thenReturn(RpcResultBuilder.success(new ServicePowerSetupOutputBuilder()
262                         .setResult("Success").build()).buildFuture());
263         ListenableFuture<RpcResult<ServicePowerSetupOutput>> output = this.olmPowerServiceRpc.servicePowerSetup(input);
264         Assert.assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), output.get()
265                 .getResult());
266         Assert.assertEquals("Success", output.get().getResult().getResult());
267         Assert.assertEquals(true, output.get().isSuccessful());
268     }
269
270
271     @Test
272     public void testServicePowerTurndown1() throws ExecutionException, InterruptedException {
273         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
274         //TODO
275         Mockito.when(this.olmPowerServiceRpc.servicePowerTurndown(Mockito.any()))
276                 .thenReturn(RpcResultBuilder.success(new ServicePowerTurndownOutputBuilder()
277                         .setResult("Success").build()).buildFuture());
278         ListenableFuture<RpcResult<ServicePowerTurndownOutput>> output = this.olmPowerServiceRpc
279             .servicePowerTurndown(input);
280         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output.get()
281             .getResult());
282         Assert.assertEquals("Success", output.get().getResult().getResult());
283         Assert.assertEquals(true, output.get().isSuccessful());
284     }
285
286     /*
287     @Test
288     public void testCalculateSpanlossBase1() throws ExecutionException, InterruptedException {
289         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
290         //TODO
291         Mockito.when(this.olmPowerServiceRpc.calculateSpanlossBase(Mockito.any()))
292                 .thenReturn(RpcResultBuilder.success(new CalculateSpanlossBaseOutputBuilder()
293                         .setResult("Failed").build()).buildFuture());
294         ListenableFuture<RpcResult<CalculateSpanlossBaseOutput>> output = this.olmPowerServiceRpc
295             .calculateSpanlossBase(input);
296         Assert.assertEquals(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build(),
297             output.get().getResult());
298         Assert.assertEquals("Failed", output.get().getResult().getResult());
299         Assert.assertEquals(true, output.get().isSuccessful());
300     }*/
301
302     //TODO
303 /**    @Test
304     public void testCalculateSpanlossCurrent1() throws ExecutionException, InterruptedException {
305         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
306         //TODO
307         Mockito.when(this.olmPowerServiceRpc.calculateSpanlossCurrent(Mockito.any()))
308                 .thenReturn(RpcResultBuilder.success(new CalculateSpanlossCurrentOutputBuilder()
309                         .setResult("success").build()).buildFuture());
310         ListenableFuture<RpcResult<CalculateSpanlossCurrentOutput>> output = this.olmPowerServiceRpc
311             .calculateSpanlossCurrent(input);
312         Assert.assertEquals(null, output.get().getResult());
313         Assert.assertEquals(true, output.get().isSuccessful());
314     }
315
316     @Test
317     public void testServicePowerResetInput() throws ExecutionException, InterruptedException {
318         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
319         //TODO
320         Mockito.when(this.olmPowerServiceRpc.calculateSpanlossCurrent(Mockito.any()))
321                 .thenReturn(RpcResultBuilder.success(new CalculateSpanlossCurrentOutputBuilder()
322                         .setResult(null).build()).buildFuture());
323         ListenableFuture<RpcResult<ServicePowerResetOutput>> output = this.olmPowerServiceRpc
324             .servicePowerReset(input);
325         Assert.assertEquals(null, output.get().getResult());
326         Assert.assertEquals(true, output.get().isSuccessful());
327     }
328 **/
329 }