Merge "BUG 2718 : Create a diagnostic utility to track append entries replies"
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / ops / get / GetConfig.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 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;
30
31 public class GetConfig extends AbstractGet {
32
33     private static final Logger LOG = LoggerFactory.getLogger(GetConfig.class);
34
35     private static final String OPERATION_NAME = "get-config";
36
37     private final TransactionProvider transactionProvider;
38
39     public GetConfig(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
40         super(netconfSessionIdForReporting, schemaContext);
41         this.transactionProvider = transactionProvider;
42     }
43
44     @Override
45     protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
46         GetConfigExecution getConfigExecution = null;
47         try {
48             getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
49
50         } catch (final NetconfDocumentedException e) {
51             LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
52             throw e;
53         }
54
55         final YangInstanceIdentifier dataRoot = ROOT;
56         // Proper exception should be thrown
57         Preconditions.checkState(getConfigExecution.getDatastore().isPresent(), "Source element missing from request");
58
59         DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore().get());
60         try {
61             final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.CONFIGURATION, dataRoot).checkedGet();
62             if (getConfigExecution.getDatastore().get() == Datastore.running) {
63                 transactionProvider.abortRunningTransaction(rwTx);
64                 rwTx = null;
65             }
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);
70         }
71     }
72
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();
78         }
79         throw new NetconfDocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
80     }
81
82     @Override
83     protected String getOperationName() {
84         return OPERATION_NAME;
85     }
86
87 }