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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
19 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
20 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
21 import org.opendaylight.controller.netconf.mdsal.connector.ops.Datastore;
22 import org.opendaylight.controller.netconf.util.xml.XmlElement;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
30 public class Get extends AbstractGet {
32 private static final Logger LOG = LoggerFactory.getLogger(Get.class);
34 private static final String OPERATION_NAME = "get";
36 private final TransactionProvider transactionProvider;
38 public Get(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
39 super(netconfSessionIdForReporting, schemaContext);
40 this.transactionProvider = transactionProvider;
44 protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
45 GetConfigExecution getConfigExecution = null;
47 getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
49 } catch (final NetconfDocumentedException e) {
50 LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
54 final YangInstanceIdentifier dataRoot = ROOT;
55 DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore());
57 final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.OPERATIONAL, dataRoot).checkedGet();
58 if (getConfigExecution.getDatastore() == Datastore.running) {
59 transactionProvider.abortRunningTransaction(rwTx);
62 return (Element) transformNormalizedNode(document, normalizedNodeOptional.get(), dataRoot);
63 } catch (ReadFailedException e) {
64 LOG.warn("Unable to read data: {}", dataRoot, e);
65 throw new IllegalStateException("Unable to read data " + dataRoot, e);
69 private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws NetconfDocumentedException{
70 if (datastore == Datastore.candidate) {
71 return transactionProvider.getOrCreateTransaction();
72 } else if (datastore == Datastore.running) {
73 return transactionProvider.createRunningTransaction();
75 throw new NetconfDocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
79 protected String getOperationName() {
80 return OPERATION_NAME;