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