Improve NETCONF session ID handling
[netconf.git] / plugins / netconf-server-mdsal / 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 java.util.Optional;
11 import java.util.concurrent.ExecutionException;
12 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
13 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
14 import org.opendaylight.netconf.api.DocumentedException;
15 import org.opendaylight.netconf.api.xml.XmlElement;
16 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
17 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
18 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
19 import org.opendaylight.netconf.mdsal.connector.ops.Datastore;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
21 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.common.ErrorType;
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     private static final Logger LOG = LoggerFactory.getLogger(GetConfig.class);
33     private static final String OPERATION_NAME = "get-config";
34
35     private final TransactionProvider transactionProvider;
36
37     public GetConfig(final SessionIdType sessionId, final CurrentSchemaContext schemaContext,
38             final TransactionProvider transactionProvider) {
39         super(sessionId, schemaContext);
40         this.transactionProvider = transactionProvider;
41     }
42
43     @Override
44     protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement)
45             throws DocumentedException {
46         GetConfigExecution getConfigExecution = null;
47         try {
48             getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
49
50         } catch (final DocumentedException e) {
51             LOG.warn("Get request processing failed on session: {}", sessionId().getValue(), e);
52             throw e;
53         }
54
55         final Optional<YangInstanceIdentifier> dataRootOptional = getDataRootFromFilter(operationElement);
56         if (dataRootOptional.isEmpty()) {
57             return document.createElement(XmlNetconfConstants.DATA_KEY);
58         }
59
60         final YangInstanceIdentifier dataRoot = dataRootOptional.orElseThrow();
61
62         // FIXME: Proper exception should be thrown
63         final var datastore = getConfigExecution.getDatastore()
64             .orElseThrow(() -> new IllegalStateException("Source element missing from request"));
65
66         final DOMDataTreeReadWriteTransaction rwTx = getTransaction(datastore);
67         try {
68             final Optional<NormalizedNode> normalizedNodeOptional = rwTx.read(
69                     LogicalDatastoreType.CONFIGURATION, dataRoot).get();
70             if (datastore == Datastore.running) {
71                 transactionProvider.abortRunningTransaction(rwTx);
72             }
73
74             if (normalizedNodeOptional.isEmpty()) {
75                 return document.createElement(XmlNetconfConstants.DATA_KEY);
76             }
77
78             return serializeNodeWithParentStructure(document, dataRoot, normalizedNodeOptional.orElseThrow());
79         } catch (final InterruptedException | ExecutionException e) {
80             LOG.warn("Unable to read data: {}", dataRoot, e);
81             throw new IllegalStateException("Unable to read data " + dataRoot, e);
82         }
83     }
84
85     private DOMDataTreeReadWriteTransaction getTransaction(final Datastore datastore) throws DocumentedException {
86         if (datastore == Datastore.candidate) {
87             return transactionProvider.getOrCreateTransaction();
88         } else if (datastore == Datastore.running) {
89             return transactionProvider.createRunningTransaction();
90         }
91         throw new DocumentedException("Incorrect Datastore: ", ErrorType.PROTOCOL, ErrorTag.BAD_ELEMENT,
92                 ErrorSeverity.ERROR);
93     }
94
95     @Override
96     protected String getOperationName() {
97         return OPERATION_NAME;
98     }
99 }