Sync Common folder
[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.crossconnect.CrossConnect;
21 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
22 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
23 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl22;
24 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
25 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
26 import org.opendaylight.transportpce.common.mapping.MappingUtils;
27 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
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.PortMappingVersion22;
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.OpenRoadmInterfacesImpl22;
36 import org.opendaylight.transportpce.olm.power.PowerMgmt;
37 import org.opendaylight.transportpce.olm.stub.MountPointServiceStub;
38 import org.opendaylight.transportpce.olm.stub.MountPointStub;
39 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
40 import org.opendaylight.transportpce.olm.util.TransactionUtils;
41 import org.opendaylight.transportpce.test.AbstractTest;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseInput;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossBaseOutput;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossCurrentInput;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.CalculateSpanlossCurrentOutput;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmInput;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmOutput;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.GetPmOutputBuilder;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerResetInput;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerResetOutput;
51 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupOutput;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupOutputBuilder;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownInput;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutput;
56 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerTurndownOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.Network;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NetworkId;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NetworkKey;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev150608.Network1;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 public class OlmPowerServiceImplTest  extends AbstractTest {
66
67     private static final Logger LOG = LoggerFactory.getLogger(OlmPowerServiceImplTest.class);
68     private MountPoint mountPoint;
69     private MountPointService mountPointService;
70     private DeviceTransactionManager deviceTransactionManager;
71     private CrossConnect crossConnect;
72     private OpenRoadmInterfaces openRoadmInterfaces;
73     private PortMapping portMapping;
74     private PowerMgmt powerMgmt;
75     private OlmPowerService olmPowerService;
76     private PowerMgmt powerMgmtMock;
77     @InjectMocks
78     private OlmPowerService olmPowerServiceMock;
79     private MappingUtils mappingUtils;
80     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
81     private OpenRoadmInterfacesImpl22 openRoadmInterfacesImpl22;
82     private PortMappingVersion22 portMappingVersion22;
83     private PortMappingVersion121 portMappingVersion121;
84     private CrossConnectImpl121 crossConnectImpl121;
85     private CrossConnectImpl22 crossConnectImpl22;
86
87
88
89     @Before
90     public void setUp() {
91         this.mountPoint = new MountPointStub(this.getDataBroker());
92         this.mountPointService = new MountPointServiceStub(mountPoint);
93         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
94         this.crossConnectImpl121 = new CrossConnectImpl121(this.deviceTransactionManager);
95         this.crossConnectImpl22 = new CrossConnectImpl22(this.deviceTransactionManager);
96         this.crossConnect = new CrossConnectImpl(this.deviceTransactionManager, this.mappingUtils,
97                 this.crossConnectImpl121, this.crossConnectImpl22);
98         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(this.deviceTransactionManager);
99         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl22(this.deviceTransactionManager);
100         this.mappingUtils = new MappingUtilsImpl(getDataBroker());
101         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl(deviceTransactionManager, mappingUtils,
102                 openRoadmInterfacesImpl121, openRoadmInterfacesImpl22);
103         this.portMappingVersion22 =
104                 new PortMappingVersion22(getDataBroker(), this.deviceTransactionManager, this.openRoadmInterfaces);
105         this.portMappingVersion121 =
106                 new PortMappingVersion121(getDataBroker(), this.deviceTransactionManager, this.openRoadmInterfaces);
107         this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion22, this.mappingUtils,
108                 this.portMappingVersion121);
109         this.powerMgmt = new PowerMgmt(this.getDataBroker(), this.openRoadmInterfaces, this.crossConnect,
110             this.deviceTransactionManager);
111         this.olmPowerService = new OlmPowerServiceImpl(this.getDataBroker(), this.powerMgmt,
112             this.deviceTransactionManager, this.portMapping);
113         this.powerMgmtMock = Mockito.mock(PowerMgmt.class);
114         this.olmPowerServiceMock = new OlmPowerServiceImpl(this.getDataBroker(), this.powerMgmtMock,
115             this.deviceTransactionManager, this.portMapping);
116         MockitoAnnotations.initMocks(this);
117     }
118
119     @Test
120     public void dummyTest() {
121         OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
122         olmPowerServiceImpl.init();
123         olmPowerServiceImpl.close();
124     }
125
126
127     @Test
128     public void testGetPm() {
129         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
130         GetPmOutput output = this.olmPowerService.getPm(input);
131         Assert.assertEquals(new GetPmOutputBuilder().build(), output);
132         Assert.assertEquals(null, output.getResourceId());
133     }
134
135     @Test
136     public void testServicePowerSetupSuccess() {
137         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
138         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
139         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
140         Assert.assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), output);
141         Assert.assertEquals("Success", output.getResult());
142     }
143
144     @Test
145     public void testServicePowerSetupFailed() {
146         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
147         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
148         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
149         Assert.assertEquals("Failed", output.getResult());
150     }
151
152     @Test
153     public void testServicePowerTurnDownSuccess() {
154         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
155         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
156         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
157         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
158         Assert.assertEquals("Success", output.getResult());
159     }
160
161     @Test
162     public void testServicePowerTurnDownFailed() {
163         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
164         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
165         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
166         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
167         Assert.assertEquals("Failed", output.getResult());
168     }
169
170     @Test
171     public void testCalculateSpanlossBase() {
172         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
173         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
174         Assert.assertEquals("Failed", output.getResult());
175
176     }
177
178     @Test
179     public void testCalculateSpanlossBase2() {
180         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
181         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
182             .augmentation(Network1.class)
183             .build();
184         Network1 network = TransactionUtils.getNetwork();
185         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
186         try {
187             Thread.sleep(1000);
188         } catch (InterruptedException e) {
189             LOG.error("Write transaction failed !",e);
190         }
191         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput();
192         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
193         Assert.assertEquals("Failed", output.getResult());
194
195     }
196
197     @Test
198     public void testCalculateSpanlossBase3() {
199         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
200         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
201             .augmentation(Network1.class)
202             .build();
203         Network1 network = TransactionUtils.getNetwork();
204         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
205         try {
206             Thread.sleep(1000);
207         } catch (InterruptedException e) {
208             LOG.error("Write transaction failed !",e);
209         }
210         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
211         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
212         Assert.assertEquals("Failed", output.getResult());
213
214     }
215
216
217     @Test
218     public void testCalculateSpanlossBase4() {
219         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
220         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
221             .augmentation(Network1.class)
222             .build();
223         Network1 network = TransactionUtils.getEmptyNetwork();
224         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
225         try {
226             Thread.sleep(1000);
227         } catch (InterruptedException e) {
228             LOG.error("Write transaction failed !",e);
229         }
230         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
231         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
232         Assert.assertEquals("Failed", output.getResult());
233
234     }
235
236     @Test
237     public void testCalculateSpanlossBase5() {
238         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
239         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
240             .augmentation(Network1.class)
241             .build();
242         Network1 network = TransactionUtils.getNullNetwork();
243         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
244         try {
245             Thread.sleep(1000);
246         } catch (InterruptedException e) {
247             LOG.error("Write transaction failed !",e);
248         }
249         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
250         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
251         Assert.assertEquals("Failed", output.getResult());
252
253     }
254
255 //    @Test
256 //    public void testCalculateSpanlossBase4() throws InterruptedException {
257 //        NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
258 //        InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
259 //            .augmentation(Network1.class)
260 //            .build();
261 //        Network1 network = TransactionUtils.getNetwork2();
262 //        TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
263 //
264 //        Thread.sleep(500);
265 //
266 //        List<NodeId> nodes = TransactionUtils.getNodes();
267 //        Node n;
268 //        SupportingNode sn;
269 //
270 //        List<SupportingNode> snl;
271 //        NodeId n5 = new NodeId("node 5");
272 //        KeyedInstanceIdentifier<Node, NodeKey> mappedNodeII2 =
273 //            InstanceIdentifiers.UNDERLAY_NETWORK_II.child(Node.class, new NodeKey(n5));
274 //
275 //        sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
276 //            .setNodeRef(n5).build();
277 //        snl = new ArrayList<>();
278 //        snl.add(sn);
279 //        n = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608
280 //            .network.NodeBuilder().setNodeId(n5).build();
281 //        TransactionUtils.writeTransaction(this.getDataBroker(), mappedNodeII2, n);
282 //        Thread.sleep(500);
283 //
284 //        for(int i=0; i < nodes.size(); i++) {
285 //            KeyedInstanceIdentifier<Node, NodeKey> mappedNodeII =
286 //                InstanceIdentifiers.OVERLAY_NETWORK_II.child(Node.class, new NodeKey(nodes.get(i)));
287 //            if (i != 0){
288 //                sn = new SupportingNodeBuilder().setNetworkRef(new NetworkId(NetworkUtils.UNDERLAY_NETWORK_ID))
289 //                    .setNodeRef(nodes.get(i-1)).build();
290 //            }else {
291 //                sn = new SupportingNodeBuilder().setNodeRef(n5).build();
292 //            }
293 //            snl = new ArrayList<>();
294 //            snl.add(sn);
295 //            n = new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608
296 //                .network.NodeBuilder().setNodeId(nodes.get(i)).setSupportingNode(snl).build();
297 //            TransactionUtils.writeTransaction(this.getDataBroker(), mappedNodeII, n);
298 //            Thread.sleep(500);
299 //        }
300 //
301 //        Thread.sleep(1000);
302 //
303 //        CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
304 //        CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
305 //        Assert.assertEquals("Failed", output.getResult());
306 //
307 //    }
308
309     @Test
310     public void testCalculateSpanlossCurrent1() {
311         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
312         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
313         Assert.assertEquals(null, output);
314     }
315
316     @Test
317     public void testCalculateSpanlossCurrent2() {
318         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
319         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Network.class, overlayTopologyKey)
320             .augmentation(Network1.class)
321             .build();
322         Network1 network = TransactionUtils.getNetwork();
323         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
324         try {
325             Thread.sleep(1000);
326         } catch (InterruptedException e) {
327             LOG.error("Write transaction failed !",e);
328         }
329         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
330         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
331         Assert.assertEquals(null, output);
332     }
333
334     @Test
335     public void testServicePowerReset() {
336         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
337         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
338         Assert.assertEquals(null, output);
339     }
340 }