SH RPC temp-service-create/delete
[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.rev171017.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).build();
167                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
168                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
169                 return OperationResult.ok(SUCCESSFUL_MESSAGE);
170             } catch (TimeoutException | InterruptedException | ExecutionException e) {
171                 String message = "Failed to modify service " + serviceName + " from Service List";
172                 LOG.warn(message, e);
173                 return OperationResult.failed(message);
174             }
175         } else {
176             String message = "Service " + serviceName + " is not present!";
177             LOG.warn(message);
178             return OperationResult.failed(message);
179         }
180     }
181
182     @Override
183     public OperationResult modifyTempService(String serviceName, State operationalState, State administrativeState) {
184         LOG.debug("Modifying '{}' Temp Service", serviceName);
185         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
186             .Services> readService = getTempService(serviceName);
187         if (readService.isPresent()) {
188             try {
189                 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
190                 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
191                     .Services> iid = InstanceIdentifier.create(TempServiceList.class)
192                         .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
193                                 .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014
194                                     .temp.service.list.ServicesKey(serviceName));
195                 org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
196                     .Services services = new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp
197                         .service.list.ServicesBuilder(readService.get()).setOperationalState(operationalState)
198                             .setAdministrativeState(administrativeState)
199                             .build();
200                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
201                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
202                 return OperationResult.ok(SUCCESSFUL_MESSAGE);
203             } catch (TimeoutException | InterruptedException | ExecutionException e) {
204                 String message = "Failed to modify temp service " + serviceName + " from Temp Service List";
205                 LOG.warn(message, e);
206                 return OperationResult.failed(message);
207             }
208         } else {
209             String message = "Temp Service " + serviceName + " is not present!";
210             LOG.warn(message);
211             return OperationResult.failed(message);
212         }
213     }
214
215     @Override
216     public OperationResult createService(ServiceCreateInput serviceCreateInput,
217             PathComputationRequestOutput outputFromPce) {
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             PathComputationRequestOutput outputFromPce) {
237         LOG.debug("Writing '{}' Temp Service", tempServiceCreateInput.getCommonId());
238         try {
239             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
240                 .Services> iid = InstanceIdentifier.create(TempServiceList.class)
241                     .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
242                             .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp
243                                 .service.list.ServicesKey(tempServiceCreateInput.getCommonId()));
244             org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
245                 .Services service = ModelMappingUtils.mappingServices(tempServiceCreateInput);
246             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
247             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
248             writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
249             return OperationResult.ok(SUCCESSFUL_MESSAGE);
250         } catch (TimeoutException | InterruptedException | ExecutionException e) {
251             String message = "Failed to create Temp service " + tempServiceCreateInput.getCommonId()
252                 + " to TempService List";
253             LOG.warn(message, e);
254             return OperationResult.failed(message);
255         }
256     }
257
258     @Override
259     public OperationResult createServicePath(ServiceInput serviceInput, PathComputationRequestOutput outputFromPce) {
260         LOG.debug("Writing '{}' ServicePath ", serviceInput.getServiceName());
261         try {
262             InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
263                     .child(ServicePaths.class, new ServicePathsKey(serviceInput.getServiceName()));
264             ServicePaths servicePath = ModelMappingUtils.mappingServicePaths(serviceInput, outputFromPce);
265             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
266             writeTx.put(LogicalDatastoreType.OPERATIONAL, servicePathsIID, servicePath);
267             writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
268             return OperationResult.ok(SUCCESSFUL_MESSAGE);
269         } catch (TimeoutException | InterruptedException | ExecutionException e) {
270             String message = "Failed to create servicePath " + serviceInput.getCommonId() + " to ServicePath List";
271             LOG.warn(message, e);
272             return OperationResult.failed(message);
273         }
274     }
275
276     @Override
277     public OperationResult deleteServicePath(String serviceName) {
278         InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
279                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
280         LOG.debug("Deleting service from {}", servicePathsIID);
281         WriteTransaction servicePathsWriteTx = this.dataBroker.newWriteOnlyTransaction();
282         servicePathsWriteTx.delete(LogicalDatastoreType.OPERATIONAL, servicePathsIID);
283         try {
284             servicePathsWriteTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
285             return OperationResult.ok(SUCCESSFUL_MESSAGE);
286         } catch (InterruptedException | ExecutionException | TimeoutException e) {
287             String message = "Unable to delete service path " + serviceName;
288             LOG.error(message, e);
289             return OperationResult.failed(message);
290         }
291     }
292
293     /*
294      * Write or Modify or Delete Service from/to SreviceList.
295      *
296      * @param serviceName Name of service
297      *
298      * @param input ServiceCreateInput
299      *
300      * @param output PathComputationRequestOutput
301      *
302      * @param choice 0 - Modify 1 - Delete 2 - Write
303      *
304      * @return String operations result, null if ok or not otherwise
305      */
306     @Deprecated
307     @Override
308     public String writeOrModifyOrDeleteServiceList(String serviceName, ServiceCreateInput input,
309             PathComputationRequestOutput output, int choice) {
310         LOG.debug("WriteOrModifyOrDeleting '{}' Service", serviceName);
311         WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
312         String result = null;
313         Optional<Services> readService = getService(serviceName);
314         if (readService.isPresent()) {
315             /*
316              * Modify / Delete Service.
317              */
318             InstanceIdentifier<Services> iid =
319                     InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
320             ServicesBuilder service = new ServicesBuilder(readService.get());
321             String action = null;
322             switch (choice) {
323                 case 0 : /* Modify. */
324                     LOG.debug("Modifying '{}' Service", serviceName);
325                     service.setOperationalState(State.InService).setAdministrativeState(State.InService);
326                     writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, service.build());
327                     action = "modifyService";
328                     break;
329                 case 1 : /* Delete */
330                     LOG.debug("Deleting '{}' Service", serviceName);
331                     writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
332                     action = "deleteService";
333                     break;
334                 default:
335                     LOG.debug("No choice found");
336                     break;
337             }
338             try {
339                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
340             } catch (InterruptedException | ExecutionException | TimeoutException e) {
341                 LOG.error("Failed to {} service from Service List", action, e);
342                 result = "Failed to " + action + " service from Service List";
343             }
344         } else {
345             if (choice == 2) { /* Write Service */
346                 LOG.debug("Writing '{}' Service", serviceName);
347                 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
348                         .child(Services.class, new ServicesKey(serviceName));
349                 Services service = ModelMappingUtils.mappingServices(input, null);
350                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
351                 try {
352                     writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
353                     result = null;
354                 } catch (InterruptedException | TimeoutException | ExecutionException e) {
355                     LOG.error("Failed to createService service to Service List", e);
356                     result = "Failed to createService service to Service List";
357                 }
358             } else {
359                 LOG.info("Service is not present ! ");
360                 result = "Service is not present ! ";
361             }
362         }
363         return result;
364     }
365 }