Merge "HostTracker hosts DB key scheme implementation"
[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.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.ModuleElementResolved;
19 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
20 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import javax.management.ObjectName;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.Map;
30 import java.util.Set;
31
32 public class EditConfigXmlParser {
33
34     private static final Logger logger = LoggerFactory.getLogger(EditConfigXmlParser.class);
35
36     public static final String EDIT_CONFIG = "edit-config";
37     public static final String DEFAULT_OPERATION_KEY = "default-operation";
38     static final String ERROR_OPTION_KEY = "error-option";
39     static final String DEFAULT_ERROR_OPTION = "stop-on-error";
40     static final String TARGET_KEY = "target";
41     static final String TEST_OPTION_KEY = "test-option";
42
43     public EditConfigXmlParser() {
44     }
45
46     EditConfigXmlParser.EditConfigExecution fromXml(final XmlElement xml, final Config cfgMapping,
47                                                     TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient)
48             throws NetconfDocumentedException {
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         XmlElement targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
56         XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
57         String datastoreValue = targetChildNode.getName();
58         Datastore targetDatastore = Datastore.valueOf(datastoreValue);
59         logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
60
61         // check target
62         Preconditions.checkArgument(targetDatastore == Datastore.candidate,
63                 "Only %s datastore supported for edit config but was: %s", Datastore.candidate, targetDatastore);
64
65         // Test option
66         TestOption testOption;
67         Optional<XmlElement> testOptionElementOpt = xml
68                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
69         if (testOptionElementOpt.isPresent()) {
70             String testOptionValue = testOptionElementOpt.get().getTextContent();
71             testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
72         } else {
73             testOption = EditConfigXmlParser.TestOption.getDefault();
74         }
75         logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
76
77         // Error option
78         Optional<XmlElement> errorOptionElement = xml
79                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
80         if (errorOptionElement.isPresent()) {
81             String errorOptionParsed = errorOptionElement.get().getTextContent();
82             if (false == errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION))
83                 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
84                         + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
85         }
86
87         // Default op
88         Optional<XmlElement> defaultContent = xml
89                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
90         if (defaultContent.isPresent()) {
91             String mergeStrategyString = defaultContent.get().getTextContent();
92             logger.trace("Setting merge strategy to {}", mergeStrategyString);
93             editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
94         }
95         Set<ObjectName> instancesForFillingServiceRefMapping = Collections.emptySet();
96         if (editStrategyType == EditStrategyType.merge) {
97             instancesForFillingServiceRefMapping = Datastore.getInstanceQueryStrategy(targetDatastore, transactionProvider)
98                     .queryInstances(configRegistryClient);
99             logger.trace("Pre-filling services from following instances: {}", instancesForFillingServiceRefMapping);
100         }
101
102         XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
103
104         return new EditConfigXmlParser.EditConfigExecution(xml, cfgMapping, configElement, testOption,
105                 instancesForFillingServiceRefMapping, editStrategyType);
106     }
107
108     private void removeMountpointsFromConfig(XmlElement configElement, XmlElement mountpointsElement) {
109         configElement.getDomElement().removeChild(mountpointsElement.getDomElement());
110     }
111
112     @VisibleForTesting
113     static enum TestOption {
114         testOnly, set, testThenSet;
115
116         static TestOption getFromXmlName(String testOptionXmlName) {
117             switch (testOptionXmlName) {
118             case "test-only":
119                 return testOnly;
120             case "test-then-set":
121                 return testThenSet;
122             case "set":
123                 return set;
124             default:
125                 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
126                         + Arrays.toString(TestOption.values()));
127             }
128         }
129
130         public static TestOption getDefault() {
131             return testThenSet;
132         }
133
134     }
135
136     @VisibleForTesting
137     static class EditConfigExecution {
138         private final XmlElement editConfigXml;
139         private final Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements;
140         private final TestOption testOption;
141         private final EditStrategyType defaultEditStrategyType;
142
143         EditConfigExecution(XmlElement xml, Config configResolver, XmlElement configElement, TestOption testOption, Set<ObjectName> instancesForFillingServiceRefMapping,
144                             EditStrategyType defaultStrategy) {
145             this.editConfigXml = xml;
146             this.resolvedXmlElements = configResolver.fromXml(configElement, instancesForFillingServiceRefMapping, defaultStrategy);
147             this.testOption = testOption;
148             this.defaultEditStrategyType = defaultStrategy;
149         }
150
151         boolean shouldTest() {
152             return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
153         }
154
155         boolean shouldSet() {
156             return testOption == TestOption.set || testOption == TestOption.testThenSet;
157         }
158
159         Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements() {
160             return resolvedXmlElements;
161         }
162
163         EditStrategyType getDefaultStrategy() {
164             return defaultEditStrategyType;
165         }
166     }
167 }