Initial code drop of netconf protocol implementation
[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         final Config configMapping = new Config(transform(configRegistryClient,
84                 yangStoreSnapshot.getModuleMXBeanEntryMap()));
85         dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement);
86
87         logger.info("{} operation successful", GET_CONFIG);
88
89         return dataElement;
90     }
91
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>>() {
97
98                     @Override
99                     public Map<String, ModuleConfig> transformEntry(String arg0, Map<String, ModuleMXBeanEntry> arg1) {
100                         return Maps.transformEntries(arg1,
101                                 new Maps.EntryTransformer<String, ModuleMXBeanEntry, ModuleConfig>() {
102
103                                     @Override
104                                     public ModuleConfig transformEntry(String key, ModuleMXBeanEntry value) {
105                                         return new ModuleConfig(key, new InstanceConfig(configRegistryClient, value
106                                                 .getAttributes()), value.getProvidedServices().values());
107                                     }
108                                 });
109                     }
110                 });
111     }
112
113     @Override
114     protected String getOperationName() {
115         return GET_CONFIG;
116     }
117
118     @Override
119     public Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
120         Datastore source;
121         try {
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);
141         }
142         return getResponseInternal(document, configRegistryClient, source);
143     }
144 }