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.controller.md.sal.binding.api.MountPoint;
18 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
19 import org.opendaylight.transportpce.common.NetworkUtils;
20 import org.opendaylight.transportpce.common.StringConstants;
21 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
22 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
23 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
24 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl221;
25 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
26 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
27 import org.opendaylight.transportpce.common.mapping.*;
28 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
29 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
30 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
31 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
32 import org.opendaylight.transportpce.olm.power.PowerMgmt;
33 import org.opendaylight.transportpce.olm.power.PowerMgmtImpl;
34 import org.opendaylight.transportpce.olm.stub.MountPointServiceStub;
35 import org.opendaylight.transportpce.olm.stub.MountPointStub;
36 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
37 import org.opendaylight.transportpce.olm.util.TransactionUtils;
38 import org.opendaylight.transportpce.test.AbstractTest;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseInput;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseOutput;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossCurrentInput;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossCurrentOutput;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmInput;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmOutput;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmOutputBuilder;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerResetInput;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerResetOutput;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupOutput;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupOutputBuilder;
51 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownInput;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutput;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 public class OlmPowerServiceImplTest extends AbstractTest {
65 private static final Logger LOG = LoggerFactory.getLogger(OlmPowerServiceImplTest.class);
66 private MountPoint mountPoint;
67 private MountPointService mountPointService;
68 private DeviceTransactionManager deviceTransactionManager;
69 private CrossConnect crossConnect;
70 private OpenRoadmInterfaces openRoadmInterfaces;
71 private PortMapping portMapping;
72 private PowerMgmt powerMgmt;
73 private CrossConnectImpl121 crossConnectImpl121;
74 private CrossConnectImpl221 crossConnectImpl22;
75 private MappingUtils mappingUtils;
76 private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
77 private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl22;
78 private PortMappingVersion221 portMappingVersion22;
79 private PortMappingVersion121 portMappingVersion121;
80 private OlmPowerService olmPowerService;
81 private PowerMgmt powerMgmtMock;
83 private OlmPowerService olmPowerServiceMock;
89 this.mountPoint = new MountPointStub(this.getDataBroker());
90 this.mountPointService = new MountPointServiceStub(mountPoint);
91 this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
92 this.mappingUtils = Mockito.spy(MappingUtils.class);
93 Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
94 .getOpenRoadmVersion(Mockito.anyString());
95 this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
96 this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
97 this.crossConnectImpl22 = new CrossConnectImpl221(deviceTransactionManager);
98 this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
99 this.crossConnectImpl22);
100 this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
101 this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager);
102 this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
103 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22);
104 this.portMappingVersion22 =
105 new PortMappingVersion221(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
106 this.portMappingVersion121 =
107 new PortMappingVersion121(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
108 this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion22, this.mappingUtils,
109 this.portMappingVersion121);
110 this.portMapping = Mockito.spy(this.portMapping);
111 this.powerMgmt = new PowerMgmtImpl(this.getDataBroker(), this.openRoadmInterfaces, this.crossConnect,
112 this.deviceTransactionManager);
113 this.olmPowerService = new OlmPowerServiceImpl(this.getDataBroker(), this.powerMgmt,
114 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
115 this.powerMgmtMock = Mockito.mock(PowerMgmt.class);
116 this.olmPowerServiceMock = new OlmPowerServiceImpl(this.getDataBroker(), this.powerMgmtMock,
117 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
118 MockitoAnnotations.initMocks(this);
122 public void dummyTest() {
123 OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
124 olmPowerServiceImpl.init();
125 olmPowerServiceImpl.close();
130 public void testGetPm() {
131 GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
132 GetPmOutput output = this.olmPowerService.getPm(input);
133 Assert.assertEquals(new GetPmOutputBuilder().build(), output);
134 Assert.assertEquals(null, output.getResourceId());
138 public void testServicePowerSetupSuccess() {
139 ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
140 Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
141 ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
142 Assert.assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), output);
143 Assert.assertEquals("Success", output.getResult());
147 public void testServicePowerSetupFailed() {
148 ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
149 Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
150 ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
151 Assert.assertEquals("Failed", output.getResult());
155 public void testServicePowerTurnDownSuccess() {
156 ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
157 Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
158 ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
159 Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
160 Assert.assertEquals("Success", output.getResult());
164 public void testServicePowerTurnDownFailed() {
165 ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
166 Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
167 ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
168 Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
169 Assert.assertEquals("Failed", output.getResult());
174 public void testCalculateSpanlossBase() {
175 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
176 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
177 Assert.assertEquals("Failed", output.getResult());
184 public void testCalculateSpanlossBase2() {
185 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
186 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
187 .child(Network.class, overlayTopologyKey)
188 .augmentation(Network1.class)
190 Network1 network = TransactionUtils.getNetwork();
191 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
194 } catch (InterruptedException e) {
195 LOG.error("Write transaction failed !",e);
197 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
198 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
199 Assert.assertEquals("Failed", output.getResult());
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)
210 Network1 network = TransactionUtils.getNetwork();
211 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
214 } catch (InterruptedException e) {
215 LOG.error("Write transaction failed !",e);
217 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
218 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
219 Assert.assertEquals("Failed", output.getResult());
225 public void testCalculateSpanlossBase4() {
226 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
227 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
228 .child(Network.class, overlayTopologyKey)
229 .augmentation(Network1.class)
231 Network1 network = TransactionUtils.getEmptyNetwork();
232 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
235 } catch (InterruptedException e) {
236 LOG.error("Write transaction failed !",e);
238 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
239 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
240 Assert.assertEquals("Failed", output.getResult());
245 public void testCalculateSpanlossBase5() {
246 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
247 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
248 .child(Network.class, overlayTopologyKey)
249 .augmentation(Network1.class)
251 Network1 network = TransactionUtils.getNullNetwork();
252 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
255 } catch (InterruptedException e) {
256 LOG.error("Write transaction failed !",e);
258 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
259 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
260 Assert.assertEquals("Failed", output.getResult());
265 // public void testCalculateSpanlossBase4() throws InterruptedException {
266 // NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
267 // InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
268 // .augmentation(Network1.class)
270 // Network1 network = TransactionUtils.getNetwork2();
271 // TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
273 // Thread.sleep(500);
275 // List<NodeId> nodes = TransactionUtils.getNodes();
277 // SupportingNode sn;
279 // List<SupportingNode> snl;
280 // NodeId n5 = new NodeId("node 5");
281 // KeyedInstanceIdentifier<Node, NodeKey> mappedNodeII2 =
282 // InstanceIdentifiers.UNDERLAY_NETWORK_II.child(Node.class, new NodeKey(n5));
284 // sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
285 // .setNodeRef(n5).build();
286 // snl = new ArrayList<>();
288 // n = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608
289 // .network.NodeBuilder().setNodeId(n5).build();
290 // TransactionUtils.writeTransaction(this.getDataBroker(), mappedNodeII2, n);
291 // Thread.sleep(500);
293 // for(int i=0; i < nodes.size(); i++) {
294 // KeyedInstanceIdentifier<Node, NodeKey> mappedNodeII =
295 // InstanceIdentifiers.OVERLAY_NETWORK_II.child(Node.class, new NodeKey(nodes.get(i)));
297 // sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
298 // .setNodeRef(nodes.get(i-1)).build();
300 // sn = new SupportingNodeBuilder().setNodeRef(n5).build();
302 // snl = new ArrayList<>();
304 // n = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608
305 // .network.NodeBuilder().setNodeId(nodes.get(i)).setSupportingNode(snl).build();
306 // TransactionUtils.writeTransaction(this.getDataBroker(), mappedNodeII, n);
307 // Thread.sleep(500);
310 // Thread.sleep(1000);
312 // CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
313 // CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
314 // Assert.assertEquals("Failed", output.getResult());
319 public void testCalculateSpanlossCurrent1() {
320 CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
321 CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
322 Assert.assertEquals(null, output);
326 public void testCalculateSpanlossCurrent2() {
327 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
328 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
329 .child(Network.class, overlayTopologyKey)
330 .augmentation(Network1.class)
332 Network1 network = TransactionUtils.getNetwork();
333 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
336 } catch (InterruptedException e) {
337 LOG.error("Write transaction failed !",e);
339 CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
340 CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
341 Assert.assertEquals(null, output);
345 public void testServicePowerReset() {
346 ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
347 ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
348 Assert.assertEquals(null, output);