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