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 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;
32 import javax.management.ObjectName;
33 import java.util.HashMap;
37 public class GetConfig extends AbstractConfigNetconfOperation {
39 public static final String GET_CONFIG = "get-config";
41 private final YangStoreSnapshot yangStoreSnapshot;
42 private final Optional<String> maybeNamespace;
44 private final TransactionProvider transactionProvider;
46 private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
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;
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);
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);
68 // Filter option - unsupported
69 if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0)
70 throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for "
73 return sourceDatastore;
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);
83 final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
84 yangStoreSnapshot.getModuleMXBeanEntryMap()));
87 ObjectName on = transactionProvider.getOrCreateTransaction();
88 ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
90 ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
91 dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
93 logger.trace("{} operation successful", GET_CONFIG);
99 protected String getOperationName() {
104 public Element handle(Document document, XmlElement xml) throws NetconfDocumentedException {
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);
127 return getResponseInternal(document, configRegistryClient, source);