Merge "Bug 116 - Revisit SanityTest"
[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.api.ServiceReferenceReadableRegistry;
16 import org.opendaylight.controller.config.util.ConfigRegistryClient;
17 import org.opendaylight.controller.config.util.ConfigTransactionClient;
18 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Config;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ModuleElementResolved;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
22 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Datastore;
23 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
24 import org.opendaylight.controller.netconf.util.xml.XmlElement;
25 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import javax.management.ObjectName;
30 import java.util.Arrays;
31 import java.util.Map;
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                                                     TransactionProvider transactionProvider, ConfigRegistryClient configRegistryClient)
49             throws NetconfDocumentedException {
50
51         //TODO remove transactionProvider and CfgRegistry from parameters, accept only service ref store
52
53         EditStrategyType editStrategyType = EditStrategyType.getDefaultStrategy();
54
55         xml.checkName(EditConfigXmlParser.EDIT_CONFIG);
56         xml.checkNamespace(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
57
58         XmlElement targetElement = xml.getOnlyChildElementWithSameNamespace(EditConfigXmlParser.TARGET_KEY);
59         XmlElement targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
60         String datastoreValue = targetChildNode.getName();
61         Datastore targetDatastore = Datastore.valueOf(datastoreValue);
62         logger.debug("Setting {} to '{}'", EditConfigXmlParser.TARGET_KEY, targetDatastore);
63
64         // check target
65         Preconditions.checkArgument(targetDatastore == Datastore.candidate,
66                 "Only %s datastore supported for edit config but was: %s", Datastore.candidate, targetDatastore);
67
68         // Test option
69         TestOption testOption;
70         Optional<XmlElement> testOptionElementOpt = xml
71                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.TEST_OPTION_KEY);
72         if (testOptionElementOpt.isPresent()) {
73             String testOptionValue = testOptionElementOpt.get().getTextContent();
74             testOption = EditConfigXmlParser.TestOption.getFromXmlName(testOptionValue);
75         } else {
76             testOption = EditConfigXmlParser.TestOption.getDefault();
77         }
78         logger.debug("Setting {} to '{}'", EditConfigXmlParser.TEST_OPTION_KEY, testOption);
79
80         // Error option
81         Optional<XmlElement> errorOptionElement = xml
82                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.ERROR_OPTION_KEY);
83         if (errorOptionElement.isPresent()) {
84             String errorOptionParsed = errorOptionElement.get().getTextContent();
85             if (false == errorOptionParsed.equals(EditConfigXmlParser.DEFAULT_ERROR_OPTION))
86                 throw new UnsupportedOperationException("Only " + EditConfigXmlParser.DEFAULT_ERROR_OPTION
87                         + " supported for " + EditConfigXmlParser.ERROR_OPTION_KEY + ", was " + errorOptionParsed);
88         }
89
90         // Default op
91         Optional<XmlElement> defaultContent = xml
92                 .getOnlyChildElementWithSameNamespaceOptionally(EditConfigXmlParser.DEFAULT_OPERATION_KEY);
93         if (defaultContent.isPresent()) {
94             String mergeStrategyString = defaultContent.get().getTextContent();
95             logger.trace("Setting merge strategy to {}", mergeStrategyString);
96             editStrategyType = EditStrategyType.valueOf(mergeStrategyString);
97         }
98
99         XmlElement configElement = xml.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.CONFIG_KEY);
100
101         ObjectName taON = transactionProvider.getOrCreateTransaction();
102         ConfigTransactionClient ta = configRegistryClient.getConfigTransactionClient(taON);
103
104         return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption,
105                 ta, editStrategyType);
106     }
107
108     @VisibleForTesting
109     static enum TestOption {
110         testOnly, set, testThenSet;
111
112         static TestOption getFromXmlName(String testOptionXmlName) {
113             switch (testOptionXmlName) {
114             case "test-only":
115                 return testOnly;
116             case "test-then-set":
117                 return testThenSet;
118             case "set":
119                 return set;
120             default:
121                 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
122                         + Arrays.toString(TestOption.values()));
123             }
124         }
125
126         public static TestOption getDefault() {
127             return testThenSet;
128         }
129
130     }
131
132     @VisibleForTesting
133     static class EditConfigExecution {
134
135         private final Map<String, Multimap<String, ModuleElementResolved>> resolvedXmlElements;
136         private final TestOption testOption;
137         private final EditStrategyType defaultEditStrategyType;
138         private final Services services;
139
140         EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, ServiceReferenceReadableRegistry ta, EditStrategyType defaultStrategy) {
141             Config.ConfigElementResolved configElementResolved = configResolver.fromXml(configElement, defaultStrategy, ta);
142             this.resolvedXmlElements = configElementResolved.getResolvedModules();
143             this.services = configElementResolved.getServices();
144             this.testOption = testOption;
145             this.defaultEditStrategyType = defaultStrategy;
146         }
147
148         boolean shouldTest() {
149             return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
150         }
151
152         boolean shouldSet() {
153             return testOption == TestOption.set || testOption == TestOption.testThenSet;
154         }
155
156         Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements() {
157             return resolvedXmlElements;
158         }
159
160         EditStrategyType getDefaultStrategy() {
161             return defaultEditStrategyType;
162         }
163
164         Services getServices() {
165             return services;
166         }
167     }
168 }