Merge "Bug 616 - Eliminate the use of xtend in md-sal/topology-lldp-discovery"
[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 org.opendaylight.controller.config.util.ConfigRegistryClient;
13 import org.opendaylight.controller.config.util.ConfigTransactionClient;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
16 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
17 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
18 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
19 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
20 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
21 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
22 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
23 import org.opendaylight.controller.netconf.util.exception.UnexpectedElementException;
24 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
27 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32
33 import javax.management.ObjectName;
34 import java.util.Set;
35
36 public class GetConfig extends AbstractConfigNetconfOperation {
37
38     public static final String GET_CONFIG = "get-config";
39
40     private final YangStoreSnapshot yangStoreSnapshot;
41     private final Optional<String> maybeNamespace;
42
43     private final TransactionProvider transactionProvider;
44
45     private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
46
47     public GetConfig(YangStoreSnapshot yangStoreSnapshot, Optional<String> maybeNamespace,
48             TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
49             String netconfSessionIdForReporting) {
50         super(configRegistryClient, netconfSessionIdForReporting);
51         this.yangStoreSnapshot = yangStoreSnapshot;
52         this.maybeNamespace = maybeNamespace;
53         this.transactionProvider = transactionProvider;
54     }
55
56     public static Datastore fromXml(XmlElement xml) throws UnexpectedNamespaceException, UnexpectedElementException, MissingNameSpaceException, NetconfDocumentedException {
57
58         xml.checkName(GET_CONFIG);
59         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
60
61         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
62                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
63         XmlElement sourceNode = sourceElement.getOnlyChildElement();
64         String sourceParsed = sourceNode.getName();
65         logger.debug("Setting source datastore to '{}'", sourceParsed);
66         Datastore sourceDatastore = Datastore.valueOf(sourceParsed);
67
68         // Filter option - unsupported
69         if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
70             throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for "
71                     + GET_CONFIG);
72
73         return sourceDatastore;
74
75     }
76
77     private Element getResponseInternal(final Document document, final ConfigRegistryClient configRegistryClient,
78             final Datastore source) throws NetconfDocumentedException {
79         Element dataElement = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
80         final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
81                 .queryInstances(configRegistryClient);
82
83         final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
84                 yangStoreSnapshot.getModuleMXBeanEntryMap()));
85
86
87         ObjectName on = transactionProvider.getOrCreateTransaction();
88         ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
89
90         ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
91         dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
92
93         logger.trace("{} operation successful", GET_CONFIG);
94
95         return dataElement;
96     }
97
98     @Override
99     protected String getOperationName() {
100         return GET_CONFIG;
101     }
102
103     @Override
104     public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
105         Datastore source;
106         source = fromXml(xml);
107         return getResponseInternal(document, configRegistryClient, source);
108     }
109 }