Add node-info in portmapping structure
[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.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);
125     }
126
127     @Test
128     public void dummyTest() {
129         OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
130         olmPowerServiceImpl.init();
131         olmPowerServiceImpl.close();
132     }
133
134
135     @Test
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());
141     }
142
143     @Test
144     public void testServicePowerSetupSuccess() {
145         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
146         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
147         //TODO
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());
153     }
154
155     @Test
156     public void testServicePowerSetupFailed() {
157         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
158         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
159         //TODO
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());
164     }
165
166     @Test
167     public void testServicePowerTurnDownSuccess() {
168         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
169         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
170         //TODO
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());
176     }
177
178     @Test
179     public void testServicePowerTurnDownFailed() {
180         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
181         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
182         //TODO
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());
188     }
189
190     /*
191     @Test
192     public void testCalculateSpanlossBase() {
193         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
194         //TODO
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());
199
200     }
201     */
202
203     /*
204     @Test
205     public void testCalculateSpanlossBase2() {
206         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
207 <<<<<<< HEAD
208         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
209             .child(Network.class, overlayTopologyKey)
210 =======
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,
214                         overlayTopologyKey)
215 >>>>>>> 87d8bf0... Retrieve OLM modifs from change 80051
216             .augmentation(Network1.class)
217             .build();
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,
221                         overlayTopologyKey)
222                 .build();
223         Network1 network = TransactionUtils.getNetwork();
224         Network ietfNetwork = TransactionUtils.getOverLayNetwork();
225         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, ietfNetwork);
226         TransactionUtils.writeTransaction(this.getDataBroker(), networkTopoIID, network);
227         try {
228             Thread.sleep(1000);
229         } catch (InterruptedException e) {
230             LOG.error("Write transaction failed !",e);
231         }
232         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
233         //TODO
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());
238
239     }
240     */
241     @Test
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)
247             .build();
248         Network1 network = TransactionUtils.getNetwork();
249         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
250         try {
251             Thread.sleep(1000);
252         } catch (InterruptedException e) {
253             LOG.error("Write transaction failed !",e);
254         }
255         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
256         //TODO
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());
261
262     }
263
264
265     @Test
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)
271             .build();
272         Network1 network = TransactionUtils.getEmptyNetwork();
273         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
274         try {
275             Thread.sleep(1000);
276         } catch (InterruptedException e) {
277             LOG.error("Write transaction failed !",e);
278         }
279         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
280         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
281         Assert.assertEquals("Failed", output.getResult());
282
283     }
284
285     @Test
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)
291             .build();
292         Network1 network = TransactionUtils.getNullNetwork();
293         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
294         try {
295             Thread.sleep(1000);
296         } catch (InterruptedException e) {
297             LOG.error("Write transaction failed !",e);
298         }
299         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
300         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
301         Assert.assertEquals("Failed", output.getResult());
302
303     }
304
305 //    @Test
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)
310 //            .build();
311 //        Network1 network = TransactionUtils.getNetwork2();
312 //        TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
313 //
314 //        Thread.sleep(500);
315 //
316 //        List<NodeId> nodes = TransactionUtils.getNodes();
317 //        Node n;
318 //        SupportingNode sn;
319 //
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));
324 //
325 //        sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
326 //            .setNodeRef(n5).build();
327 //        snl = new ArrayList<>();
328 //        snl.add(sn);
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);
333 //
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)));
337 //            if (i != 0){
338 //                sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
339 //                    .setNodeRef(nodes.get(i-1)).build();
340 //            }else {
341 //                sn = new SupportingNodeBuilder().setNodeRef(n5).build();
342 //            }
343 //            snl = new ArrayList<>();
344 //            snl.add(sn);
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);
349 //        }
350 //
351 //        Thread.sleep(1000);
352 //
353 //        CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
354 //        CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
355 //        Assert.assertEquals("Failed", output.getResult());
356 //
357 //    }
358
359     @Test
360     public void testCalculateSpanlossCurrent1() {
361         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
362         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
363         Assert.assertEquals(null, output);
364     }
365
366     @Test
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)
372             .build();
373         Network1 network = TransactionUtils.getNetwork();
374         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
375         try {
376             Thread.sleep(1000);
377         } catch (InterruptedException e) {
378             LOG.error("Write transaction failed !",e);
379         }
380         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
381         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
382         Assert.assertEquals(null, output);
383     }
384
385     @Test
386     public void testServicePowerReset() {
387         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
388         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
389         Assert.assertEquals(null, output);
390     }
391 }