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