Adapt TransportPCE code to Sulfur
[transportpce.git] / olm / src / test / java / org / opendaylight / transportpce / olm / service / OlmPowerServiceImplTest.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.service;
10
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.InjectMocks;
15 import org.mockito.Mockito;
16 import org.mockito.MockitoAnnotations;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.MountPoint;
19 import org.opendaylight.mdsal.binding.api.MountPointService;
20 import org.opendaylight.transportpce.common.NetworkUtils;
21 import org.opendaylight.transportpce.common.ResponseCodes;
22 import org.opendaylight.transportpce.common.StringConstants;
23 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
24 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
25 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
26 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl221;
27 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl710;
28 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
29 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
30 import org.opendaylight.transportpce.common.mapping.MappingUtils;
31 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
32 import org.opendaylight.transportpce.common.mapping.PortMapping;
33 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
34 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
35 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
36 import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
37 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
38 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
39 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
40 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
41 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl710;
42 import org.opendaylight.transportpce.olm.power.PowerMgmt;
43 import org.opendaylight.transportpce.olm.power.PowerMgmtImpl;
44 import org.opendaylight.transportpce.olm.stub.MountPointServiceStub;
45 import org.opendaylight.transportpce.olm.stub.MountPointStub;
46 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
47 import org.opendaylight.transportpce.olm.util.TransactionUtils;
48 import org.opendaylight.transportpce.test.AbstractTest;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseInput;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseOutput;
51 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseOutputBuilder;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentInput;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentOutput;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInput;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutput;
56 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutputBuilder;
57 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetInput;
58 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetOutput;
59 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
60 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutput;
61 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutputBuilder;
62 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInput;
63 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutput;
64 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73
74 public class OlmPowerServiceImplTest  extends AbstractTest {
75
76     private static final Logger LOG = LoggerFactory.getLogger(OlmPowerServiceImplTest.class);
77     private MountPoint mountPoint;
78     private MountPointService mountPointService;
79     private DeviceTransactionManager deviceTransactionManager;
80     private CrossConnect crossConnect;
81     private OpenRoadmInterfaces openRoadmInterfaces;
82     private PortMapping portMapping;
83     private PowerMgmt powerMgmt;
84     private CrossConnectImpl121 crossConnectImpl121;
85     private CrossConnectImpl221 crossConnectImpl22;
86     private CrossConnectImpl710 crossConnectImpl710;
87     private MappingUtils mappingUtils;
88     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
89     private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl22;
90     private OpenRoadmInterfacesImpl710 openRoadmInterfacesImpl710;
91     private PortMappingVersion710 portMappingVersion710;
92     private PortMappingVersion221 portMappingVersion22;
93     private PortMappingVersion121 portMappingVersion121;
94     private OlmPowerService olmPowerService;
95     private DataBroker dataBroker;
96     private PowerMgmt powerMgmtMock;
97     @InjectMocks
98     private OlmPowerService olmPowerServiceMock;
99
100
101     @Before
102     public void setUp() {
103         this.dataBroker =  getNewDataBroker();
104         this.mountPoint = new MountPointStub(this.dataBroker);
105         this.mountPointService = new MountPointServiceStub(mountPoint);
106         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
107         this.mappingUtils = Mockito.spy(new MappingUtilsImpl(dataBroker));
108         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
109                 .getOpenRoadmVersion(Mockito.anyString());
110         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
111         this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
112         this.crossConnectImpl22 = new CrossConnectImpl221(deviceTransactionManager);
113         this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
114                 this.crossConnectImpl22, this.crossConnectImpl710);
115         this.portMappingVersion22 = new PortMappingVersion221(dataBroker, deviceTransactionManager);
116         this.portMappingVersion121 = new PortMappingVersion121(dataBroker, deviceTransactionManager);
117         this.portMappingVersion710 = new PortMappingVersion710(dataBroker, deviceTransactionManager);
118         this.portMapping = new PortMappingImpl(dataBroker, this.portMappingVersion710,
119             this.portMappingVersion22, this.portMappingVersion121);
120         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
121         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager, this.portMapping,
122             this.portMappingVersion22);
123         this.openRoadmInterfacesImpl710 = new OpenRoadmInterfacesImpl710(deviceTransactionManager, this.portMapping,
124             this.portMappingVersion710);
125         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
126                 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22,
127             this.openRoadmInterfacesImpl710);
128         this.portMapping = Mockito.spy(this.portMapping);
129         this.powerMgmt = new PowerMgmtImpl(this.dataBroker, this.openRoadmInterfaces, this.crossConnect,
130             this.deviceTransactionManager);
131         this.olmPowerService = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmt,
132             this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
133         this.powerMgmtMock = Mockito.mock(PowerMgmtImpl.class);
134         this.olmPowerServiceMock = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmtMock,
135             this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
136         this.olmPowerServiceMock = Mockito.mock(OlmPowerServiceImpl.class);
137         MockitoAnnotations.openMocks(this);
138     }
139
140     @Test
141     public void dummyTest() {
142         OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
143         olmPowerServiceImpl.init();
144         olmPowerServiceImpl.close();
145     }
146
147
148     @Test
149     public void testGetPm() {
150         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
151         GetPmOutput output = this.olmPowerService.getPm(input);
152         Assert.assertEquals(new GetPmOutputBuilder().build(), output);
153         Assert.assertEquals(null, output.getResourceId());
154     }
155
156     @Test
157     public void testServicePowerSetupSuccess() {
158         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
159         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
160         //TODO
161         Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
162                 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Success").build());
163         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
164         Assert.assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), output);
165         Assert.assertEquals("Success", output.getResult());
166     }
167
168     @Test
169     public void testServicePowerSetupFailed() {
170         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
171         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
172         //TODO
173         Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
174                 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Failed").build());
175         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
176         Assert.assertEquals("Failed", output.getResult());
177     }
178
179     @Test
180     public void testServicePowerTurnDownSuccess() {
181         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
182         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
183         //TODO
184         Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
185                 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Success").build());
186         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
187         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
188         Assert.assertEquals("Success", output.getResult());
189     }
190
191     @Test
192     public void testServicePowerTurnDownFailed() {
193         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
194         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
195         //TODO
196         Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
197                 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Failed").build());
198         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
199         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
200         Assert.assertEquals("Failed", output.getResult());
201     }
202
203     @Test
204     public void testCalculateSpanlossBase3() {
205         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
206         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
207             .child(Network.class, overlayTopologyKey)
208             .augmentation(Network1.class)
209             .build();
210         Network1 network = TransactionUtils.getNetwork();
211         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
212         try {
213             Thread.sleep(1000);
214         } catch (InterruptedException e) {
215             LOG.error("Write transaction failed !",e);
216         }
217         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
218         //TODO
219         Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
220                 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
221         CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
222         Assert.assertEquals("Failed", output.getResult());
223
224     }
225
226
227     @Test
228     public void testCalculateSpanlossBase4() {
229         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
230         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
231             .child(Network.class, overlayTopologyKey)
232             .augmentation(Network1.class)
233             .build();
234         Network1 network = TransactionUtils.getEmptyNetwork();
235         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
236         try {
237             Thread.sleep(1000);
238         } catch (InterruptedException e) {
239             LOG.error("Write transaction failed !",e);
240         }
241         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
242         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
243         Assert.assertEquals("Failed", output.getResult());
244
245     }
246
247     @Test
248     public void testCalculateSpanlossBase5() {
249         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
250         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
251             .child(Network.class, overlayTopologyKey)
252             .augmentation(Network1.class)
253             .build();
254         Network1 network = TransactionUtils.getNullNetwork();
255         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
256         try {
257             Thread.sleep(1000);
258         } catch (InterruptedException e) {
259             LOG.error("Write transaction failed !",e);
260         }
261         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
262         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
263         Assert.assertEquals("Failed", output.getResult());
264
265     }
266
267     @Test
268     public void testCalculateSpanlossCurrent1() {
269         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
270         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
271         Assert.assertEquals(null, output);
272     }
273
274     @Test
275     public void testCalculateSpanlossCurrent2() {
276         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
277         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
278             .child(Network.class, overlayTopologyKey)
279             .augmentation(Network1.class)
280             .build();
281         Network1 network = TransactionUtils.getNetwork();
282         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
283         try {
284             Thread.sleep(1000);
285         } catch (InterruptedException e) {
286             LOG.error("Write transaction failed !",e);
287         }
288         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
289         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
290         Assert.assertEquals(null, output);
291     }
292
293     @Test
294     public void testServicePowerReset() {
295         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
296         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
297         Assert.assertEquals(null, output);
298     }
299
300     @Test
301     public void testServicePowerTurndownSuccessResult() {
302         ServicePowerTurndownInput servicePowerTurndownInput = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
303         ServicePowerTurndownOutput servicePowerTurndownOutput =
304                 this.olmPowerService.servicePowerTurndown(servicePowerTurndownInput);
305         Assert.assertEquals(ResponseCodes.SUCCESS_RESULT, servicePowerTurndownOutput.getResult());
306     }
307
308     @Test
309     public void testServicePowerTurndownFailResult() {
310         ServicePowerTurndownInput servicePowerTurndownInput =
311                 OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput2();
312         ServicePowerTurndownOutput servicePowerTurndownOutput =
313                 this.olmPowerService.servicePowerTurndown(servicePowerTurndownInput);
314         Assert.assertEquals(ResponseCodes.FAILED_RESULT, servicePowerTurndownOutput.getResult());
315     }
316
317     @Test
318     public void testServicePowerSetupSuccessResult() {
319         ServicePowerSetupInput servicePowerSetupInput =
320                 OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
321         ServicePowerSetupOutput servicePowerSetupOutput =
322                 this.olmPowerService.servicePowerSetup(servicePowerSetupInput);
323         Assert.assertEquals(ResponseCodes.SUCCESS_RESULT, servicePowerSetupOutput.getResult());
324     }
325
326     @Test
327     public void testServicePowerSetupFailResult() {
328         ServicePowerSetupInput servicePowerSetupInput = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
329         Mockito.when(powerMgmtMock.setPower(servicePowerSetupInput)).thenReturn(Boolean.FALSE);
330         OlmPowerService olmPowerServiceWithMock = new OlmPowerServiceImpl(dataBroker, powerMgmtMock,
331                 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
332         ServicePowerSetupOutput servicePowerSetupOutput =
333                 olmPowerServiceWithMock.servicePowerSetup(servicePowerSetupInput);
334         Assert.assertEquals(ResponseCodes.FAILED_RESULT, servicePowerSetupOutput.getResult());
335     }
336 }