Merge "Bug 509: Fixed incorrect merging of Data Store Writes / Events"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / config / Config.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.mapping.config;
10
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.lang.String.format;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Date;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
22
23 import javax.management.ObjectName;
24
25 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
26 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
27 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
28 import org.opendaylight.controller.netconf.util.xml.XmlElement;
29 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
30 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36 import com.google.common.base.Optional;
37 import com.google.common.base.Preconditions;
38 import com.google.common.collect.HashMultimap;
39 import com.google.common.collect.Lists;
40 import com.google.common.collect.Maps;
41 import com.google.common.collect.Multimap;
42
43 public class Config {
44     private final Logger logger = LoggerFactory.getLogger(Config.class);
45
46     private final Map<String/* Namespace from yang file */,
47             Map<String /* Name of module entry from yang file */, ModuleConfig>> moduleConfigs;
48     private final Map<String, ModuleConfig> moduleNamesToConfigs;
49
50     private final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap;
51
52     public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs) {
53         this(moduleConfigs, Collections.<String, Map<Date, EditConfig.IdentityMapping>>emptyMap());
54     }
55
56     public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs, Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) {
57         this.moduleConfigs = moduleConfigs;
58         Map<String, ModuleConfig> moduleNamesToConfigs = new HashMap<>();
59         for (Entry<String, Map<String, ModuleConfig>> entry : moduleConfigs.entrySet()) {
60             moduleNamesToConfigs.putAll(entry.getValue());
61         }
62         this.moduleNamesToConfigs = Collections.unmodifiableMap(moduleNamesToConfigs);
63         this.identityMap = identityMap;
64     }
65
66     public static Map<String, Map<String, Collection<ObjectName>>> getMappedInstances(Set<ObjectName> instancesToMap,
67                                                                                 Map<String, Map<String, ModuleConfig>> configs) {
68         Multimap<String, ObjectName> moduleToInstances = mapInstancesToModules(instancesToMap);
69
70         Map<String, Map<String, Collection<ObjectName>>> retVal = Maps.newLinkedHashMap();
71
72         for (String namespace : configs.keySet()) {
73
74             Map<String, Collection<ObjectName>> innerRetVal = Maps.newHashMap();
75
76             for (Entry<String, ModuleConfig> mbeEntry : configs.get(namespace).entrySet()) {
77
78                 String moduleName = mbeEntry.getKey();
79                 Collection<ObjectName> instances = moduleToInstances.get(moduleName);
80
81                 // TODO, this code does not support same module names from different namespaces
82                 // Namespace should be present in ObjectName
83
84                 if (instances == null)
85                     continue;
86
87                 innerRetVal.put(moduleName, instances);
88
89             }
90
91             retVal.put(namespace, innerRetVal);
92         }
93         return retVal;
94     }
95
96     private static Multimap<String, ObjectName> mapInstancesToModules(Set<ObjectName> instancesToMap) {
97         Multimap<String, ObjectName> retVal = HashMultimap.create();
98
99         for (ObjectName objectName : instancesToMap) {
100             String factoryName = ObjectNameUtil.getFactoryName(objectName);
101             retVal.put(factoryName, objectName);
102         }
103         return retVal;
104     }
105
106     // public Element toXml(Set<ObjectName> instancesToMap, String namespace,
107     // Document document) {
108     // return toXml(instancesToMap, Optional.of(namespace), document);
109     // }
110
111     public Element toXml(Set<ObjectName> instancesToMap, Optional<String> maybeNamespace, Document document,
112             Element dataElement, ServiceRegistryWrapper serviceTracker) {
113
114         Map<String, Map<String, Collection<ObjectName>>> moduleToInstances = getMappedInstances(instancesToMap,
115                 moduleConfigs);
116
117         Element root = dataElement;
118         if (maybeNamespace.isPresent()) {
119             root.setAttributeNS(maybeNamespace.get(), dataElement.getNodeName(), "xmlns");
120         }
121
122         Element modulesElement = XmlUtil.createElement(document, XmlNetconfConstants.MODULES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
123         root.appendChild(modulesElement);
124         for (String moduleNamespace : moduleToInstances.keySet()) {
125             for (Entry<String, Collection<ObjectName>> moduleMappingEntry : moduleToInstances.get(moduleNamespace)
126                     .entrySet()) {
127
128                 ModuleConfig mapping = moduleConfigs.get(moduleNamespace).get(moduleMappingEntry.getKey());
129
130                 if (moduleMappingEntry.getValue().isEmpty()) {
131                     continue;
132                 }
133
134                 for (ObjectName objectName : moduleMappingEntry.getValue()) {
135                     modulesElement.appendChild(mapping.toXml(objectName, serviceTracker, document, moduleNamespace));
136                 }
137
138             }
139         }
140
141         root.appendChild(Services.toXml(serviceTracker, document));
142
143         return root;
144     }
145
146     // TODO refactor, replace string representing namespace with namespace class
147     // TODO refactor, replace Map->Multimap with e.g. ConfigElementResolved
148     // class
149
150     public Map<String, Multimap<String, ModuleElementResolved>> fromXmlModulesResolved(XmlElement xml, EditStrategyType defaultEditStrategyType, ServiceRegistryWrapper serviceTracker) {
151         Optional<XmlElement> modulesElement = getModulesElement(xml);
152         List<XmlElement> moduleElements = getModulesElementList(modulesElement);
153
154         Map<String, Multimap<String, ModuleElementResolved>> retVal = Maps.newHashMap();
155
156         for (XmlElement moduleElement : moduleElements) {
157             ResolvingStrategy<ModuleElementResolved> resolvingStrategy = new ResolvingStrategy<ModuleElementResolved>() {
158                 @Override
159                 public ModuleElementResolved resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker, String instanceName, String moduleNamespace, EditStrategyType defaultStrategy) {
160                     return moduleMapping.fromXml(moduleElement, serviceTracker,
161                             instanceName, moduleNamespace, defaultStrategy, identityMap);
162                 }
163             };
164
165             resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
166         }
167         return retVal;
168     }
169
170     /**
171      * return a map containing namespace -> moduleName -> instanceName map. Attribute parsing is omitted.
172      */
173     public Map<String, Multimap<String, ModuleElementDefinition>> fromXmlModulesMap(XmlElement xml,
174             EditStrategyType defaultEditStrategyType, ServiceRegistryWrapper serviceTracker) {
175         Optional<XmlElement> modulesElement = getModulesElement(xml);
176         List<XmlElement> moduleElements = getModulesElementList(modulesElement);
177
178         Map<String, Multimap<String, ModuleElementDefinition>> retVal = Maps.newHashMap();
179
180         for (XmlElement moduleElement : moduleElements) {
181             ResolvingStrategy<ModuleElementDefinition> resolvingStrategy = new ResolvingStrategy<ModuleElementDefinition>() {
182                 @Override
183                 public ModuleElementDefinition resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement,
184                         ServiceRegistryWrapper serviceTracker, String instanceName, String moduleNamespace,
185                         EditStrategyType defaultStrategy) {
186                     // TODO: add check for conflicts between global and local
187                     // edit strategy
188                     String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
189                             XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
190                     return new ModuleElementDefinition(instanceName, perInstanceEditStrategy, defaultStrategy);
191                 }
192             };
193
194             resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
195         }
196         return retVal;
197     }
198
199     private static Optional<XmlElement> getModulesElement(XmlElement xml) {
200         return xml.getOnlyChildElementOptionally(XmlNetconfConstants.MODULES_KEY,
201                     XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
202     }
203
204     private List<XmlElement> getModulesElementList(Optional<XmlElement> modulesElement) {
205         List<XmlElement> moduleElements;
206
207         if (modulesElement.isPresent()) {
208             moduleElements = modulesElement.get().getChildElementsWithSameNamespace(XmlNetconfConstants.MODULE_KEY);
209             modulesElement.get().checkUnrecognisedElements(moduleElements);
210         } else {
211             moduleElements = Lists.newArrayList();
212         }
213         return moduleElements;
214     }
215
216     private <T> void resolveModule(Map<String, Multimap<String, T>> retVal, ServiceRegistryWrapper serviceTracker,
217             XmlElement moduleElement, EditStrategyType defaultStrategy, ResolvingStrategy<T> resolvingStrategy) {
218         XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
219         Entry<String, String> prefixToNamespace = typeElement.findNamespaceOfTextContent();
220         String moduleNamespace = prefixToNamespace.getValue();
221         XmlElement nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
222         String instanceName = nameElement.getTextContent();
223         String factoryNameWithPrefix = typeElement.getTextContent();
224         String prefixOrEmptyString = prefixToNamespace.getKey();
225         String factoryName = getFactoryName(factoryNameWithPrefix, prefixOrEmptyString);
226
227         ModuleConfig moduleMapping = getModuleMapping(moduleNamespace, instanceName, factoryName);
228
229         Multimap<String, T> innerMap = retVal.get(moduleNamespace);
230         if (innerMap == null) {
231             innerMap = HashMultimap.create();
232             retVal.put(moduleNamespace, innerMap);
233         }
234
235         T resolvedElement = resolvingStrategy.resolveElement(moduleMapping, moduleElement, serviceTracker,
236                 instanceName, moduleNamespace, defaultStrategy);
237
238         innerMap.put(factoryName, resolvedElement);
239     }
240
241     public Services fromXmlServices(XmlElement xml) {
242         Optional<XmlElement> servicesElement = getServicesElement(xml);
243
244         Services services;
245         if (servicesElement.isPresent()) {
246             services = Services.fromXml(servicesElement.get());
247         } else {
248             services = new Services();
249         }
250
251         return services;
252     }
253
254     private static Optional<XmlElement> getServicesElement(XmlElement xml) {
255         return xml.getOnlyChildElementOptionally(XmlNetconfConstants.SERVICES_KEY,
256                     XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
257     }
258
259     public static void checkUnrecognisedChildren(XmlElement parent) {
260         Optional<XmlElement> servicesOpt = getServicesElement(parent);
261         Optional<XmlElement> modulesOpt = getModulesElement(parent);
262
263         List<XmlElement> recognised = Lists.newArrayList();
264         if(servicesOpt.isPresent())
265             recognised.add(servicesOpt.get());
266         if(modulesOpt.isPresent())
267             recognised.add(modulesOpt.get());
268
269         parent.checkUnrecognisedElements(recognised);
270     }
271
272     private String getFactoryName(String factoryNameWithPrefix, String prefixOrEmptyString) {
273         checkState(
274                 factoryNameWithPrefix.startsWith(prefixOrEmptyString),
275                 format("Internal error: text " + "content '%s' of type node does not start with prefix '%s'",
276                         factoryNameWithPrefix, prefixOrEmptyString));
277
278         int factoryNameAfterPrefixIndex;
279         if (prefixOrEmptyString.isEmpty()) {
280             factoryNameAfterPrefixIndex = 0;
281         } else {
282             factoryNameAfterPrefixIndex = prefixOrEmptyString.length() + 1;
283         }
284         return factoryNameWithPrefix.substring(factoryNameAfterPrefixIndex);
285     }
286
287     private ModuleConfig getModuleMapping(String moduleNamespace, String instanceName, String factoryName) {
288         Map<String, ModuleConfig> mappingsFromNamespace = moduleConfigs.get(moduleNamespace);
289
290         Preconditions.checkNotNull(mappingsFromNamespace,
291                 "Namespace %s, defined in: module %s of type %s not found, available namespaces: %s", moduleNamespace,
292                 instanceName, factoryName, moduleConfigs.keySet());
293
294         ModuleConfig moduleMapping = mappingsFromNamespace.get(factoryName);
295         checkState(moduleMapping != null, "Cannot find mapping for module type " + factoryName);
296         return moduleMapping;
297     }
298
299     private interface ResolvingStrategy<T> {
300         public T resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker,
301                 String instanceName, String moduleNamespace, EditStrategyType defaultStrategy);
302     }
303 }