Bug 460 - Fix warning throughout netconf subsystem
[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 org.opendaylight.controller.config.util.ConfigRegistryClient;
13 import org.opendaylight.controller.config.util.ConfigTransactionClient;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
16 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
17 import org.opendaylight.controller.netconf.confignetconfconnector.operations.AbstractConfigNetconfOperation;
18 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
19 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
20 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
21 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
22 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
23 import org.opendaylight.controller.netconf.util.exception.UnexpectedElementException;
24 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
27 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32
33 import javax.management.ObjectName;
34 import java.util.Set;
35
36 public class GetConfig extends AbstractConfigNetconfOperation {
37
38     public static final String GET_CONFIG = "get-config";
39
40     private final YangStoreSnapshot yangStoreSnapshot;
41     private final Optional<String> maybeNamespace;
42
43     private final TransactionProvider transactionProvider;
44
45     private static final Logger logger = LoggerFactory.getLogger(GetConfig.class);
46
47     public GetConfig(YangStoreSnapshot yangStoreSnapshot, Optional<String> maybeNamespace,
48             TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient,
49             String netconfSessionIdForReporting) {
50         super(configRegistryClient, netconfSessionIdForReporting);
51         this.yangStoreSnapshot = yangStoreSnapshot;
52         this.maybeNamespace = maybeNamespace;
53         this.transactionProvider = transactionProvider;
54     }
55
56     public static Datastore fromXml(XmlElement xml) throws UnexpectedNamespaceException, UnexpectedElementException, MissingNameSpaceException, NetconfDocumentedException {
57
58         xml.checkName(GET_CONFIG);
59         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
60
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);
67
68         // Filter option - unsupported
69         if (xml.getChildElements(XmlNetconfConstants.FILTER).size() != 0){
70             throw new UnsupportedOperationException("Unsupported option " + XmlNetconfConstants.FILTER + " for "
71                     + GET_CONFIG);
72         }
73
74         return sourceDatastore;
75
76     }
77
78     private Element getResponseInternal(final Document document, final ConfigRegistryClient configRegistryClient,
79             final Datastore source) throws NetconfDocumentedException {
80         Element dataElement = XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
81         final Set<ObjectName> instances = Datastore.getInstanceQueryStrategy(source, this.transactionProvider)
82                 .queryInstances(configRegistryClient);
83
84         final Config configMapping = new Config(EditConfig.transformMbeToModuleConfigs(configRegistryClient,
85                 yangStoreSnapshot.getModuleMXBeanEntryMap()));
86
87
88         ObjectName on = transactionProvider.getOrCreateTransaction();
89         ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(on);
90
91         ServiceRegistryWrapper serviceTracker = new ServiceRegistryWrapper(ta);
92         dataElement = configMapping.toXml(instances, this.maybeNamespace, document, dataElement, serviceTracker);
93
94         logger.trace("{} operation successful", GET_CONFIG);
95
96         return dataElement;
97     }
98
99     @Override
100     protected String getOperationName() {
101         return GET_CONFIG;
102     }
103
104     @Override
105     public Element handleWithNoSubsequentOperations(Document document, XmlElement xml) throws NetconfDocumentedException {
106         Datastore source;
107         source = fromXml(xml);
108         return getResponseInternal(document, getConfigRegistryClient(), source);
109     }
110 }