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