Merge "bug 741 - Make sure to stop threads on bundle stop on TopologyServiceShim"
[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 java.util.HashMap;
12 import java.util.Map;
13 import java.util.Set;
14
15 import javax.management.ObjectName;
16
17 import org.opendaylight.controller.config.util.ConfigRegistryClient;
18 import org.opendaylight.controller.config.util.ConfigTransactionClient;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
21 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
22 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
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.operations.editconfig.EditConfig;
28 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
29 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
30 import org.opendaylight.controller.netconf.util.xml.XmlElement;
31 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
32 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38 import com.google.common.base.Optional;
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 = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
83         final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
84                 .queryInstances(configRegistryClient);
85
86         final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
87                 yangStoreSnapshot.getModuleMXBeanEntryMap()));
88
89
90         ObjectName on = transactionProvider.getOrCreateTransaction();
91         ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
92
93         ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
94         dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
95
96         logger.trace("{} operation successful", GET_CONFIG);
97
98         return dataElement;
99     }
100
101     @Override
102     protected String getOperationName() {
103         return GET_CONFIG;
104     }
105
106     @Override
107     public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
108         Datastore source;
109         try {
110             source = fromXml(xml);
111         } catch (final IllegalArgumentException e) {
112             logger.warn("Rpc error: {}", ErrorTag.bad_attribute, e);
113             final Map<String, String> errorInfo = new HashMap<>();
114             errorInfo.put(ErrorTag.bad_attribute.name(), e.getMessage());
115             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.bad_attribute,
116                     ErrorSeverity.error, errorInfo);
117         } catch (final IllegalStateException e) {
118             logger.warn("Rpc error: {}", ErrorTag.missing_attribute, e);
119             final Map<String, String> errorInfo = new HashMap<>();
120             errorInfo.put(ErrorTag.missing_attribute.name(), "Missing datasource attribute value");
121             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.rpc, ErrorTag.missing_attribute,
122                     ErrorSeverity.error, errorInfo);
123         } catch (final UnsupportedOperationException e) {
124             logger.warn("Unsupported", e);
125             final Map<String, String> errorInfo = new HashMap<>();
126             errorInfo.put(ErrorTag.operation_not_supported.name(), "Unsupported option for get");
127             throw new NetconfDocumentedException(e.getMessage(), e, ErrorType.application,
128                     ErrorTag.operation_not_supported, ErrorSeverity.error, errorInfo);
129         }
130         return getResponseInternal(document, configRegistryClient, source);
131     }
132 }