Merge "BUG 2676 : Use transaction-dispatcher for ShardTransaction"
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / ops / get / Get.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 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;
29
30 public class Get extends AbstractGet {
31
32     private static final Logger LOG = LoggerFactory.getLogger(Get.class);
33
34     private static final String OPERATION_NAME = "get";
35
36     private final TransactionProvider transactionProvider;
37
38     public Get(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
39         super(netconfSessionIdForReporting, schemaContext);
40         this.transactionProvider = transactionProvider;
41     }
42
43     @Override
44     protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
45         GetConfigExecution getConfigExecution = null;
46         try {
47             getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
48
49         } catch (final NetconfDocumentedException e) {
50             LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
51             throw e;
52         }
53
54         final YangInstanceIdentifier dataRoot = ROOT;
55         DOMDataReadWriteTransaction rwTx = getTransaction(Datastore.running);
56         try {
57             final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.OPERATIONAL, dataRoot).checkedGet();
58             transactionProvider.abortRunningTransaction(rwTx);
59             return (Element) transformNormalizedNode(document, normalizedNodeOptional.get(), dataRoot);
60         } catch (ReadFailedException e) {
61             LOG.warn("Unable to read data: {}", dataRoot, e);
62             throw new IllegalStateException("Unable to read data " + dataRoot, e);
63         }
64     }
65
66     private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws NetconfDocumentedException{
67         if (datastore == Datastore.candidate) {
68             return transactionProvider.getOrCreateTransaction();
69         } else if (datastore == Datastore.running) {
70             return transactionProvider.createRunningTransaction();
71         }
72         throw new NetconfDocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
73     }
74
75     @Override
76     protected String getOperationName() {
77         return OPERATION_NAME;
78     }
79 }