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 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 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.transportpce.common.OperationResult;
20 import org.opendaylight.transportpce.common.Timeouts;
21 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestOutput;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.State;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceList;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceListBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.ServicesKey;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.ServicePathList;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePaths;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev171017.service.path.list.ServicePathsKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
39 private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
40 private static final String SUCCESSFUL_MESSAGE = "Successful";
42 private DataBroker dataBroker;
44 public ServiceDataStoreOperationsImpl(DataBroker dataBroker) {
45 this.dataBroker = dataBroker;
49 public void initialize() {
51 LOG.info("initializing service registry");
52 WriteTransaction transaction = this.dataBroker.newWriteOnlyTransaction();
53 InstanceIdentifier<ServiceList> iid = InstanceIdentifier.create(ServiceList.class);
54 ServiceList initialRegistry = new ServiceListBuilder().build();
55 transaction.put(LogicalDatastoreType.OPERATIONAL, iid, initialRegistry);
56 Future<Void> future = transaction.submit();
57 future.get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
58 } catch (InterruptedException | ExecutionException | TimeoutException e) {
59 LOG.warn("init failed: {}", e.getMessage());
64 public Optional<Services> getService(String serviceName) {
66 ReadOnlyTransaction readTx = this.dataBroker.newReadOnlyTransaction();
67 InstanceIdentifier<Services> iid = InstanceIdentifier
68 .create(ServiceList.class).child(Services.class, new ServicesKey(serviceName));
69 Future<com.google.common.base.Optional<Services>> future
70 = readTx.read(LogicalDatastoreType.OPERATIONAL, iid);
71 return future.get(Timeouts.DATASTORE_READ, TimeUnit.MILLISECONDS).toJavaUtil();
72 } catch (InterruptedException | ExecutionException | TimeoutException e) {
73 LOG.warn("Reading service {} failed:", serviceName, e);
75 return Optional.empty();
79 public OperationResult deleteService(String serviceName) {
80 LOG.debug("Deleting '{}' Service", serviceName);
82 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
83 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
84 .child(Services.class, new ServicesKey(serviceName));
85 writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
86 writeTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
87 return OperationResult.ok(SUCCESSFUL_MESSAGE);
88 } catch (TimeoutException | InterruptedException | ExecutionException e) {
89 String message = "Failed to delete service " + serviceName + " from Service List";
91 return OperationResult.failed(message);
96 public OperationResult modifyService(String serviceName, State operationalState, State administrativeState) {
97 LOG.debug("Modifying '{}' Service", serviceName);
98 Optional<Services> readService = getService(serviceName);
99 if (readService.isPresent()) {
101 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
102 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class)
103 .child(Services.class, new ServicesKey(serviceName));
104 Services services = new ServicesBuilder(readService.get())
105 .setOperationalState(operationalState)
106 .setAdministrativeState(administrativeState)
108 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, services);
109 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
110 return OperationResult.ok(SUCCESSFUL_MESSAGE);
111 } catch (TimeoutException | InterruptedException | ExecutionException e) {
112 String message = "Failed to modify service " + serviceName + " from Service List";
113 LOG.warn(message, e);
114 return OperationResult.failed(message);
117 String message = "Service " + serviceName + " is not present!";
119 return OperationResult.failed(message);
124 public OperationResult createService(ServiceCreateInput serviceCreateInput,
125 PathComputationRequestOutput outputFromPce) {
126 LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
128 InstanceIdentifier<Services> iid = InstanceIdentifier
129 .create(ServiceList.class).child(Services.class,
130 new ServicesKey(serviceCreateInput.getServiceName()));
131 Services service = ModelMappingUtils.mappingServices(serviceCreateInput, null);
132 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
133 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
134 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
135 return OperationResult.ok(SUCCESSFUL_MESSAGE);
136 } catch (TimeoutException | InterruptedException | ExecutionException e) {
137 String message = "Failed to create service " + serviceCreateInput.getServiceName() + " to Service List";
138 LOG.warn(message, e);
139 return OperationResult.failed(message);
144 public OperationResult createServicePath(ServiceCreateInput serviceCreateInput,
145 PathComputationRequestOutput outputFromPce) {
146 LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
148 InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier
149 .create(ServicePathList.class)
150 .child(ServicePaths.class, new ServicePathsKey(serviceCreateInput.getServiceName()));
151 ServicePaths servicePath = ModelMappingUtils.mappingServicePaths(serviceCreateInput, null, outputFromPce);
152 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
153 writeTx.put(LogicalDatastoreType.OPERATIONAL, servicePathsIID, servicePath);
154 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
155 return OperationResult.ok(SUCCESSFUL_MESSAGE);
156 } catch (TimeoutException | InterruptedException | ExecutionException e) {
157 String message = "Failed to create servicePath " + serviceCreateInput.getServiceName()
158 + " to ServicePath List";
159 LOG.warn(message, e);
160 return OperationResult.failed(message);
165 public OperationResult deleteServicePath(String serviceName) {
166 InstanceIdentifier<ServicePaths> servicePathsIID = InstanceIdentifier.create(ServicePathList.class)
167 .child(ServicePaths.class, new ServicePathsKey(serviceName));
168 LOG.debug("Deleting service from {}", servicePathsIID);
169 WriteTransaction servicePathsWriteTx = this.dataBroker.newWriteOnlyTransaction();
170 servicePathsWriteTx.delete(LogicalDatastoreType.OPERATIONAL, servicePathsIID);
172 servicePathsWriteTx.submit().get(Timeouts.DATASTORE_DELETE, TimeUnit.MILLISECONDS);
173 return OperationResult.ok(SUCCESSFUL_MESSAGE);
174 } catch (InterruptedException | ExecutionException | TimeoutException e) {
175 String message = "Unable to delete service path " + serviceName;
176 LOG.error(message, e);
177 return OperationResult.failed(message);
182 * Write or Modify or Delete Service from/to SreviceList.
189 * PathComputationRequestOutput
191 * 0 - Modify 1 - Delete 2 - Write
192 * @return String operations result, null if ok or not otherwise
196 public String writeOrModifyOrDeleteServiceList(String serviceName, ServiceCreateInput input,
197 PathComputationRequestOutput output, int choice) {
198 LOG.debug("WriteOrModifyOrDeleting '{}' Service",serviceName);
199 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
200 String result = null;
201 Optional<Services> readService = getService(serviceName);
202 if (readService.isPresent()) {
204 * Modify / Delete Service.
206 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
207 new ServicesKey(serviceName));
208 ServicesBuilder service = new ServicesBuilder(readService.get());
210 String action = null;
212 case 0: /* Modify. */
213 LOG.debug("Modifying '{}' Service", serviceName);
214 service.setOperationalState(State.InService).setAdministrativeState(State.InService);
215 writeTx.merge(LogicalDatastoreType.OPERATIONAL, iid, service.build());
216 action = "modifyService";
220 LOG.debug("Deleting '{}' Service", serviceName);
221 writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
222 action = "deleteService";
226 LOG.debug("No choice found");
231 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
232 } catch (InterruptedException | ExecutionException | TimeoutException e) {
233 LOG.error("Failed to {} service from Service List", action, e);
234 result = "Failed to " + action + " service from Service List";
237 if (choice == 2) { /* Write Service */
238 LOG.debug("Writing '{}' Service", serviceName);
239 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
240 new ServicesKey(serviceName));
242 Services service = ModelMappingUtils.mappingServices(input, null);
243 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
245 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
247 } catch (InterruptedException | TimeoutException | ExecutionException e) {
248 LOG.error("Failed to createService service to Service List", e);
249 result = "Failed to createService service to Service List";
252 LOG.info("Service is not present ! ");
253 result = "Service is not present ! ";