Update transportpce-pce YANG
[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.rev220808.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.rev211210.ServiceCreateInput;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.ServiceList;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.ServiceListBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.TempServiceCreateInput;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.TempServiceList;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.TempServiceListBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.Services;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.ServicesBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.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.rev211210.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.rev211210.temp.service.list
150                 .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(
151                     org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list.Services.class,
152                     new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list.ServicesKey(
153                         serviceName));
154             FluentFuture<Optional<
155                 org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list.Services>> future
156                 = readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
157             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
158         } catch (InterruptedException | ExecutionException | TimeoutException e) {
159             LOG.warn("Reading service {} failed:", serviceName, e);
160         }
161         return Optional.empty();
162     }
163
164     @Override
165     public OperationResult deleteService(String serviceName) {
166         LOG.debug(DELETING_SERVICE_MSG, serviceName);
167         try {
168             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
169             InstanceIdentifier<Services> iid =
170                     InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
171             writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
172             writeTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
173             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
174         } catch (TimeoutException | InterruptedException | ExecutionException e) {
175             LOG.warn("deleteService : {}", LogMessages.failedTo("delete", serviceName), e);
176             return OperationResult.failed(LogMessages.failedTo("delete", serviceName));
177         }
178     }
179
180     @Override
181     public OperationResult deleteTempService(String commonId) {
182         LOG.debug(DELETING_SERVICE_MSG, commonId);
183         try {
184             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
185             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list
186                 .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
187                         .http.org.openroadm.service.rev211210.temp.service.list.Services.class,
188                         new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list
189                             .ServicesKey(commonId));
190             writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
191             writeTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
192             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
193         } catch (TimeoutException | InterruptedException | ExecutionException e) {
194             LOG.warn("deleteTempService : {}", LogMessages.failedTo("delete Temp", commonId), e);
195             return OperationResult.failed(LogMessages.failedTo("delete Temp", commonId));
196         }
197     }
198
199     @Override
200     public OperationResult modifyService(String serviceName, State operationalState, AdminStates administrativeState) {
201         LOG.debug("Modifying '{}' Service", serviceName);
202         Optional<Services> readService = getService(serviceName);
203         if (!readService.isPresent()) {
204             LOG.warn("modifyService: {}", LogMessages.SERVICE_NOT_FOUND);
205             return OperationResult.failed(LogMessages.SERVICE_NOT_FOUND);
206         }
207         try {
208             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
209             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
210                     .child(Services.class, new ServicesKey(serviceName));
211             Services services = new ServicesBuilder(readService.get())
212                 .setOperationalState(operationalState)
213                 .setAdministrativeState(administrativeState)
214                 .build();
215             writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
216             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
217             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
218         } catch (TimeoutException | InterruptedException | ExecutionException e) {
219             LOG.warn("modifyService : {}", LogMessages.failedTo("modify", serviceName), e);
220             return OperationResult.failed(LogMessages.failedTo("modify", serviceName));
221         }
222     }
223
224     @Override
225     public OperationResult modifyTempService(String serviceName, State operationalState,
226         AdminStates administrativeState) {
227         LOG.debug("Modifying '{}' Temp Service", serviceName);
228         Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list
229             .Services> readService = getTempService(serviceName);
230         if (!readService.isPresent()) {
231             LOG.warn("modifyTempService: {}", LogMessages.SERVICE_NOT_FOUND);
232             return OperationResult.failed(LogMessages.SERVICE_NOT_FOUND);
233         }
234         try {
235             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
236             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list
237                 .Services> iid = InstanceIdentifier.create(TempServiceList.class)
238                     .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list
239                             .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210
240                                 .temp.service.list.ServicesKey(serviceName));
241             org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list.Services services =
242                 new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list.ServicesBuilder(
243                     readService.get())
244                 .setOperationalState(operationalState)
245                 .setAdministrativeState(administrativeState)
246                 .build();
247             writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
248             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
249             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
250         } catch (TimeoutException | InterruptedException | ExecutionException e) {
251             LOG.warn("modifyTempService : {}", LogMessages.failedTo("modify Temp", serviceName), e);
252             return OperationResult.failed(LogMessages.failedTo("modify Temp", serviceName));
253         }
254     }
255
256     @Override
257     public OperationResult createService(ServiceCreateInput serviceCreateInput) {
258         LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
259         try {
260             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
261                     .child(Services.class, new ServicesKey(serviceCreateInput.getServiceName()));
262             Services service = ModelMappingUtils.mappingServices(serviceCreateInput, null);
263             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
264             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
265             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
266             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
267         } catch (TimeoutException | InterruptedException | ExecutionException e) {
268             LOG.warn("createService : {}", LogMessages.failedTo(CREATE_MSG, serviceCreateInput.getServiceName()), e);
269             return OperationResult.failed(LogMessages.failedTo(CREATE_MSG, serviceCreateInput.getServiceName()));
270         }
271     }
272
273     @Override
274     public OperationResult createTempService(TempServiceCreateInput tempServiceCreateInput) {
275         LOG.debug("Writing '{}' Temp Service", tempServiceCreateInput.getCommonId());
276         try {
277             InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list
278                 .Services> iid = InstanceIdentifier.create(TempServiceList.class)
279                     .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list
280                             .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp
281                                 .service.list.ServicesKey(tempServiceCreateInput.getCommonId()));
282             org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.temp.service.list
283                 .Services service = ModelMappingUtils.mappingServices(tempServiceCreateInput);
284             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
285             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
286             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
287             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
288         } catch (TimeoutException | InterruptedException | ExecutionException e) {
289             LOG.warn("createTempService : {}",
290                     LogMessages.failedTo("create Temp", tempServiceCreateInput.getCommonId()), e);
291             return OperationResult.failed(LogMessages.failedTo("create Temp", tempServiceCreateInput.getCommonId()));
292         }
293     }
294
295     @Override
296     public Optional<ServicePathList> getServicePaths() {
297         LOG.debug("Retrieving list of ServicePath...");
298         try {
299             ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
300             InstanceIdentifier<ServicePathList> servicePathListIID = InstanceIdentifier.create(ServicePathList.class);
301             Future<java.util.Optional<ServicePathList>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,
302                     servicePathListIID);
303             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
304         } catch (InterruptedException | ExecutionException | TimeoutException e) {
305             LOG.error("Reading service path list failed. Error={}", e.getMessage());
306         }
307         return Optional.empty();
308     }
309
310     @Override
311     public Optional<ServicePaths> getServicePath(String serviceName) {
312         LOG.debug("Retrieving service path of service {}", serviceName);
313         try {
314             ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
315             InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
316                     .child(ServicePaths.class, new ServicePathsKey(serviceName));
317             Future<java.util.Optional<ServicePaths>> future = readTx.read(LogicalDatastoreType.OPERATIONAL,
318                     servicePathsIID);
319             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
320         } catch (InterruptedException | ExecutionException | TimeoutException e) {
321             LOG.error("Reading service path failed. Error={}", e.getMessage());
322         }
323         return Optional.empty();
324     }
325
326     @Override
327     public OperationResult createServicePath(ServiceInput serviceInput, PathComputationRequestOutput outputFromPce) {
328         LOG.debug("Writing '{}' ServicePath ", serviceInput.getServiceName());
329         try {
330             InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
331                     .child(ServicePaths.class, new ServicePathsKey(serviceInput.getServiceName()));
332             ServicePaths servicePath = ModelMappingUtils.mappingServicePaths(serviceInput, outputFromPce);
333             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
334             writeTx.put(LogicalDatastoreType.OPERATIONAL, servicePathsIID, servicePath);
335             writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
336             return OperationResult.ok(LogMessages.SUCCESSFUL_MESSAGE);
337         } catch (TimeoutException | InterruptedException | ExecutionException e) {
338             LOG.warn("createServicePath : {}",
339                     LogMessages.failedTo("create servicePath", serviceInput.getCommonId()), e);
340             return OperationResult.failed(LogMessages.failedTo("create servicePath", serviceInput.getCommonId()));
341         }
342     }
343
344     @Override
345     public OperationResult modifyServicePath(PathDescription pathDescription, String serviceName) {
346         LOG.debug("Updating servicePath because of a change in the openroadm-topology");
347         Optional<ServicePaths> readServicePath = getServicePath(serviceName);
348         if (!readServicePath.isPresent()) {
349             LOG.warn("modifyServicePath: {}", LogMessages.SERVICE_PATH_NOT_FOUND);
350             return OperationResult.failed(LogMessages.SERVICE_PATH_NOT_FOUND);
351         }
352         try {
353             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
354             InstanceIdentifier<ServicePaths> iid = InstanceIdentifier.create(ServicePathList.class)
355                     .child(ServicePaths.class, new ServicePathsKey(serviceName));
356             ServicePaths servicePaths = new ServicePathsBuilder()
357                     .setServiceAEnd(readServicePath.get().getServiceAEnd())
358                     .setServiceHandlerHeader(readServicePath.get().getServiceHandlerHeader())
359                     .setServicePathName(readServicePath.get().getServicePathName())
360                     .setServiceZEnd(readServicePath.get().getServiceZEnd())
361                     .setSupportingServiceName(readServicePath.get().getSupportingServiceName())
362                     .setEquipmentSrgs(readServicePath.get().getEquipmentSrgs())
363                     .setFiberSpanSrlgs(readServicePath.get().getFiberSpanSrlgs())
364                     .setHardConstraints(readServicePath.get().getHardConstraints())
365                     .setLatency(readServicePath.get().getLatency())
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(State.InService)
451                     .setAdministrativeState(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 }