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