BUG-2801: Added filtering for get and getConfig in netconf mdsal northbound.
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / ops / get / GetConfig.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.netconf.mdsal.connector.ops.get;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
20 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
22 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
23 import org.opendaylight.controller.netconf.mdsal.connector.ops.Datastore;
24 import org.opendaylight.controller.netconf.util.xml.XmlElement;
25 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32
33 public class GetConfig extends AbstractGet {
34
35     private static final Logger LOG = LoggerFactory.getLogger(GetConfig.class);
36
37     private static final String OPERATION_NAME = "get-config";
38     private final TransactionProvider transactionProvider;
39
40     public GetConfig(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
41         super(netconfSessionIdForReporting, schemaContext);
42         this.transactionProvider = transactionProvider;
43     }
44
45     @Override
46     protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
47         GetConfigExecution getConfigExecution = null;
48         try {
49             getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
50
51         } catch (final NetconfDocumentedException e) {
52             LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
53             throw e;
54         }
55
56         final Optional<YangInstanceIdentifier> dataRootOptional = getDataRootFromFilter(operationElement);
57         if (!dataRootOptional.isPresent()) {
58             return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
59         }
60
61         final YangInstanceIdentifier dataRoot = dataRootOptional.get();
62
63         // Proper exception should be thrown
64         Preconditions.checkState(getConfigExecution.getDatastore().isPresent(), "Source element missing from request");
65
66         DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore().get());
67         try {
68             final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.CONFIGURATION, dataRoot).checkedGet();
69             if (getConfigExecution.getDatastore().get() == Datastore.running) {
70                 transactionProvider.abortRunningTransaction(rwTx);
71             }
72
73             if (!normalizedNodeOptional.isPresent()) {
74                 return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
75             }
76
77             return serializeNodeWithParentStructure(document, dataRoot, normalizedNodeOptional.get());
78         } catch (ReadFailedException e) {
79             LOG.warn("Unable to read data: {}", dataRoot, e);
80             throw new IllegalStateException("Unable to read data " + dataRoot, e);
81         }
82     }
83
84     private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws NetconfDocumentedException{
85         if (datastore == Datastore.candidate) {
86             return transactionProvider.getOrCreateTransaction();
87         } else if (datastore == Datastore.running) {
88             return transactionProvider.createRunningTransaction();
89         }
90         throw new NetconfDocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
91     }
92
93     @Override
94     protected String getOperationName() {
95         return OPERATION_NAME;
96     }
97
98 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.