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;
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;
38 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
40 private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
41 private static final String SUCCESSFUL_MESSAGE = "Successful";
43 private DataBroker dataBroker;
45 public ServiceDataStoreOperationsImpl(DataBroker dataBroker) {
46 this.dataBroker = dataBroker;
50 public void initialize() {
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());
65 public Optional<Services> getService(String serviceName) {
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);
76 return Optional.empty();
80 public OperationResult deleteService(String serviceName) {
81 LOG.debug("Deleting '{}' Service", serviceName);
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";
92 return OperationResult.failed(message);
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()) {
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)
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);
118 String message = "Service " + serviceName + " is not present!";
120 return OperationResult.failed(message);
125 public OperationResult createService(ServiceCreateInput serviceCreateInput,
126 PathComputationRequestOutput outputFromPce) {
127 LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
129 InstanceIdentifier<Services> iid = InstanceIdentifier
130 .create(ServiceList.class).child(Services.class,
131 new ServicesKey(serviceCreateInput.getServiceName()));
132 Services service = ModelMappingUtils.mappingServices(serviceCreateInput, null, outputFromPce);
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);
145 public OperationResult createServicePath(ServiceCreateInput serviceCreateInput,
146 PathComputationRequestOutput outputFromPce) {
147 LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
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);
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);
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);
183 * Write or Modify or Delete Service from/to SreviceList.
190 * PathComputationRequestOutput
192 * 0 - Modify 1 - Delete 2 - Write
193 * @return String operations result, null if ok or not otherwise
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()) {
205 * Modify / Delete Service.
207 InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
208 new ServicesKey(serviceName));
209 ServicesBuilder service = new ServicesBuilder(readService.get());
211 String action = null;
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";
221 LOG.debug("Deleting '{}' Service", serviceName);
222 writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
223 action = "deleteService";
227 LOG.debug("No choice found");
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";
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));
243 Services service = ModelMappingUtils.mappingServices(input, null, output);
244 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
246 writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
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";
253 LOG.info("Service is not present ! ");
254 result = "Service is not present ! ";