2 * Copyright © 2018 Orange, Inc. and others. All rights reserved.
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
9 package org.opendaylight.transportpce.olm.service;
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;
73 public class OlmPowerServiceImplTest extends AbstractTest {
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;
94 private OlmPowerService olmPowerServiceMock;
95 private OtsPmHolder otsPmHolder;
100 this.dataBroker = getNewDataBroker();
101 this.mountPoint = new MountPointStub(this.dataBroker);
102 this.mountPointService = new MountPointServiceStub(mountPoint);
103 this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
104 this.mappingUtils = Mockito.spy(new MappingUtilsImpl(dataBroker));
105 Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
106 .getOpenRoadmVersion(Mockito.anyString());
107 this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
108 this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
109 this.crossConnectImpl22 = new CrossConnectImpl221(deviceTransactionManager);
110 this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
111 this.crossConnectImpl22);
112 this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
113 this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager);
114 this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
115 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22);
116 this.portMappingVersion22 =
117 new PortMappingVersion221(dataBroker, deviceTransactionManager, this.openRoadmInterfaces);
118 this.portMappingVersion121 =
119 new PortMappingVersion121(dataBroker, deviceTransactionManager, this.openRoadmInterfaces);
120 this.portMapping = new PortMappingImpl(dataBroker, this.portMappingVersion22, this.portMappingVersion121);
121 this.portMapping = Mockito.spy(this.portMapping);
122 this.powerMgmt = new PowerMgmtImpl(this.dataBroker, this.openRoadmInterfaces, this.crossConnect,
123 this.deviceTransactionManager);
124 this.olmPowerService = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmt,
125 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
126 this.powerMgmtMock = PowerMockito.mock(PowerMgmtImpl.class);
127 this.olmPowerServiceMock = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmtMock,
128 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
129 this.olmPowerServiceMock = Mockito.mock(OlmPowerServiceImpl.class);
130 MockitoAnnotations.initMocks(this);
134 public void dummyTest() {
135 OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
136 olmPowerServiceImpl.init();
137 olmPowerServiceImpl.close();
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());
150 public void testServicePowerSetupSuccess() {
151 ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
152 Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
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());
162 public void testServicePowerSetupFailed() {
163 ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
164 Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
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());
173 public void testServicePowerTurnDownSuccess() {
174 ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
175 Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
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());
185 public void testServicePowerTurnDownFailed() {
186 ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
187 Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
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());
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)
203 Network1 network = TransactionUtils.getNetwork();
204 TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
207 } catch (InterruptedException e) {
208 LOG.error("Write transaction failed !",e);
210 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
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());
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)
227 Network1 network = TransactionUtils.getEmptyNetwork();
228 TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
231 } catch (InterruptedException e) {
232 LOG.error("Write transaction failed !",e);
234 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
235 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
236 Assert.assertEquals("Failed", output.getResult());
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)
247 Network1 network = TransactionUtils.getNullNetwork();
248 TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
251 } catch (InterruptedException e) {
252 LOG.error("Write transaction failed !",e);
254 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
255 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
256 Assert.assertEquals("Failed", output.getResult());
261 public void testCalculateSpanlossCurrent1() {
262 CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
263 CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
264 Assert.assertEquals(null, output);
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)
274 Network1 network = TransactionUtils.getNetwork();
275 TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
278 } catch (InterruptedException e) {
279 LOG.error("Write transaction failed !",e);
281 CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
282 CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
283 Assert.assertEquals(null, output);
287 public void testServicePowerReset() {
288 ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
289 ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
290 Assert.assertEquals(null, output);
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());
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());
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());
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());