Remove netconf from commons/opendaylight pom
[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.config.util.xml.DocumentedException;
14 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
15 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
16 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
17 import org.opendaylight.controller.config.util.xml.XmlElement;
18 import org.opendaylight.controller.config.util.xml.XmlUtil;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
22 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
24 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
25 import org.opendaylight.controller.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, final TransactionProvider transactionProvider) {
41         super(netconfSessionIdForReporting, schemaContext);
42         this.transactionProvider = transactionProvider;
43     }
44
45     @Override
46     protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws DocumentedException {
47         GetConfigExecution getConfigExecution = null;
48         try {
49             getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
50
51         } catch (final DocumentedException e) {
52             LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
53             throw e;
54         }
55
56         final Optional<YangInstanceIdentifier> dataRootOptional = getDataRootFromFilter(operationElement);
57         if (!dataRootOptional.isPresent()) {
58             return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
59         }
60
61         final YangInstanceIdentifier dataRoot = dataRootOptional.get();
62
63         // Proper exception should be thrown
64         Preconditions.checkState(getConfigExecution.getDatastore().isPresent(), "Source element missing from request");
65
66         DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore().get());
67         try {
68             final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.CONFIGURATION, dataRoot).checkedGet();
69             if (getConfigExecution.getDatastore().get() == Datastore.running) {
70                 transactionProvider.abortRunningTransaction(rwTx);
71             }
72
73             if (!normalizedNodeOptional.isPresent()) {
74                 return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
75             }
76
77             return serializeNodeWithParentStructure(document, dataRoot, normalizedNodeOptional.get());
78         } catch (ReadFailedException e) {
79             LOG.warn("Unable to read data: {}", dataRoot, e);
80             throw new IllegalStateException("Unable to read data " + dataRoot, e);
81         }
82     }
83
84     private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws DocumentedException{
85         if (datastore == Datastore.candidate) {
86             return transactionProvider.getOrCreateTransaction();
87         } else if (datastore == Datastore.running) {
88             return transactionProvider.createRunningTransaction();
89         }
90         throw new DocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
91     }
92
93     @Override
94     protected String getOperationName() {
95         return OPERATION_NAME;
96     }
97
98 }