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