Remove getNode method from OlmUtils
[transportpce.git] / olm / src / test / java / org / opendaylight / transportpce / olm / power / PowerMgmtTest.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.power;
10
11 import java.util.List;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Ignore;
15 import org.junit.Test;
16 import org.mockito.Mockito;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.MountPoint;
19 import org.opendaylight.mdsal.binding.api.MountPointService;
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.crossconnect.CrossConnectImpl710;
26 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
27 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
28 import org.opendaylight.transportpce.common.mapping.MappingUtils;
29 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
30 import org.opendaylight.transportpce.common.mapping.PortMapping;
31 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
32 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
33 import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
34 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
35 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
36 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
37 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
38 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl710;
39 import org.opendaylight.transportpce.olm.stub.MountPointServiceStub;
40 import org.opendaylight.transportpce.olm.stub.MountPointStub;
41 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
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.rev210618.ServicePowerSetupInput;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInput;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.OpenroadmNodeVersion;
47 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.network.NodesBuilder;
48 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev220316.network.nodes.NodeInfoBuilder;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.types.rev191129.NodeTypes;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
51
52
53 public class PowerMgmtTest extends AbstractTest {
54     private MountPoint mountPoint;
55     private MountPointService mountPointService;
56     private DeviceTransactionManager deviceTransactionManager;
57     private CrossConnect crossConnect;
58     private OpenRoadmInterfaces openRoadmInterfaces;
59     private PortMapping portMapping;
60     private PowerMgmt powerMgmt;
61     private CrossConnectImpl121 crossConnectImpl121;
62     private CrossConnectImpl221 crossConnectImpl22;
63     private CrossConnectImpl710 crossConnectImpl710;
64     private MappingUtils mappingUtils;
65     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
66     private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl22;
67     private OpenRoadmInterfacesImpl710 openRoadmInterfacesImpl710;
68     private PortMappingVersion710 portMappingVersion710;
69     private PortMappingVersion221 portMappingVersion22;
70     private PortMappingVersion121 portMappingVersion121;
71     private DataBroker dataBroker;
72
73     @Before
74     public void setUp() {
75         dataBroker = this.getNewDataBroker();
76         this.mountPoint = new MountPointStub(dataBroker);
77         this.mountPointService = new MountPointServiceStub(mountPoint);
78         this.mappingUtils = Mockito.spy(new MappingUtilsImpl(dataBroker));
79         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
80                 .getOpenRoadmVersion(Mockito.anyString());
81         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
82         this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
83         this.crossConnectImpl22 = new CrossConnectImpl221(deviceTransactionManager);
84         this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
85                 this.crossConnectImpl22, this.crossConnectImpl710);
86         this.portMappingVersion22 = new PortMappingVersion221(dataBroker, deviceTransactionManager);
87         this.portMappingVersion121 = new PortMappingVersion121(dataBroker, deviceTransactionManager);
88 //        this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion710,
89 //                this.portMappingVersion22, this.portMappingVersion121);
90         this.portMapping = Mockito.mock(PortMapping.class);
91         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
92         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager, this.portMapping,
93             this.portMappingVersion22);
94         this.openRoadmInterfacesImpl710 = new OpenRoadmInterfacesImpl710(deviceTransactionManager, this.portMapping,
95             this.portMappingVersion710);
96         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
97                 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22,
98             this.openRoadmInterfacesImpl710);
99         this.openRoadmInterfaces = Mockito.spy(this.openRoadmInterfaces);
100         this.portMapping = Mockito.spy(this.portMapping);
101         this.powerMgmt = new PowerMgmtImpl(this.dataBroker, this.openRoadmInterfaces, this.crossConnect,
102                 this.deviceTransactionManager, this.portMapping);
103         Mockito.when(this.portMapping.getNode(Mockito.anyString()))
104                 .thenReturn(new NodesBuilder()
105                         .setNodeId("node")
106                         .setNodeInfo(new NodeInfoBuilder()
107                                 .setNodeType(NodeTypes.Xpdr)
108                                 .setOpenroadmVersion(OpenroadmNodeVersion._121)
109                                 .build())
110                         .build());
111     }
112
113     @Test
114     public void testSetPowerWhenMappingReturnNull() {
115         Mockito.when(this.portMapping.getNode(Mockito.anyString())).thenReturn(null);
116         boolean output = this.powerMgmt.setPower(OlmPowerServiceRpcImplUtil.getServicePowerSetupInput());
117         Assert.assertEquals(true, output);
118     }
119
120     @Test
121     public void testSetPower() {
122         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
123         boolean output = this.powerMgmt.setPower(input);
124         Assert.assertEquals(true, output);
125     }
126
127     @Ignore
128     @Test
129     public void testSetPower2() {
130         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
131         boolean output = this.powerMgmt.setPower(input);
132         Assert.assertEquals(true, output);
133     }
134
135     @Test
136     public void testSetPower3() {
137         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
138         boolean output = this.powerMgmt.setPower(input);
139         Assert.assertEquals(true, output);
140     }
141
142     @Test
143     public void testSetPower4() {
144         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
145         boolean output = this.powerMgmt.setPower(input);
146         Assert.assertEquals(true, output);
147     }
148
149     @Test
150     public void testPowerTurnDown() {
151         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
152         boolean output = this.powerMgmt.powerTurnDown(input);
153         Assert.assertEquals(true, output);
154     }
155
156     @Test
157     public void testPowerTurnDown2() {
158         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput2();
159         boolean output = this.powerMgmt.powerTurnDown(input);
160         Assert.assertEquals(false, output);
161     }
162
163     @Test
164     public void testPowerTurnDown3() {
165         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput3();
166         boolean output = this.powerMgmt.powerTurnDown(input);
167         Assert.assertEquals(true, output);
168     }
169
170     @Test
171     public void testPowerTurnDown4() {
172         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput4();
173         boolean output = this.powerMgmt.powerTurnDown(input);
174         Assert.assertEquals(false, output);
175     }
176
177     @Test
178     public void testSetPowerPresentNodes() throws InterruptedException {
179         List<NodeId> nodes = TransactionUtils.getNodeIds();
180         for (NodeId nodeId : nodes) {
181             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, null);
182             Thread.sleep(1000);
183         }
184         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
185         boolean output = this.powerMgmt.setPower(input);
186         Assert.assertEquals(true, output);
187     }
188
189     @Test
190     public void testSetPowerPresentNodes2() throws InterruptedException {
191         List<NodeId> nodes = TransactionUtils.getNodeIds();
192         for (NodeId nodeId : nodes) {
193             TransactionUtils.writeNodeTransaction2(nodeId.getValue(), this.dataBroker, null);
194             Thread.sleep(500);
195         }
196         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
197         boolean output = this.powerMgmt.setPower(input);
198         Assert.assertEquals(true, output);
199     }
200
201     @Test
202     public void testSetPowerPresentNodes3() throws InterruptedException {
203         List<NodeId> nodes = TransactionUtils.getNodeIds();
204         for (NodeId nodeId : nodes) {
205             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.dataBroker, null);
206             Thread.sleep(500);
207         }
208         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
209         boolean output = this.powerMgmt.setPower(input);
210         Assert.assertEquals(true, output);
211     }
212     /*
213     TODO: see if this test is always relevant
214     @Test
215     public void testSetPowerPresentNodes31() throws InterruptedException {
216         List<NodeId> nodes = TransactionUtils.getNodeIds();
217         for (NodeId nodeId : nodes) {
218             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), "deg");
219             Thread.sleep(500);
220         }
221         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
222         boolean output = this.powerMgmt.setPower(input);
223         Assert.assertEquals(false, output);
224     }*/
225
226     @Test
227     public void testSetPowerPresentNodes312() throws InterruptedException {
228         List<NodeId> nodes = TransactionUtils.getNodeIds();
229         for (NodeId nodeId : nodes) {
230             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.dataBroker, "deg");
231             Thread.sleep(500);
232         }
233         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
234         boolean output = this.powerMgmt.setPower(input);
235         Assert.assertEquals(true, output);
236     }
237
238     @Test
239     public void testSetPowerPresentNodes32() throws InterruptedException {
240         List<NodeId> nodes = TransactionUtils.getNodeIds();
241         for (NodeId nodeId : nodes) {
242             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.dataBroker, null);
243             Thread.sleep(500);
244         }
245         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
246         boolean output = this.powerMgmt.setPower(input);
247         Assert.assertEquals(true, output);
248     }
249
250     @Ignore
251     @Test
252     public void testSetPowerPresentNodes4() throws InterruptedException {
253         List<NodeId> nodes = TransactionUtils.getNodeIds();
254         for (NodeId nodeId : nodes) {
255             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, "network");
256             Thread.sleep(500);
257         }
258         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
259         boolean output = this.powerMgmt.setPower(input);
260         Assert.assertEquals(true, output);
261     }
262
263     /*
264     TODO: see if this test is always relevant
265     @Test
266     public void testSetPowerPresentNodes41() throws InterruptedException {
267         List<NodeId> nodes = TransactionUtils.getNodeIds();
268         for (NodeId nodeId : nodes) {
269             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), null);
270             Thread.sleep(500);
271         }
272         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
273         boolean output = this.powerMgmt.setPower(input);
274         Assert.assertEquals(false, output);
275     }*/
276
277     @Test
278     public void testSetPowerPresentNodes42() throws InterruptedException {
279         List<NodeId> nodes = TransactionUtils.getNodeIds();
280         for (NodeId nodeId : nodes) {
281             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, "deg");
282             Thread.sleep(500);
283         }
284         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
285         boolean output = this.powerMgmt.setPower(input);
286         Assert.assertEquals(true, output);
287     }
288
289     @Test
290     public void testSetPowerPresentNodes422() throws InterruptedException {
291         List<NodeId> nodes = TransactionUtils.getNodeIds();
292         for (NodeId nodeId : nodes) {
293             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, "deg");
294             Thread.sleep(500);
295         }
296         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
297         boolean output = this.powerMgmt.setPower(input);
298         Assert.assertEquals(true, output);
299     }
300
301     @Test
302     public void testSetPowerPresentNodes43() throws InterruptedException {
303         List<NodeId> nodes = TransactionUtils.getNodeIds();
304         for (NodeId nodeId : nodes) {
305             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, null);
306             Thread.sleep(500);
307         }
308         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
309         boolean output = this.powerMgmt.setPower(input);
310         Assert.assertEquals(true, output);
311     }
312
313 }