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.mdsal.connector.CurrentSchemaContext;
21 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
22 import org.opendaylight.controller.netconf.mdsal.connector.ops.Datastore;
23 import org.opendaylight.controller.netconf.util.xml.XmlElement;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
31 public class GetConfig extends AbstractGet {
33 private static final Logger LOG = LoggerFactory.getLogger(GetConfig.class);
35 private static final String OPERATION_NAME = "get-config";
37 private final TransactionProvider transactionProvider;
39 public GetConfig(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
40 super(netconfSessionIdForReporting, schemaContext);
41 this.transactionProvider = transactionProvider;
45 protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
46 GetConfigExecution getConfigExecution = null;
48 getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
50 } catch (final NetconfDocumentedException e) {
51 LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
55 final YangInstanceIdentifier dataRoot = ROOT;
56 // Proper exception should be thrown
57 Preconditions.checkState(getConfigExecution.getDatastore().isPresent(), "Source element missing from request");
59 DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore().get());
61 final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.CONFIGURATION, dataRoot).checkedGet();
62 if (getConfigExecution.getDatastore().get() == Datastore.running) {
63 transactionProvider.abortRunningTransaction(rwTx);
66 return (Element) transformNormalizedNode(document, normalizedNodeOptional.get(), dataRoot);
67 } catch (ReadFailedException e) {
68 LOG.warn("Unable to read data: {}", dataRoot, e);
69 throw new IllegalStateException("Unable to read data " + dataRoot, e);
73 private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws NetconfDocumentedException{
74 if (datastore == Datastore.candidate) {
75 return transactionProvider.getOrCreateTransaction();
76 } else if (datastore == Datastore.running) {
77 return transactionProvider.createRunningTransaction();
79 throw new NetconfDocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
83 protected String getOperationName() {
84 return OPERATION_NAME;