83ff2ab76839d3eb5c02103310e60accab3a1efc
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / service / ServiceDataStoreOperationsImplTest.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 package org.opendaylight.transportpce.servicehandler.service;
9
10 import static org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperationsImpl.LogMessages;
11
12 import java.util.Optional;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Ignore;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.transportpce.common.OperationResult;
19 import org.opendaylight.transportpce.common.ResponseCodes;
20 import org.opendaylight.transportpce.servicehandler.ServiceInput;
21 import org.opendaylight.transportpce.servicehandler.utils.ServiceDataUtils;
22 import org.opendaylight.transportpce.test.AbstractTest;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutput;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommon;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev181130.AdminStates;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceCreateInput;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.TempServiceCreateInput;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.AToZDirectionBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev200629.path.description.ZToADirectionBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParameters;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParametersBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.response.parameters.PathDescriptionBuilder;
37
38 @Ignore
39 //writeOrModifyOrDeleteServiceList deprecated method should not raise warnings in tests
40 @SuppressWarnings("deprecation")
41 public class ServiceDataStoreOperationsImplTest extends AbstractTest {
42
43     private ServiceDataStoreOperationsImpl serviceDataStoreOperations;
44
45     @Before
46     public void init() {
47         DataBroker dataBroker = this.getNewDataBroker();
48         this.serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(dataBroker);
49     }
50
51     @Test
52     public void modifyIfServiceNotPresent() {
53         OperationResult result =
54                 this.serviceDataStoreOperations.modifyService("service 1", State.InService, AdminStates.InService);
55         Assert.assertFalse(result.isSuccess());
56         Assert.assertEquals(LogMessages.SERVICE_NOT_FOUND, result.getResultMessage());
57     }
58
59     @Test
60     public void writeOrModifyOrDeleteServiceListNotPresentWithNoWriteChoice() {
61
62         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
63         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
64                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
65                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
66         PathComputationRequestOutput pathComputationRequestOutput = new PathComputationRequestOutputBuilder()
67                 .setConfigurationResponseCommon(configurationResponseCommon).build();
68         String result = serviceDataStoreOperations.writeOrModifyOrDeleteServiceList("serviceCreateInput",
69             createInput, pathComputationRequestOutput, 3);
70
71         Assert.assertEquals(LogMessages.SERVICE_NOT_FOUND, result);
72     }
73
74     @Test
75     public void writeOrModifyOrDeleteServiceListNotPresentWithWriteChoice() {
76
77         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
78         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
79                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
80                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
81         PathComputationRequestOutput pathComputationRequestOutput = new PathComputationRequestOutputBuilder()
82                 .setConfigurationResponseCommon(configurationResponseCommon).build();
83         String result = serviceDataStoreOperations.writeOrModifyOrDeleteServiceList("service 1",
84             createInput, pathComputationRequestOutput, 2);
85
86         Assert.assertNull(result);
87     }
88
89     @Test
90     public void writeOrModifyOrDeleteServiceListPresentWithModifyChoice() {
91         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
92         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
93                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
94                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
95         PathComputationRequestOutput pathComputationRequestOutput = new PathComputationRequestOutputBuilder()
96                 .setConfigurationResponseCommon(configurationResponseCommon).build();
97         OperationResult createOutput = this.serviceDataStoreOperations.createService(createInput);
98         String result = serviceDataStoreOperations.writeOrModifyOrDeleteServiceList("service 1",
99             createInput, pathComputationRequestOutput, 0);
100         Assert.assertNull(result);
101     }
102
103     @Test
104     public void writeOrModifyOrDeleteServiceListPresentWithDeleteChoice() {
105         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
106
107         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
108                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
109                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
110         PathComputationRequestOutput pathComputationRequestOutput = new PathComputationRequestOutputBuilder()
111                 .setConfigurationResponseCommon(configurationResponseCommon).build();
112         OperationResult createOutput = this.serviceDataStoreOperations.createService(createInput);
113         String result = serviceDataStoreOperations.writeOrModifyOrDeleteServiceList("service 1",
114             createInput, pathComputationRequestOutput, 1);
115         Assert.assertNull(result);
116     }
117
118     @Test
119     public void writeOrModifyOrDeleteServiceListPresentWithNoValidChoice() {
120         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
121         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
122                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
123                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
124         PathComputationRequestOutput pathComputationRequestOutput = new PathComputationRequestOutputBuilder()
125                 .setConfigurationResponseCommon(configurationResponseCommon).build();
126         OperationResult createOutput = this.serviceDataStoreOperations.createService(createInput);
127         String result = serviceDataStoreOperations.writeOrModifyOrDeleteServiceList("service 1",
128             createInput, pathComputationRequestOutput, 2);
129         Assert.assertNull(result);
130
131     }
132
133     @Test
134     public void getServiceFromEmptyDataStoreShouldBeEmpty() {
135         Optional<Services> optService = this.serviceDataStoreOperations.getService("service 1");
136         Assert.assertFalse(optService.isPresent());
137     }
138
139     @Test
140     public void createServiceShouldBeSuccessForValidInput() {
141         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
142         OperationResult result = this.serviceDataStoreOperations.createService(createInput);
143         Assert.assertTrue(result.isSuccess());
144     }
145
146     @Test
147     public void getServiceShouldReturnTheCorrectServiceForTheCreatedService() {
148         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
149         this.serviceDataStoreOperations.createService(createInput);
150
151         Optional<Services> optService = this.serviceDataStoreOperations.getService(createInput.getServiceName());
152         Assert.assertTrue(optService.isPresent());
153         Assert.assertEquals(createInput.getServiceName(), optService.get().getServiceName());
154     }
155
156     @Test
157     public void deleteServiceShouldBeSuccessfulForDeletingService() {
158         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
159         this.serviceDataStoreOperations.createService(createInput);
160         OperationResult result = this.serviceDataStoreOperations.deleteService(createInput.getServiceName());
161         Assert.assertTrue(result.isSuccess());
162     }
163
164 //    @Test
165 //    public void deleteServiceShouldBeFailedIfServiceDoNotExists() {
166 //        OperationResult result = this.serviceDataStoreOperations.deleteService("Any service");
167 //        Assert.assertFalse(result.isSuccess());
168 //    }
169
170     @Test
171     public void modifyServiceIsSuccessfulForPresentService() {
172         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
173         this.serviceDataStoreOperations.createService(createInput);
174         OperationResult result = this.serviceDataStoreOperations.modifyService(createInput.getServiceName(),
175             State.InService, AdminStates.InService);
176         Assert.assertTrue(result.isSuccess());
177     }
178
179     @Test
180     public void getTempServiceFromEmptyDataStoreShouldBeEmpty() {
181         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
182                 .Services> optService = this.serviceDataStoreOperations.getTempService("service 1");
183         Assert.assertFalse(optService.isPresent());
184     }
185
186     @Test
187     public void createTempServiceShouldBeSuccessForValidInput() {
188         TempServiceCreateInput createInput = ServiceDataUtils.buildTempServiceCreateInput();
189         OperationResult result = this.serviceDataStoreOperations.createTempService(createInput);
190         Assert.assertTrue(result.isSuccess());
191     }
192
193     @Test
194     public void getTempServiceShouldReturnTheCorrectTempServiceForTheCreatedService() {
195         TempServiceCreateInput createInput = ServiceDataUtils.buildTempServiceCreateInput();
196         this.serviceDataStoreOperations.createTempService(createInput);
197
198         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
199                 .Services> optService = this.serviceDataStoreOperations.getTempService(createInput.getCommonId());
200         Assert.assertTrue(optService.isPresent());
201         Assert.assertEquals(createInput.getCommonId(), optService.get().getCommonId());
202     }
203
204     @Test
205     public void deleteTempServiceShouldBeSuccessfulForDeletingTempService() {
206         TempServiceCreateInput createInput = ServiceDataUtils.buildTempServiceCreateInput();
207         this.serviceDataStoreOperations.createTempService(createInput);
208         OperationResult result = this.serviceDataStoreOperations.deleteTempService(createInput.getCommonId());
209         Assert.assertTrue(result.isSuccess());
210     }
211
212     @Test
213     public void modifyTempServiceIsSuccessfulForPresentTempService() {
214         TempServiceCreateInput createInput = ServiceDataUtils.buildTempServiceCreateInput();
215         this.serviceDataStoreOperations.createTempService(createInput);
216         OperationResult result = this.serviceDataStoreOperations.modifyTempService(
217             createInput.getCommonId(), State.InService, AdminStates.InService);
218         Assert.assertTrue(result.isSuccess());
219     }
220
221     @Test
222     public void createServicePathShouldBeSuccessfulForValidInput() {
223         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
224         this.serviceDataStoreOperations.createService(createInput);
225         ServiceInput serviceInput = new ServiceInput(createInput);
226         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
227                 .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
228                 .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
229         ResponseParameters responseParameters = new ResponseParametersBuilder()
230             .setPathDescription(new PathDescriptionBuilder()
231                 .setAToZDirection(new AToZDirectionBuilder().setAToZWavelengthNumber(1L).setRate(1L).build())
232                 .setZToADirection(new ZToADirectionBuilder().setZToAWavelengthNumber(1L).setRate(1L).build()).build())
233             .build();
234         PathComputationRequestOutput pathComputationRequestOutput = new PathComputationRequestOutputBuilder()
235             .setConfigurationResponseCommon(configurationResponseCommon).setResponseParameters(responseParameters)
236             .build();
237         OperationResult result =
238             this.serviceDataStoreOperations.createServicePath(serviceInput, pathComputationRequestOutput);
239         Assert.assertTrue(result.isSuccess());
240     }
241
242     @Test
243     public void createServicePathShouldFailForInvalidInput() {
244         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
245         this.serviceDataStoreOperations.createService(createInput);
246         ServiceInput serviceInput = new ServiceInput(createInput);
247         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
248             .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
249             .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
250         ResponseParameters responseParameters = new ResponseParametersBuilder().build();
251         PathComputationRequestOutput pathComputationRequestOutput = new PathComputationRequestOutputBuilder()
252             .setConfigurationResponseCommon(configurationResponseCommon).setResponseParameters(responseParameters)
253             .build();
254         OperationResult result =
255             this.serviceDataStoreOperations.createServicePath(serviceInput, pathComputationRequestOutput);
256         Assert.assertFalse(result.isSuccess());
257     }
258
259     @Test
260     public void deleteServicePathShouldBeSuccessForDeletingServicePath() {
261         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
262         this.serviceDataStoreOperations.createService(createInput);
263         ServiceInput serviceInput = new ServiceInput(createInput);
264         ConfigurationResponseCommon configurationResponseCommon = new ConfigurationResponseCommonBuilder()
265             .setRequestId("request 1").setAckFinalIndicator(ResponseCodes.FINAL_ACK_NO)
266             .setResponseCode(ResponseCodes.RESPONSE_OK).setResponseMessage("PCE calculation in progress").build();
267         ResponseParameters responseParameters = new ResponseParametersBuilder()
268             .setPathDescription(new PathDescriptionBuilder()
269                 .setAToZDirection(new AToZDirectionBuilder().setAToZWavelengthNumber(1L).setRate(1L).build())
270                 .setZToADirection(new ZToADirectionBuilder().setZToAWavelengthNumber(1L).setRate(1L).build()).build())
271             .build();
272         PathComputationRequestOutput pathComputationRequestOutput = new PathComputationRequestOutputBuilder()
273             .setConfigurationResponseCommon(configurationResponseCommon).setResponseParameters(responseParameters)
274             .build();
275         this.serviceDataStoreOperations.createServicePath(serviceInput, pathComputationRequestOutput);
276
277         OperationResult result = this.serviceDataStoreOperations.deleteServicePath(serviceInput.getServiceName());
278         Assert.assertTrue(result.isSuccess());
279     }
280 }