2 * Copyright © 2017 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.servicehandler.service;
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.rev190624.PathComputationRequestOutput;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.State;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceList;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceListBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateInput;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceList;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceListBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesKey;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
45 private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
46 private static final String SUCCESSFUL_MESSAGE = "Successful";
47 private DataBroker dataBroker;
49 public ServiceDataStoreOperationsImpl(DataBroker dataBroker) {
50 this.dataBroker = dataBroker;
54 public void initialize() {
55 initializeServiceList();
56 initializeTempServiceList();
59 private void initializeServiceList() {
61 LOG.info("initializing service registry");
62 WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
63 InstanceIdentifier<ServiceList> iid = InstanceIdentifier.create(ServiceList.class);
64 ServiceList initialRegistry = new ServiceListBuilder().build();
65 transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
66 FluentFuture<? extends @NonNull CommitInfo> future = transaction.commit();
67 future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
68 } catch (InterruptedException | ExecutionException | TimeoutException e) {
69 LOG.warn("init failed: {}", e.getMessage());
73 private void initializeTempServiceList() {
75 LOG.info("initializing temp service registry");
76 WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
77 InstanceIdentifier<TempServiceList> iid = InstanceIdentifier.create(TempServiceList.class);
78 TempServiceList initialRegistry = new TempServiceListBuilder().build();
79 transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
80 FluentFuture<? extends @NonNull CommitInfo> future = transaction.commit();
81 future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
82 } catch (InterruptedException | ExecutionException | TimeoutException e) {
83 LOG.warn("init failed: {}", e.getMessage());
88 public Optional<Services> getService(String serviceName) {
90 ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
91 InstanceIdentifier<Services> iid =
92 InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
93 Future<java.util.Optional<Services>> future =
94 readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
95 return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
96 } catch (InterruptedException | ExecutionException | TimeoutException e) {
97 LOG.warn("Reading service {} failed:", serviceName, e);
99 return Optional.empty();
103 public Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
104 .Services> getTempService(String serviceName) {
106 ReadTransaction readTx = this.dataBroker.newReadOnlyTransaction();
107 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
108 .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
109 .http.org.openroadm.service.rev161014.temp.service.list.Services.class,
110 new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
111 .ServicesKey(serviceName));
112 Future<java.util.Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014
113 .temp.service.list.Services>> future = readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
114 return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS);
115 } catch (InterruptedException | ExecutionException | TimeoutException e) {
116 LOG.warn("Reading service {} failed:", serviceName, e);
118 return Optional.empty();
122 public OperationResult deleteService(String serviceName) {
123 LOG.debug("Deleting '{}' Service", serviceName);
125 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
126 InstanceIdentifier<Services> iid =
127 InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
128 writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
129 writeTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
130 return OperationResult.ok(SUCCESSFUL_MESSAGE);
131 } catch (TimeoutException | InterruptedException | ExecutionException e) {
132 String message = "Failed to delete service " + serviceName + " from Service List";
133 LOG.warn(message, e);
134 return OperationResult.failed(message);
139 public OperationResult deleteTempService(String commonId) {
140 LOG.debug("Deleting '{}' Service", commonId);
142 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
143 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
144 .Services> iid = InstanceIdentifier.create(TempServiceList.class).child(org.opendaylight.yang.gen.v1
145 .http.org.openroadm.service.rev161014.temp.service.list.Services.class,
146 new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
147 .ServicesKey(commonId));
148 writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
149 writeTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
150 return OperationResult.ok(SUCCESSFUL_MESSAGE);
151 } catch (TimeoutException | InterruptedException | ExecutionException e) {
152 String message = "Failed to delete service " + commonId + " from Service List";
153 LOG.warn(message, e);
154 return OperationResult.failed(message);
159 public OperationResult modifyService(String serviceName, State operationalState, State administrativeState) {
160 LOG.debug("Modifying '{}' Service", serviceName);
161 Optional<Services> readService = getService(serviceName);
162 if (readService.isPresent()) {
164 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
165 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
166 .child(Services.class, new ServicesKey(serviceName));
167 Services services = new ServicesBuilder(readService.get()).setOperationalState(operationalState)
168 .setAdministrativeState(administrativeState)
170 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
171 writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
172 return OperationResult.ok(SUCCESSFUL_MESSAGE);
173 } catch (TimeoutException | InterruptedException | ExecutionException e) {
174 String message = "Failed to modify service " + serviceName + " from Service List";
175 LOG.warn(message, e);
176 return OperationResult.failed(message);
179 String message = "Service " + serviceName + " is not present!";
181 return OperationResult.failed(message);
186 public OperationResult modifyTempService(String serviceName, State operationalState, State administrativeState) {
187 LOG.debug("Modifying '{}' Temp Service", serviceName);
188 Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
189 .Services> readService = getTempService(serviceName);
190 if (readService.isPresent()) {
192 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
193 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
194 .Services> iid = InstanceIdentifier.create(TempServiceList.class)
195 .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
196 .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014
197 .temp.service.list.ServicesKey(serviceName));
198 org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
199 .Services services = new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp
200 .service.list.ServicesBuilder(readService.get()).setOperationalState(operationalState)
201 .setAdministrativeState(administrativeState)
203 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
204 writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
205 return OperationResult.ok(SUCCESSFUL_MESSAGE);
206 } catch (TimeoutException | InterruptedException | ExecutionException e) {
207 String message = "Failed to modify temp service " + serviceName + " from Temp Service List";
208 LOG.warn(message, e);
209 return OperationResult.failed(message);
212 String message = "Temp Service " + serviceName + " is not present!";
214 return OperationResult.failed(message);
219 public OperationResult createService(ServiceCreateInput serviceCreateInput) {
220 LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
222 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
223 .child(Services.class, new ServicesKey(serviceCreateInput.getServiceName()));
224 Services service = ModelMappingUtils.mappingServices(serviceCreateInput, null);
225 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
226 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
227 writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
228 return OperationResult.ok(SUCCESSFUL_MESSAGE);
229 } catch (TimeoutException | InterruptedException | ExecutionException e) {
230 String message = "Failed to create service " + serviceCreateInput.getServiceName() + " to Service List";
231 LOG.warn(message, e);
232 return OperationResult.failed(message);
237 public OperationResult createTempService(TempServiceCreateInput tempServiceCreateInput) {
238 LOG.debug("Writing '{}' Temp Service", tempServiceCreateInput.getCommonId());
240 InstanceIdentifier<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
241 .Services> iid = InstanceIdentifier.create(TempServiceList.class)
242 .child(org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
243 .Services.class, new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp
244 .service.list.ServicesKey(tempServiceCreateInput.getCommonId()));
245 org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
246 .Services service = ModelMappingUtils.mappingServices(tempServiceCreateInput);
247 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
248 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
249 writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
250 return OperationResult.ok(SUCCESSFUL_MESSAGE);
251 } catch (TimeoutException | InterruptedException | ExecutionException e) {
252 String message = "Failed to create Temp service " + tempServiceCreateInput.getCommonId()
253 + " to TempService List";
254 LOG.warn(message, e);
255 return OperationResult.failed(message);
260 public OperationResult createServicePath(ServiceInput serviceInput, PathComputationRequestOutput outputFromPce) {
261 LOG.debug("Writing '{}' ServicePath ", serviceInput.getServiceName());
263 InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
264 .child(ServicePaths.class, new ServicePathsKey(serviceInput.getServiceName()));
265 ServicePaths servicePath = ModelMappingUtils.mappingServicePaths(serviceInput, outputFromPce);
266 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
267 writeTx.put(LogicalDatastoreType.OPERATIONAL, servicePathsIID, servicePath);
268 writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
269 return OperationResult.ok(SUCCESSFUL_MESSAGE);
270 } catch (TimeoutException | InterruptedException | ExecutionException e) {
271 String message = "Failed to create servicePath " + serviceInput.getCommonId() + " to ServicePath List";
272 LOG.warn(message, e);
273 return OperationResult.failed(message);
278 public OperationResult deleteServicePath(String serviceName) {
279 InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
280 .child(ServicePaths.class, new ServicePathsKey(serviceName));
281 LOG.debug("Deleting service from {}", servicePathsIID);
282 WriteTransaction servicePathsWriteTx = this.dataBroker.newWriteOnlyTransaction();
283 servicePathsWriteTx.delete(LogicalDatastoreType.OPERATIONAL, servicePathsIID);
285 servicePathsWriteTx.commit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
286 return OperationResult.ok(SUCCESSFUL_MESSAGE);
287 } catch (InterruptedException | ExecutionException | TimeoutException e) {
288 String message = "Unable to delete service path " + serviceName;
289 LOG.error(message, e);
290 return OperationResult.failed(message);
295 * Write or Modify or Delete Service from/to SreviceList.
297 * @param serviceName Name of service
299 * @param input ServiceCreateInput
301 * @param output PathComputationRequestOutput
303 * @param choice 0 - Modify 1 - Delete 2 - Write
305 * @return String operations result, null if ok or not otherwise
309 public String writeOrModifyOrDeleteServiceList(String serviceName, ServiceCreateInput input,
310 PathComputationRequestOutput output, int choice) {
311 LOG.debug("WriteOrModifyOrDeleting '{}' Service", serviceName);
312 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
313 String result = null;
314 Optional<Services> readService = getService(serviceName);
315 if (readService.isPresent()) {
317 * Modify / Delete Service.
319 InstanceIdentifier<Services> iid =
320 InstanceIdentifier.create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
321 ServicesBuilder service = new ServicesBuilder(readService.get());
322 String action = null;
324 case 0 : /* Modify. */
325 LOG.debug("Modifying '{}' Service", serviceName);
326 service.setOperationalState(State.InService).setAdministrativeState(State.InService);
327 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, service.build());
328 action = "modifyService";
330 case 1 : /* Delete */
331 LOG.debug("Deleting '{}' Service", serviceName);
332 writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
333 action = "deleteService";
336 LOG.debug("No choice found");
340 writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
341 } catch (InterruptedException | ExecutionException | TimeoutException e) {
342 LOG.error("Failed to {} service from Service List", action, e);
343 result = "Failed to " + action + " service from Service List";
350 LOG.debug("Writing '{}' Service", serviceName);
351 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
352 .child(Services.class, new ServicesKey(serviceName));
353 Services service = ModelMappingUtils.mappingServices(input, null);
354 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
356 writeTx.commit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
358 } catch (InterruptedException | TimeoutException | ExecutionException e) {
359 LOG.error("Failed to createService service to Service List", e);
360 result = "Failed to createService service to Service List";
363 LOG.info("Service is not present ! ");
364 result = "Service is not present ! ";