Sync Common folder
[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
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mockito;
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.StringConstants;
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.stub.MountPointServiceStub;
37 import org.opendaylight.transportpce.olm.stub.MountPointStub;
38 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
39 import org.opendaylight.transportpce.olm.util.TransactionUtils;
40 import org.opendaylight.transportpce.test.AbstractTest;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev150608.NodeId;
43
44 public class PowerMgmtTest extends AbstractTest {
45
46     private MountPoint mountPoint;
47     private MountPointService mountPointService;
48     private DeviceTransactionManager deviceTransactionManager;
49     private CrossConnect crossConnect;
50     private OpenRoadmInterfaces openRoadmInterfaces;
51     private PortMapping portMapping;
52     private PowerMgmt powerMgmt;
53     private MappingUtils mappingUtils;
54     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
55     private OpenRoadmInterfacesImpl22 openRoadmInterfacesImpl22;
56     private PortMappingVersion22 portMappingVersion22;
57     private PortMappingVersion121 portMappingVersion121;
58     private CrossConnectImpl121 crossConnectImpl121;
59     private CrossConnectImpl22 crossConnectImpl22;
60
61     @Before
62     public void setUp() {
63         this.mountPoint = new MountPointStub(this.getDataBroker());
64         this.mountPointService = new MountPointServiceStub(mountPoint);
65         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
66         this.mappingUtils = new MappingUtilsImpl(getDataBroker());
67         this.mappingUtils = Mockito.spy(this.mappingUtils);
68         this.crossConnectImpl121 = new CrossConnectImpl121(this.deviceTransactionManager);
69         this.crossConnectImpl22 = new CrossConnectImpl22(this.deviceTransactionManager);
70         this.crossConnect = new CrossConnectImpl(this.deviceTransactionManager, this.mappingUtils,
71                 this.crossConnectImpl121, this.crossConnectImpl22);
72         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(this.deviceTransactionManager);
73         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl22(this.deviceTransactionManager);
74         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl(deviceTransactionManager, mappingUtils,
75                 openRoadmInterfacesImpl121, openRoadmInterfacesImpl22);
76         this.portMappingVersion22 =
77                 new PortMappingVersion22(getDataBroker(), this.deviceTransactionManager, this.openRoadmInterfaces);
78         this.portMappingVersion121 =
79                 new PortMappingVersion121(getDataBroker(), this.deviceTransactionManager, this.openRoadmInterfaces);
80         this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion22, this.mappingUtils,
81                 this.portMappingVersion121);
82         this.powerMgmt = new PowerMgmt(this.getDataBroker(), this.openRoadmInterfaces, this.crossConnect,
83             this.deviceTransactionManager);
84     }
85
86     @Test
87     public void testSetPower() {
88
89         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
90         boolean output = this.powerMgmt.setPower(input);
91         Assert.assertEquals(true, output);
92
93     }
94
95     @Test
96     public void testSetPower2() {
97
98         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
99         boolean output = this.powerMgmt.setPower(input);
100         Assert.assertEquals(true, output);
101
102     }
103
104     @Test
105     public void testSetPower3() {
106
107         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
108         boolean output = this.powerMgmt.setPower(input);
109         Assert.assertEquals(true, output);
110
111     }
112
113     @Test
114     public void testSetPower4() {
115
116         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
117         boolean output = this.powerMgmt.setPower(input);
118         Assert.assertEquals(true, output);
119
120     }
121
122     @Test
123     public void testSetPowerPresentNodes() throws InterruptedException {
124         List<NodeId> nodes = TransactionUtils.getNodeIds();
125         for (NodeId nodeId : nodes) {
126             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), null);
127             Thread.sleep(500);
128         }
129         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
130         boolean output = this.powerMgmt.setPower(input);
131         Assert.assertEquals(true, output);
132     }
133
134     @Test
135     public void testSetPowerPresentNodes2() throws InterruptedException {
136         List<NodeId> nodes = TransactionUtils.getNodeIds();
137         for (NodeId nodeId : nodes) {
138             TransactionUtils.writeNodeTransaction2(nodeId.getValue(), this.getDataBroker(), null);
139             Thread.sleep(500);
140         }
141         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
142         boolean output = this.powerMgmt.setPower(input);
143         Assert.assertEquals(true, output);
144     }
145
146     @Test
147     public void testSetPowerPresentNodes3() throws InterruptedException {
148         List<NodeId> nodes = TransactionUtils.getNodeIds();
149         for (NodeId nodeId : nodes) {
150             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), null);
151             Thread.sleep(500);
152         }
153         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
154         boolean output = this.powerMgmt.setPower(input);
155         Assert.assertEquals(true, output);
156     }
157
158     @Test
159     public void testSetPowerPresentNodes31() throws InterruptedException {
160         List<NodeId> nodes = TransactionUtils.getNodeIds();
161         for (NodeId nodeId : nodes) {
162             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), "deg");
163             Thread.sleep(500);
164         }
165         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
166         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(this.mappingUtils)
167                 .getOpenRoadmVersion("node 1");
168         boolean output = this.powerMgmt.setPower(input);
169         Assert.assertEquals(false, output);
170     }
171
172     @Test
173     public void testSetPowerPresentNodes312() throws InterruptedException {
174         List<NodeId> nodes = TransactionUtils.getNodeIds();
175         for (NodeId nodeId : nodes) {
176             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), "deg");
177             Thread.sleep(500);
178         }
179         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
180         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(this.mappingUtils)
181                 .getOpenRoadmVersion("node 1");
182         boolean output = this.powerMgmt.setPower(input);
183         Assert.assertEquals(true, output);
184     }
185
186     @Test
187     public void testSetPowerPresentNodes32() throws InterruptedException {
188         List<NodeId> nodes = TransactionUtils.getNodeIds();
189         for (NodeId nodeId : nodes) {
190             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), null);
191             Thread.sleep(500);
192         }
193         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
194         boolean output = this.powerMgmt.setPower(input);
195         Assert.assertEquals(true, output);
196     }
197
198     @Test
199     public void testSetPowerPresentNodes4() throws InterruptedException {
200         List<NodeId> nodes = TransactionUtils.getNodeIds();
201         for (NodeId nodeId : nodes) {
202             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), "network");
203             Thread.sleep(500);
204         }
205         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
206         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(this.mappingUtils)
207                 .getOpenRoadmVersion("node 1");
208         boolean output = this.powerMgmt.setPower(input);
209         Assert.assertEquals(true, output);
210     }
211
212     @Test
213     public void testSetPowerPresentNodes41() throws InterruptedException {
214         List<NodeId> nodes = TransactionUtils.getNodeIds();
215         for (NodeId nodeId : nodes) {
216             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), null);
217             Thread.sleep(500);
218         }
219         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
220         boolean output = this.powerMgmt.setPower(input);
221         Assert.assertEquals(false, output);
222     }
223
224     @Test
225     public void testSetPowerPresentNodes42() throws InterruptedException {
226         List<NodeId> nodes = TransactionUtils.getNodeIds();
227         for (NodeId nodeId : nodes) {
228             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), "deg");
229             Thread.sleep(500);
230         }
231         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
232         boolean output = this.powerMgmt.setPower(input);
233         Assert.assertEquals(true, output);
234     }
235
236     @Test
237     public void testSetPowerPresentNodes422() throws InterruptedException {
238         List<NodeId> nodes = TransactionUtils.getNodeIds();
239         for (NodeId nodeId : nodes) {
240             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), "deg");
241             Thread.sleep(500);
242         }
243         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
244         boolean output = this.powerMgmt.setPower(input);
245         Assert.assertEquals(true, output);
246     }
247
248     @Test
249     public void testSetPowerPresentNodes43() throws InterruptedException {
250         List<NodeId> nodes = TransactionUtils.getNodeIds();
251         for (NodeId nodeId : nodes) {
252             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), null);
253             Thread.sleep(500);
254         }
255         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
256         boolean output = this.powerMgmt.setPower(input);
257         Assert.assertEquals(true, output);
258     }
259
260 }