bf7bfaeb3e37916e30c4d36ff1cc6f16eaaa16e8
[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.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;
68
69 public class OlmPowerServiceImplTest  extends AbstractTest {
70
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;
88     @InjectMocks
89     private OlmPowerService olmPowerServiceMock;
90     private OtsPmHolder otsPmHolder;
91
92
93     @Before
94     public void setUp() {
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.mappingUtils,
115                 this.portMappingVersion121);
116         this.portMapping = Mockito.spy(this.portMapping);
117         this.powerMgmt = new PowerMgmtImpl(this.getDataBroker(), this.openRoadmInterfaces, this.crossConnect,
118             this.deviceTransactionManager);
119         this.olmPowerService = new OlmPowerServiceImpl(this.getDataBroker(), this.powerMgmt,
120             this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
121         this.powerMgmtMock = Mockito.mock(PowerMgmt.class);
122         this.olmPowerServiceMock = new OlmPowerServiceImpl(this.getDataBroker(), this.powerMgmtMock,
123             this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
124         this.olmPowerServiceMock = Mockito.mock(OlmPowerServiceImpl.class);
125         MockitoAnnotations.initMocks(this);
126     }
127
128     @Test
129     public void dummyTest() {
130         OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
131         olmPowerServiceImpl.init();
132         olmPowerServiceImpl.close();
133     }
134
135
136     @Test
137     public void testGetPm() {
138         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
139         GetPmOutput output = this.olmPowerService.getPm(input);
140         Assert.assertEquals(new GetPmOutputBuilder().build(), output);
141         Assert.assertEquals(null, output.getResourceId());
142     }
143
144     @Test
145     public void testServicePowerSetupSuccess() {
146         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
147         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
148         //TODO
149         Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
150                 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Success").build());
151         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
152         Assert.assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), output);
153         Assert.assertEquals("Success", output.getResult());
154     }
155
156     @Test
157     public void testServicePowerSetupFailed() {
158         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
159         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
160         //TODO
161         Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
162                 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Failed").build());
163         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
164         Assert.assertEquals("Failed", output.getResult());
165     }
166
167     @Test
168     public void testServicePowerTurnDownSuccess() {
169         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
170         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
171         //TODO
172         Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
173                 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Success").build());
174         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
175         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
176         Assert.assertEquals("Success", output.getResult());
177     }
178
179     @Test
180     public void testServicePowerTurnDownFailed() {
181         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
182         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
183         //TODO
184         Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
185                 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Failed").build());
186         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
187         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
188         Assert.assertEquals("Failed", output.getResult());
189     }
190
191     /*
192     @Test
193     public void testCalculateSpanlossBase() {
194         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
195         //TODO
196         Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
197                 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
198         CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
199         Assert.assertEquals("Failed", output.getResult());
200
201     }
202     */
203
204     /*
205     @Test
206     public void testCalculateSpanlossBase2() {
207         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
208 <<<<<<< HEAD
209         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
210             .child(Network.class, overlayTopologyKey)
211 =======
212         InstanceIdentifier<Network1> networkTopoIID = InstanceIdentifier.builder(Networks.class)
213                 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
214                                 .ietf.network.rev180226.networks.Network.class,
215                         overlayTopologyKey)
216 >>>>>>> 87d8bf0... Retrieve OLM modifs from change 80051
217             .augmentation(Network1.class)
218             .build();
219         InstanceIdentifier<Network> networkIID = InstanceIdentifier.builder(Networks.class)
220                 .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
221                                 .ietf.network.rev180226.networks.Network.class,
222                         overlayTopologyKey)
223                 .build();
224         Network1 network = TransactionUtils.getNetwork();
225         Network ietfNetwork = TransactionUtils.getOverLayNetwork();
226         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, ietfNetwork);
227         TransactionUtils.writeTransaction(this.getDataBroker(), networkTopoIID, network);
228         try {
229             Thread.sleep(1000);
230         } catch (InterruptedException e) {
231             LOG.error("Write transaction failed !",e);
232         }
233         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
234         //TODO
235         Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
236                 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
237         CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
238         Assert.assertEquals("Failed", output.getResult());
239
240     }
241     */
242     @Test
243     public void testCalculateSpanlossBase3() {
244         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
245         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
246             .child(Network.class, overlayTopologyKey)
247             .augmentation(Network1.class)
248             .build();
249         Network1 network = TransactionUtils.getNetwork();
250         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
251         try {
252             Thread.sleep(1000);
253         } catch (InterruptedException e) {
254             LOG.error("Write transaction failed !",e);
255         }
256         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
257         //TODO
258         Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
259                 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
260         CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
261         Assert.assertEquals("Failed", output.getResult());
262
263     }
264
265
266     @Test
267     public void testCalculateSpanlossBase4() {
268         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
269         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
270             .child(Network.class, overlayTopologyKey)
271             .augmentation(Network1.class)
272             .build();
273         Network1 network = TransactionUtils.getEmptyNetwork();
274         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
275         try {
276             Thread.sleep(1000);
277         } catch (InterruptedException e) {
278             LOG.error("Write transaction failed !",e);
279         }
280         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
281         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
282         Assert.assertEquals("Failed", output.getResult());
283
284     }
285
286     @Test
287     public void testCalculateSpanlossBase5() {
288         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
289         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
290             .child(Network.class, overlayTopologyKey)
291             .augmentation(Network1.class)
292             .build();
293         Network1 network = TransactionUtils.getNullNetwork();
294         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
295         try {
296             Thread.sleep(1000);
297         } catch (InterruptedException e) {
298             LOG.error("Write transaction failed !",e);
299         }
300         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
301         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
302         Assert.assertEquals("Failed", output.getResult());
303
304     }
305
306 //    @Test
307 //    public void testCalculateSpanlossBase4() throws InterruptedException {
308 //        NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
309 //        InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
310 //            .augmentation(Network1.class)
311 //            .build();
312 //        Network1 network = TransactionUtils.getNetwork2();
313 //        TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
314 //
315 //        Thread.sleep(500);
316 //
317 //        List<NodeId> nodes = TransactionUtils.getNodes();
318 //        Node n;
319 //        SupportingNode sn;
320 //
321 //        List<SupportingNode> snl;
322 //        NodeId n5 = new NodeId("node 5");
323 //        KeyedInstanceIdentifier<Node, NodeKey> mappedNodeII2 =
324 //            InstanceIdentifiers.UNDERLAY_NETWORK_II.child(Node.class, new NodeKey(n5));
325 //
326 //        sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
327 //            .setNodeRef(n5).build();
328 //        snl = new ArrayList<>();
329 //        snl.add(sn);
330 //        n = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608
331 //            .network.NodeBuilder().setNodeId(n5).build();
332 //        TransactionUtils.writeTransaction(this.getDataBroker(), mappedNodeII2, n);
333 //        Thread.sleep(500);
334 //
335 //        for(int i=0; i < nodes.size(); i++) {
336 //            KeyedInstanceIdentifier<Node, NodeKey> mappedNodeII =
337 //                InstanceIdentifiers.OVERLAY_NETWORK_II.child(Node.class, new NodeKey(nodes.get(i)));
338 //            if (i != 0){
339 //                sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
340 //                    .setNodeRef(nodes.get(i-1)).build();
341 //            }else {
342 //                sn = new SupportingNodeBuilder().setNodeRef(n5).build();
343 //            }
344 //            snl = new ArrayList<>();
345 //            snl.add(sn);
346 //            n = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608
347 //                .network.NodeBuilder().setNodeId(nodes.get(i)).setSupportingNode(snl).build();
348 //            TransactionUtils.writeTransaction(this.getDataBroker(), mappedNodeII, n);
349 //            Thread.sleep(500);
350 //        }
351 //
352 //        Thread.sleep(1000);
353 //
354 //        CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
355 //        CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
356 //        Assert.assertEquals("Failed", output.getResult());
357 //
358 //    }
359
360     @Test
361     public void testCalculateSpanlossCurrent1() {
362         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
363         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
364         Assert.assertEquals(null, output);
365     }
366
367     @Test
368     public void testCalculateSpanlossCurrent2() {
369         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
370         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
371             .child(Network.class, overlayTopologyKey)
372             .augmentation(Network1.class)
373             .build();
374         Network1 network = TransactionUtils.getNetwork();
375         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
376         try {
377             Thread.sleep(1000);
378         } catch (InterruptedException e) {
379             LOG.error("Write transaction failed !",e);
380         }
381         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
382         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
383         Assert.assertEquals(null, output);
384     }
385
386     @Test
387     public void testServicePowerReset() {
388         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
389         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
390         Assert.assertEquals(null, output);
391     }
392 }