d835dfd30f0abc0a95bbf51dc4cca7cfe5443a30
[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.base.Preconditions;
14 import com.google.common.collect.Multimap;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
18 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import java.util.Arrays;
25 import java.util.Map;
26
27 public class EditConfigXmlParser {
28
29     private static final Logger logger = LoggerFactory.getLogger(EditConfigXmlParser.class);
30
31     public static final String EDIT_CONFIG = "edit-config";
32     public static final String DEFAULT_OPERATION_KEY = "default-operation";
33     static final String ERROR_OPTION_KEY = "error-option";
34     static final String DEFAULT_ERROR_OPTION = "stop-on-error";
35     static final String TARGET_KEY = "target";
36     static final String TEST_OPTION_KEY = "test-option";
37
38     public EditConfigXmlParser() {
39     }
40
41     EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping)
42             throws NetconfDocumentedException {
43
44         EditStrategyType.resetDefaultStrategy();
45
46         xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
47         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
48
49         XmlElement targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
50         XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
51         String datastoreValue = targetChildNode.getName();
52         Datastore targetDatastore = Datastore.valueOf(datastoreValue);
53         logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
54
55         // check target
56         Preconditions.checkArgument(targetDatastore == Datastore.candidate,
57                 "Only %s datastore supported for edit config but was: %s", Datastore.candidate, targetDatastore);
58
59         // Test option
60         TestOption testOption;
61         Optional<XmlElement> testOptionElementOpt = xml
62                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
63         if (testOptionElementOpt.isPresent()) {
64             String testOptionValue = testOptionElementOpt.get().getTextContent();
65             testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
66         } else {
67             testOption = EditConfigXmlParser.TestOption.getDefault();
68         }
69         logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
70
71         // Error option
72         Optional<XmlElement> errorOptionElement = xml
73                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
74         if (errorOptionElement.isPresent()) {
75             String errorOptionParsed = errorOptionElement.get().getTextContent();
76             if (false == errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION))
77                 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
78                         + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
79         }
80
81         // Default op
82         Optional<XmlElement> defaultContent = xml
83                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
84         if (defaultContent.isPresent())
85             EditStrategyType.setDefaultStrategy(EditStrategyType.valueOf(defaultContent.get().getTextContent()));
86
87         XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
88
89         return new EditConfigXmlParser.EditConfigExecution(xml, cfgMapping, configElement, testOption);
90     }
91
92     private void removeMountpointsFromConfig(XmlElement configElement, XmlElement mountpointsElement) {
93         configElement.getDomElement().removeChild(mountpointsElement.getDomElement());
94     }
95
96     @VisibleForTesting
97     static enum TestOption {
98         testOnly, set, testThenSet;
99
100         static TestOption getFromXmlName(String testOptionXmlName) {
101             switch (testOptionXmlName) {
102             case "test-only":
103                 return testOnly;
104             case "test-then-set":
105                 return testThenSet;
106             case "set":
107                 return set;
108             default:
109                 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
110                         + Arrays.toString(TestOption.values()));
111             }
112         }
113
114         public static TestOption getDefault() {
115             return testThenSet;
116         }
117
118     }
119
120     @VisibleForTesting
121     static class EditConfigExecution {
122         XmlElement editConfigXml;
123         Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements;
124         TestOption testOption;
125
126         EditConfigExecution(XmlElement xml, Config configResolver, XmlElement configElement, TestOption testOption) {
127             this.editConfigXml = xml;
128             this.resolvedXmlElements = configResolver.fromXml(configElement);
129             this.testOption = testOption;
130         }
131
132         boolean shouldTest() {
133             return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
134         }
135
136         boolean shouldSet() {
137             return testOption == TestOption.set || testOption == TestOption.testThenSet;
138         }
139     }
140 }