Merge "Add prefixes to type attribute in generated xml from netconf Add support for...
[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 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;
32
33 import javax.management.ObjectName;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.Set;
37
38 public class GetConfig extends AbstractConfigNetconfOperation {
39
40     public static final String GET_CONFIG = "get-config";
41
42     private final YangStoreSnapshot yangStoreSnapshot;
43     private final Optional<String> maybeNamespace;
44
45     private final TransactionProvider transactionProvider;
46
47     private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
48
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;
56     }
57
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);
61
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);
68
69         // Filter option - unsupported
70         if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
71             throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for "
72                     + GET_CONFIG);
73
74         return sourceDatastore;
75
76     }
77
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
84         final Config configMapping = new Config(transform(configRegistryClient,
85                 yangStoreSnapshot.getModuleMXBeanEntryMap()));
86         dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement);
87
88         logger.info("{} operation successful", GET_CONFIG);
89
90         return dataElement;
91     }
92
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>>() {
98
99                     @Override
100                     public Map<String, ModuleConfig> transformEntry(String arg0, Map<String, ModuleMXBeanEntry> arg1) {
101                         return Maps.transformEntries(arg1,
102                                 new Maps.EntryTransformer<String, ModuleMXBeanEntry, ModuleConfig>() {
103
104                                     @Override
105                                     public ModuleConfig transformEntry(String key, ModuleMXBeanEntry value) {
106                                         return new ModuleConfig(key, new InstanceConfig(configRegistryClient, value
107                                                 .getAttributes()), value.getProvidedServices().values());
108                                     }
109                                 });
110                     }
111                 });
112     }
113
114     @Override
115     protected String getOperationName() {
116         return GET_CONFIG;
117     }
118
119     @Override
120     public Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
121         Datastore source;
122         try {
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);
142         }
143         return getResponseInternal(document, configRegistryClient, source);
144     }
145 }