d75cfd5d6f049d9d586ac856fa0ccd7b70b3c00c
[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.util.ConfigTransactionClient;
15 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
16 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
20 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.InstanceConfig;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleConfig;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
25 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
26 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
27 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
28 import org.opendaylight.controller.netconf.util.xml.XmlElement;
29 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34
35 import javax.management.ObjectName;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.Set;
39
40 public class GetConfig extends AbstractConfigNetconfOperation {
41
42     public static final String GET_CONFIG = "get-config";
43
44     private final YangStoreSnapshot yangStoreSnapshot;
45     private final Optional<String> maybeNamespace;
46
47     private final TransactionProvider transactionProvider;
48
49     private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
50
51     public GetConfig(YangStoreSnapshot yangStoreSnapshot, Optional<String> maybeNamespace,
52             TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
53             String netconfSessionIdForReporting) {
54         super(configRegistryClient, netconfSessionIdForReporting);
55         this.yangStoreSnapshot = yangStoreSnapshot;
56         this.maybeNamespace = maybeNamespace;
57         this.transactionProvider = transactionProvider;
58     }
59
60     public static Datastore fromXml(XmlElement xml) {
61         xml.checkName(GET_CONFIG);
62         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
63
64         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
65                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
66         XmlElement sourceNode = sourceElement.getOnlyChildElement();
67         String sourceParsed = sourceNode.getName();
68         logger.debug("Setting source datastore to '{}'", sourceParsed);
69         Datastore sourceDatastore = Datastore.valueOf(sourceParsed);
70
71         // Filter option - unsupported
72         if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
73             throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for "
74                     + GET_CONFIG);
75
76         return sourceDatastore;
77
78     }
79
80     private Element getResponseInternal(final Document document, final ConfigRegistryClient configRegistryClient,
81             final Datastore source) throws NetconfDocumentedException {
82         Element dataElement = document.createElement(XmlNetconfConstants.DATA_KEY);
83         final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
84                 .queryInstances(configRegistryClient);
85
86         final Config configMapping = new Config(transform(configRegistryClient,
87                 yangStoreSnapshot.getModuleMXBeanEntryMap()));
88
89
90         ObjectName on = transactionProvider.getOrCreateTransaction();
91         ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
92
93         Services serviceTracker = new Services(ta);
94         dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
95
96         logger.info("{} operation successful", GET_CONFIG);
97
98         return dataElement;
99     }
100
101     // TODO refactor ... duplicate code
102     public static Map<String, Map<String, ModuleConfig>> transform(final ConfigRegistryClient configRegistryClient,
103             Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries) {
104         return Maps.transformEntries(mBeanEntries,
105                 new Maps.EntryTransformer<String, Map<String, ModuleMXBeanEntry>, Map<String, ModuleConfig>>() {
106
107                     @Override
108                     public Map<String, ModuleConfig> transformEntry(String arg0, Map<String, ModuleMXBeanEntry> arg1) {
109                         return Maps.transformEntries(arg1,
110                                 new Maps.EntryTransformer<String, ModuleMXBeanEntry, ModuleConfig>() {
111
112                                     @Override
113                                     public ModuleConfig transformEntry(String key, ModuleMXBeanEntry value) {
114                                         return new ModuleConfig(key, new InstanceConfig(configRegistryClient, value
115                                                 .getAttributes()), value.getProvidedServices().values());
116                                     }
117                                 });
118                     }
119                 });
120     }
121
122     @Override
123     protected String getOperationName() {
124         return GET_CONFIG;
125     }
126
127     @Override
128     public Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
129         Datastore source;
130         try {
131             source = fromXml(xml);
132         } catch (final IllegalArgumentException e) {
133             logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
134             final Map<String, String> errorInfo = new HashMap<>();
135             errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
136             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
137                     ErrorSeverity.error, errorInfo);
138         } catch (final IllegalStateException e) {
139             logger.warn("Rpc error: {}", ErrorTag.missing_attribute, e);
140             final Map<String, String> errorInfo = new HashMap<>();
141             errorInfo.put(ErrorTag.missing_attribute.name(), "Missing datasource attribute value");
142             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.missing_attribute,
143                     ErrorSeverity.error, errorInfo);
144         } catch (final UnsupportedOperationException e) {
145             logger.warn("Unsupported", e);
146             final Map<String, String> errorInfo = new HashMap<>();
147             errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for get");
148             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
149                     ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
150         }
151         return getResponseInternal(document, configRegistryClient, source);
152     }
153 }