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 com.google.common.collect.Maps;
13 import org.opendaylight.controller.config.util.ConfigRegistryClient;
14 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
15 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
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.confignetconfconnector.mapping.config.Config;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleConfig;
23 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
24 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
25 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
26 import org.opendaylight.controller.netconf.util.xml.XmlElement;
27 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
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;
34 import java.util.HashMap;
38 public class GetConfig extends AbstractConfigNetconfOperation {
40 public static final String GET_CONFIG = "get-config";
42 private final YangStoreSnapshot yangStoreSnapshot;
43 private final Optional<String> maybeNamespace;
45 private final TransactionProvider transactionProvider;
47 private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
49 public GetConfig(YangStoreSnapshot yangStoreSnapshot, Optional<String> maybeNamespace,
50 TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
51 String netconfSessionIdForReporting) {
52 super(configRegistryClient, netconfSessionIdForReporting);
53 this.yangStoreSnapshot = yangStoreSnapshot;
54 this.maybeNamespace = maybeNamespace;
55 this.transactionProvider = transactionProvider;
58 public static Datastore fromXml(XmlElement xml) {
59 xml.checkName(GET_CONFIG);
60 xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
62 XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
63 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
64 XmlElement sourceNode = sourceElement.getOnlyChildElement();
65 String sourceParsed = sourceNode.getName();
66 logger.debug("Setting source datastore to '{}'", sourceParsed);
67 Datastore sourceDatastore = Datastore.valueOf(sourceParsed);
69 // Filter option - unsupported
70 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
71 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 = document.createElement(XmlNetconfConstants.DATA_KEY);
81 final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
82 .queryInstances(configRegistryClient);
83 final Config configMapping = new Config(transform(configRegistryClient,
84 yangStoreSnapshot.getModuleMXBeanEntryMap()));
85 dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement);
87 logger.info("{} operation successful", GET_CONFIG);
92 // TODO refactor ... duplicate code
93 private Map<String, Map<String, ModuleConfig>> transform(final ConfigRegistryClient configRegistryClient,
94 Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
95 return Maps.transformEntries(mBeanEntries,
96 new Maps.EntryTransformer<String, Map<String, ModuleMXBeanEntry>, Map<String, ModuleConfig>>() {
99 public Map<String, ModuleConfig> transformEntry(String arg0, Map<String, ModuleMXBeanEntry> arg1) {
100 return Maps.transformEntries(arg1,
101 new Maps.EntryTransformer<String, ModuleMXBeanEntry, ModuleConfig>() {
104 public ModuleConfig transformEntry(String key, ModuleMXBeanEntry value) {
105 return new ModuleConfig(key, new InstanceConfig(configRegistryClient, value
106 .getAttributes()), value.getProvidedServices().values());
114 protected String getOperationName() {
119 public Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
122 source = fromXml(xml);
123 } catch (final IllegalArgumentException e) {
124 logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
125 final Map<String, String> errorInfo = new HashMap<>();
126 errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
127 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
128 ErrorSeverity.error, errorInfo);
129 } catch (final IllegalStateException e) {
130 logger.warn("Rpc error: {}", ErrorTag.missing_attribute, e);
131 final Map<String, String> errorInfo = new HashMap<>();
132 errorInfo.put(ErrorTag.missing_attribute.name(), "Missing datasource attribute value");
133 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.missing_attribute,
134 ErrorSeverity.error, errorInfo);
135 } catch (final UnsupportedOperationException e) {
136 logger.warn("Unsupported", e);
137 final Map<String, String> errorInfo = new HashMap<>();
138 errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for get");
139 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
140 ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
142 return getResponseInternal(document, configRegistryClient, source);