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.Lists;
14 import com.google.common.collect.Maps;
15 import org.opendaylight.controller.config.util.ConfigRegistryClient;
16 import org.opendaylight.controller.config.yangjmxgenerator.RuntimeBeanEntry;
17 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeReadingStrategy;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectXmlReader;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.AttributeResolvingStrategy;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.ObjectResolver;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.AttributeWritingStrategy;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
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.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
35 import javax.management.ObjectName;
36 import javax.management.openmbean.OpenType;
37 import java.util.List;
39 import java.util.Map.Entry;
41 public final class InstanceConfig {
42 private static final Logger logger = LoggerFactory.getLogger(InstanceConfig.class);
44 private final Map<String, AttributeIfc> yangToAttrConfig;
45 private final Map<String, AttributeIfc> jmxToAttrConfig;
46 private final ConfigRegistryClient configRegistryClient;
48 public InstanceConfig(ConfigRegistryClient configRegistryClient, Map<String, AttributeIfc> yangNamesToAttributes) {
49 this.yangToAttrConfig = yangNamesToAttributes;
50 this.jmxToAttrConfig = reverseMap(yangNamesToAttributes);
51 this.configRegistryClient = configRegistryClient;
54 private Map<String, Object> getMappedConfiguration(ObjectName on, Services depTracker) {
56 // TODO make field, mappingStrategies can be instantiated only once
57 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper(depTracker)
58 .prepareMapping(jmxToAttrConfig);
60 Map<String, Object> toXml = Maps.newHashMap();
62 for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) {
64 // Skip children runtime beans as they are mapped by InstanceRuntime
65 if (configDefEntry.getValue() instanceof RuntimeBeanEntry)
68 Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
70 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies
71 .get(configDefEntry.getKey());
72 Optional<?> a = attributeMappingStrategy.mapAttribute(value);
73 if (a.isPresent() == false)
76 toXml.put(configDefEntry.getValue().getAttributeYangName(), a.get());
77 } catch (Exception e) {
78 throw new IllegalStateException("Unable to map value " + value + " to attribute "
79 + configDefEntry.getKey(), e);
86 public Element toXml(ObjectName on, Services depTracker, String namespace, Document document, Element rootElement) {
88 Element cfgElement = rootElement;
90 Map<String, AttributeWritingStrategy> strats = new ObjectXmlWriter().prepareWriting(yangToAttrConfig, document);
92 Map<String, Object> mappedConfig = getMappedConfiguration(on, depTracker);
94 for (Entry<String, ?> mappingEntry : mappedConfig.entrySet()) {
96 strats.get(mappingEntry.getKey()).writeElement(cfgElement, namespace, mappingEntry.getValue());
97 } catch (Exception e) {
98 throw new IllegalStateException("Unable to write value " + mappingEntry.getValue() + " for attribute "
99 + mappingEntry.getValue(), e);
106 private void resolveConfiguration(InstanceConfigElementResolved mappedConfig, Services depTracker) {
108 // TODO make field, resolvingStrategies can be instantiated only once
109 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(
110 depTracker).prepareResolving(yangToAttrConfig);
112 for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) {
113 AttributeConfigElement value = configDefEntry.getValue();
114 String attributeName = configDefEntry.getKey();
116 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
118 logger.trace("Trying to set value {} of attribute {} with {}", value, attributeName, attributeResolvingStrategy);
120 value.resolveValue(attributeResolvingStrategy, attributeName);
122 yangToAttrConfig.get(attributeName).getUpperCaseCammelCase());
123 } catch (Exception e) {
124 throw new IllegalStateException("Unable to resolve value " + value
125 + " to attribute " + attributeName, e);
130 public InstanceConfigElementResolved fromXml(XmlElement moduleElement, Services services, String moduleNamespace,
131 EditStrategyType defaultStrategy) {
132 Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
134 Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig);
135 List<XmlElement> recognisedChildren = Lists.newArrayList();
137 XmlElement type = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
138 XmlElement name = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
139 List<XmlElement> typeAndName = Lists.newArrayList(type, name);
141 for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
142 List<XmlElement> configNodes = getConfigNodes(moduleElement, moduleNamespace, readStratEntry.getKey(),
143 recognisedChildren, typeAndName);
144 AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
145 retVal.put(readStratEntry.getKey(), readElement);
148 recognisedChildren.addAll(typeAndName);
149 moduleElement.checkUnrecognisedElements(recognisedChildren);
151 // TODO: add check for conflicts between global and local edit strategy
152 String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
153 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
155 InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("") ? new InstanceConfigElementResolved(
156 retVal, defaultStrategy) : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy);
158 resolveConfiguration(instanceConfigElementResolved, services);
159 return instanceConfigElementResolved;
162 private List<XmlElement> getConfigNodes(XmlElement moduleElement, String moduleNamespace, String name,
163 List<XmlElement> recognisedChildren, List<XmlElement> typeAndName) {
164 List<XmlElement> foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name, moduleNamespace);
165 if (foundConfigNodes.isEmpty()) {
166 logger.debug("No config nodes {}:{} found in {}", moduleNamespace, name, moduleElement);
167 logger.debug("Trying lookup of config nodes without specified namespace");
168 foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name,
169 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
170 // In case module type or name element is not present in config it
171 // would be matched with config type or name
172 // We need to remove config type and name from available module
174 foundConfigNodes.removeAll(typeAndName);
175 logger.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
178 List<XmlElement> foundWithoutNamespaceNodes = moduleElement.getChildElementsWithinNamespace(name,
179 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
180 foundWithoutNamespaceNodes.removeAll(typeAndName);
181 Preconditions.checkState(foundWithoutNamespaceNodes.isEmpty(),
182 "Element %s present multiple times with different namespaces: %s, %s", name, foundConfigNodes,
183 foundWithoutNamespaceNodes);
186 recognisedChildren.addAll(foundConfigNodes);
187 return foundConfigNodes;
190 private static Map<String, AttributeIfc> reverseMap(Map<String, AttributeIfc> yangNameToAttr) {
191 Map<String, AttributeIfc> reversednameToAtr = Maps.newHashMap();
193 for (Entry<String, AttributeIfc> entry : yangNameToAttr.entrySet()) {
194 reversednameToAtr.put(entry.getValue().getUpperCaseCammelCase(), entry.getValue());
197 return reversednameToAtr;