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.MappingUtils;
28 import org.opendaylight.transportpce.common.mapping.PortMapping;
29 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
30 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
31 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
32 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
33 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
34 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
35 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
36 import org.opendaylight.transportpce.olm.power.PowerMgmt;
37 import org.opendaylight.transportpce.olm.power.PowerMgmtImpl;
38 import org.opendaylight.transportpce.olm.stub.MountPointServiceStub;
39 import org.opendaylight.transportpce.olm.stub.MountPointStub;
40 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
41 import org.opendaylight.transportpce.olm.util.OtsPmHolder;
42 import org.opendaylight.transportpce.olm.util.TransactionUtils;
43 import org.opendaylight.transportpce.test.AbstractTest;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseInput;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseOutput;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseOutputBuilder;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossCurrentInput;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossCurrentOutput;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmInput;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmOutput;
51 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmOutputBuilder;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerResetInput;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerResetOutput;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupOutput;
56 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupOutputBuilder;
57 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownInput;
58 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutput;
59 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutputBuilder;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
65 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
69 public class OlmPowerServiceImplTest extends AbstractTest {
71 private static final Logger LOG = LoggerFactory.getLogger(OlmPowerServiceImplTest.class);
72 private MountPoint mountPoint;
73 private MountPointService mountPointService;
74 private DeviceTransactionManager deviceTransactionManager;
75 private CrossConnect crossConnect;
76 private OpenRoadmInterfaces openRoadmInterfaces;
77 private PortMapping portMapping;
78 private PowerMgmt powerMgmt;
79 private CrossConnectImpl121 crossConnectImpl121;
80 private CrossConnectImpl221 crossConnectImpl22;
81 private MappingUtils mappingUtils;
82 private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
83 private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl22;
84 private PortMappingVersion221 portMappingVersion22;
85 private PortMappingVersion121 portMappingVersion121;
86 private OlmPowerService olmPowerService;
87 private PowerMgmt powerMgmtMock;
89 private OlmPowerService olmPowerServiceMock;
90 private OtsPmHolder otsPmHolder;
95 this.mountPoint = new MountPointStub(this.getDataBroker());
96 this.mountPointService = new MountPointServiceStub(mountPoint);
97 this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
98 this.mappingUtils = Mockito.spy(MappingUtils.class);
99 Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
100 .getOpenRoadmVersion(Mockito.anyString());
101 this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
102 this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
103 this.crossConnectImpl22 = new CrossConnectImpl221(deviceTransactionManager);
104 this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
105 this.crossConnectImpl22);
106 this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
107 this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager);
108 this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
109 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22);
110 this.portMappingVersion22 =
111 new PortMappingVersion221(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
112 this.portMappingVersion121 =
113 new PortMappingVersion121(getDataBroker(), deviceTransactionManager, this.openRoadmInterfaces);
114 this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion22, this.portMappingVersion121);
115 this.portMapping = Mockito.spy(this.portMapping);
116 this.powerMgmt = new PowerMgmtImpl(this.getDataBroker(), this.openRoadmInterfaces, this.crossConnect,
117 this.deviceTransactionManager);
118 this.olmPowerService = new OlmPowerServiceImpl(this.getDataBroker(), this.powerMgmt,
119 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
120 this.powerMgmtMock = Mockito.mock(PowerMgmt.class);
121 this.olmPowerServiceMock = new OlmPowerServiceImpl(this.getDataBroker(), this.powerMgmtMock,
122 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
123 this.olmPowerServiceMock = Mockito.mock(OlmPowerServiceImpl.class);
124 MockitoAnnotations.initMocks(this);
128 public void dummyTest() {
129 OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
130 olmPowerServiceImpl.init();
131 olmPowerServiceImpl.close();
136 public void testGetPm() {
137 GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
138 GetPmOutput output = this.olmPowerService.getPm(input);
139 Assert.assertEquals(new GetPmOutputBuilder().build(), output);
140 Assert.assertEquals(null, output.getResourceId());
144 public void testServicePowerSetupSuccess() {
145 ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
146 Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
148 Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
149 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Success").build());
150 ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
151 Assert.assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), output);
152 Assert.assertEquals("Success", output.getResult());
156 public void testServicePowerSetupFailed() {
157 ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
158 Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
160 Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
161 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Failed").build());
162 ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
163 Assert.assertEquals("Failed", output.getResult());
167 public void testServicePowerTurnDownSuccess() {
168 ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
169 Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
171 Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
172 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Success").build());
173 ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
174 Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
175 Assert.assertEquals("Success", output.getResult());
179 public void testServicePowerTurnDownFailed() {
180 ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
181 Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
183 Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
184 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Failed").build());
185 ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
186 Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
187 Assert.assertEquals("Failed", output.getResult());
192 public void testCalculateSpanlossBase() {
193 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
195 Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
196 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
197 CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
198 Assert.assertEquals("Failed", output.getResult());
205 public void testCalculateSpanlossBase2() {
206 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
208 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
209 .child(Network.class, overlayTopologyKey)
211 InstanceIdentifier<Network1> networkTopoIID = InstanceIdentifier.builder(Networks.class)
212 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
213 .ietf.network.rev180226.networks.Network.class,
215 >>>>>>> 87d8bf0... Retrieve OLM modifs from change 80051
216 .augmentation(Network1.class)
218 InstanceIdentifier<Network> networkIID = InstanceIdentifier.builder(Networks.class)
219 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
220 .ietf.network.rev180226.networks.Network.class,
223 Network1 network = TransactionUtils.getNetwork();
224 Network ietfNetwork = TransactionUtils.getOverLayNetwork();
225 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, ietfNetwork);
226 TransactionUtils.writeTransaction(this.getDataBroker(), networkTopoIID, network);
229 } catch (InterruptedException e) {
230 LOG.error("Write transaction failed !",e);
232 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
234 Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
235 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
236 CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
237 Assert.assertEquals("Failed", output.getResult());
242 public void testCalculateSpanlossBase3() {
243 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
244 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
245 .child(Network.class, overlayTopologyKey)
246 .augmentation(Network1.class)
248 Network1 network = TransactionUtils.getNetwork();
249 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
252 } catch (InterruptedException e) {
253 LOG.error("Write transaction failed !",e);
255 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
257 Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
258 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
259 CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
260 Assert.assertEquals("Failed", output.getResult());
266 public void testCalculateSpanlossBase4() {
267 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
268 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
269 .child(Network.class, overlayTopologyKey)
270 .augmentation(Network1.class)
272 Network1 network = TransactionUtils.getEmptyNetwork();
273 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
276 } catch (InterruptedException e) {
277 LOG.error("Write transaction failed !",e);
279 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
280 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
281 Assert.assertEquals("Failed", output.getResult());
286 public void testCalculateSpanlossBase5() {
287 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
288 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
289 .child(Network.class, overlayTopologyKey)
290 .augmentation(Network1.class)
292 Network1 network = TransactionUtils.getNullNetwork();
293 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
296 } catch (InterruptedException e) {
297 LOG.error("Write transaction failed !",e);
299 CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
300 CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
301 Assert.assertEquals("Failed", output.getResult());
306 // public void testCalculateSpanlossBase4() throws InterruptedException {
307 // NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
308 // InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
309 // .augmentation(Network1.class)
311 // Network1 network = TransactionUtils.getNetwork2();
312 // TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
314 // Thread.sleep(500);
316 // List<NodeId> nodes = TransactionUtils.getNodes();
318 // SupportingNode sn;
320 // List<SupportingNode> snl;
321 // NodeId n5 = new NodeId("node 5");
322 // KeyedInstanceIdentifier<Node, NodeKey> mappedNodeII2 =
323 // InstanceIdentifiers.UNDERLAY_NETWORK_II.child(Node.class, new NodeKey(n5));
325 // sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
326 // .setNodeRef(n5).build();
327 // snl = new ArrayList<>();
329 // n = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608
330 // .network.NodeBuilder().setNodeId(n5).build();
331 // TransactionUtils.writeTransaction(this.getDataBroker(), mappedNodeII2, n);
332 // Thread.sleep(500);
334 // for(int i=0; i < nodes.size(); i++) {
335 // KeyedInstanceIdentifier<Node, NodeKey> mappedNodeII =
336 // InstanceIdentifiers.OVERLAY_NETWORK_II.child(Node.class, new NodeKey(nodes.get(i)));
338 // sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
339 // .setNodeRef(nodes.get(i-1)).build();
341 // sn = new SupportingNodeBuilder().setNodeRef(n5).build();
343 // snl = new ArrayList<>();
345 // n = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608
346 // .network.NodeBuilder().setNodeId(nodes.get(i)).setSupportingNode(snl).build();
347 // TransactionUtils.writeTransaction(this.getDataBroker(), mappedNodeII, n);
348 // Thread.sleep(500);
351 // Thread.sleep(1000);
353 // CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
354 // CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
355 // Assert.assertEquals("Failed", output.getResult());
360 public void testCalculateSpanlossCurrent1() {
361 CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
362 CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
363 Assert.assertEquals(null, output);
367 public void testCalculateSpanlossCurrent2() {
368 NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
369 InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
370 .child(Network.class, overlayTopologyKey)
371 .augmentation(Network1.class)
373 Network1 network = TransactionUtils.getNetwork();
374 TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
377 } catch (InterruptedException e) {
378 LOG.error("Write transaction failed !",e);
380 CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
381 CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
382 Assert.assertEquals(null, output);
386 public void testServicePowerReset() {
387 ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
388 ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
389 Assert.assertEquals(null, output);