Merge "Bug 2731: Discard changes only when transaction exist."
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / getconfig / GetConfig.java
1 /*
2  * Copyright (c) 2013 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.confignetconfconnector.operations.getconfig;
10
11 import com.google.common.base.Optional;
12 import java.util.Set;
13 import javax.management.ObjectName;
14 import org.opendaylight.controller.config.util.ConfigRegistryClient;
15 import org.opendaylight.controller.config.util.ConfigTransactionClient;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
20 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
21 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
22 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
23 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreContext;
24 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
25 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
26 import org.opendaylight.controller.netconf.util.exception.UnexpectedElementException;
27 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
28 import org.opendaylight.controller.netconf.util.xml.XmlElement;
29 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34
35 public class GetConfig extends AbstractConfigNetconfOperation {
36
37     public static final String GET_CONFIG = "get-config";
38
39     private final YangStoreContext yangStoreSnapshot;
40     private final Optional<String> maybeNamespace;
41
42     private final TransactionProvider transactionProvider;
43
44     private static final Logger LOG = LoggerFactory.getLogger(GetConfig.class);
45
46     public GetConfig(YangStoreContext yangStoreSnapshot, Optional<String> maybeNamespace,
47             TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
48             String netconfSessionIdForReporting) {
49         super(configRegistryClient, netconfSessionIdForReporting);
50         this.yangStoreSnapshot = yangStoreSnapshot;
51         this.maybeNamespace = maybeNamespace;
52         this.transactionProvider = transactionProvider;
53     }
54
55     public static Datastore fromXml(XmlElement xml) throws UnexpectedNamespaceException, UnexpectedElementException, MissingNameSpaceException, NetconfDocumentedException {
56
57         xml.checkName(GET_CONFIG);
58         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
59
60         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
61                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
62         XmlElement sourceNode = sourceElement.getOnlyChildElement();
63         String sourceParsed = sourceNode.getName();
64         LOG.debug("Setting source datastore to '{}'", sourceParsed);
65         Datastore sourceDatastore = Datastore.valueOf(sourceParsed);
66
67         // Filter option: ignore for now, TODO only load modules specified by the filter
68
69         return sourceDatastore;
70
71     }
72
73     private Element getResponseInternal(final Document document, final ConfigRegistryClient configRegistryClient,
74             final Datastore source) {
75
76         final ConfigTransactionClient registryClient;
77         // Read current state from a transaction, if running is source, then start new transaction just for reading
78         // in case of candidate, get current transaction representing candidate
79         if(source == Datastore.running) {
80             final ObjectName readTx = transactionProvider.getOrCreateReadTransaction();
81             registryClient = getConfigRegistryClient().getConfigTransactionClient(readTx);
82         } else {
83             registryClient  = getConfigRegistryClient().getConfigTransactionClient(transactionProvider.getOrCreateTransaction());
84         }
85
86         try {
87             Element dataElement = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
88             final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
89                     .queryInstances(configRegistryClient);
90
91             final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(registryClient,
92                     yangStoreSnapshot.getModuleMXBeanEntryMap()));
93
94             ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(registryClient);
95             dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
96
97             LOG.trace("{} operation successful", GET_CONFIG);
98
99             return dataElement;
100         } finally {
101             if(source == Datastore.running) {
102                 transactionProvider.closeReadTransaction();
103             }
104         }
105     }
106
107     @Override
108     protected String getOperationName() {
109         return GET_CONFIG;
110     }
111
112     @Override
113     public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
114         Datastore source;
115         source = fromXml(xml);
116         return getResponseInternal(document, getConfigRegistryClient(), source);
117     }
118 }