6eb94f2f1eb58d17837eceb8255252d2f0caec9d
[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             }
65             return (Element) transformNormalizedNode(document, normalizedNodeOptional.get(), dataRoot);
66         } catch (ReadFailedException e) {
67             LOG.warn("Unable to read data: {}", dataRoot, e);
68             throw new IllegalStateException("Unable to read data " + dataRoot, e);
69         }
70     }
71
72     private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws NetconfDocumentedException{
73         if (datastore == Datastore.candidate) {
74             return transactionProvider.getOrCreateTransaction();
75         } else if (datastore == Datastore.running) {
76             return transactionProvider.createRunningTransaction();
77         }
78         throw new NetconfDocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
79     }
80
81     @Override
82     protected String getOperationName() {
83         return OPERATION_NAME;
84     }
85
86 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.