2 * Copyright (c) 2014 Cisco Systems, 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.netconf.sal.restconf.impl;
10 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
11 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.concurrent.ExecutionException;
22 import javax.ws.rs.core.Response.Status;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
31 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
32 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
33 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
34 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
35 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
36 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
37 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
38 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
39 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
40 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
41 import org.opendaylight.netconf.sal.streams.listeners.NotificationListenerAdapter;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class BrokerFacade {
55 private final static Logger LOG = LoggerFactory.getLogger(BrokerFacade.class);
57 private final static BrokerFacade INSTANCE = new BrokerFacade();
58 private volatile DOMRpcService rpcService;
59 private volatile ConsumerSession context;
60 private DOMDataBroker domDataBroker;
61 private DOMNotificationService domNotification;
63 private BrokerFacade() {
66 public void setRpcService(final DOMRpcService router) {
67 this.rpcService = router;
70 public void setDomNotificationService(final DOMNotificationService domNotification) {
71 this.domNotification = domNotification;
74 public void setContext(final ConsumerSession context) {
75 this.context = context;
78 public static BrokerFacade getInstance() {
79 return BrokerFacade.INSTANCE;
82 private void checkPreconditions() {
83 if ((this.context == null) || (this.domDataBroker == null)) {
84 throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
89 public NormalizedNode<?, ?> readConfigurationData(final YangInstanceIdentifier path) {
91 return readDataViaTransaction(this.domDataBroker.newReadOnlyTransaction(), CONFIGURATION, path);
94 public NormalizedNode<?, ?> readConfigurationData(final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
95 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
96 if (domDataBrokerService.isPresent()) {
97 return readDataViaTransaction(domDataBrokerService.get().newReadOnlyTransaction(), CONFIGURATION, path);
99 final String errMsg = "DOM data broker service isn't available for mount point " + path;
101 throw new RestconfDocumentedException(errMsg);
105 public NormalizedNode<?, ?> readOperationalData(final YangInstanceIdentifier path) {
106 checkPreconditions();
107 return readDataViaTransaction(this.domDataBroker.newReadOnlyTransaction(), OPERATIONAL, path);
110 public NormalizedNode<?, ?> readOperationalData(final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
111 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
112 if (domDataBrokerService.isPresent()) {
113 return readDataViaTransaction(domDataBrokerService.get().newReadOnlyTransaction(), OPERATIONAL, path);
115 final String errMsg = "DOM data broker service isn't available for mount point " + path;
117 throw new RestconfDocumentedException(errMsg);
121 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPut(
122 final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
123 checkPreconditions();
124 return putDataViaTransaction(this.domDataBroker.newReadWriteTransaction(), CONFIGURATION, path, payload, globalSchema);
127 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPut(
128 final DOMMountPoint mountPoint, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
129 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
130 if (domDataBrokerService.isPresent()) {
131 return putDataViaTransaction(domDataBrokerService.get().newReadWriteTransaction(), CONFIGURATION, path,
132 payload, mountPoint.getSchemaContext());
134 final String errMsg = "DOM data broker service isn't available for mount point " + path;
136 throw new RestconfDocumentedException(errMsg);
139 public PATCHStatusContext patchConfigurationDataWithinTransaction(final PATCHContext context,
140 final SchemaContext globalSchema) {
141 final DOMDataReadWriteTransaction patchTransaction = this.domDataBroker.newReadWriteTransaction();
142 final List<PATCHStatusEntity> editCollection = new ArrayList<>();
143 List<RestconfError> editErrors;
144 final List<RestconfError> globalErrors = null;
145 int errorCounter = 0;
147 for (final PATCHEntity patchEntity : context.getData()) {
148 final PATCHEditOperation operation = PATCHEditOperation.valueOf(patchEntity.getOperation().toUpperCase());
152 if (errorCounter == 0) {
154 postDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
155 patchEntity.getNode(), globalSchema);
156 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
157 } catch (final RestconfDocumentedException e) {
158 editErrors = new ArrayList<>();
159 editErrors.addAll(e.getErrors());
160 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
166 if (errorCounter == 0) {
168 putDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
169 .getTargetNode(), patchEntity.getNode(), globalSchema);
170 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
171 } catch (final RestconfDocumentedException e) {
172 editErrors = new ArrayList<>();
173 editErrors.addAll(e.getErrors());
174 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
180 if (errorCounter == 0) {
182 deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
184 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
185 } catch (final RestconfDocumentedException e) {
186 editErrors = new ArrayList<>();
187 editErrors.addAll(e.getErrors());
188 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
194 if (errorCounter == 0) {
196 deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
198 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
199 } catch (final RestconfDocumentedException e) {
200 LOG.error("Error removing {} by {} operation", patchEntity.getTargetNode().toString(),
201 patchEntity.getEditId(), e);
206 if (errorCounter == 0) {
208 mergeDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
209 patchEntity.getNode(), globalSchema);
210 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
211 } catch (RestconfDocumentedException e) {
212 editErrors = new ArrayList<>();
213 editErrors.addAll(e.getErrors());
214 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
222 //TODO: make sure possible global errors are filled up correctly and decide transaction submission based on that
223 //globalErrors = new ArrayList<>();
224 if (errorCounter == 0) {
225 final CheckedFuture<Void, TransactionCommitFailedException> submit = patchTransaction.submit();
226 return new PATCHStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), true,
229 patchTransaction.cancel();
230 return new PATCHStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), false,
235 // POST configuration
236 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
237 final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
238 checkPreconditions();
239 return postDataViaTransaction(this.domDataBroker.newReadWriteTransaction(), CONFIGURATION, path, payload, globalSchema);
242 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
243 final DOMMountPoint mountPoint, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
244 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
245 if (domDataBrokerService.isPresent()) {
246 return postDataViaTransaction(domDataBrokerService.get().newReadWriteTransaction(), CONFIGURATION, path,
247 payload, mountPoint.getSchemaContext());
249 final String errMsg = "DOM data broker service isn't available for mount point " + path;
251 throw new RestconfDocumentedException(errMsg);
254 // DELETE configuration
255 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
256 final YangInstanceIdentifier path) {
257 checkPreconditions();
258 return deleteDataViaTransaction(this.domDataBroker.newWriteOnlyTransaction(), CONFIGURATION, path);
261 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
262 final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
263 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
264 if (domDataBrokerService.isPresent()) {
265 return deleteDataViaTransaction(domDataBrokerService.get().newWriteOnlyTransaction(), CONFIGURATION, path);
267 final String errMsg = "DOM data broker service isn't available for mount point " + path;
269 throw new RestconfDocumentedException(errMsg);
273 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
274 checkPreconditions();
275 if (this.rpcService == null) {
276 throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
278 LOG.trace("Invoke RPC {} with input: {}", type, input);
279 return this.rpcService.invokeRpc(type, input);
282 public void registerToListenDataChanges(final LogicalDatastoreType datastore, final DataChangeScope scope,
283 final ListenerAdapter listener) {
284 checkPreconditions();
286 if (listener.isListening()) {
290 final YangInstanceIdentifier path = listener.getPath();
291 final ListenerRegistration<DOMDataChangeListener> registration = this.domDataBroker.registerDataChangeListener(
292 datastore, path, listener, scope);
294 listener.setRegistration(registration);
297 private NormalizedNode<?, ?> readDataViaTransaction(final DOMDataReadTransaction transaction,
298 final LogicalDatastoreType datastore, final YangInstanceIdentifier path) {
299 LOG.trace("Read {} via Restconf: {}", datastore.name(), path);
300 final ListenableFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = transaction.read(datastore, path);
301 if (listenableFuture != null) {
302 Optional<NormalizedNode<?, ?>> optional;
304 LOG.debug("Reading result data from transaction.");
305 optional = listenableFuture.get();
306 } catch (InterruptedException | ExecutionException e) {
307 LOG.warn("Exception by reading {} via Restconf: {}", datastore.name(), path, e);
308 throw new RestconfDocumentedException("Problem to get data from transaction.", e.getCause());
311 if (optional != null) {
312 if (optional.isPresent()) {
313 return optional.get();
320 private CheckedFuture<Void, TransactionCommitFailedException> postDataViaTransaction(
321 final DOMDataReadWriteTransaction rWTransaction, final LogicalDatastoreType datastore,
322 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
323 // FIXME: This is doing correct post for container and list children
324 // not sure if this will work for choice case
325 if(payload instanceof MapNode) {
326 LOG.trace("POST {} via Restconf: {} with payload {}", datastore.name(), path, payload);
327 final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
328 rWTransaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
329 ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
330 for(final MapEntryNode child : ((MapNode) payload).getValue()) {
331 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
332 checkItemDoesNotExists(rWTransaction, datastore, childPath);
333 rWTransaction.put(datastore, childPath, child);
336 checkItemDoesNotExists(rWTransaction,datastore, path);
337 ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
338 rWTransaction.put(datastore, path, payload);
340 return rWTransaction.submit();
343 private void postDataWithinTransaction(
344 final DOMDataReadWriteTransaction rWTransaction, final LogicalDatastoreType datastore,
345 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
346 // FIXME: This is doing correct post for container and list children
347 // not sure if this will work for choice case
348 if(payload instanceof MapNode) {
349 LOG.trace("POST {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
350 final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
351 rWTransaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
352 ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
353 for(final MapEntryNode child : ((MapNode) payload).getValue()) {
354 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
355 checkItemDoesNotExists(rWTransaction, datastore, childPath);
356 rWTransaction.put(datastore, childPath, child);
359 checkItemDoesNotExists(rWTransaction,datastore, path);
360 ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
361 rWTransaction.put(datastore, path, payload);
365 private void checkItemDoesNotExists(final DOMDataReadWriteTransaction rWTransaction,final LogicalDatastoreType store, final YangInstanceIdentifier path) {
366 final ListenableFuture<Boolean> futureDatastoreData = rWTransaction.exists(store, path);
368 if (futureDatastoreData.get()) {
369 final String errMsg = "Post Configuration via Restconf was not executed because data already exists";
370 LOG.trace("{}:{}", errMsg, path);
371 rWTransaction.cancel();
372 throw new RestconfDocumentedException("Data already exists for path: " + path, ErrorType.PROTOCOL,
373 ErrorTag.DATA_EXISTS);
375 } catch (InterruptedException | ExecutionException e) {
376 LOG.warn("It wasn't possible to get data loaded from datastore at path {}", path, e);
381 private CheckedFuture<Void, TransactionCommitFailedException> putDataViaTransaction(
382 final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
383 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
384 LOG.trace("Put {} via Restconf: {} with payload {}", datastore.name(), path, payload);
385 ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
386 writeTransaction.put(datastore, path, payload);
387 return writeTransaction.submit();
390 private void putDataWithinTransaction(
391 final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
392 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
393 LOG.trace("Put {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
394 ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
395 writeTransaction.put(datastore, path, payload);
398 private CheckedFuture<Void, TransactionCommitFailedException> deleteDataViaTransaction(
399 final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
400 final YangInstanceIdentifier path) {
401 LOG.trace("Delete {} via Restconf: {}", datastore.name(), path);
402 writeTransaction.delete(datastore, path);
403 return writeTransaction.submit();
406 private void deleteDataWithinTransaction(
407 final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
408 final YangInstanceIdentifier path) {
409 LOG.trace("Delete {} within Restconf PATCH: {}", datastore.name(), path);
410 writeTransaction.delete(datastore, path);
413 private void mergeDataWithinTransaction(
414 final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
415 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
416 LOG.trace("Merge {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
417 ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
419 // merging is necessary only for lists otherwise we can call put method
420 if (payload instanceof MapNode) {
421 writeTransaction.merge(datastore, path, payload);
423 writeTransaction.put(datastore, path, payload);
427 public void setDomDataBroker(final DOMDataBroker domDataBroker) {
428 this.domDataBroker = domDataBroker;
431 private void ensureParentsByMerge(final LogicalDatastoreType store,
432 final YangInstanceIdentifier normalizedPath, final DOMDataReadWriteTransaction rwTx, final SchemaContext schemaContext) {
433 final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
434 YangInstanceIdentifier rootNormalizedPath = null;
436 final Iterator<PathArgument> it = normalizedPath.getPathArguments().iterator();
438 while(it.hasNext()) {
439 final PathArgument pathArgument = it.next();
440 if(rootNormalizedPath == null) {
441 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
444 // Skip last element, its not a parent
446 normalizedPathWithoutChildArgs.add(pathArgument);
450 // No parent structure involved, no need to ensure parents
451 if(normalizedPathWithoutChildArgs.isEmpty()) {
455 Preconditions.checkArgument(rootNormalizedPath != null, "Empty path received");
457 final NormalizedNode<?, ?> parentStructure =
458 ImmutableNodes.fromInstanceId(schemaContext, YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
459 rwTx.merge(store, rootNormalizedPath, parentStructure);
462 public void registerToListenNotification(final NotificationListenerAdapter listener) {
463 checkPreconditions();
465 if (listener.isListening()) {
469 final SchemaPath path = listener.getSchemaPath();
470 final ListenerRegistration<DOMNotificationListener> registration = this.domNotification
471 .registerNotificationListener(listener, path);
473 listener.setRegistration(registration);