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;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.List;
25 import java.util.Map.Entry;
28 import javax.management.ObjectName;
30 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
31 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
32 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
33 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
34 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
35 import org.opendaylight.controller.netconf.util.xml.XmlElement;
36 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
43 private final Map<String/* Namespace from yang file */,
44 Map<String /* Name of module entry from yang file */, ModuleConfig>> moduleConfigs;
46 private final Map<String, Map<Date, EditConfig.IdentityMapping>> identityMap;
48 public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs) {
49 this(moduleConfigs, Collections.<String, Map<Date, EditConfig.IdentityMapping>>emptyMap());
52 public Config(Map<String, Map<String, ModuleConfig>> moduleConfigs, Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) {
53 this.moduleConfigs = moduleConfigs;
54 this.identityMap = identityMap;
57 public static Map<String, Map<String, Collection<ObjectName>>> getMappedInstances(Set<ObjectName> instancesToMap,
58 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 (String namespace : configs.keySet()) {
65 Map<String, Collection<ObjectName>> innerRetVal = Maps.newHashMap();
67 for (Entry<String, ModuleConfig> mbeEntry : configs.get(namespace).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(namespace, innerRetVal);
88 private static Multimap<String, ObjectName> mapInstancesToModules(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(Set<ObjectName> instancesToMap, Optional<String> maybeNamespace, Document document,
99 Element dataElement, 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, XmlNetconfConstants.MODULES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
109 dataElement.appendChild(modulesElement);
110 for (String moduleNamespace : moduleToInstances.keySet()) {
111 for (Entry<String, Collection<ObjectName>> moduleMappingEntry : moduleToInstances.get(moduleNamespace)
114 ModuleConfig mapping = moduleConfigs.get(moduleNamespace).get(moduleMappingEntry.getKey());
116 if (moduleMappingEntry.getValue().isEmpty()) {
120 for (ObjectName objectName : moduleMappingEntry.getValue()) {
121 modulesElement.appendChild(mapping.toXml(objectName, document, moduleNamespace));
127 dataElement.appendChild(Services.toXml(serviceTracker, document));
132 // TODO refactor, replace string representing namespace with namespace class
133 // TODO refactor, replace Map->Multimap with e.g. ConfigElementResolved
136 public Map<String, Multimap<String, ModuleElementResolved>> fromXmlModulesResolved(XmlElement xml, EditStrategyType defaultEditStrategyType, ServiceRegistryWrapper serviceTracker) throws NetconfDocumentedException {
137 Optional<XmlElement> modulesElement = getModulesElement(xml);
138 List<XmlElement> moduleElements = getModulesElementList(modulesElement);
140 Map<String, Multimap<String, ModuleElementResolved>> retVal = Maps.newHashMap();
142 for (XmlElement moduleElement : moduleElements) {
143 ResolvingStrategy<ModuleElementResolved> resolvingStrategy = new ResolvingStrategy<ModuleElementResolved>() {
145 public ModuleElementResolved resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker, String instanceName, String moduleNamespace, EditStrategyType defaultStrategy) throws NetconfDocumentedException {
146 return moduleMapping.fromXml(moduleElement, serviceTracker,
147 instanceName, moduleNamespace, defaultStrategy, identityMap);
151 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
157 * return a map containing namespace -> moduleName -> instanceName map. Attribute parsing is omitted.
159 public Map<String, Multimap<String, ModuleElementDefinition>> fromXmlModulesMap(XmlElement xml,
160 EditStrategyType defaultEditStrategyType, ServiceRegistryWrapper serviceTracker) throws NetconfDocumentedException {
161 Optional<XmlElement> modulesElement = getModulesElement(xml);
162 List<XmlElement> moduleElements = getModulesElementList(modulesElement);
164 Map<String, Multimap<String, ModuleElementDefinition>> retVal = Maps.newHashMap();
166 for (XmlElement moduleElement : moduleElements) {
167 ResolvingStrategy<ModuleElementDefinition> resolvingStrategy = new ResolvingStrategy<ModuleElementDefinition>() {
169 public ModuleElementDefinition resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement,
170 ServiceRegistryWrapper serviceTracker, String instanceName, String moduleNamespace,
171 EditStrategyType defaultStrategy) {
172 // TODO: add check for conflicts between global and local
174 String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
175 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
176 return new ModuleElementDefinition(instanceName, perInstanceEditStrategy, defaultStrategy);
180 resolveModule(retVal, serviceTracker, moduleElement, defaultEditStrategyType, resolvingStrategy);
185 private static Optional<XmlElement> getModulesElement(XmlElement xml) {
186 return xml.getOnlyChildElementOptionally(XmlNetconfConstants.MODULES_KEY,
187 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
190 private List<XmlElement> getModulesElementList(Optional<XmlElement> modulesElement) throws NetconfDocumentedException {
191 List<XmlElement> moduleElements;
193 if (modulesElement.isPresent()) {
194 moduleElements = modulesElement.get().getChildElementsWithSameNamespace(XmlNetconfConstants.MODULE_KEY);
195 modulesElement.get().checkUnrecognisedElements(moduleElements);
197 moduleElements = Lists.newArrayList();
199 return moduleElements;
202 private <T> void resolveModule(Map<String, Multimap<String, T>> retVal, ServiceRegistryWrapper serviceTracker,
203 XmlElement moduleElement, EditStrategyType defaultStrategy, ResolvingStrategy<T> resolvingStrategy) throws NetconfDocumentedException {
204 XmlElement typeElement = null;
205 typeElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
206 Entry<String, String> prefixToNamespace = typeElement.findNamespaceOfTextContent();
207 String moduleNamespace = prefixToNamespace.getValue();
208 XmlElement nameElement = null;
209 nameElement = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
210 String instanceName = nameElement.getTextContent();
211 String factoryNameWithPrefix = typeElement.getTextContent();
212 String prefixOrEmptyString = prefixToNamespace.getKey();
213 String factoryName = getFactoryName(factoryNameWithPrefix, prefixOrEmptyString);
215 ModuleConfig moduleMapping = getModuleMapping(moduleNamespace, instanceName, factoryName);
217 Multimap<String, T> innerMap = retVal.get(moduleNamespace);
218 if (innerMap == null) {
219 innerMap = HashMultimap.create();
220 retVal.put(moduleNamespace, innerMap);
223 T resolvedElement = resolvingStrategy.resolveElement(moduleMapping, moduleElement, serviceTracker,
224 instanceName, moduleNamespace, defaultStrategy);
226 innerMap.put(factoryName, resolvedElement);
229 public Services fromXmlServices(XmlElement xml) throws NetconfDocumentedException {
230 Optional<XmlElement> servicesElement = getServicesElement(xml);
233 if (servicesElement.isPresent()) {
234 services = Services.fromXml(servicesElement.get());
236 services = new Services();
242 private static Optional<XmlElement> getServicesElement(XmlElement xml) {
243 return xml.getOnlyChildElementOptionally(XmlNetconfConstants.SERVICES_KEY,
244 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
247 public static void checkUnrecognisedChildren(XmlElement parent) throws NetconfDocumentedException {
248 Optional<XmlElement> servicesOpt = getServicesElement(parent);
249 Optional<XmlElement> modulesOpt = getModulesElement(parent);
251 List<XmlElement> recognised = Lists.newArrayList();
252 if(servicesOpt.isPresent()){
253 recognised.add(servicesOpt.get());
255 if(modulesOpt.isPresent()){
256 recognised.add(modulesOpt.get());
259 parent.checkUnrecognisedElements(recognised);
262 private String getFactoryName(String factoryNameWithPrefix, String prefixOrEmptyString) {
264 factoryNameWithPrefix.startsWith(prefixOrEmptyString),
265 String.format("Internal error: text " + "content '%s' of type node does not start with prefix '%s'",
266 factoryNameWithPrefix, prefixOrEmptyString));
268 int factoryNameAfterPrefixIndex;
269 if (prefixOrEmptyString.isEmpty()) {
270 factoryNameAfterPrefixIndex = 0;
272 factoryNameAfterPrefixIndex = prefixOrEmptyString.length() + 1;
274 return factoryNameWithPrefix.substring(factoryNameAfterPrefixIndex);
277 private ModuleConfig getModuleMapping(String moduleNamespace, String instanceName, String factoryName) {
278 Map<String, ModuleConfig> mappingsFromNamespace = moduleConfigs.get(moduleNamespace);
280 Preconditions.checkNotNull(mappingsFromNamespace,
281 "Namespace %s, defined in: module %s of type %s not found, available namespaces: %s", moduleNamespace,
282 instanceName, factoryName, moduleConfigs.keySet());
284 ModuleConfig moduleMapping = mappingsFromNamespace.get(factoryName);
285 checkState(moduleMapping != null, "Cannot find mapping for module type " + factoryName);
286 return moduleMapping;
289 private interface ResolvingStrategy<T> {
290 public T resolveElement(ModuleConfig moduleMapping, XmlElement moduleElement, ServiceRegistryWrapper serviceTracker,
291 String instanceName, String moduleNamespace, EditStrategyType defaultStrategy) throws NetconfDocumentedException;