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.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;
37 import com.google.common.base.Optional;
39 public class GetConfig extends AbstractConfigNetconfOperation {
41 public static final String GET_CONFIG = "get-config";
43 private final YangStoreSnapshot yangStoreSnapshot;
44 private final Optional<String> maybeNamespace;
46 private final TransactionProvider transactionProvider;
48 private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
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;
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);
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);
70 // Filter option - unsupported
71 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
72 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for "
75 return sourceDatastore;
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);
85 final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
86 yangStoreSnapshot.getModuleMXBeanEntryMap()));
89 ObjectName on = transactionProvider.getOrCreateTransaction();
90 ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
92 ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
93 dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
95 logger.trace("{} operation successful", GET_CONFIG);
101 protected String getOperationName() {
106 public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
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);
129 return getResponseInternal(document, configRegistryClient, source);