0a5409cb10cde37af69f37fef0f08e450a5295a4
[netconf.git] / opendaylight / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / restconf / impl / BrokerFacade.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, 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.netconf.sal.restconf.impl;
9
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;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import javax.ws.rs.core.Response.Status;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
31 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
32 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
33 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
36 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class BrokerFacade {
50     private final static Logger LOG = LoggerFactory.getLogger(BrokerFacade.class);
51
52     private final static BrokerFacade INSTANCE = new BrokerFacade();
53     private volatile DOMRpcService rpcService;
54     private volatile ConsumerSession context;
55     private DOMDataBroker domDataBroker;
56
57     private BrokerFacade() {
58     }
59
60     public void setRpcService(final DOMRpcService router) {
61         rpcService = router;
62     }
63
64     public void setContext(final ConsumerSession context) {
65         this.context = context;
66     }
67
68     public static BrokerFacade getInstance() {
69         return BrokerFacade.INSTANCE;
70     }
71
72     private void checkPreconditions() {
73         if (context == null || domDataBroker == null) {
74             throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
75         }
76     }
77
78     // READ configuration
79     public NormalizedNode<?, ?> readConfigurationData(final YangInstanceIdentifier path) {
80         checkPreconditions();
81         return readDataViaTransaction(domDataBroker.newReadOnlyTransaction(), CONFIGURATION, path);
82     }
83
84     public NormalizedNode<?, ?> readConfigurationData(final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
85         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
86         if (domDataBrokerService.isPresent()) {
87             return readDataViaTransaction(domDataBrokerService.get().newReadOnlyTransaction(), CONFIGURATION, path);
88         }
89         final String errMsg = "DOM data broker service isn't available for mount point " + path;
90         LOG.warn(errMsg);
91         throw new RestconfDocumentedException(errMsg);
92     }
93
94     // READ operational
95     public NormalizedNode<?, ?> readOperationalData(final YangInstanceIdentifier path) {
96         checkPreconditions();
97         return readDataViaTransaction(domDataBroker.newReadOnlyTransaction(), OPERATIONAL, path);
98     }
99
100     public NormalizedNode<?, ?> readOperationalData(final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
101         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
102         if (domDataBrokerService.isPresent()) {
103             return readDataViaTransaction(domDataBrokerService.get().newReadOnlyTransaction(), OPERATIONAL, path);
104         }
105         final String errMsg = "DOM data broker service isn't available for mount point " + path;
106         LOG.warn(errMsg);
107         throw new RestconfDocumentedException(errMsg);
108     }
109
110     // PUT configuration
111     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPut(
112             final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
113         checkPreconditions();
114         return putDataViaTransaction(domDataBroker, CONFIGURATION, path, payload, globalSchema);
115     }
116
117     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPut(
118             final DOMMountPoint mountPoint, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
119         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
120         if (domDataBrokerService.isPresent()) {
121             return putDataViaTransaction(domDataBrokerService.get(), CONFIGURATION, path,
122                     payload, mountPoint.getSchemaContext());
123         }
124         final String errMsg = "DOM data broker service isn't available for mount point " + path;
125         LOG.warn(errMsg);
126         throw new RestconfDocumentedException(errMsg);
127     }
128
129     // POST configuration
130     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
131             final SchemaContext globalSchema, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
132         checkPreconditions();
133         return postDataViaTransaction(domDataBroker, CONFIGURATION, path, payload, globalSchema);
134     }
135
136     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataPost(
137             final DOMMountPoint mountPoint, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload) {
138         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
139         if (domDataBrokerService.isPresent()) {
140             return postDataViaTransaction(domDataBrokerService.get(), CONFIGURATION, path,
141                     payload, mountPoint.getSchemaContext());
142         }
143         final String errMsg = "DOM data broker service isn't available for mount point " + path;
144         LOG.warn(errMsg);
145         throw new RestconfDocumentedException(errMsg);
146     }
147
148     // DELETE configuration
149     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
150             final YangInstanceIdentifier path) {
151         checkPreconditions();
152         return deleteDataViaTransaction(domDataBroker.newWriteOnlyTransaction(), CONFIGURATION, path);
153     }
154
155     public CheckedFuture<Void, TransactionCommitFailedException> commitConfigurationDataDelete(
156             final DOMMountPoint mountPoint, final YangInstanceIdentifier path) {
157         final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
158         if (domDataBrokerService.isPresent()) {
159             return deleteDataViaTransaction(domDataBrokerService.get().newWriteOnlyTransaction(), CONFIGURATION, path);
160         }
161         final String errMsg = "DOM data broker service isn't available for mount point " + path;
162         LOG.warn(errMsg);
163         throw new RestconfDocumentedException(errMsg);
164     }
165
166     // RPC
167     public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
168         checkPreconditions();
169         if (rpcService == null) {
170             throw new RestconfDocumentedException(Status.SERVICE_UNAVAILABLE);
171         }
172         LOG.trace("Invoke RPC {} with input: {}", type, input);
173         return rpcService.invokeRpc(type, input);
174     }
175
176     public void registerToListenDataChanges(final LogicalDatastoreType datastore, final DataChangeScope scope,
177             final ListenerAdapter listener) {
178         checkPreconditions();
179
180         if (listener.isListening()) {
181             return;
182         }
183
184         final YangInstanceIdentifier path = listener.getPath();
185         final ListenerRegistration<DOMDataChangeListener> registration = domDataBroker.registerDataChangeListener(
186                 datastore, path, listener, scope);
187
188         listener.setRegistration(registration);
189     }
190
191     private NormalizedNode<?, ?> readDataViaTransaction(final DOMDataReadTransaction transaction,
192             final LogicalDatastoreType datastore, final YangInstanceIdentifier path) {
193         LOG.trace("Read " + datastore.name() + " via Restconf: {}", path);
194         final ListenableFuture<Optional<NormalizedNode<?, ?>>> listenableFuture = transaction.read(datastore, path);
195         if (listenableFuture != null) {
196             Optional<NormalizedNode<?, ?>> optional;
197             try {
198                 LOG.debug("Reading result data from transaction.");
199                 optional = listenableFuture.get();
200             } catch (InterruptedException | ExecutionException e) {
201                 LOG.warn("Exception by reading " + datastore.name() + " via Restconf: {}", path, e);
202                 throw new RestconfDocumentedException("Problem to get data from transaction.", e.getCause());
203
204             }
205             if (optional != null) {
206                 if (optional.isPresent()) {
207                     return optional.get();
208                 }
209             }
210         }
211         return null;
212     }
213
214     private CheckedFuture<Void, TransactionCommitFailedException> postDataViaTransaction(
215             final DOMDataBroker domDataBroker, final LogicalDatastoreType datastore,
216             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
217         // FIXME: This is doing correct post for container and list children
218         //        not sure if this will work for choice case
219         DOMDataReadWriteTransaction transaction = domDataBroker.newReadWriteTransaction();
220         if(payload instanceof MapNode) {
221             LOG.trace("POST " + datastore.name() + " via Restconf: {} with payload {}", path, payload);
222             final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
223             try {
224                 transaction.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
225             } catch (RuntimeException e) {
226                 // FIXME: Figure out and catch specific RunTimeExceptions thrown by NETCONF instead of generic one.
227                 //        to make this cleaner and easier to maintain.
228                 transaction.cancel();
229                 transaction = domDataBroker.newReadWriteTransaction();
230                 LOG.debug("Empty subtree merge failed", e);
231             }
232             if (!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
233                 transaction.cancel();
234                 transaction = domDataBroker.newReadWriteTransaction();
235             }
236             for(final MapEntryNode child : ((MapNode) payload).getValue()) {
237                 final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
238                 checkItemDoesNotExists(transaction, datastore, childPath);
239                 transaction.put(datastore, childPath, child);
240             }
241         } else {
242             checkItemDoesNotExists(transaction,datastore, path);
243             if(!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
244                 transaction.cancel();
245                 transaction = domDataBroker.newReadWriteTransaction();
246             }
247             transaction.put(datastore, path, payload);
248         }
249         return transaction.submit();
250     }
251
252     private void checkItemDoesNotExists(final DOMDataReadWriteTransaction rWTransaction,final LogicalDatastoreType store, final YangInstanceIdentifier path) {
253         final ListenableFuture<Boolean> futureDatastoreData = rWTransaction.exists(store, path);
254         try {
255             if (futureDatastoreData.get()) {
256                 final String errMsg = "Post Configuration via Restconf was not executed because data already exists";
257                 LOG.trace(errMsg + ":{}", path);
258                 rWTransaction.cancel();
259                 throw new RestconfDocumentedException("Data already exists for path: " + path, ErrorType.PROTOCOL,
260                         ErrorTag.DATA_EXISTS);
261             }
262         } catch (InterruptedException | ExecutionException e) {
263             LOG.warn("It wasn't possible to get data loaded from datastore at path " + path, e);
264         }
265
266     }
267
268     private CheckedFuture<Void, TransactionCommitFailedException> putDataViaTransaction(
269             final DOMDataBroker domDataBroker, final LogicalDatastoreType datastore,
270             final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext)
271     {
272         DOMDataReadWriteTransaction transaction = domDataBroker.newReadWriteTransaction();
273         LOG.trace("Put " + datastore.name() + " via Restconf: {} with payload {}", path, payload);
274         if (!ensureParentsByMerge(datastore, path, transaction, schemaContext)) {
275             transaction.cancel();
276             transaction = domDataBroker.newReadWriteTransaction();
277         }
278         transaction.put(datastore, path, payload);
279         return transaction.submit();
280     }
281
282     private CheckedFuture<Void, TransactionCommitFailedException> deleteDataViaTransaction(
283             final DOMDataWriteTransaction writeTransaction, final LogicalDatastoreType datastore,
284             final YangInstanceIdentifier path) {
285         LOG.trace("Delete " + datastore.name() + " via Restconf: {}", path);
286         writeTransaction.delete(datastore, path);
287         return writeTransaction.submit();
288     }
289
290     public void setDomDataBroker(final DOMDataBroker domDataBroker) {
291         this.domDataBroker = domDataBroker;
292     }
293
294     private boolean ensureParentsByMerge(final LogicalDatastoreType store,
295                                       final YangInstanceIdentifier normalizedPath, final DOMDataReadWriteTransaction rwTx, final SchemaContext schemaContext) {
296
297         boolean mergeResult = true;
298         final List<PathArgument> normalizedPathWithoutChildArgs = new ArrayList<>();
299         YangInstanceIdentifier rootNormalizedPath = null;
300
301         final Iterator<PathArgument> it = normalizedPath.getPathArguments().iterator();
302
303         while(it.hasNext()) {
304             final PathArgument pathArgument = it.next();
305             if(rootNormalizedPath == null) {
306                 rootNormalizedPath = YangInstanceIdentifier.create(pathArgument);
307             }
308
309             // Skip last element, its not a parent
310             if(it.hasNext()) {
311                 normalizedPathWithoutChildArgs.add(pathArgument);
312             }
313         }
314
315         // No parent structure involved, no need to ensure parents
316         if(normalizedPathWithoutChildArgs.isEmpty()) {
317             return mergeResult;
318         }
319
320         Preconditions.checkArgument(rootNormalizedPath != null, "Empty path received");
321
322         final NormalizedNode<?, ?> parentStructure =
323                 ImmutableNodes.fromInstanceId(schemaContext, YangInstanceIdentifier.create(normalizedPathWithoutChildArgs));
324         try {
325             rwTx.merge(store, rootNormalizedPath, parentStructure);
326         } catch (RuntimeException e) {
327             /*
328              * Catching the exception here, logging it and proceeding further
329              * for the following reasons.
330              *
331              * 1. For MD-SAL store if it fails we'll go with the next call
332              * anyway and let the failure happen there. 2. For NETCONF devices
333              * that can not handle these calls such as creation of empty lists
334              * etc, instead of failing we'll go with the actual call. Devices
335              * should be able to handle the actual calls made without the need
336              * to create parents. So instead of failing we will give a device a
337              * chance to configure the management entity in question. 3. If this
338              * merge call is handled properly by MD-SAL data store or a Netconf
339              * device this is a no-op.
340              */
341              // FIXME: Figure out and catch specific RunTimeExceptions thrown by NETCONF instead of generic one.
342              //        to make this cleaner and easier to maintain.
343             mergeResult = false;
344             LOG.debug("Exception while creating the parent in ensureParentsByMerge. Proceeding with the actual request", e);
345         }
346         return mergeResult;
347     }
348 }