Merge "BUG 652 leafref CCE & BUG 720 colons problem"
[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 (!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
107         // Default op
108         Optional<XmlElement> defaultContent = xml
109                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
110         if (defaultContent.isPresent()) {
111             String mergeStrategyString = defaultContent.get().getTextContent();
112             logger.trace("Setting merge strategy to {}", mergeStrategyString);
113             editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
114         }
115
116         XmlElement configElement = null;
117         try {
118             configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
119         } catch (MissingNameSpaceException e) {
120             logger.trace("Can't get only child element with same namespace due to {}",e);
121             throw NetconfDocumentedException.wrap(e);
122         }
123
124         return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
125     }
126
127     @VisibleForTesting
128     static enum TestOption {
129         testOnly, set, testThenSet;
130
131         static TestOption getFromXmlName(String testOptionXmlName) {
132             switch (testOptionXmlName) {
133             case "test-only":
134                 return testOnly;
135             case "test-then-set":
136                 return testThenSet;
137             case "set":
138                 return set;
139             default:
140                 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
141                         + Arrays.toString(TestOption.values()));
142             }
143         }
144
145         public static TestOption getDefault() {
146             return testThenSet;
147         }
148
149     }
150
151     @VisibleForTesting
152     static class EditConfigExecution {
153
154         private final TestOption testOption;
155         private final EditStrategyType defaultEditStrategyType;
156         private final Services services;
157         private final Config configResolver;
158         private final XmlElement configElement;
159
160         EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) throws NetconfDocumentedException {
161             Config.checkUnrecognisedChildren(configElement);
162             this.configResolver = configResolver;
163             this.configElement = configElement;
164             this.services = configResolver.fromXmlServices(configElement);
165             this.testOption = testOption;
166             this.defaultEditStrategyType = defaultStrategy;
167         }
168
169         boolean shouldTest() {
170             return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
171         }
172
173         boolean shouldSet() {
174             return testOption == TestOption.set || testOption == TestOption.testThenSet;
175         }
176
177         Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
178             return configResolver.fromXmlModulesResolved(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
179         }
180
181         ServiceRegistryWrapper getServiceRegistryWrapper(ServiceReferenceReadableRegistry serviceRegistry) {
182             // TODO cache service registry
183             return new ServiceRegistryWrapper(serviceRegistry);
184         }
185
186         Map<String, Multimap<String,ModuleElementDefinition>> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) throws NetconfDocumentedException {
187             return configResolver.fromXmlModulesMap(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
188         }
189
190         EditStrategyType getDefaultStrategy() {
191             return defaultEditStrategyType;
192         }
193
194         Services getServices() {
195             return services;
196         }
197     }
198 }