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