Merge "BUG-832 Add initial configuration for controller self mount"
[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.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.confignetconfconnector.transactions.TransactionProvider;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
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         return new EditConfigXmlParser.EditConfigExecution(cfgMapping, configElement, testOption, editStrategyType);
102     }
103
104     @VisibleForTesting
105     static enum TestOption {
106         testOnly, set, testThenSet;
107
108         static TestOption getFromXmlName(String testOptionXmlName) {
109             switch (testOptionXmlName) {
110             case "test-only":
111                 return testOnly;
112             case "test-then-set":
113                 return testThenSet;
114             case "set":
115                 return set;
116             default:
117                 throw new IllegalArgumentException("Unsupported test option " + testOptionXmlName + " supported: "
118                         + Arrays.toString(TestOption.values()));
119             }
120         }
121
122         public static TestOption getDefault() {
123             return testThenSet;
124         }
125
126     }
127
128     @VisibleForTesting
129     static class EditConfigExecution {
130
131         private final TestOption testOption;
132         private final EditStrategyType defaultEditStrategyType;
133         private final Services services;
134         private final Config configResolver;
135         private final XmlElement configElement;
136
137         EditConfigExecution(Config configResolver, XmlElement configElement, TestOption testOption, EditStrategyType defaultStrategy) {
138             Config.checkUnrecognisedChildren(configElement);
139             this.configResolver = configResolver;
140             this.configElement = configElement;
141             this.services = configResolver.fromXmlServices(configElement);
142             this.testOption = testOption;
143             this.defaultEditStrategyType = defaultStrategy;
144         }
145
146         boolean shouldTest() {
147             return testOption == TestOption.testOnly || testOption == TestOption.testThenSet;
148         }
149
150         boolean shouldSet() {
151             return testOption == TestOption.set || testOption == TestOption.testThenSet;
152         }
153
154         Map<String, Multimap<String, ModuleElementResolved>> getResolvedXmlElements(ServiceReferenceReadableRegistry serviceRegistry) {
155             return configResolver.fromXmlModulesResolved(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
156         }
157
158         ServiceRegistryWrapper getServiceRegistryWrapper(ServiceReferenceReadableRegistry serviceRegistry) {
159             // TODO cache service registry
160             return new ServiceRegistryWrapper(serviceRegistry);
161         }
162
163         Map<String, Multimap<String,ModuleElementDefinition>> getModulesDefinition(ServiceReferenceReadableRegistry serviceRegistry) {
164             return configResolver.fromXmlModulesMap(configElement, defaultEditStrategyType, getServiceRegistryWrapper(serviceRegistry));
165         }
166
167         EditStrategyType getDefaultStrategy() {
168             return defaultEditStrategyType;
169         }
170
171         Services getServices() {
172             return services;
173         }
174     }
175 }