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
8 package org.opendaylight.netconf.mdsal.connector.ops.get;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
15 import org.opendaylight.netconf.api.DocumentedException;
16 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
17 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
18 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
19 import org.opendaylight.netconf.api.xml.XmlElement;
20 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
22 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
23 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
24 import org.opendaylight.netconf.mdsal.connector.ops.Datastore;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
32 public class GetConfig extends AbstractGet {
34 private static final Logger LOG = LoggerFactory.getLogger(GetConfig.class);
36 private static final String OPERATION_NAME = "get-config";
37 private final TransactionProvider transactionProvider;
39 public GetConfig(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext,
40 final TransactionProvider transactionProvider) {
41 super(netconfSessionIdForReporting, schemaContext);
42 this.transactionProvider = transactionProvider;
46 protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
47 throws DocumentedException {
48 GetConfigExecution getConfigExecution = null;
50 getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
52 } catch (final DocumentedException e) {
53 LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
57 final Optional<YangInstanceIdentifier> dataRootOptional = getDataRootFromFilter(operationElement);
58 if (!dataRootOptional.isPresent()) {
59 return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.absent());
62 final YangInstanceIdentifier dataRoot = dataRootOptional.get();
64 // Proper exception should be thrown
65 Preconditions.checkState(getConfigExecution.getDatastore().isPresent(), "Source element missing from request");
67 final DOMDataTreeReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore().get());
69 final java.util.Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(
70 LogicalDatastoreType.CONFIGURATION, dataRoot).get();
71 if (getConfigExecution.getDatastore().get() == Datastore.running) {
72 transactionProvider.abortRunningTransaction(rwTx);
75 if (!normalizedNodeOptional.isPresent()) {
76 return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.absent());
79 return serializeNodeWithParentStructure(document, dataRoot, normalizedNodeOptional.get());
80 } catch (final InterruptedException | ExecutionException e) {
81 LOG.warn("Unable to read data: {}", dataRoot, e);
82 throw new IllegalStateException("Unable to read data " + dataRoot, e);
86 private DOMDataTreeReadWriteTransaction getTransaction(final Datastore datastore) throws DocumentedException {
87 if (datastore == Datastore.candidate) {
88 return transactionProvider.getOrCreateTransaction();
89 } else if (datastore == Datastore.running) {
90 return transactionProvider.createRunningTransaction();
92 throw new DocumentedException("Incorrect Datastore: ", ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT,
97 protected String getOperationName() {
98 return OPERATION_NAME;