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