Fix config-netconf-connector needed for interacting with yuma's yangcli.
[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.resetDefaultStrategy();
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 = EditStrategyType.valueOf(mergeStrategyString);
94             // FIXME: thread safety, remove global state
95             EditStrategyType.setDefaultStrategy(editStrategyType);
96         }
97         // FIXME: thread safety, remove global state
98         Set<ObjectName> instancesForFillingServiceRefMapping = Collections.emptySet();
99         if (EditStrategyType.defaultStrategy() == EditStrategyType.merge) {
100             instancesForFillingServiceRefMapping = Datastore.getInstanceQueryStrategy(targetDatastore, transactionProvider)
101                     .queryInstances(configRegistryClient);
102             logger.trace("Pre-filling services from following instances: {}", instancesForFillingServiceRefMapping);
103         }
104
105         XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
106
107         return new EditConfigXmlParser.EditConfigExecution(xml, cfgMapping, configElement, testOption, instancesForFillingServiceRefMapping);
108     }
109
110     private void removeMountpointsFromConfig(XmlElement configElement, XmlElement mountpointsElement) {
111         configElement.getDomElement().removeChild(mountpointsElement.getDomElement());
112     }
113
114     @VisibleForTesting
115     static enum TestOption {
116         testOnly, set, testThenSet;
117
118         static TestOption getFromXmlName(String testOptionXmlName) {
119             switch (testOptionXmlName) {
120             case "test-only":
121                 return testOnly;
122             case "test-then-set":
123                 return testThenSet;
124             case "set":
125                 return set;
126             default:
127                 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
128                         + Arrays.toString(TestOption.values()));
129             }
130         }
131
132         public static TestOption getDefault() {
133             return testThenSet;
134         }
135
136     }
137
138     @VisibleForTesting
139     static class EditConfigExecution {
140         XmlElement editConfigXml;
141         Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements;
142         TestOption testOption;
143
144         EditConfigExecution(XmlElement xml, Config configResolver, XmlElement configElement, TestOption testOption, Set<ObjectName> instancesForFillingServiceRefMapping) {
145             this.editConfigXml = xml;
146             this.resolvedXmlElements = configResolver.fromXml(configElement, instancesForFillingServiceRefMapping);
147             this.testOption = testOption;
148         }
149
150         boolean shouldTest() {
151             return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
152         }
153
154         boolean shouldSet() {
155             return testOption == TestOption.set || testOption == TestOption.testThenSet;
156         }
157     }
158 }