add gnpy results to pce:path-computation-request
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / service / ServiceDataStoreOperationsImpl.java
1 /*
2  * Copyright © 2017 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 java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.transportpce.common.OperationResult;
21 import org.opendaylight.transportpce.common.Timeouts;
22 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
23 import org.opendaylight.transportpce.servicehandler.ServiceInput;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev190624.PathComputationRequestOutput;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.State;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceList;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceListBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateInput;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceList;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceListBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesKey;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
43     private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
44     private static final String SUCCESSFUL_MESSAGE = "Successful";
45     private DataBroker dataBroker;
46
47     public ServiceDataStoreOperationsImpl(DataBroker dataBroker) {
48         this.dataBroker = dataBroker;
49     }
50
51     @Override
52     public void initialize() {
53         initializeServiceList();
54         initializeTempServiceList();
55     }
56
57     private void initializeServiceList() {
58         try {
59             LOG.info("initializing service registry");
60             WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
61             InstanceIdentifier<ServiceList> iid = InstanceIdentifier.create(ServiceList.class);
62             ServiceList initialRegistry = new ServiceListBuilder().build();
63             transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
64             Future<Void> future = transaction.submit();
65             future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
66         } catch (InterruptedException | ExecutionException | TimeoutException e) {
67             LOG.warn("init failed: {}", e.getMessage());
68         }
69     }
70
71     private void initializeTempServiceList() {
72         try {
73             LOG.info("initializing temp service registry");
74             WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
75             InstanceIdentifier<TempServiceList> iid = InstanceIdentifier.create(TempServiceList.class);
76             TempServiceList initialRegistry = new TempServiceListBuilder().build();
77             transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
78             Future<Void> future = transaction.submit();
79             future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
80         } catch (InterruptedException | ExecutionException | TimeoutException e) {
81             LOG.warn("init failed: {}", e.getMessage());
82         }
83     }
84
85     @Override
86     public Optional<Services> getService(String serviceName) {
87         try {
88             ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction();
89             InstanceIdentifier<Services> iid =
90                     InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
91             Future<com.google.common.base.Optional<Services>> future =
92                     readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
93             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).toJavaUtil();
94         } catch (InterruptedException | ExecutionException | TimeoutException e) {
95             LOG.warn("Reading service {} failed:", serviceName, e);
96         }
97         return Optional.empty();
98     }
99
100     @Override
101     public Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
102         .Services> getTempService(String serviceName) {
103         try {
104             ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction();
105             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
106                 .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
107                         .http.org.openroadm.service.rev161014.temp.service.list.Services.class,
108                         new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
109                             .ServicesKey(serviceName));
110             Future<com.google.common.base.Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014
111                 .temp.service.list.Services>> future =  readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
112             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).toJavaUtil();
113         } catch (InterruptedException | ExecutionException | TimeoutException e) {
114             LOG.warn("Reading service {} failed:", serviceName, e);
115         }
116         return Optional.empty();
117     }
118
119     @Override
120     public OperationResult deleteService(String serviceName) {
121         LOG.debug("Deleting '{}' Service", serviceName);
122         try {
123             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
124             InstanceIdentifier<Services> iid =
125                     InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
126             writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
127             writeTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
128             return OperationResult.ok(SUCCESSFUL_MESSAGE);
129         } catch (TimeoutException | InterruptedException | ExecutionException e) {
130             String message = "Failed to delete service " + serviceName + " from Service List";
131             LOG.warn(message, e);
132             return OperationResult.failed(message);
133         }
134     }
135
136     @Override
137     public OperationResult deleteTempService(String commonId) {
138         LOG.debug("Deleting '{}' Service", commonId);
139         try {
140             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
141             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
142                 .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
143                         .http.org.openroadm.service.rev161014.temp.service.list.Services.class,
144                         new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
145                             .ServicesKey(commonId));
146             writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
147             writeTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
148             return OperationResult.ok(SUCCESSFUL_MESSAGE);
149         } catch (TimeoutException | InterruptedException | ExecutionException e) {
150             String message = "Failed to delete service " + commonId + " from Service List";
151             LOG.warn(message, e);
152             return OperationResult.failed(message);
153         }
154     }
155
156     @Override
157     public OperationResult modifyService(String serviceName, State operationalState, State administrativeState) {
158         LOG.debug("Modifying '{}' Service", serviceName);
159         Optional<Services> readService = getService(serviceName);
160         if (readService.isPresent()) {
161             try {
162                 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
163                 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
164                         .child(Services.class, new ServicesKey(serviceName));
165                 Services services = new ServicesBuilder(readService.get()).setOperationalState(operationalState)
166                         .setAdministrativeState(administrativeState)
167                         .build();
168                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
169                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
170                 return OperationResult.ok(SUCCESSFUL_MESSAGE);
171             } catch (TimeoutException | InterruptedException | ExecutionException e) {
172                 String message = "Failed to modify service " + serviceName + " from Service List";
173                 LOG.warn(message, e);
174                 return OperationResult.failed(message);
175             }
176         } else {
177             String message = "Service " + serviceName + " is not present!";
178             LOG.warn(message);
179             return OperationResult.failed(message);
180         }
181     }
182
183     @Override
184     public OperationResult modifyTempService(String serviceName, State operationalState, State administrativeState) {
185         LOG.debug("Modifying '{}' Temp Service", serviceName);
186         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
187             .Services> readService = getTempService(serviceName);
188         if (readService.isPresent()) {
189             try {
190                 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
191                 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
192                     .Services> iid = InstanceIdentifier.create(TempServiceList.class)
193                         .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
194                                 .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014
195                                     .temp.service.list.ServicesKey(serviceName));
196                 org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
197                     .Services services = new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp
198                         .service.list.ServicesBuilder(readService.get()).setOperationalState(operationalState)
199                             .setAdministrativeState(administrativeState)
200                             .build();
201                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
202                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
203                 return OperationResult.ok(SUCCESSFUL_MESSAGE);
204             } catch (TimeoutException | InterruptedException | ExecutionException e) {
205                 String message = "Failed to modify temp service " + serviceName + " from Temp Service List";
206                 LOG.warn(message, e);
207                 return OperationResult.failed(message);
208             }
209         } else {
210             String message = "Temp Service " + serviceName + " is not present!";
211             LOG.warn(message);
212             return OperationResult.failed(message);
213         }
214     }
215
216     @Override
217     public OperationResult createService(ServiceCreateInput serviceCreateInput) {
218         LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
219         try {
220             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
221                     .child(Services.class, new ServicesKey(serviceCreateInput.getServiceName()));
222             Services service = ModelMappingUtils.mappingServices(serviceCreateInput, null);
223             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
224             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
225             writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
226             return OperationResult.ok(SUCCESSFUL_MESSAGE);
227         } catch (TimeoutException | InterruptedException | ExecutionException e) {
228             String message = "Failed to create service " + serviceCreateInput.getServiceName() + " to Service List";
229             LOG.warn(message, e);
230             return OperationResult.failed(message);
231         }
232     }
233
234     @Override
235     public OperationResult createTempService(TempServiceCreateInput tempServiceCreateInput) {
236         LOG.debug("Writing '{}' Temp Service", tempServiceCreateInput.getCommonId());
237         try {
238             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
239                 .Services> iid = InstanceIdentifier.create(TempServiceList.class)
240                     .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
241                             .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp
242                                 .service.list.ServicesKey(tempServiceCreateInput.getCommonId()));
243             org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
244                 .Services service = ModelMappingUtils.mappingServices(tempServiceCreateInput);
245             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
246             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
247             writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
248             return OperationResult.ok(SUCCESSFUL_MESSAGE);
249         } catch (TimeoutException | InterruptedException | ExecutionException e) {
250             String message = "Failed to create Temp service " + tempServiceCreateInput.getCommonId()
251                 + " to TempService List";
252             LOG.warn(message, e);
253             return OperationResult.failed(message);
254         }
255     }
256
257     @Override
258     public OperationResult createServicePath(ServiceInput serviceInput, PathComputationRequestOutput outputFromPce) {
259         LOG.debug("Writing '{}' ServicePath ", serviceInput.getServiceName());
260         try {
261             InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
262                     .child(ServicePaths.class, new ServicePathsKey(serviceInput.getServiceName()));
263             ServicePaths servicePath = ModelMappingUtils.mappingServicePaths(serviceInput, outputFromPce);
264             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
265             writeTx.put(LogicalDatastoreType.OPERATIONAL, servicePathsIID, servicePath);
266             writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
267             return OperationResult.ok(SUCCESSFUL_MESSAGE);
268         } catch (TimeoutException | InterruptedException | ExecutionException e) {
269             String message = "Failed to create servicePath " + serviceInput.getCommonId() + " to ServicePath List";
270             LOG.warn(message, e);
271             return OperationResult.failed(message);
272         }
273     }
274
275     @Override
276     public OperationResult deleteServicePath(String serviceName) {
277         InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
278                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
279         LOG.debug("Deleting service from {}", servicePathsIID);
280         WriteTransaction servicePathsWriteTx = this.dataBroker.newWriteOnlyTransaction();
281         servicePathsWriteTx.delete(LogicalDatastoreType.OPERATIONAL, servicePathsIID);
282         try {
283             servicePathsWriteTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
284             return OperationResult.ok(SUCCESSFUL_MESSAGE);
285         } catch (InterruptedException | ExecutionException | TimeoutException e) {
286             String message = "Unable to delete service path " + serviceName;
287             LOG.error(message, e);
288             return OperationResult.failed(message);
289         }
290     }
291
292     /*
293      * Write or Modify or Delete Service from/to SreviceList.
294      *
295      * @param serviceName Name of service
296      *
297      * @param input ServiceCreateInput
298      *
299      * @param output PathComputationRequestOutput
300      *
301      * @param choice 0 - Modify 1 - Delete 2 - Write
302      *
303      * @return String operations result, null if ok or not otherwise
304      */
305     @Deprecated
306     @Override
307     public String writeOrModifyOrDeleteServiceList(String serviceName, ServiceCreateInput input,
308             PathComputationRequestOutput output, int choice) {
309         LOG.debug("WriteOrModifyOrDeleting '{}' Service", serviceName);
310         WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
311         String result = null;
312         Optional<Services> readService = getService(serviceName);
313         if (readService.isPresent()) {
314             /*
315              * Modify / Delete Service.
316              */
317             InstanceIdentifier<Services> iid =
318                     InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
319             ServicesBuilder service = new ServicesBuilder(readService.get());
320             String action = null;
321             switch (choice) {
322                 case 0 : /* Modify. */
323                     LOG.debug("Modifying '{}' Service", serviceName);
324                     service.setOperationalState(State.InService).setAdministrativeState(State.InService);
325                     writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, service.build());
326                     action = "modifyService";
327                     break;
328                 case 1 : /* Delete */
329                     LOG.debug("Deleting '{}' Service", serviceName);
330                     writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
331                     action = "deleteService";
332                     break;
333                 default:
334                     LOG.debug("No choice found");
335                     break;
336             }
337             try {
338                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
339             } catch (InterruptedException | ExecutionException | TimeoutException e) {
340                 LOG.error("Failed to {} service from Service List", action, e);
341                 result = "Failed to " + action + " service from Service List";
342             }
343         } else {
344             if (choice == 2) { /* Write Service */
345                 LOG.debug("Writing '{}' Service", serviceName);
346                 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
347                         .child(Services.class, new ServicesKey(serviceName));
348                 Services service = ModelMappingUtils.mappingServices(input, null);
349                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
350                 try {
351                     writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
352                     result = null;
353                 } catch (InterruptedException | TimeoutException | ExecutionException e) {
354                     LOG.error("Failed to createService service to Service List", e);
355                     result = "Failed to createService service to Service List";
356                 }
357             } else {
358                 LOG.info("Service is not present ! ");
359                 result = "Service is not present ! ";
360             }
361         }
362         return result;
363     }
364 }