Merge "NECONF-524 : Setting the netconf keepalive logic to be more proactive."
[netconf.git] / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / 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.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.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
18 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
19 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
20 import org.opendaylight.netconf.api.xml.XmlElement;
21 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
24 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
25 import org.opendaylight.netconf.mdsal.connector.ops.Datastore;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32
33 public class GetConfig extends AbstractGet {
34
35     private static final Logger LOG = LoggerFactory.getLogger(GetConfig.class);
36
37     private static final String OPERATION_NAME = "get-config";
38     private final TransactionProvider transactionProvider;
39
40     public GetConfig(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext,
41                      final TransactionProvider transactionProvider) {
42         super(netconfSessionIdForReporting, schemaContext);
43         this.transactionProvider = transactionProvider;
44     }
45
46     @Override
47     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
48             throws DocumentedException {
49         GetConfigExecution getConfigExecution = null;
50         try {
51             getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
52
53         } catch (final DocumentedException e) {
54             LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
55             throw e;
56         }
57
58         final Optional<YangInstanceIdentifier> dataRootOptional = getDataRootFromFilter(operationElement);
59         if (!dataRootOptional.isPresent()) {
60             return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.absent());
61         }
62
63         final YangInstanceIdentifier dataRoot = dataRootOptional.get();
64
65         // Proper exception should be thrown
66         Preconditions.checkState(getConfigExecution.getDatastore().isPresent(), "Source element missing from request");
67
68         final DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore().get());
69         try {
70             final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(
71                     LogicalDatastoreType.CONFIGURATION, dataRoot).checkedGet();
72             if (getConfigExecution.getDatastore().get() == Datastore.running) {
73                 transactionProvider.abortRunningTransaction(rwTx);
74             }
75
76             if (!normalizedNodeOptional.isPresent()) {
77                 return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.absent());
78             }
79
80             return serializeNodeWithParentStructure(document, dataRoot, normalizedNodeOptional.get());
81         } catch (final ReadFailedException e) {
82             LOG.warn("Unable to read data: {}", dataRoot, e);
83             throw new IllegalStateException("Unable to read data " + dataRoot, e);
84         }
85     }
86
87     private DOMDataReadWriteTransaction getTransaction(final Datastore datastore) throws DocumentedException {
88         if (datastore == Datastore.candidate) {
89             return transactionProvider.getOrCreateTransaction();
90         } else if (datastore == Datastore.running) {
91             return transactionProvider.createRunningTransaction();
92         }
93         throw new DocumentedException("Incorrect Datastore: ", ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT,
94                 ErrorSeverity.ERROR);
95     }
96
97     @Override
98     protected String getOperationName() {
99         return OPERATION_NAME;
100     }
101
102 }