2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig;
11 import java.util.HashMap;
15 import javax.management.ObjectName;
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;
38 import com.google.common.base.Optional;
40 public class GetConfig extends AbstractConfigNetconfOperation {
42 public static final String GET_CONFIG = "get-config";
44 private final YangStoreSnapshot yangStoreSnapshot;
45 private final Optional<String> maybeNamespace;
47 private final TransactionProvider transactionProvider;
49 private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
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;
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);
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);
71 // Filter option - unsupported
72 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
73 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for "
76 return sourceDatastore;
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);
86 final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
87 yangStoreSnapshot.getModuleMXBeanEntryMap()));
90 ObjectName on = transactionProvider.getOrCreateTransaction();
91 ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
93 ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
94 dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
96 logger.trace("{} operation successful", GET_CONFIG);
102 protected String getOperationName() {
107 public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
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);
130 return getResponseInternal(document, configRegistryClient, source);