Bug 977: Return RpcError result on neconf failure
[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 java.util.Set;
13 import javax.management.ObjectName;
14 import org.opendaylight.controller.config.util.ConfigRegistryClient;
15 import org.opendaylight.controller.config.util.ConfigTransactionClient;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
20 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
21 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
22 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
23 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
24 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
25 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
26 import org.opendaylight.controller.netconf.util.exception.UnexpectedElementException;
27 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
28 import org.opendaylight.controller.netconf.util.xml.XmlElement;
29 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34
35 public class GetConfig extends AbstractConfigNetconfOperation {
36
37     public static final String GET_CONFIG = "get-config";
38
39     private final YangStoreSnapshot yangStoreSnapshot;
40     private final Optional<String> maybeNamespace;
41
42     private final TransactionProvider transactionProvider;
43
44     private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
45
46     public GetConfig(YangStoreSnapshot yangStoreSnapshot, Optional<String> maybeNamespace,
47             TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
48             String netconfSessionIdForReporting) {
49         super(configRegistryClient, netconfSessionIdForReporting);
50         this.yangStoreSnapshot = yangStoreSnapshot;
51         this.maybeNamespace = maybeNamespace;
52         this.transactionProvider = transactionProvider;
53     }
54
55     public static Datastore fromXml(XmlElement xml) throws UnexpectedNamespaceException, UnexpectedElementException, MissingNameSpaceException, NetconfDocumentedException {
56
57         xml.checkName(GET_CONFIG);
58         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
59
60         XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
61                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
62         XmlElement sourceNode = sourceElement.getOnlyChildElement();
63         String sourceParsed = sourceNode.getName();
64         logger.debug("Setting source datastore to '{}'", sourceParsed);
65         Datastore sourceDatastore = Datastore.valueOf(sourceParsed);
66
67         // Filter option: ignore for now, TODO only load modules specified by the filter
68
69         return sourceDatastore;
70
71     }
72
73     private Element getResponseInternal(final Document document, final ConfigRegistryClient configRegistryClient,
74             final Datastore source) {
75         Element dataElement = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
76         final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
77                 .queryInstances(configRegistryClient);
78
79         final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
80                 yangStoreSnapshot.getModuleMXBeanEntryMap()));
81
82
83         ObjectName on = transactionProvider.getOrCreateTransaction();
84         ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
85
86         ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
87         dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
88
89         logger.trace("{} operation successful", GET_CONFIG);
90
91         return dataElement;
92     }
93
94     @Override
95     protected String getOperationName() {
96         return GET_CONFIG;
97     }
98
99     @Override
100     public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
101         Datastore source;
102         source = fromXml(xml);
103         return getResponseInternal(document, getConfigRegistryClient(), source);
104     }
105 }