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);
84 final Config configMapping = new Config(transform(configRegistryClient,
85 yangStoreSnapshot.getModuleMXBeanEntryMap()));
86 dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement);
88 logger.info("{} operation successful", GET_CONFIG);
93 // TODO refactor ... duplicate code
94 private Map<String, Map<String, ModuleConfig>> transform(final ConfigRegistryClient configRegistryClient,
95 Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
96 return Maps.transformEntries(mBeanEntries,
97 new Maps.EntryTransformer<String, Map<String, ModuleMXBeanEntry>, Map<String, ModuleConfig>>() {
100 public Map<String, ModuleConfig> transformEntry(String arg0, Map<String, ModuleMXBeanEntry> arg1) {
101 return Maps.transformEntries(arg1,
102 new Maps.EntryTransformer<String, ModuleMXBeanEntry, ModuleConfig>() {
105 public ModuleConfig transformEntry(String key, ModuleMXBeanEntry value) {
106 return new ModuleConfig(key, new InstanceConfig(configRegistryClient, value
107 .getAttributes()), value.getProvidedServices().values());
115 protected String getOperationName() {
120 public Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
123 source = fromXml(xml);
124 } catch (final IllegalArgumentException e) {
125 logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
126 final Map<String, String> errorInfo = new HashMap<>();
127 errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
128 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
129 ErrorSeverity.error, errorInfo);
130 } catch (final IllegalStateException e) {
131 logger.warn("Rpc error: {}", ErrorTag.missing_attribute, e);
132 final Map<String, String> errorInfo = new HashMap<>();
133 errorInfo.put(ErrorTag.missing_attribute.name(), "Missing datasource attribute value");
134 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.missing_attribute,
135 ErrorSeverity.error, errorInfo);
136 } catch (final UnsupportedOperationException e) {
137 logger.warn("Unsupported", e);
138 final Map<String, String> errorInfo = new HashMap<>();
139 errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for get");
140 throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
141 ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
143 return getResponseInternal(document, configRegistryClient, source);