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