Merge "Bug 616 - Eliminate the use of xtend in md-sal/topology-lldp-discovery"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / editconfig / EditConfigXmlParser.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.editconfig;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Multimap;
14 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
15 import org.opendaylight.controller.config.util.ConfigRegistryClient;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementDefinition;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
22 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
23 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
24 import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
25 import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
26 import org.opendaylight.controller.netconf.util.xml.XmlElement;
27 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.util.Arrays;
32 import java.util.Map;
33
34 public class EditConfigXmlParser {
35
36     private static final Logger logger = LoggerFactory.getLogger(EditConfigXmlParser.class);
37
38     public static final String EDIT_CONFIG = "edit-config";
39     public static final String DEFAULT_OPERATION_KEY = "default-operation";
40     static final String ERROR_OPTION_KEY = "error-option";
41     static final String DEFAULT_ERROR_OPTION = "stop-on-error";
42     static final String TARGET_KEY = "target";
43     static final String TEST_OPTION_KEY = "test-option";
44
45     public EditConfigXmlParser() {
46     }
47
48     EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping,
49                                                     TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient)
50             throws NetconfDocumentedException {
51
52         //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
53
54         EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
55
56         xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
57         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
58
59
60         XmlElement targetElement = null;
61         XmlElement targetChildNode = null;
62         try {
63             targetElement  = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
64             targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
65         } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) {
66             logger.trace("Can't get only child element with same namespace due to {}",e);
67             throw NetconfDocumentedException.wrap(e);
68         }
69         String datastoreValue = targetChildNode.getName();
70         Datastore targetDatastore = Datastore.valueOf(datastoreValue);
71         logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
72
73         // check target
74         if (targetDatastore != Datastore.candidate){
75             throw new NetconfDocumentedException(String.format(
76                     "Only %s datastore supported for edit config but was: %s",
77                     Datastore.candidate,
78                     targetDatastore),
79                     NetconfDocumentedException.ErrorType.application,
80                     NetconfDocumentedException.ErrorTag.invalid_value,
81                     NetconfDocumentedException.ErrorSeverity.error);
82         }
83
84         // Test option
85         TestOption testOption;
86         Optional<XmlElement> testOptionElementOpt = xml
87                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
88         if (testOptionElementOpt.isPresent()) {
89             String testOptionValue = testOptionElementOpt.get().getTextContent();
90             testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
91         } else {
92             testOption = EditConfigXmlParser.TestOption.getDefault();
93         }
94         logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
95
96         // Error option
97         Optional<XmlElement> errorOptionElement = xml
98                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
99         if (errorOptionElement.isPresent()) {
100             String errorOptionParsed = errorOptionElement.get().getTextContent();
101             if (false == errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION))
102                 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
103                         + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
104         }
105
106         // Default op
107         Optional<XmlElement> defaultContent = xml
108                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
109         if (defaultContent.isPresent()) {
110             String mergeStrategyString = defaultContent.get().getTextContent();
111             logger.trace("Setting merge strategy to {}", mergeStrategyString);
112             editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
113         }
114
115         XmlElement configElement = null;
116         try {
117             configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
118         } catch (MissingNameSpaceException e) {
119             logger.trace("Can't get only child element with same namespace due to {}",e);
120             throw NetconfDocumentedException.wrap(e);
121         }
122
123         return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
124     }
125
126     @VisibleForTesting
127     static enum TestOption {
128         testOnly, set, testThenSet;
129
130         static TestOption getFromXmlName(String testOptionXmlName) {
131             switch (testOptionXmlName) {
132             case "test-only":
133                 return testOnly;
134             case "test-then-set":
135                 return testThenSet;
136             case "set":
137                 return set;
138             default:
139                 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
140                         + Arrays.toString(TestOption.values()));
141             }
142         }
143
144         public static TestOption getDefault() {
145             return testThenSet;
146         }
147
148     }
149
150     @VisibleForTesting
151     static class EditConfigExecution {
152
153         private final TestOption testOption;
154         private final EditStrategyType defaultEditStrategyType;
155         private final Services services;
156         private final Config configResolver;
157         private final XmlElement configElement;
158
159         EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) throws NetconfDocumentedException {
160             Config.checkUnrecognisedChildren(configElement);
161             this.configResolver = configResolver;
162             this.configElement = configElement;
163             this.services = configResolver.fromXmlServices(configElement);
164             this.testOption = testOption;
165             this.defaultEditStrategyType = defaultStrategy;
166         }
167
168         boolean shouldTest() {
169             return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
170         }
171
172         boolean shouldSet() {
173             return testOption == TestOption.set || testOption == TestOption.testThenSet;
174         }
175
176         Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
177             return configResolver.fromXmlModulesResolved(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
178         }
179
180         ServiceRegistryWrapper getServiceRegistryWrapper(ServiceReferenceReadableRegistry serviceRegistry) {
181             // TODO cache service registry
182             return new ServiceRegistryWrapper(serviceRegistry);
183         }
184
185         Map<String, Multimap<String,ModuleElementDefinition>> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
186             return configResolver.fromXmlModulesMap(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
187         }
188
189         EditStrategyType getDefaultStrategy() {
190             return defaultEditStrategyType;
191         }
192
193         Services getServices() {
194             return services;
195         }
196     }
197 }