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