Device renderer for muxponder
[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.rev170418.CalculateSpanlossBaseInput;
51 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseOutput;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseOutputBuilder;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossCurrentInput;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossCurrentOutput;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmInput;
56 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmOutput;
57 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmOutputBuilder;
58 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerResetInput;
59 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerResetOutput;
60 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
61 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupOutput;
62 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupOutputBuilder;
63 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownInput;
64 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutput;
65 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.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.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
119         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager);
120         this.openRoadmInterfacesImpl710 = new OpenRoadmInterfacesImpl710(deviceTransactionManager);
121         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
122                 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22,
123             this.openRoadmInterfacesImpl710);
124         this.portMappingVersion22 =
125                 new PortMappingVersion221(dataBroker, deviceTransactionManager, this.openRoadmInterfaces);
126         this.portMappingVersion121 =
127                 new PortMappingVersion121(dataBroker, deviceTransactionManager, this.openRoadmInterfaces);
128         this.portMappingVersion710 =
129             new PortMappingVersion710(dataBroker, deviceTransactionManager, this.openRoadmInterfaces);
130         this.portMapping = new PortMappingImpl(dataBroker, this.portMappingVersion710,
131             this.portMappingVersion22, this.portMappingVersion121);
132         this.portMapping = Mockito.spy(this.portMapping);
133         this.powerMgmt = new PowerMgmtImpl(this.dataBroker, this.openRoadmInterfaces, this.crossConnect,
134             this.deviceTransactionManager);
135         this.olmPowerService = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmt,
136             this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
137         this.powerMgmtMock = PowerMockito.mock(PowerMgmtImpl.class);
138         this.olmPowerServiceMock = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmtMock,
139             this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
140         this.olmPowerServiceMock = Mockito.mock(OlmPowerServiceImpl.class);
141         MockitoAnnotations.initMocks(this);
142     }
143
144     @Test
145     public void dummyTest() {
146         OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
147         olmPowerServiceImpl.init();
148         olmPowerServiceImpl.close();
149     }
150
151
152     @Test
153     public void testGetPm() {
154         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
155         GetPmOutput output = this.olmPowerService.getPm(input);
156         Assert.assertEquals(new GetPmOutputBuilder().build(), output);
157         Assert.assertEquals(null, output.getResourceId());
158     }
159
160     @Test
161     public void testServicePowerSetupSuccess() {
162         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
163         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
164         //TODO
165         Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
166                 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Success").build());
167         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
168         Assert.assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), output);
169         Assert.assertEquals("Success", output.getResult());
170     }
171
172     @Test
173     public void testServicePowerSetupFailed() {
174         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
175         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
176         //TODO
177         Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
178                 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Failed").build());
179         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
180         Assert.assertEquals("Failed", output.getResult());
181     }
182
183     @Test
184     public void testServicePowerTurnDownSuccess() {
185         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
186         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
187         //TODO
188         Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
189                 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Success").build());
190         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
191         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
192         Assert.assertEquals("Success", output.getResult());
193     }
194
195     @Test
196     public void testServicePowerTurnDownFailed() {
197         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
198         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
199         //TODO
200         Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
201                 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Failed").build());
202         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
203         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
204         Assert.assertEquals("Failed", output.getResult());
205     }
206
207     @Test
208     public void testCalculateSpanlossBase3() {
209         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
210         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
211             .child(Network.class, overlayTopologyKey)
212             .augmentation(Network1.class)
213             .build();
214         Network1 network = TransactionUtils.getNetwork();
215         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
216         try {
217             Thread.sleep(1000);
218         } catch (InterruptedException e) {
219             LOG.error("Write transaction failed !",e);
220         }
221         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
222         //TODO
223         Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
224                 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
225         CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
226         Assert.assertEquals("Failed", output.getResult());
227
228     }
229
230
231     @Test
232     public void testCalculateSpanlossBase4() {
233         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
234         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
235             .child(Network.class, overlayTopologyKey)
236             .augmentation(Network1.class)
237             .build();
238         Network1 network = TransactionUtils.getEmptyNetwork();
239         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
240         try {
241             Thread.sleep(1000);
242         } catch (InterruptedException e) {
243             LOG.error("Write transaction failed !",e);
244         }
245         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
246         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
247         Assert.assertEquals("Failed", output.getResult());
248
249     }
250
251     @Test
252     public void testCalculateSpanlossBase5() {
253         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
254         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
255             .child(Network.class, overlayTopologyKey)
256             .augmentation(Network1.class)
257             .build();
258         Network1 network = TransactionUtils.getNullNetwork();
259         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
260         try {
261             Thread.sleep(1000);
262         } catch (InterruptedException e) {
263             LOG.error("Write transaction failed !",e);
264         }
265         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
266         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
267         Assert.assertEquals("Failed", output.getResult());
268
269     }
270
271     @Test
272     public void testCalculateSpanlossCurrent1() {
273         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
274         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
275         Assert.assertEquals(null, output);
276     }
277
278     @Test
279     public void testCalculateSpanlossCurrent2() {
280         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
281         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
282             .child(Network.class, overlayTopologyKey)
283             .augmentation(Network1.class)
284             .build();
285         Network1 network = TransactionUtils.getNetwork();
286         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
287         try {
288             Thread.sleep(1000);
289         } catch (InterruptedException e) {
290             LOG.error("Write transaction failed !",e);
291         }
292         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
293         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
294         Assert.assertEquals(null, output);
295     }
296
297     @Test
298     public void testServicePowerReset() {
299         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
300         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
301         Assert.assertEquals(null, output);
302     }
303
304     @Test
305     public void testServicePowerTurndownSuccessResult() {
306         ServicePowerTurndownInput servicePowerTurndownInput = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
307         ServicePowerTurndownOutput servicePowerTurndownOutput =
308                 this.olmPowerService.servicePowerTurndown(servicePowerTurndownInput);
309         Assert.assertEquals(ResponseCodes.SUCCESS_RESULT, servicePowerTurndownOutput.getResult());
310     }
311
312     @Test
313     public void testServicePowerTurndownFailResult() {
314         ServicePowerTurndownInput servicePowerTurndownInput =
315                 OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput2();
316         ServicePowerTurndownOutput servicePowerTurndownOutput =
317                 this.olmPowerService.servicePowerTurndown(servicePowerTurndownInput);
318         Assert.assertEquals(ResponseCodes.FAILED_RESULT, servicePowerTurndownOutput.getResult());
319     }
320
321     @Test
322     public void testServicePowerSetupSuccessResult() {
323         ServicePowerSetupInput servicePowerSetupInput =
324                 OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
325         ServicePowerSetupOutput servicePowerSetupOutput =
326                 this.olmPowerService.servicePowerSetup(servicePowerSetupInput);
327         Assert.assertEquals(ResponseCodes.SUCCESS_RESULT, servicePowerSetupOutput.getResult());
328     }
329
330     @Test
331     public void testServicePowerSetupFailResult() {
332         ServicePowerSetupInput servicePowerSetupInput = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
333         Mockito.when(powerMgmtMock.setPower(servicePowerSetupInput)).thenReturn(Boolean.FALSE);
334         OlmPowerService olmPowerServiceWithMock = new OlmPowerServiceImpl(dataBroker, powerMgmtMock,
335                 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
336         ServicePowerSetupOutput servicePowerSetupOutput =
337                 olmPowerServiceWithMock.servicePowerSetup(servicePowerSetupInput);
338         Assert.assertEquals(ResponseCodes.FAILED_RESULT, servicePowerSetupOutput.getResult());
339     }
340 }