2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.facade.xml.mapping.config;
11 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.HashMultimap;
15 import com.google.common.collect.Lists;
16 import com.google.common.collect.Maps;
17 import com.google.common.collect.Multimap;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.List;
23 import java.util.Map.Entry;
25 import javax.management.ObjectName;
26 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
27 import org.opendaylight.controller.config.facade.xml.mapping.IdentityMapping;
28 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
29 import org.opendaylight.controller.config.facade.xml.strategy.EditStrategyType;
30 import org.opendaylight.controller.config.util.xml.DocumentedException;
31 import org.opendaylight.controller.config.util.xml.XmlElement;
32 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
33 import org.opendaylight.controller.config.util.xml.XmlUtil;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
40 private final Map<String/* Namespace from yang file */,
41 Map<String /* Name of module entry from yang file */, ModuleConfig>> moduleConfigs;
43 private final Map<String, Map<Date, IdentityMapping>> identityMap;
45 private final EnumResolver enumResolver;
47 public Config(final Map<String, Map<String, ModuleConfig>> moduleConfigs, final EnumResolver enumResolver) {
48 this(moduleConfigs, Collections.<String, Map<Date, IdentityMapping>>emptyMap(), enumResolver);
51 public Config(final Map<String, Map<String, ModuleConfig>> moduleConfigs, final Map<String, Map<Date, IdentityMapping>> identityMap, final EnumResolver enumResolver) {
52 this.moduleConfigs = moduleConfigs;
53 this.identityMap = identityMap;
54 this.enumResolver = enumResolver;
57 public static Map<String, Map<String, Collection<ObjectName>>> getMappedInstances(final Set<ObjectName> instancesToMap,
58 final Map<String, Map<String, ModuleConfig>> configs) {
59 Multimap<String, ObjectName> moduleToInstances = mapInstancesToModules(instancesToMap);
61 Map<String, Map<String, Collection<ObjectName>>> retVal = Maps.newLinkedHashMap();
63 for (Entry<String, Map<String, ModuleConfig>> namespaceToModuleToConfigEntry : configs.entrySet()) {
65 Map<String, Collection<ObjectName>> innerRetVal = Maps.newHashMap();
67 for (Entry<String, ModuleConfig> mbeEntry : namespaceToModuleToConfigEntry.getValue().entrySet()) {
69 String moduleName = mbeEntry.getKey();
70 Collection<ObjectName> instances = moduleToInstances.get(moduleName);
72 // TODO, this code does not support same module names from different namespaces
73 // Namespace should be present in ObjectName
75 if (instances == null){
79 innerRetVal.put(moduleName, instances);
83 retVal.put(namespaceToModuleToConfigEntry.getKey(), innerRetVal);
88 private static Multimap<String, ObjectName> mapInstancesToModules(final Set<ObjectName> instancesToMap) {
89 Multimap<String, ObjectName> retVal = HashMultimap.create();
91 for (ObjectName objectName : instancesToMap) {
92 String factoryName = ObjectNameUtil.getFactoryName(objectName);
93 retVal.put(factoryName, objectName);
98 public Element toXml(final Set<ObjectName> instancesToMap, final Optional<String> maybeNamespace, final Document document,
99 final Element dataElement, final ServiceRegistryWrapper serviceTracker) {
101 Map<String, Map<String, Collection<ObjectName>>> moduleToInstances = getMappedInstances(instancesToMap,
104 if (maybeNamespace.isPresent()) {
105 dataElement.setAttributeNS(maybeNamespace.get(), dataElement.getNodeName(), "xmlns");
108 Element modulesElement = XmlUtil.createElement(document, XmlMappingConstants.MODULES_KEY, Optional.of(XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
109 dataElement.appendChild(modulesElement);
110 for (Entry<String, Map<String, Collection<ObjectName>>> moduleToInstanceEntry : moduleToInstances.entrySet()) {
111 for (Entry<String, Collection<ObjectName>> moduleMappingEntry : moduleToInstanceEntry.getValue()
114 ModuleConfig mapping = moduleConfigs.get(moduleToInstanceEntry.getKey()).get(moduleMappingEntry.getKey());
116 if (moduleMappingEntry.getValue().isEmpty()) {
120 for (ObjectName objectName : moduleMappingEntry.getValue()) {
121 modulesElement.appendChild(mapping.toXml(objectName, document, moduleToInstanceEntry.getKey(), enumResolver));
127 dataElement.appendChild(Services.toXml(serviceTracker, document));
132 public Element moduleToXml(final String moduleNamespace, final String factoryName, final String instanceName,
133 final ObjectName instanceON, final Document document) {
134 ModuleConfig moduleConfig = getModuleMapping(moduleNamespace, instanceName, factoryName);
135 return moduleConfig.toXml(instanceON, document, moduleNamespace, enumResolver);
138 // TODO refactor, replace string representing namespace with namespace class
139 // TODO refactor, replace Map->Multimap with e.g. ConfigElementResolved
142 public Map<String, Multimap<String, ModuleElementResolved>> fromXmlModulesResolved(final XmlElement xml, final EditStrategyType defaultEditStrategyType, final ServiceRegistryWrapper serviceTracker) throws DocumentedException {
143 Optional<XmlElement> modulesElement = getModulesElement(xml);
144 List<XmlElement> moduleElements = getModulesElementList(modulesElement);
146 Map<String, Multimap<String, ModuleElementResolved>> retVal = Maps.newHashMap();
148 for (XmlElement moduleElement : moduleElements) {
149 ResolvingStrategy<ModuleElementResolved> resolvingStrategy = new ResolvingStrategy<ModuleElementResolved>() {
151 public ModuleElementResolved resolveElement(final ModuleConfig moduleMapping, final XmlElement moduleElement, final ServiceRegistryWrapper serviceTracker, final String instanceName, final String moduleNamespace, final EditStrategyType defaultStrategy) throws DocumentedException {
152 return moduleMapping.fromXml(moduleElement, serviceTracker,
153 instanceName, moduleNamespace, defaultStrategy, identityMap, enumResolver);
157 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
163 * return a map containing namespace -> moduleName -> instanceName map. Attribute parsing is omitted.
165 public Map<String, Multimap<String, ModuleElementDefinition>> fromXmlModulesMap(final XmlElement xml,
166 final EditStrategyType defaultEditStrategyType, final ServiceRegistryWrapper serviceTracker) throws DocumentedException {
167 Optional<XmlElement> modulesElement = getModulesElement(xml);
168 List<XmlElement> moduleElements = getModulesElementList(modulesElement);
170 Map<String, Multimap<String, ModuleElementDefinition>> retVal = Maps.newHashMap();
172 for (XmlElement moduleElement : moduleElements) {
173 ResolvingStrategy<ModuleElementDefinition> resolvingStrategy = new ResolvingStrategy<ModuleElementDefinition>() {
175 public ModuleElementDefinition resolveElement(final ModuleConfig moduleMapping, final XmlElement moduleElement,
176 final ServiceRegistryWrapper serviceTracker, final String instanceName, final String moduleNamespace,
177 final EditStrategyType defaultStrategy) {
178 // TODO: add check for conflicts between global and local
180 String perInstanceEditStrategy = moduleElement.getAttribute(XmlMappingConstants.OPERATION_ATTR_KEY,
181 XmlMappingConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
182 return new ModuleElementDefinition(instanceName, perInstanceEditStrategy, defaultStrategy);
186 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
191 private static Optional<XmlElement> getModulesElement(final XmlElement xml) {
192 return xml.getOnlyChildElementOptionally(XmlMappingConstants.MODULES_KEY,
193 XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
196 private List<XmlElement> getModulesElementList(final Optional<XmlElement> modulesElement) throws DocumentedException {
197 List<XmlElement> moduleElements;
199 if (modulesElement.isPresent()) {
200 moduleElements = modulesElement.get().getChildElementsWithSameNamespace(XmlMappingConstants.MODULE_KEY);
201 modulesElement.get().checkUnrecognisedElements(moduleElements);
203 moduleElements = Lists.newArrayList();
205 return moduleElements;
208 private <T> void resolveModule(final Map<String, Multimap<String, T>> retVal, final ServiceRegistryWrapper serviceTracker,
209 final XmlElement moduleElement, final EditStrategyType defaultStrategy, final ResolvingStrategy<T> resolvingStrategy) throws DocumentedException {
210 XmlElement typeElement = null;
211 typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.TYPE_KEY);
212 Entry<String, String> prefixToNamespace = typeElement.findNamespaceOfTextContent();
213 String moduleNamespace = prefixToNamespace.getValue();
214 XmlElement nameElement = null;
215 nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlMappingConstants.NAME_KEY);
216 String instanceName = nameElement.getTextContent();
217 String factoryNameWithPrefix = typeElement.getTextContent();
218 String prefixOrEmptyString = prefixToNamespace.getKey();
219 String factoryName = getFactoryName(factoryNameWithPrefix, prefixOrEmptyString);
221 ModuleConfig moduleMapping = getModuleMapping(moduleNamespace, instanceName, factoryName);
223 Multimap<String, T> innerMap = retVal.computeIfAbsent(moduleNamespace, k -> HashMultimap.create());
225 T resolvedElement = resolvingStrategy.resolveElement(moduleMapping, moduleElement, serviceTracker,
226 instanceName, moduleNamespace, defaultStrategy);
228 innerMap.put(factoryName, resolvedElement);
231 public Services fromXmlServices(final XmlElement xml) throws DocumentedException {
232 Optional<XmlElement> servicesElement = getServicesElement(xml);
235 if (servicesElement.isPresent()) {
236 services = Services.fromXml(servicesElement.get());
238 services = new Services();
244 private static Optional<XmlElement> getServicesElement(final XmlElement xml) {
245 return xml.getOnlyChildElementOptionally(XmlMappingConstants.SERVICES_KEY,
246 XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
249 public static void checkUnrecognisedChildren(final XmlElement parent) throws DocumentedException {
250 Optional<XmlElement> servicesOpt = getServicesElement(parent);
251 Optional<XmlElement> modulesOpt = getModulesElement(parent);
253 List<XmlElement> recognised = Lists.newArrayList();
254 if(servicesOpt.isPresent()){
255 recognised.add(servicesOpt.get());
257 if(modulesOpt.isPresent()){
258 recognised.add(modulesOpt.get());
261 parent.checkUnrecognisedElements(recognised);
264 private String getFactoryName(final String factoryNameWithPrefix, final String prefixOrEmptyString) {
266 factoryNameWithPrefix.startsWith(prefixOrEmptyString),
267 String.format("Internal error: text " + "content '%s' of type node does not start with prefix '%s'",
268 factoryNameWithPrefix, prefixOrEmptyString));
270 int factoryNameAfterPrefixIndex;
271 if (prefixOrEmptyString.isEmpty()) {
272 factoryNameAfterPrefixIndex = 0;
274 factoryNameAfterPrefixIndex = prefixOrEmptyString.length() + 1;
276 return factoryNameWithPrefix.substring(factoryNameAfterPrefixIndex);
279 private ModuleConfig getModuleMapping(final String moduleNamespace, final String instanceName, final String factoryName) {
280 Map<String, ModuleConfig> mappingsFromNamespace = moduleConfigs.get(moduleNamespace);
282 Preconditions.checkNotNull(mappingsFromNamespace,
283 "Namespace %s, defined in: module %s of type %s not found, available namespaces: %s", moduleNamespace,
284 instanceName, factoryName, moduleConfigs.keySet());
286 ModuleConfig moduleMapping = mappingsFromNamespace.get(factoryName);
287 checkState(moduleMapping != null, "Cannot find mapping for module type " + factoryName);
288 return moduleMapping;
291 private interface ResolvingStrategy<T> {
292 T resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker,
293 String instanceName, String moduleNamespace, EditStrategyType defaultStrategy) throws DocumentedException;