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