Merge "ROADM To ROADM Path Calculation"
[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 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;
36
37 public class ServiceDataStoreOperationsImpl implements ServiceDataStoreOperations {
38
39     private static final Logger LOG = LoggerFactory.getLogger(ServiceDataStoreOperationsImpl.class);
40     private static final String SUCCESSFUL_MESSAGE = "Successful";
41
42     private DataBroker dataBroker;
43
44     public ServiceDataStoreOperationsImpl(DataBroker dataBroker) {
45         this.dataBroker = dataBroker;
46     }
47
48     @Override
49     public void initialize() {
50         try {
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());
60         }
61     }
62
63     @Override
64     public Optional<Services> getService(String serviceName) {
65         try {
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);
74         }
75         return Optional.empty();
76     }
77
78     @Override
79     public OperationResult deleteService(String serviceName) {
80         LOG.debug("Deleting '{}' Service", serviceName);
81         try {
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";
90             LOG.warn(message, e);
91             return OperationResult.failed(message);
92         }
93     }
94
95     @Override
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()) {
100             try {
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)
107                         .build();
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);
115             }
116         } else {
117             String message = "Service " + serviceName + " is not present!";
118             LOG.warn(message);
119             return OperationResult.failed(message);
120         }
121     }
122
123     @Override
124     public OperationResult createService(ServiceCreateInput serviceCreateInput,
125             PathComputationRequestOutput outputFromPce) {
126         LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
127         try {
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);
140         }
141     }
142
143     @Override
144     public OperationResult createServicePath(ServiceCreateInput serviceCreateInput,
145             PathComputationRequestOutput outputFromPce) {
146         LOG.debug("Writing '{}' Service", serviceCreateInput.getServiceName());
147         try {
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);
161         }
162     }
163
164     @Override
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);
171         try {
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);
178         }
179     }
180
181     /*
182      * Write or Modify or Delete Service from/to SreviceList.
183      *
184      * @param serviceName
185      *            Name of service
186      * @param input
187      *            ServiceCreateInput
188      * @param output
189      *            PathComputationRequestOutput
190      * @param choice
191      *            0 - Modify 1 - Delete 2 - Write
192      * @return String operations result, null if ok or not otherwise
193      */
194     @Deprecated
195     @Override
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()) {
203             /*
204              * Modify / Delete Service.
205              */
206             InstanceIdentifier<Services> iid = InstanceIdentifier.create(ServiceList.class).child(Services.class,
207                     new ServicesKey(serviceName));
208             ServicesBuilder service = new ServicesBuilder(readService.get());
209
210             String action = null;
211             switch (choice) {
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";
217                     break;
218
219                 case 1: /* Delete */
220                     LOG.debug("Deleting '{}' Service", serviceName);
221                     writeTx.delete(LogicalDatastoreType.OPERATIONAL, iid);
222                     action = "deleteService";
223                     break;
224
225                 default:
226                     LOG.debug("No choice found");
227                     break;
228
229             }
230             try {
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";
235             }
236         } else {
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));
241
242                 Services service = ModelMappingUtils.mappingServices(input, null);
243                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, service);
244                 try {
245                     writeTx.submit().get(Timeouts.DATASTORE_WRITE, TimeUnit.MILLISECONDS);
246                     result = null;
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";
250                 }
251             } else {
252                 LOG.info("Service is not present ! ");
253                 result = "Service is not present ! ";
254             }
255         }
256         return result;
257     }
258
259 }