2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.netconf.mdsal.connector.ops.get;
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;
33 public class GetConfig extends AbstractGet {
35 private static final Logger LOG = LoggerFactory.getLogger(GetConfig.class);
37 private static final String OPERATION_NAME = "get-config";
38 private final TransactionProvider transactionProvider;
40 public GetConfig(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
41 super(netconfSessionIdForReporting, schemaContext);
42 this.transactionProvider = transactionProvider;
46 protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
47 GetConfigExecution getConfigExecution = null;
49 getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
51 } catch (final NetconfDocumentedException e) {
52 LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
56 final Optional<YangInstanceIdentifier> dataRootOptional = getDataRootFromFilter(operationElement);
57 if (!dataRootOptional.isPresent()) {
58 return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
61 final YangInstanceIdentifier dataRoot = dataRootOptional.get();
63 // Proper exception should be thrown
64 Preconditions.checkState(getConfigExecution.getDatastore().isPresent(), "Source element missing from request");
66 DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore().get());
68 final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.CONFIGURATION, dataRoot).checkedGet();
69 if (getConfigExecution.getDatastore().get() == Datastore.running) {
70 transactionProvider.abortRunningTransaction(rwTx);
73 if (!normalizedNodeOptional.isPresent()) {
74 return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
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);
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();
90 throw new NetconfDocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
94 protected String getOperationName() {
95 return OPERATION_NAME;