2 * Copyright (c) 2013 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.netconf.confignetconfconnector.mapping.config;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.HashMultimap;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Multimap;
17 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
18 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
27 import javax.management.ObjectName;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Map.Entry;
36 import static com.google.common.base.Preconditions.checkState;
37 import static java.lang.String.format;
40 private final Logger logger = LoggerFactory.getLogger(Config.class);
42 private final Map<String/* Namespace from yang file */,
43 Map<String /* Name of module entry from yang file */, ModuleConfig>> moduleConfigs;
44 private final Map<String, ModuleConfig> moduleNamesToConfigs;
46 public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs) {
47 this.moduleConfigs = moduleConfigs;
48 Map<String, ModuleConfig> moduleNamesToConfigs = new HashMap<>();
49 for (Entry<String, Map<String, ModuleConfig>> entry : moduleConfigs.entrySet()) {
50 moduleNamesToConfigs.putAll(entry.getValue());
52 this.moduleNamesToConfigs = Collections.unmodifiableMap(moduleNamesToConfigs);
55 public static Map<String, Map<String, Collection<ObjectName>>> getMappedInstances(Set<ObjectName> instancesToMap,
56 Map<String, Map<String, ModuleConfig>> configs) {
57 Multimap<String, ObjectName> moduleToInstances = mapInstancesToModules(instancesToMap);
59 Map<String, Map<String, Collection<ObjectName>>> retVal = Maps.newLinkedHashMap();
61 for (String namespace : configs.keySet()) {
63 Map<String, Collection<ObjectName>> innerRetVal = Maps.newHashMap();
65 for (Entry<String, ModuleConfig> mbeEntry : configs.get(namespace).entrySet()) {
67 String moduleName = mbeEntry.getKey();
68 Collection<ObjectName> instances = moduleToInstances.get(moduleName);
70 // TODO, this code does not support same module names from different namespaces
71 // Namespace should be present in ObjectName
73 if (instances == null)
76 innerRetVal.put(moduleName, instances);
80 retVal.put(namespace, innerRetVal);
85 private static Multimap<String, ObjectName> mapInstancesToModules(Set<ObjectName> instancesToMap) {
86 Multimap<String, ObjectName> retVal = HashMultimap.create();
88 for (ObjectName objectName : instancesToMap) {
89 String factoryName = ObjectNameUtil.getFactoryName(objectName);
90 retVal.put(factoryName, objectName);
95 // public Element toXml(Set<ObjectName> instancesToMap, String namespace,
96 // Document document) {
97 // return toXml(instancesToMap, Optional.of(namespace), document);
100 public Element toXml(Set<ObjectName> instancesToMap, Optional<String> maybeNamespace, Document document,
101 Element dataElement, ServiceRegistryWrapper serviceTracker) {
103 Map<String, Map<String, Collection<ObjectName>>> moduleToInstances = getMappedInstances(instancesToMap,
106 Element root = dataElement;
107 if (maybeNamespace.isPresent()) {
108 XmlUtil.addNamespaceAttr(root, maybeNamespace.get());
111 Element modulesElement = document.createElement(XmlNetconfConstants.MODULES_KEY);
112 XmlUtil.addNamespaceAttr(modulesElement,
113 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
114 root.appendChild(modulesElement);
115 for (String moduleNamespace : moduleToInstances.keySet()) {
116 for (Entry<String, Collection<ObjectName>> moduleMappingEntry : moduleToInstances.get(moduleNamespace)
119 ModuleConfig mapping = moduleConfigs.get(moduleNamespace).get(moduleMappingEntry.getKey());
121 if (moduleMappingEntry.getValue().isEmpty()) {
125 for (ObjectName objectName : moduleMappingEntry.getValue()) {
126 modulesElement.appendChild(mapping.toXml(objectName, serviceTracker, document, moduleNamespace));
132 root.appendChild(Services.toXml(serviceTracker, document));
137 // TODO refactor, replace string representing namespace with namespace class
138 // TODO refactor, replace Map->Multimap with e.g. ConfigElementResolved
141 public Map<String, Multimap<String, ModuleElementResolved>> fromXmlModulesResolved(XmlElement xml, EditStrategyType defaultEditStrategyType, ServiceRegistryWrapper serviceTracker) {
142 Optional<XmlElement> modulesElement = getModulesElement(xml);
143 List<XmlElement> moduleElements = getModulesElementList(modulesElement);
145 Map<String, Multimap<String, ModuleElementResolved>> retVal = Maps.newHashMap();
147 for (XmlElement moduleElement : moduleElements) {
148 ResolvingStrategy<ModuleElementResolved> resolvingStrategy = new ResolvingStrategy<ModuleElementResolved>() {
150 public ModuleElementResolved resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker, String instanceName, String moduleNamespace, EditStrategyType defaultStrategy) {
151 return moduleMapping.fromXml(moduleElement, serviceTracker,
152 instanceName, moduleNamespace, defaultStrategy);
156 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
162 * return a map containing namespace -> moduleName -> instanceName map. Attribute parsing is omitted.
164 public Map<String, Multimap<String, ModuleElementDefinition>> fromXmlModulesMap(XmlElement xml,
165 EditStrategyType defaultEditStrategyType, ServiceRegistryWrapper serviceTracker) {
166 Optional<XmlElement> modulesElement = getModulesElement(xml);
167 List<XmlElement> moduleElements = getModulesElementList(modulesElement);
169 Map<String, Multimap<String, ModuleElementDefinition>> retVal = Maps.newHashMap();
171 for (XmlElement moduleElement : moduleElements) {
172 ResolvingStrategy<ModuleElementDefinition> resolvingStrategy = new ResolvingStrategy<ModuleElementDefinition>() {
174 public ModuleElementDefinition resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement,
175 ServiceRegistryWrapper serviceTracker, String instanceName, String moduleNamespace,
176 EditStrategyType defaultStrategy) {
177 // TODO: add check for conflicts between global and local
179 String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
180 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
181 return new ModuleElementDefinition(instanceName, perInstanceEditStrategy, defaultStrategy);
185 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
190 private static Optional<XmlElement> getModulesElement(XmlElement xml) {
191 return xml.getOnlyChildElementOptionally(XmlNetconfConstants.MODULES_KEY,
192 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
195 private List<XmlElement> getModulesElementList(Optional<XmlElement> modulesElement) {
196 List<XmlElement> moduleElements;
198 if (modulesElement.isPresent()) {
199 moduleElements = modulesElement.get().getChildElementsWithSameNamespace(XmlNetconfConstants.MODULE_KEY);
200 modulesElement.get().checkUnrecognisedElements(moduleElements);
202 moduleElements = Lists.newArrayList();
204 return moduleElements;
207 private <T> void resolveModule(Map<String, Multimap<String, T>> retVal, ServiceRegistryWrapper serviceTracker,
208 XmlElement moduleElement, EditStrategyType defaultStrategy, ResolvingStrategy<T> resolvingStrategy) {
209 XmlElement typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
210 Entry<String, String> prefixToNamespace = typeElement.findNamespaceOfTextContent();
211 String moduleNamespace = prefixToNamespace.getValue();
212 XmlElement nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
213 String instanceName = nameElement.getTextContent();
214 String factoryNameWithPrefix = typeElement.getTextContent();
215 String prefixOrEmptyString = prefixToNamespace.getKey();
216 String factoryName = getFactoryName(factoryNameWithPrefix, prefixOrEmptyString);
218 ModuleConfig moduleMapping = getModuleMapping(moduleNamespace, instanceName, factoryName);
220 Multimap<String, T> innerMap = retVal.get(moduleNamespace);
221 if (innerMap == null) {
222 innerMap = HashMultimap.create();
223 retVal.put(moduleNamespace, innerMap);
226 T resolvedElement = resolvingStrategy.resolveElement(moduleMapping, moduleElement, serviceTracker,
227 instanceName, moduleNamespace, defaultStrategy);
229 innerMap.put(factoryName, resolvedElement);
232 public Services fromXmlServices(XmlElement xml) {
233 Optional<XmlElement> servicesElement = getServicesElement(xml);
236 if (servicesElement.isPresent()) {
237 services = Services.fromXml(servicesElement.get());
239 services = new Services();
245 private static Optional<XmlElement> getServicesElement(XmlElement xml) {
246 return xml.getOnlyChildElementOptionally(XmlNetconfConstants.SERVICES_KEY,
247 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
250 public static void checkUnrecognisedChildren(XmlElement parent) {
251 Optional<XmlElement> servicesOpt = getServicesElement(parent);
252 Optional<XmlElement> modulesOpt = getModulesElement(parent);
254 List<XmlElement> recognised = Lists.newArrayList();
255 if(servicesOpt.isPresent())
256 recognised.add(servicesOpt.get());
257 if(modulesOpt.isPresent())
258 recognised.add(modulesOpt.get());
260 parent.checkUnrecognisedElements(recognised);
263 private String getFactoryName(String factoryNameWithPrefix, String prefixOrEmptyString) {
265 factoryNameWithPrefix.startsWith(prefixOrEmptyString),
266 format("Internal error: text " + "content '%s' of type node does not start with prefix '%s'",
267 factoryNameWithPrefix, prefixOrEmptyString));
269 int factoryNameAfterPrefixIndex;
270 if (prefixOrEmptyString.isEmpty()) {
271 factoryNameAfterPrefixIndex = 0;
273 factoryNameAfterPrefixIndex = prefixOrEmptyString.length() + 1;
275 return factoryNameWithPrefix.substring(factoryNameAfterPrefixIndex);
278 private ModuleConfig getModuleMapping(String moduleNamespace, String instanceName, String factoryName) {
279 Map<String, ModuleConfig> mappingsFromNamespace = moduleConfigs.get(moduleNamespace);
281 Preconditions.checkNotNull(mappingsFromNamespace,
282 "Namespace %s, defined in: module %s of type %s not found, available namespaces: %s", moduleNamespace,
283 instanceName, factoryName, moduleConfigs.keySet());
285 ModuleConfig moduleMapping = mappingsFromNamespace.get(factoryName);
286 checkState(moduleMapping != null, "Cannot find mapping for module type " + factoryName);
287 return moduleMapping;
290 private interface ResolvingStrategy<T> {
291 public T resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker,
292 String instanceName, String moduleNamespace, EditStrategyType defaultStrategy);