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