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