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;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.HashMultimap;
16 import com.google.common.collect.Lists;
17 import com.google.common.collect.Maps;
18 import com.google.common.collect.Multimap;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.List;
24 import java.util.Map.Entry;
26 import javax.management.ObjectName;
27 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
28 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
29 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
30 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
31 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
32 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.EnumResolver;
33 import org.opendaylight.controller.netconf.util.xml.XmlElement;
34 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
41 private final Map<String/* Namespace from yang file */,
42 Map<String /* Name of module entry from yang file */, ModuleConfig>> moduleConfigs;
44 private final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap;
46 private final EnumResolver enumResolver;
48 public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs, final EnumResolver enumResolver) {
49 this(moduleConfigs, Collections.<String, Map<Date, EditConfig.IdentityMapping>>emptyMap(), enumResolver);
52 public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs, Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap, final EnumResolver enumResolver) {
53 this.moduleConfigs = moduleConfigs;
54 this.identityMap = identityMap;
55 this.enumResolver = enumResolver;
58 public static Map<String, Map<String, Collection<ObjectName>>> getMappedInstances(Set<ObjectName> instancesToMap,
59 Map<String, Map<String, ModuleConfig>> configs) {
60 Multimap<String, ObjectName> moduleToInstances = mapInstancesToModules(instancesToMap);
62 Map<String, Map<String, Collection<ObjectName>>> retVal = Maps.newLinkedHashMap();
64 for (Entry<String, Map<String, ModuleConfig>> namespaceToModuleToConfigEntry : configs.entrySet()) {
66 Map<String, Collection<ObjectName>> innerRetVal = Maps.newHashMap();
68 for (Entry<String, ModuleConfig> mbeEntry : namespaceToModuleToConfigEntry.getValue().entrySet()) {
70 String moduleName = mbeEntry.getKey();
71 Collection<ObjectName> instances = moduleToInstances.get(moduleName);
73 // TODO, this code does not support same module names from different namespaces
74 // Namespace should be present in ObjectName
76 if (instances == null){
80 innerRetVal.put(moduleName, instances);
84 retVal.put(namespaceToModuleToConfigEntry.getKey(), innerRetVal);
89 private static Multimap<String, ObjectName> mapInstancesToModules(Set<ObjectName> instancesToMap) {
90 Multimap<String, ObjectName> retVal = HashMultimap.create();
92 for (ObjectName objectName : instancesToMap) {
93 String factoryName = ObjectNameUtil.getFactoryName(objectName);
94 retVal.put(factoryName, objectName);
99 public Element toXml(Set<ObjectName> instancesToMap, Optional<String> maybeNamespace, Document document,
100 Element dataElement, ServiceRegistryWrapper serviceTracker) {
102 Map<String, Map<String, Collection<ObjectName>>> moduleToInstances = getMappedInstances(instancesToMap,
105 if (maybeNamespace.isPresent()) {
106 dataElement.setAttributeNS(maybeNamespace.get(), dataElement.getNodeName(), "xmlns");
109 Element modulesElement = XmlUtil.createElement(document, XmlNetconfConstants.MODULES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
110 dataElement.appendChild(modulesElement);
111 for (Entry<String, Map<String, Collection<ObjectName>>> moduleToInstanceEntry : moduleToInstances.entrySet()) {
112 for (Entry<String, Collection<ObjectName>> moduleMappingEntry : moduleToInstanceEntry.getValue()
115 ModuleConfig mapping = moduleConfigs.get(moduleToInstanceEntry.getKey()).get(moduleMappingEntry.getKey());
117 if (moduleMappingEntry.getValue().isEmpty()) {
121 for (ObjectName objectName : moduleMappingEntry.getValue()) {
122 modulesElement.appendChild(mapping.toXml(objectName, document, moduleToInstanceEntry.getKey(), enumResolver));
128 dataElement.appendChild(Services.toXml(serviceTracker, document));
133 // TODO refactor, replace string representing namespace with namespace class
134 // TODO refactor, replace Map->Multimap with e.g. ConfigElementResolved
137 public Map<String, Multimap<String, ModuleElementResolved>> fromXmlModulesResolved(XmlElement xml, EditStrategyType defaultEditStrategyType, ServiceRegistryWrapper serviceTracker) throws NetconfDocumentedException {
138 Optional<XmlElement> modulesElement = getModulesElement(xml);
139 List<XmlElement> moduleElements = getModulesElementList(modulesElement);
141 Map<String, Multimap<String, ModuleElementResolved>> retVal = Maps.newHashMap();
143 for (XmlElement moduleElement : moduleElements) {
144 ResolvingStrategy<ModuleElementResolved> resolvingStrategy = new ResolvingStrategy<ModuleElementResolved>() {
146 public ModuleElementResolved resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker, String instanceName, String moduleNamespace, EditStrategyType defaultStrategy) throws NetconfDocumentedException {
147 return moduleMapping.fromXml(moduleElement, serviceTracker,
148 instanceName, moduleNamespace, defaultStrategy, identityMap, enumResolver);
152 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
158 * return a map containing namespace -> moduleName -> instanceName map. Attribute parsing is omitted.
160 public Map<String, Multimap<String, ModuleElementDefinition>> fromXmlModulesMap(XmlElement xml,
161 EditStrategyType defaultEditStrategyType, ServiceRegistryWrapper serviceTracker) throws NetconfDocumentedException {
162 Optional<XmlElement> modulesElement = getModulesElement(xml);
163 List<XmlElement> moduleElements = getModulesElementList(modulesElement);
165 Map<String, Multimap<String, ModuleElementDefinition>> retVal = Maps.newHashMap();
167 for (XmlElement moduleElement : moduleElements) {
168 ResolvingStrategy<ModuleElementDefinition> resolvingStrategy = new ResolvingStrategy<ModuleElementDefinition>() {
170 public ModuleElementDefinition resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement,
171 ServiceRegistryWrapper serviceTracker, String instanceName, String moduleNamespace,
172 EditStrategyType defaultStrategy) {
173 // TODO: add check for conflicts between global and local
175 String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
176 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
177 return new ModuleElementDefinition(instanceName, perInstanceEditStrategy, defaultStrategy);
181 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
186 private static Optional<XmlElement> getModulesElement(XmlElement xml) {
187 return xml.getOnlyChildElementOptionally(XmlNetconfConstants.MODULES_KEY,
188 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
191 private List<XmlElement> getModulesElementList(Optional<XmlElement> modulesElement) throws NetconfDocumentedException {
192 List<XmlElement> moduleElements;
194 if (modulesElement.isPresent()) {
195 moduleElements = modulesElement.get().getChildElementsWithSameNamespace(XmlNetconfConstants.MODULE_KEY);
196 modulesElement.get().checkUnrecognisedElements(moduleElements);
198 moduleElements = Lists.newArrayList();
200 return moduleElements;
203 private <T> void resolveModule(Map<String, Multimap<String, T>> retVal, ServiceRegistryWrapper serviceTracker,
204 XmlElement moduleElement, EditStrategyType defaultStrategy, ResolvingStrategy<T> resolvingStrategy) throws NetconfDocumentedException {
205 XmlElement typeElement = null;
206 typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
207 Entry<String, String> prefixToNamespace = typeElement.findNamespaceOfTextContent();
208 String moduleNamespace = prefixToNamespace.getValue();
209 XmlElement nameElement = null;
210 nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
211 String instanceName = nameElement.getTextContent();
212 String factoryNameWithPrefix = typeElement.getTextContent();
213 String prefixOrEmptyString = prefixToNamespace.getKey();
214 String factoryName = getFactoryName(factoryNameWithPrefix, prefixOrEmptyString);
216 ModuleConfig moduleMapping = getModuleMapping(moduleNamespace, instanceName, factoryName);
218 Multimap<String, T> innerMap = retVal.get(moduleNamespace);
219 if (innerMap == null) {
220 innerMap = HashMultimap.create();
221 retVal.put(moduleNamespace, innerMap);
224 T resolvedElement = resolvingStrategy.resolveElement(moduleMapping, moduleElement, serviceTracker,
225 instanceName, moduleNamespace, defaultStrategy);
227 innerMap.put(factoryName, resolvedElement);
230 public Services fromXmlServices(XmlElement xml) throws NetconfDocumentedException {
231 Optional<XmlElement> servicesElement = getServicesElement(xml);
234 if (servicesElement.isPresent()) {
235 services = Services.fromXml(servicesElement.get());
237 services = new Services();
243 private static Optional<XmlElement> getServicesElement(XmlElement xml) {
244 return xml.getOnlyChildElementOptionally(XmlNetconfConstants.SERVICES_KEY,
245 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
248 public static void checkUnrecognisedChildren(XmlElement parent) throws NetconfDocumentedException {
249 Optional<XmlElement> servicesOpt = getServicesElement(parent);
250 Optional<XmlElement> modulesOpt = getModulesElement(parent);
252 List<XmlElement> recognised = Lists.newArrayList();
253 if(servicesOpt.isPresent()){
254 recognised.add(servicesOpt.get());
256 if(modulesOpt.isPresent()){
257 recognised.add(modulesOpt.get());
260 parent.checkUnrecognisedElements(recognised);
263 private String getFactoryName(String factoryNameWithPrefix, String prefixOrEmptyString) {
265 factoryNameWithPrefix.startsWith(prefixOrEmptyString),
266 String.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) throws NetconfDocumentedException;