2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig;
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;
33 import javax.management.ObjectName;
36 public class GetConfig extends AbstractConfigNetconfOperation {
38 public static final String GET_CONFIG = "get-config";
40 private final YangStoreSnapshot yangStoreSnapshot;
41 private final Optional<String> maybeNamespace;
43 private final TransactionProvider transactionProvider;
45 private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
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;
56 public static Datastore fromXml(XmlElement xml) throws UnexpectedNamespaceException, UnexpectedElementException, MissingNameSpaceException, NetconfDocumentedException {
58 xml.checkName(GET_CONFIG);
59 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
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);
68 // Filter option - unsupported
69 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0){
70 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for "
74 return sourceDatastore;
78 private Element getResponseInternal(final Document document, final ConfigRegistryClient configRegistryClient,
79 final Datastore source) throws NetconfDocumentedException {
80 Element dataElement = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
81 final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
82 .queryInstances(configRegistryClient);
84 final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
85 yangStoreSnapshot.getModuleMXBeanEntryMap()));
88 ObjectName on = transactionProvider.getOrCreateTransaction();
89 ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
91 ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
92 dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
94 logger.trace("{} operation successful", GET_CONFIG);
100 protected String getOperationName() {
105 public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
107 source = fromXml(xml);
108 return getResponseInternal(document, getConfigRegistryClient(), source);