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, 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(), 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 throws TransactionCommitFailedException {
142 final DOMDataReadWriteTransaction patchTransaction = this.domDataBroker.newReadWriteTransaction();
143 final List<PATCHStatusEntity> editCollection = new ArrayList<>();
144 List<RestconfError> editErrors;
145 final List<RestconfError> globalErrors = null;
146 int errorCounter = 0;
148 for (final PATCHEntity patchEntity : context.getData()) {
149 final PATCHEditOperation operation = PATCHEditOperation.valueOf(patchEntity.getOperation().toUpperCase());
153 if (errorCounter == 0) {
155 postDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
156 patchEntity.getNode(), globalSchema);
157 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
158 } catch (final RestconfDocumentedException e) {
159 editErrors = new ArrayList<>();
160 editErrors.addAll(e.getErrors());
161 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
167 if (errorCounter == 0) {
169 putDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
170 .getTargetNode(), patchEntity.getNode(), globalSchema);
171 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
172 } catch (final RestconfDocumentedException e) {
173 editErrors = new ArrayList<>();
174 editErrors.addAll(e.getErrors());
175 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
181 if (errorCounter == 0) {
183 deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
185 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
186 } catch (final RestconfDocumentedException e) {
187 editErrors = new ArrayList<>();
188 editErrors.addAll(e.getErrors());
189 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
195 if (errorCounter == 0) {
197 deleteDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity
199 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
200 } catch (final RestconfDocumentedException e) {
201 LOG.error("Error removing {} by {} operation", patchEntity.getTargetNode().toString(),
202 patchEntity.getEditId(), e);
207 if (errorCounter == 0) {
209 mergeDataWithinTransaction(patchTransaction, CONFIGURATION, patchEntity.getTargetNode(),
210 patchEntity.getNode(), globalSchema);
211 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), true, null));
212 } catch (final RestconfDocumentedException e) {
213 editErrors = new ArrayList<>();
214 editErrors.addAll(e.getErrors());
215 editCollection.add(new PATCHStatusEntity(patchEntity.getEditId(), false, editErrors));
223 //TODO: make sure possible global errors are filled up correctly and decide transaction submission based on that
224 //globalErrors = new ArrayList<>();
225 if (errorCounter == 0) {
226 patchTransaction.submit().checkedGet();
227 return new PATCHStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), true,
230 patchTransaction.cancel();
231 return new PATCHStatusContext(context.getPatchId(), ImmutableList.copyOf(editCollection), false,
236 // POST configuration
237 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
238 final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
239 checkPreconditions();
240 return postDataViaTransaction(this.domDataBroker, CONFIGURATION, path, payload, globalSchema);
243 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
244 final DOMMountPoint mountPoint, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
245 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
246 if (domDataBrokerService.isPresent()) {
247 return postDataViaTransaction(domDataBrokerService.get(), CONFIGURATION, path,
248 payload, mountPoint.getSchemaContext());
250 final String errMsg = "DOM data broker service isn't available for mount point " + path;
252 throw new RestconfDocumentedException(errMsg);
255 // DELETE configuration
256 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
257 final YangInstanceIdentifier path) {
258 checkPreconditions();
259 return deleteDataViaTransaction(this.domDataBroker.newReadWriteTransaction(), CONFIGURATION, path);
262 public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
263 final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
264 final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
265 if (domDataBrokerService.isPresent()) {
266 return deleteDataViaTransaction(domDataBrokerService.get().newReadWriteTransaction(), CONFIGURATION, path);
268 final String errMsg = "DOM data broker service isn't available for mount point " + path;
270 throw new RestconfDocumentedException(errMsg);
274 public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
275 checkPreconditions();
276 if (this.rpcService == null) {
277 throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
279 LOG.trace("Invoke RPC {} with input: {}", type, input);
280 return this.rpcService.invokeRpc(type, input);
283 public void registerToListenDataChanges(final LogicalDatastoreType datastore, final DataChangeScope scope,
284 final ListenerAdapter listener) {
285 checkPreconditions();
287 if (listener.isListening()) {
291 final YangInstanceIdentifier path = listener.getPath();
292 final ListenerRegistration<DOMDataChangeListener> registration = this.domDataBroker.registerDataChangeListener(
293 datastore, path, listener, scope);
295 listener.setRegistration(registration);
298 private NormalizedNode<?, ?> readDataViaTransaction(final DOMDataReadTransaction transaction,
299 final LogicalDatastoreType datastore, final YangInstanceIdentifier path) {
300 LOG.trace("Read {} via Restconf: {}", datastore.name(), path);
301 final ListenableFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = transaction.read(datastore, path);
302 if (listenableFuture != null) {
303 Optional<NormalizedNode<?, ?>> optional;
305 LOG.debug("Reading result data from transaction.");
306 optional = listenableFuture.get();
307 } catch (InterruptedException | ExecutionException e) {
308 LOG.warn("Exception by reading {} via Restconf: {}", datastore.name(), path, e);
309 throw new RestconfDocumentedException("Problem to get data from transaction.", e.getCause());
312 if (optional != null) {
313 if (optional.isPresent()) {
314 return optional.get();
321 private CheckedFuture<Void, TransactionCommitFailedException> postDataViaTransaction(
322 final DOMDataBroker domDataBroker, final LogicalDatastoreType datastore,
323 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
324 // FIXME: This is doing correct post for container and list children
325 // not sure if this will work for choice case
326 DOMDataReadWriteTransaction transaction = domDataBroker.newReadWriteTransaction();
327 if(payload instanceof MapNode) {
328 LOG.trace("POST {} via Restconf: {} with payload {}", datastore.name(), path, payload);
329 final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
331 transaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
332 } catch (RuntimeException e) {
333 // FIXME: Figure out and catch specific RunTimeExceptions thrown by NETCONF instead of generic one.
334 // to make this cleaner and easier to maintain.
335 transaction.cancel();
336 transaction = domDataBroker.newReadWriteTransaction();
337 LOG.debug("Empty subtree merge failed", e);
339 if (!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
340 transaction.cancel();
341 transaction = domDataBroker.newReadWriteTransaction();
343 for(final MapEntryNode child : ((MapNode) payload).getValue()) {
344 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
345 checkItemDoesNotExists(transaction, datastore, childPath);
346 transaction.put(datastore, childPath, child);
349 checkItemDoesNotExists(transaction,datastore, path);
350 if(!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
351 transaction.cancel();
352 transaction = domDataBroker.newReadWriteTransaction();
354 transaction.put(datastore, path, payload);
356 return transaction.submit();
359 private void postDataWithinTransaction(
360 final DOMDataReadWriteTransaction rWTransaction, final LogicalDatastoreType datastore,
361 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
362 // FIXME: This is doing correct post for container and list children
363 // not sure if this will work for choice case
364 if(payload instanceof MapNode) {
365 LOG.trace("POST {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
366 final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
367 rWTransaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
368 ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
369 for(final MapEntryNode child : ((MapNode) payload).getValue()) {
370 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
371 checkItemDoesNotExists(rWTransaction, datastore, childPath);
372 rWTransaction.put(datastore, childPath, child);
375 checkItemDoesNotExists(rWTransaction,datastore, path);
376 ensureParentsByMerge(datastore, path, rWTransaction, schemaContext);
377 rWTransaction.put(datastore, path, payload);
381 private void checkItemExists(final DOMDataReadWriteTransaction rWTransaction,
382 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
383 final ListenableFuture<Boolean> futureDatastoreData = rWTransaction.exists(store, path);
385 if (!futureDatastoreData.get()) {
386 final String errMsg = "Operation via Restconf was not executed because data does not exist";
387 LOG.trace("{}:{}", errMsg, path);
388 rWTransaction.cancel();
389 throw new RestconfDocumentedException("Data does not exist for path: " + path, ErrorType.PROTOCOL,
390 ErrorTag.DATA_MISSING);
392 } catch (InterruptedException | ExecutionException e) {
393 LOG.warn("It wasn't possible to get data loaded from datastore at path {}", path, e);
397 private void checkItemDoesNotExists(final DOMDataReadWriteTransaction rWTransaction,
398 final LogicalDatastoreType store, final YangInstanceIdentifier path) {
399 final ListenableFuture<Boolean> futureDatastoreData = rWTransaction.exists(store, path);
401 if (futureDatastoreData.get()) {
402 final String errMsg = "Operation via Restconf was not executed because data already exists";
403 LOG.trace("{}:{}", errMsg, path);
404 rWTransaction.cancel();
405 throw new RestconfDocumentedException("Data already exists for path: " + path, ErrorType.PROTOCOL,
406 ErrorTag.DATA_EXISTS);
408 } catch (InterruptedException | ExecutionException e) {
409 LOG.warn("It wasn't possible to get data loaded from datastore at path {}", path, e);
413 private CheckedFuture<Void, TransactionCommitFailedException> putDataViaTransaction(
414 final DOMDataBroker domDataBroker, final LogicalDatastoreType datastore,
415 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext)
417 DOMDataReadWriteTransaction transaction = domDataBroker.newReadWriteTransaction();
418 LOG.trace("Put {} via Restconf: {} with payload {}", datastore.name(), path, payload);
419 if (!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
420 transaction.cancel();
421 transaction = domDataBroker.newReadWriteTransaction();
423 transaction.put(datastore, path, payload);
424 return transaction.submit();
427 private void putDataWithinTransaction(
428 final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
429 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
430 LOG.trace("Put {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
431 ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
432 writeTransaction.put(datastore, path, payload);
435 private CheckedFuture<Void, TransactionCommitFailedException> deleteDataViaTransaction(
436 final DOMDataReadWriteTransaction readWriteTransaction, final LogicalDatastoreType datastore,
437 final YangInstanceIdentifier path) {
438 LOG.trace("Delete {} via Restconf: {}", datastore.name(), path);
439 checkItemExists(readWriteTransaction, datastore, path);
440 readWriteTransaction.delete(datastore, path);
441 return readWriteTransaction.submit();
444 private void deleteDataWithinTransaction(
445 final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
446 final YangInstanceIdentifier path) {
447 LOG.trace("Delete {} within Restconf PATCH: {}", datastore.name(), path);
448 writeTransaction.delete(datastore, path);
451 private void mergeDataWithinTransaction(
452 final DOMDataReadWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
453 final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
454 LOG.trace("Merge {} within Restconf PATCH: {} with payload {}", datastore.name(), path, payload);
455 ensureParentsByMerge(datastore, path, writeTransaction, schemaContext);
457 // merging is necessary only for lists otherwise we can call put method
458 if (payload instanceof MapNode) {
459 writeTransaction.merge(datastore, path, payload);
461 writeTransaction.put(datastore, path, payload);
465 public void setDomDataBroker(final DOMDataBroker domDataBroker) {
466 this.domDataBroker = domDataBroker;
469 private boolean ensureParentsByMerge(final LogicalDatastoreType store,
470 final YangInstanceIdentifier normalizedPath, final DOMDataReadWriteTransaction rwTx, final SchemaContext schemaContext) {
472 boolean mergeResult = true;
473 final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
474 YangInstanceIdentifier rootNormalizedPath = null;
476 final Iterator<PathArgument> it = normalizedPath.getPathArguments().iterator();
478 while(it.hasNext()) {
479 final PathArgument pathArgument = it.next();
480 if(rootNormalizedPath == null) {
481 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
484 // Skip last element, its not a parent
486 normalizedPathWithoutChildArgs.add(pathArgument);
490 // No parent structure involved, no need to ensure parents
491 if(normalizedPathWithoutChildArgs.isEmpty()) {
495 Preconditions.checkArgument(rootNormalizedPath != null, "Empty path received");
497 final NormalizedNode<?, ?> parentStructure =
498 ImmutableNodes.fromInstanceId(schemaContext, YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
500 rwTx.merge(store, rootNormalizedPath, parentStructure);
501 } catch (RuntimeException e) {
503 * Catching the exception here, logging it and proceeding further
504 * for the following reasons.
506 * 1. For MD-SAL store if it fails we'll go with the next call
507 * anyway and let the failure happen there. 2. For NETCONF devices
508 * that can not handle these calls such as creation of empty lists
509 * etc, instead of failing we'll go with the actual call. Devices
510 * should be able to handle the actual calls made without the need
511 * to create parents. So instead of failing we will give a device a
512 * chance to configure the management entity in question. 3. If this
513 * merge call is handled properly by MD-SAL data store or a Netconf
514 * device this is a no-op.
516 // FIXME: Figure out and catch specific RunTimeExceptions thrown by NETCONF instead of generic one.
517 // to make this cleaner and easier to maintain.
519 LOG.debug("Exception while creating the parent in ensureParentsByMerge. Proceeding with the actual request", e);
524 public void registerToListenNotification(final NotificationListenerAdapter listener) {
525 checkPreconditions();
527 if (listener.isListening()) {
531 final SchemaPath path = listener.getSchemaPath();
532 final ListenerRegistration<DOMNotificationListener> registration = this.domNotification
533 .registerNotificationListener((DOMNotificationListener) listener, path);
535 listener.setRegistration(registration);