f3e033599eae5415749fc31a9987d363f6ebd70c
[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.rev220118.PathComputationRequestOutput;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.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.service.types.rev220118.service.path.PathDescription;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsBuilder;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
48     private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
49     private static final String CREATE_MSG = "create";
50     private static final String DELETING_SERVICE_MSG = "Deleting '{}' Service";
51     private DataBroker dataBroker;
52
53     // This is class is public so that these messages can be accessed from Junit (avoid duplications).
54     public static final class LogMessages {
55
56         public static final String SUCCESSFUL_MESSAGE;
57         public static final String SERVICE_NOT_FOUND;
58         public static final String SERVICE_PATH_NOT_FOUND;
59
60         // Static blocks are generated once and spare memory.
61         static {
62             SUCCESSFUL_MESSAGE = "Successful";
63             SERVICE_NOT_FOUND = "Service not found";
64             SERVICE_PATH_NOT_FOUND = "Service path not found";
65         }
66
67         public static String failedTo(String action, String serviceName) {
68             return  "Failed to " + action + " service " + serviceName;
69         }
70
71         private LogMessages() {
72         }
73     }
74
75
76     public ServiceDataStoreOperationsImpl(DataBroker dataBroker) {
77         this.dataBroker = dataBroker;
78     }
79
80     @Override
81     public void initialize() {
82         initializeServiceList();
83         initializeTempServiceList();
84     }
85
86     private void initializeServiceList() {
87         try {
88             LOG.info("initializing service registry");
89             WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
90             InstanceIdentifier<ServiceList> iid = InstanceIdentifier.create(ServiceList.class);
91             ServiceList initialRegistry = new ServiceListBuilder().build();
92             transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
93             FluentFuture<? extends @NonNull CommitInfo> future = transaction.commit();
94             future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
95         } catch (InterruptedException | ExecutionException | TimeoutException e) {
96             LOG.error("init failed: ", e);
97         }
98     }
99
100     private void initializeTempServiceList() {
101         try {
102             LOG.info("initializing temp service registry");
103             WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
104             InstanceIdentifier<TempServiceList> iid = InstanceIdentifier.create(TempServiceList.class);
105             TempServiceList initialRegistry = new TempServiceListBuilder().build();
106             transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
107             FluentFuture<? extends @NonNull CommitInfo> future = transaction.commit();
108             future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
109         } catch (InterruptedException | ExecutionException | TimeoutException e) {
110             LOG.error("init failed: ", e);
111         }
112     }
113
114     @Override
115     public Optional<Services> getService(String serviceName) {
116         try {
117             ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
118             InstanceIdentifier<Services> iid =
119                     InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
120             Future<java.util.Optional<Services>> future =
121                     readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
122             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
123         } catch (InterruptedException | ExecutionException | TimeoutException e) {
124             LOG.warn("Reading service {} failed:", serviceName, e);
125         }
126         return Optional.empty();
127     }
128
129     @Override
130     public Optional<ServiceList> getServices() {
131         try {
132             ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
133             InstanceIdentifier<ServiceList> iid =
134                     InstanceIdentifier.create(ServiceList.class);
135             Future<java.util.Optional<ServiceList>> future =
136                     readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
137             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
138         } catch (InterruptedException | ExecutionException | TimeoutException e) {
139             LOG.warn("Reading services failed:", e);
140         }
141         return Optional.empty();
142     }
143
144     @Override
145     public Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list.Services>
146             getTempService(String serviceName) {
147         try {
148             ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
149             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
150                 .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
151                         .http.org.openroadm.service.rev190531.temp.service.list.Services.class,
152                         new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
153                             .ServicesKey(serviceName));
154             Future<java.util.Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531
155                 .temp.service.list.Services>> future =  readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
156             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
157         } catch (InterruptedException | ExecutionException | TimeoutException e) {
158             LOG.warn("Reading service {} failed:", serviceName, e);
159         }
160         return Optional.empty();
161     }
162
163     @Override
164     public OperationResult deleteService(String serviceName) {
165         LOG.debug(DELETING_SERVICE_MSG, serviceName);
166         try {
167             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
168             InstanceIdentifier<Services> iid =
169                     InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
170             writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
171             writeTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
172             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
173         } catch (TimeoutException | InterruptedException | ExecutionException e) {
174             LOG.warn("deleteService : {}", LogMessages.failedTo("delete", serviceName), e);
175             return OperationResult.failed(LogMessages.failedTo("delete", serviceName));
176         }
177     }
178
179     @Override
180     public OperationResult deleteTempService(String commonId) {
181         LOG.debug(DELETING_SERVICE_MSG, commonId);
182         try {
183             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
184             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
185                 .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
186                         .http.org.openroadm.service.rev190531.temp.service.list.Services.class,
187                         new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
188                             .ServicesKey(commonId));
189             writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
190             writeTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
191             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
192         } catch (TimeoutException | InterruptedException | ExecutionException e) {
193             LOG.warn("deleteTempService : {}", LogMessages.failedTo("delete Temp", commonId), e);
194             return OperationResult.failed(LogMessages.failedTo("delete Temp", commonId));
195         }
196     }
197
198     @Override
199     public OperationResult modifyService(String serviceName, State operationalState, AdminStates administrativeState) {
200         LOG.debug("Modifying '{}' Service", serviceName);
201         Optional<Services> readService = getService(serviceName);
202         if (!readService.isPresent()) {
203             LOG.warn("modifyService: {}", LogMessages.SERVICE_NOT_FOUND);
204             return OperationResult.failed(LogMessages.SERVICE_NOT_FOUND);
205         }
206         try {
207             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
208             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
209                     .child(Services.class, new ServicesKey(serviceName));
210             Services services = new ServicesBuilder(readService.get())
211                 .setOperationalState(convertOperState(operationalState))
212                 .setAdministrativeState(convertAdminState(administrativeState))
213                 .build();
214             writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
215             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
216             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
217         } catch (TimeoutException | InterruptedException | ExecutionException e) {
218             LOG.warn("modifyService : {}", LogMessages.failedTo("modify", serviceName), e);
219             return OperationResult.failed(LogMessages.failedTo("modify", serviceName));
220         }
221     }
222
223     @Override
224     public OperationResult modifyTempService(String serviceName, State operationalState,
225         AdminStates administrativeState) {
226         LOG.debug("Modifying '{}' Temp Service", serviceName);
227         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
228             .Services> readService = getTempService(serviceName);
229         if (!readService.isPresent()) {
230             LOG.warn("modifyTempService: {}", LogMessages.SERVICE_NOT_FOUND);
231             return OperationResult.failed(LogMessages.SERVICE_NOT_FOUND);
232         }
233         try {
234             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
235             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
236                 .Services> iid = InstanceIdentifier.create(TempServiceList.class)
237                     .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
238                             .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531
239                                 .temp.service.list.ServicesKey(serviceName));
240             org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list.Services services =
241                 new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list.ServicesBuilder(
242                     readService.get())
243                 .setOperationalState(convertOperState(operationalState))
244                 .setAdministrativeState(convertAdminState(administrativeState))
245                 .build();
246             writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
247             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
248             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
249         } catch (TimeoutException | InterruptedException | ExecutionException e) {
250             LOG.warn("modifyTempService : {}", LogMessages.failedTo("modify Temp", serviceName), e);
251             return OperationResult.failed(LogMessages.failedTo("modify Temp", serviceName));
252         }
253     }
254
255     @Override
256     public OperationResult createService(ServiceCreateInput serviceCreateInput) {
257         LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
258         try {
259             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
260                     .child(Services.class, new ServicesKey(serviceCreateInput.getServiceName()));
261             Services service = ModelMappingUtils.mappingServices(serviceCreateInput, null);
262             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
263             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
264             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
265             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
266         } catch (TimeoutException | InterruptedException | ExecutionException e) {
267             LOG.warn("createService : {}", LogMessages.failedTo(CREATE_MSG, serviceCreateInput.getServiceName()), e);
268             return OperationResult.failed(LogMessages.failedTo(CREATE_MSG, serviceCreateInput.getServiceName()));
269         }
270     }
271
272     @Override
273     public OperationResult createTempService(TempServiceCreateInput tempServiceCreateInput) {
274         LOG.debug("Writing '{}' Temp Service", tempServiceCreateInput.getCommonId());
275         try {
276             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
277                 .Services> iid = InstanceIdentifier.create(TempServiceList.class)
278                     .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
279                             .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp
280                                 .service.list.ServicesKey(tempServiceCreateInput.getCommonId()));
281             org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.temp.service.list
282                 .Services service = ModelMappingUtils.mappingServices(tempServiceCreateInput);
283             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
284             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
285             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
286             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
287         } catch (TimeoutException | InterruptedException | ExecutionException e) {
288             LOG.warn("createTempService : {}",
289                     LogMessages.failedTo("create Temp", tempServiceCreateInput.getCommonId()), e);
290             return OperationResult.failed(LogMessages.failedTo("create Temp", tempServiceCreateInput.getCommonId()));
291         }
292     }
293
294     @Override
295     public Optional<ServicePathList> getServicePaths() {
296         LOG.debug("Retrieving list of ServicePath...");
297         try {
298             ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
299             InstanceIdentifier<ServicePathList> servicePathListIID = InstanceIdentifier.create(ServicePathList.class);
300             Future<java.util.Optional<ServicePathList>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,
301                     servicePathListIID);
302             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
303         } catch (InterruptedException | ExecutionException | TimeoutException e) {
304             LOG.error("Reading service path list failed. Error={}", e.getMessage());
305         }
306         return Optional.empty();
307     }
308
309     @Override
310     public Optional<ServicePaths> getServicePath(String serviceName) {
311         LOG.debug("Retrieving service path of service {}", serviceName);
312         try {
313             ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
314             InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
315                     .child(ServicePaths.class, new ServicePathsKey(serviceName));
316             Future<java.util.Optional<ServicePaths>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,
317                     servicePathsIID);
318             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
319         } catch (InterruptedException | ExecutionException | TimeoutException e) {
320             LOG.error("Reading service path failed. Error={}", e.getMessage());
321         }
322         return Optional.empty();
323     }
324
325     @Override
326     public OperationResult createServicePath(ServiceInput serviceInput, PathComputationRequestOutput outputFromPce) {
327         LOG.debug("Writing '{}' ServicePath ", serviceInput.getServiceName());
328         try {
329             InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
330                     .child(ServicePaths.class, new ServicePathsKey(serviceInput.getServiceName()));
331             ServicePaths servicePath = ModelMappingUtils.mappingServicePaths(serviceInput, outputFromPce);
332             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
333             writeTx.put(LogicalDatastoreType.OPERATIONAL, servicePathsIID, servicePath);
334             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
335             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
336         } catch (TimeoutException | InterruptedException | ExecutionException e) {
337             LOG.warn("createServicePath : {}",
338                     LogMessages.failedTo("create servicePath", serviceInput.getCommonId()), e);
339             return OperationResult.failed(LogMessages.failedTo("create servicePath", serviceInput.getCommonId()));
340         }
341     }
342
343     @Override
344     public OperationResult modifyServicePath(PathDescription pathDescription, String serviceName) {
345         LOG.debug("Updating servicePath because of a change in the openroadm-topology");
346         Optional<ServicePaths> readServicePath = getServicePath(serviceName);
347         if (!readServicePath.isPresent()) {
348             LOG.warn("modifyServicePath: {}", LogMessages.SERVICE_PATH_NOT_FOUND);
349             return OperationResult.failed(LogMessages.SERVICE_PATH_NOT_FOUND);
350         }
351         try {
352             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
353             InstanceIdentifier<ServicePaths> iid = InstanceIdentifier.create(ServicePathList.class)
354                     .child(ServicePaths.class, new ServicePathsKey(serviceName));
355             ServicePaths servicePaths = new ServicePathsBuilder()
356                     .setServiceAEnd(readServicePath.get().getServiceAEnd())
357                     .setServiceHandlerHeader(readServicePath.get().getServiceHandlerHeader())
358                     .setServicePathName(readServicePath.get().getServicePathName())
359                     .setServiceZEnd(readServicePath.get().getServiceZEnd())
360                     .setSupportingServiceName(readServicePath.get().getSupportingServiceName())
361                     .setEquipmentSrgs(readServicePath.get().getEquipmentSrgs())
362                     .setFiberSpanSrlgs(readServicePath.get().getFiberSpanSrlgs())
363                     .setHardConstraints(readServicePath.get().getHardConstraints())
364                     .setLatency(readServicePath.get().getLatency())
365                     .setLocallyProtectedLinks(readServicePath.get().getLocallyProtectedLinks())
366                     .setPathDescription(pathDescription)
367                     .setPceRoutingMetric(readServicePath.get().getPceRoutingMetric())
368                     .setSoftConstraints(readServicePath.get().getSoftConstraints())
369                     .build();
370
371             writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, servicePaths);
372             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
373             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
374         } catch (TimeoutException | InterruptedException | ExecutionException e) {
375             LOG.warn("modifyServicePath : {}", LogMessages.failedTo("modify service path", serviceName), e);
376             return OperationResult.failed(LogMessages.failedTo("modify service path", serviceName));
377         }
378     }
379
380     @Override
381     public OperationResult deleteServicePath(String serviceName) {
382         InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
383                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
384         LOG.debug("Deleting service from {}", servicePathsIID);
385         WriteTransaction servicePathsWriteTx = this.dataBroker.newWriteOnlyTransaction();
386         servicePathsWriteTx.delete(LogicalDatastoreType.OPERATIONAL, servicePathsIID);
387         try {
388             servicePathsWriteTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
389             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
390         } catch (InterruptedException | ExecutionException | TimeoutException e) {
391             LOG.error("deleteServicePath : {}", LogMessages.failedTo("delete servicePath", serviceName), e);
392             return OperationResult.failed(LogMessages.failedTo("delete servicePath", serviceName));
393         }
394     }
395
396     /*
397      * Write or Modify or Delete Service from/to SreviceList.
398      *
399      * @param serviceName Name of service
400      *
401      * @param input ServiceCreateInput
402      *
403      * @param output PathComputationRequestOutput
404      *
405      * @param choice 0 - Modify 1 - Delete 2 - Write
406      *
407      * @return String operations result, null if ok or not otherwise
408      */
409     @Deprecated
410     @Override
411     public String writeOrModifyOrDeleteServiceList(String serviceName, ServiceCreateInput input,
412             PathComputationRequestOutput output, int choice) {
413         LOG.debug("WriteOrModifyOrDeleting '{}' Service", serviceName);
414         WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
415         Optional<Services> readService = getService(serviceName);
416
417         /*
418          * Write Service.
419          */
420         if (!readService.isPresent()) {
421             if (choice != 2) {
422                 LOG.warn("writeOrModifyOrDeleteServiceList: {}", LogMessages.SERVICE_NOT_FOUND);
423                 return LogMessages.SERVICE_NOT_FOUND;
424             }
425
426             LOG.debug("Writing '{}' Service", serviceName);
427             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
428                     .child(Services.class, new ServicesKey(serviceName));
429             Services service = ModelMappingUtils.mappingServices(input, null);
430             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
431             try {
432                 writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
433                 return null;
434             } catch (InterruptedException | TimeoutException | ExecutionException e) {
435                 LOG.error("writeOrModifyOrDeleteServiceList : {}", LogMessages.failedTo(CREATE_MSG, serviceName), e);
436                 return LogMessages.failedTo(CREATE_MSG, serviceName);
437             }
438         }
439
440         /*
441          * Modify / Delete Service.
442          */
443         InstanceIdentifier<Services> iid =
444                 InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
445         ServicesBuilder service = new ServicesBuilder(readService.get());
446         String action = null;
447         switch (choice) {
448             case 0 : /* Modify. */
449                 LOG.debug("Modifying '{}' Service", serviceName);
450                 service.setOperationalState(convertOperState(State.InService))
451                     .setAdministrativeState(convertAdminState(AdminStates.InService));
452                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, service.build());
453                 action = "modifyService";
454                 break;
455             case 1 : /* Delete */
456                 LOG.debug(DELETING_SERVICE_MSG, serviceName);
457                 writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
458                 action = "deleteService";
459                 break;
460             default:
461                 LOG.debug("No choice found");
462                 break;
463         }
464         try {
465             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
466         } catch (InterruptedException | ExecutionException | TimeoutException e) {
467             LOG.error("writeOrModifyOrDeleteServiceList : {}", LogMessages.failedTo(action, serviceName), e);
468             return LogMessages.failedTo(action, serviceName);
469         }
470
471         return null;
472     }
473
474     private org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev181130.AdminStates
475         convertAdminState(AdminStates adminState61) {
476         return org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev181130.AdminStates
477             .valueOf(adminState61.name());
478     }
479
480     private org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State
481         convertOperState(State operState61) {
482         return org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State
483             .valueOf(operState61.name());
484     }
485 }