Bug correction in ServiceDatastore Operations
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / service / ServiceDataStoreOperationsImpl.java
1 /*
2  * Copyright © 2017 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.servicehandler.service;
9
10 import java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14 import java.util.concurrent.TimeoutException;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.transportpce.common.OperationResult;
21 import org.opendaylight.transportpce.common.Timeouts;
22 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev170426.PathComputationRequestOutput;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.State;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceList;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceListBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesKey;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.ServicePathList;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.service.path.list.ServicePaths;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.service.path.list.ServicePathsKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
39
40     private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
41     private static final String SUCCESSFUL_MESSAGE = "Successful";
42
43     private DataBroker dataBroker;
44
45     public ServiceDataStoreOperationsImpl(DataBroker dataBroker) {
46         this.dataBroker = dataBroker;
47     }
48
49     @Override
50     public void initialize() {
51         try {
52             LOG.info("initializing service registry");
53             WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
54             InstanceIdentifier<ServiceList> iid = InstanceIdentifier.create(ServiceList.class);
55             ServiceList initialRegistry = new ServiceListBuilder().build();
56             transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
57             Future<Void> future = transaction.submit();
58             future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
59         } catch (InterruptedException | ExecutionException | TimeoutException e) {
60             LOG.warn("init failed: {}", e.getMessage());
61         }
62     }
63
64     @Override
65     public Optional<Services> getService(String serviceName) {
66         try {
67             ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction();
68             InstanceIdentifier<Services> iid = InstanceIdentifier
69                     .create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
70             Future<com.google.common.base.Optional<Services>> future
71                     = readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
72             return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).toJavaUtil();
73         } catch (InterruptedException | ExecutionException | TimeoutException e) {
74             LOG.warn("Reading service {} failed:", serviceName, e);
75         }
76         return Optional.empty();
77     }
78
79     @Override
80     public OperationResult deleteService(String serviceName) {
81         LOG.debug("Deleting '{}' Service", serviceName);
82         try {
83             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
84             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
85                     .child(Services.class, new ServicesKey(serviceName));
86             writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
87             writeTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
88             return OperationResult.ok(SUCCESSFUL_MESSAGE);
89         } catch (TimeoutException | InterruptedException | ExecutionException e) {
90             String message = "Failed to delete service " + serviceName + " from Service List";
91             LOG.warn(message, e);
92             return OperationResult.failed(message);
93         }
94     }
95
96     @Override
97     public OperationResult modifyService(String serviceName, State operationalState, State administrativeState) {
98         LOG.debug("Modifying '{}' Service", serviceName);
99         Optional<Services> readService = getService(serviceName);
100         if (readService.isPresent()) {
101             try {
102                 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
103                 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
104                         .child(Services.class, new ServicesKey(serviceName));
105                 Services services = new ServicesBuilder(readService.get())
106                         .setOperationalState(operationalState)
107                         .setAdministrativeState(administrativeState)
108                         .build();
109                 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
110                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
111                 return OperationResult.ok(SUCCESSFUL_MESSAGE);
112             } catch (TimeoutException | InterruptedException | ExecutionException e) {
113                 String message = "Failed to modify service " + serviceName + " from Service List";
114                 LOG.warn(message, e);
115                 return OperationResult.failed(message);
116             }
117         } else {
118             String message = "Service " + serviceName + " is not present!";
119             LOG.warn(message);
120             return OperationResult.failed(message);
121         }
122     }
123
124     @Override
125     public OperationResult createService(ServiceCreateInput serviceCreateInput,
126             PathComputationRequestOutput outputFromPce) {
127         LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
128         try {
129             InstanceIdentifier<Services> iid = InstanceIdentifier
130                     .create(ServiceList.class).child(Services.class,
131                             new ServicesKey(serviceCreateInput.getServiceName()));
132             Services service = ModelMappingUtils.mappingServices(serviceCreateInput, null);
133             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
134             writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
135             writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
136             return OperationResult.ok(SUCCESSFUL_MESSAGE);
137         } catch (TimeoutException | InterruptedException | ExecutionException e) {
138             String message = "Failed to create service " + serviceCreateInput.getServiceName() + " to Service List";
139             LOG.warn(message, e);
140             return OperationResult.failed(message);
141         }
142     }
143
144     @Override
145     public OperationResult createServicePath(ServiceCreateInput serviceCreateInput,
146             PathComputationRequestOutput outputFromPce) {
147         LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
148         try {
149             InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier
150                     .create(ServicePathList.class)
151                     .child(ServicePaths.class, new ServicePathsKey(serviceCreateInput.getServiceName()));
152             ServicePaths servicePath =  ModelMappingUtils.mappingServicePaths(serviceCreateInput, null, outputFromPce);
153             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
154             writeTx.put(LogicalDatastoreType.OPERATIONAL, servicePathsIID, servicePath);
155             writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
156             return OperationResult.ok(SUCCESSFUL_MESSAGE);
157         } catch (TimeoutException | InterruptedException | ExecutionException e) {
158             String message = "Failed to create servicePath " + serviceCreateInput.getServiceName()
159                 + " to ServicePath List";
160             LOG.warn(message, e);
161             return OperationResult.failed(message);
162         }
163     }
164
165     @Override
166     public OperationResult deleteServicePath(String serviceName) {
167         InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
168                 .child(ServicePaths.class, new ServicePathsKey(serviceName));
169         LOG.debug("Deleting service from {}", servicePathsIID);
170         WriteTransaction servicePathsWriteTx = this.dataBroker.newWriteOnlyTransaction();
171         servicePathsWriteTx.delete(LogicalDatastoreType.OPERATIONAL, servicePathsIID);
172         try {
173             servicePathsWriteTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
174             return OperationResult.ok(SUCCESSFUL_MESSAGE);
175         } catch (InterruptedException | ExecutionException | TimeoutException e) {
176             String message = "Unable to delete service path " + serviceName;
177             LOG.error(message, e);
178             return OperationResult.failed(message);
179         }
180     }
181
182     /*
183      * Write or Modify or Delete Service from/to SreviceList.
184      *
185      * @param serviceName
186      *            Name of service
187      * @param input
188      *            ServiceCreateInput
189      * @param output
190      *            PathComputationRequestOutput
191      * @param choice
192      *            0 - Modify 1 - Delete 2 - Write
193      * @return String operations result, null if ok or not otherwise
194      */
195     @Deprecated
196     @Override
197     public String writeOrModifyOrDeleteServiceList(String serviceName, ServiceCreateInput input,
198                                                     PathComputationRequestOutput output, int choice) {
199         LOG.debug("WriteOrModifyOrDeleting '{}' Service",serviceName);
200         WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
201         String result = null;
202         Optional<Services> readService = getService(serviceName);
203         if (readService.isPresent()) {
204             /*
205              * Modify / Delete Service.
206              */
207             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
208                     new ServicesKey(serviceName));
209             ServicesBuilder service = new ServicesBuilder(readService.get());
210
211             String action = null;
212             switch (choice) {
213                 case 0: /* Modify. */
214                     LOG.debug("Modifying '{}' Service", serviceName);
215                     service.setOperationalState(State.InService).setAdministrativeState(State.InService);
216                     writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, service.build());
217                     action = "modifyService";
218                     break;
219
220                 case 1: /* Delete */
221                     LOG.debug("Deleting '{}' Service", serviceName);
222                     writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
223                     action = "deleteService";
224                     break;
225
226                 default:
227                     LOG.debug("No choice found");
228                     break;
229
230             }
231             try {
232                 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
233             } catch (InterruptedException | ExecutionException | TimeoutException e) {
234                 LOG.error("Failed to {} service from Service List", action, e);
235                 result = "Failed to " + action + " service from Service List";
236             }
237         } else {
238             if (choice == 2) { /* Write Service */
239                 LOG.debug("Writing '{}' Service", serviceName);
240                 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
241                         new ServicesKey(serviceName));
242
243                 Services service = ModelMappingUtils.mappingServices(input, null);
244                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
245                 try {
246                     writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
247                     result = null;
248                 } catch (InterruptedException | TimeoutException | ExecutionException e) {
249                     LOG.error("Failed to createService service to Service List", e);
250                     result = "Failed to createService service to Service List";
251                 }
252             } else {
253                 LOG.info("Service is not present ! ");
254                 result = "Service is not present ! ";
255             }
256         }
257         return result;
258     }
259
260 }