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;
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.YangStoreSnapshot;
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;
35 public class GetConfig extends AbstractConfigNetconfOperation {
37 public static final String GET_CONFIG = "get-config";
39 private final YangStoreSnapshot yangStoreSnapshot;
40 private final Optional<String> maybeNamespace;
42 private final TransactionProvider transactionProvider;
44 private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
46 public GetConfig(YangStoreSnapshot 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;
55 public static Datastore fromXml(XmlElement xml) throws UnexpectedNamespaceException, UnexpectedElementException, MissingNameSpaceException, NetconfDocumentedException {
57 xml.checkName(GET_CONFIG);
58 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
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 logger.debug("Setting source datastore to '{}'", sourceParsed);
65 Datastore sourceDatastore = Datastore.valueOf(sourceParsed);
67 // Filter option: ignore for now, TODO only load modules specified by the filter
69 return sourceDatastore;
73 private Element getResponseInternal(final Document document, final ConfigRegistryClient configRegistryClient,
74 final Datastore source) {
75 Element dataElement = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
76 final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
77 .queryInstances(configRegistryClient);
79 final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
80 yangStoreSnapshot.getModuleMXBeanEntryMap()));
83 ObjectName on = transactionProvider.getOrCreateTransaction();
84 ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
86 ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
87 dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
89 logger.trace("{} operation successful", GET_CONFIG);
95 protected String getOperationName() {
100 public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
102 source = fromXml(xml);
103 return getResponseInternal(document, getConfigRegistryClient(), source);