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 static com.google.common.base.Preconditions.checkState;
12 import static java.lang.String.format;
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;
20 import java.util.Map.Entry;
23 import javax.management.ObjectName;
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;
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;
44 private final Logger logger = LoggerFactory.getLogger(Config.class);
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;
50 private final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap;
52 public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs) {
53 this(moduleConfigs, Collections.<String, Map<Date, EditConfig.IdentityMapping>>emptyMap());
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());
62 this.moduleNamesToConfigs = Collections.unmodifiableMap(moduleNamesToConfigs);
63 this.identityMap = identityMap;
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);
70 Map<String, Map<String, Collection<ObjectName>>> retVal = Maps.newLinkedHashMap();
72 for (String namespace : configs.keySet()) {
74 Map<String, Collection<ObjectName>> innerRetVal = Maps.newHashMap();
76 for (Entry<String, ModuleConfig> mbeEntry : configs.get(namespace).entrySet()) {
78 String moduleName = mbeEntry.getKey();
79 Collection<ObjectName> instances = moduleToInstances.get(moduleName);
81 // TODO, this code does not support same module names from different namespaces
82 // Namespace should be present in ObjectName
84 if (instances == null)
87 innerRetVal.put(moduleName, instances);
91 retVal.put(namespace, innerRetVal);
96 private static Multimap<String, ObjectName> mapInstancesToModules(Set<ObjectName> instancesToMap) {
97 Multimap<String, ObjectName> retVal = HashMultimap.create();
99 for (ObjectName objectName : instancesToMap) {
100 String factoryName = ObjectNameUtil.getFactoryName(objectName);
101 retVal.put(factoryName, objectName);
106 // public Element toXml(Set<ObjectName> instancesToMap, String namespace,
107 // Document document) {
108 // return toXml(instancesToMap, Optional.of(namespace), document);
111 public Element toXml(Set<ObjectName> instancesToMap, Optional<String> maybeNamespace, Document document,
112 Element dataElement, ServiceRegistryWrapper serviceTracker) {
114 Map<String, Map<String, Collection<ObjectName>>> moduleToInstances = getMappedInstances(instancesToMap,
117 Element root = dataElement;
118 if (maybeNamespace.isPresent()) {
119 root.setAttributeNS(maybeNamespace.get(), dataElement.getNodeName(), "xmlns");
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)
128 ModuleConfig mapping = moduleConfigs.get(moduleNamespace).get(moduleMappingEntry.getKey());
130 if (moduleMappingEntry.getValue().isEmpty()) {
134 for (ObjectName objectName : moduleMappingEntry.getValue()) {
135 modulesElement.appendChild(mapping.toXml(objectName, serviceTracker, document, moduleNamespace));
141 root.appendChild(Services.toXml(serviceTracker, document));
146 // TODO refactor, replace string representing namespace with namespace class
147 // TODO refactor, replace Map->Multimap with e.g. ConfigElementResolved
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);
154 Map<String, Multimap<String, ModuleElementResolved>> retVal = Maps.newHashMap();
156 for (XmlElement moduleElement : moduleElements) {
157 ResolvingStrategy<ModuleElementResolved> resolvingStrategy = new ResolvingStrategy<ModuleElementResolved>() {
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);
165 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
171 * return a map containing namespace -> moduleName -> instanceName map. Attribute parsing is omitted.
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);
178 Map<String, Multimap<String, ModuleElementDefinition>> retVal = Maps.newHashMap();
180 for (XmlElement moduleElement : moduleElements) {
181 ResolvingStrategy<ModuleElementDefinition> resolvingStrategy = new ResolvingStrategy<ModuleElementDefinition>() {
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
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);
194 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
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);
204 private List<XmlElement> getModulesElementList(Optional<XmlElement> modulesElement) {
205 List<XmlElement> moduleElements;
207 if (modulesElement.isPresent()) {
208 moduleElements = modulesElement.get().getChildElementsWithSameNamespace(XmlNetconfConstants.MODULE_KEY);
209 modulesElement.get().checkUnrecognisedElements(moduleElements);
211 moduleElements = Lists.newArrayList();
213 return moduleElements;
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);
227 ModuleConfig moduleMapping = getModuleMapping(moduleNamespace, instanceName, factoryName);
229 Multimap<String, T> innerMap = retVal.get(moduleNamespace);
230 if (innerMap == null) {
231 innerMap = HashMultimap.create();
232 retVal.put(moduleNamespace, innerMap);
235 T resolvedElement = resolvingStrategy.resolveElement(moduleMapping, moduleElement, serviceTracker,
236 instanceName, moduleNamespace, defaultStrategy);
238 innerMap.put(factoryName, resolvedElement);
241 public Services fromXmlServices(XmlElement xml) {
242 Optional<XmlElement> servicesElement = getServicesElement(xml);
245 if (servicesElement.isPresent()) {
246 services = Services.fromXml(servicesElement.get());
248 services = new Services();
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);
259 public static void checkUnrecognisedChildren(XmlElement parent) {
260 Optional<XmlElement> servicesOpt = getServicesElement(parent);
261 Optional<XmlElement> modulesOpt = getModulesElement(parent);
263 List<XmlElement> recognised = Lists.newArrayList();
264 if(servicesOpt.isPresent())
265 recognised.add(servicesOpt.get());
266 if(modulesOpt.isPresent())
267 recognised.add(modulesOpt.get());
269 parent.checkUnrecognisedElements(recognised);
272 private String getFactoryName(String factoryNameWithPrefix, String prefixOrEmptyString) {
274 factoryNameWithPrefix.startsWith(prefixOrEmptyString),
275 format("Internal error: text " + "content '%s' of type node does not start with prefix '%s'",
276 factoryNameWithPrefix, prefixOrEmptyString));
278 int factoryNameAfterPrefixIndex;
279 if (prefixOrEmptyString.isEmpty()) {
280 factoryNameAfterPrefixIndex = 0;
282 factoryNameAfterPrefixIndex = prefixOrEmptyString.length() + 1;
284 return factoryNameWithPrefix.substring(factoryNameAfterPrefixIndex);
287 private ModuleConfig getModuleMapping(String moduleNamespace, String instanceName, String factoryName) {
288 Map<String, ModuleConfig> mappingsFromNamespace = moduleConfigs.get(moduleNamespace);
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());
294 ModuleConfig moduleMapping = mappingsFromNamespace.get(factoryName);
295 checkState(moduleMapping != null, "Cannot find mapping for module type " + factoryName);
296 return moduleMapping;
299 private interface ResolvingStrategy<T> {
300 public T resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker,
301 String instanceName, String moduleNamespace, EditStrategyType defaultStrategy);