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.collect.Lists;
13 import com.google.common.collect.Maps;
14 import com.google.common.collect.Multimap;
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.api.NetconfDocumentedException;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeConfigElement;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.AttributeReadingStrategy;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectXmlReader;
22 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.AttributeMappingStrategy;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping.ObjectMapper;
24 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.AttributeResolvingStrategy;
25 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.resolving.ObjectResolver;
26 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.AttributeWritingStrategy;
27 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml.ObjectXmlWriter;
28 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
29 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditStrategyType;
30 import org.opendaylight.controller.netconf.util.xml.XmlElement;
31 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
37 import javax.management.ObjectName;
38 import javax.management.openmbean.OpenType;
39 import java.util.Date;
40 import java.util.List;
42 import java.util.Map.Entry;
44 public final class InstanceConfig {
45 private static final Logger logger = LoggerFactory.getLogger(InstanceConfig.class);
47 private final Map<String, AttributeIfc> yangToAttrConfig;
48 private final Map<String, AttributeIfc> jmxToAttrConfig;
49 private final ConfigRegistryClient configRegistryClient;
51 public InstanceConfig(ConfigRegistryClient configRegistryClient, Map<String, AttributeIfc> yangNamesToAttributes) {
52 this.yangToAttrConfig = yangNamesToAttributes;
53 this.jmxToAttrConfig = reverseMap(yangNamesToAttributes);
54 this.configRegistryClient = configRegistryClient;
57 private Map<String, Object> getMappedConfiguration(ObjectName on, ServiceRegistryWrapper depTracker) {
59 // TODO make field, mappingStrategies can be instantiated only once
60 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper(depTracker)
61 .prepareMapping(jmxToAttrConfig);
63 Map<String, Object> toXml = Maps.newHashMap();
65 for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) {
66 // Skip children runtime beans as they are mapped by InstanceRuntime
67 if (configDefEntry.getValue() instanceof RuntimeBeanEntry){
70 Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
72 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies
73 .get(configDefEntry.getKey());
74 Optional<?> a = attributeMappingStrategy.mapAttribute(value);
78 toXml.put(configDefEntry.getValue().getAttributeYangName(), a.get());
79 } catch (Exception e) {
80 throw new IllegalStateException("Unable to map value " + value + " to attribute "
81 + configDefEntry.getKey(), e);
87 public Element toXml(ObjectName on, ServiceRegistryWrapper depTracker, String namespace, Document document, Element rootElement) {
89 Element cfgElement = rootElement;
91 Map<String, AttributeWritingStrategy> strats = new ObjectXmlWriter().prepareWriting(yangToAttrConfig, document);
93 Map<String, Object> mappedConfig = getMappedConfiguration(on, depTracker);
95 for (Entry<String, ?> mappingEntry : mappedConfig.entrySet()) {
97 strats.get(mappingEntry.getKey()).writeElement(cfgElement, namespace, mappingEntry.getValue());
98 } catch (Exception e) {
99 throw new IllegalStateException("Unable to write value " + mappingEntry.getValue() + " for attribute "
100 + mappingEntry.getValue(), e);
107 private void resolveConfiguration(InstanceConfigElementResolved mappedConfig, ServiceRegistryWrapper depTracker) {
109 // TODO make field, resolvingStrategies can be instantiated only once
110 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(
111 depTracker).prepareResolving(yangToAttrConfig);
113 for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) {
114 AttributeConfigElement value = configDefEntry.getValue();
115 String attributeName = configDefEntry.getKey();
117 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
119 logger.trace("Trying to set value {} of attribute {} with {}", value, attributeName, attributeResolvingStrategy);
121 value.resolveValue(attributeResolvingStrategy, attributeName);
123 yangToAttrConfig.get(attributeName).getUpperCaseCammelCase());
124 } catch (Exception e) {
125 throw new IllegalStateException("Unable to resolve value " + value
126 + " to attribute " + attributeName, e);
131 public InstanceConfigElementResolved fromXml(XmlElement moduleElement, ServiceRegistryWrapper services, String moduleNamespace,
132 EditStrategyType defaultStrategy, Multimap<String, String> providedServices, Map<String, Map<Date,EditConfig.IdentityMapping>> identityMap) throws NetconfDocumentedException {
133 Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
135 Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig, identityMap);
136 List<XmlElement> recognisedChildren = Lists.newArrayList();
138 XmlElement type = null;
139 XmlElement name = null;
140 type = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
141 name = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
142 List<XmlElement> typeAndName = Lists.newArrayList(type, name);
144 for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
145 List<XmlElement> configNodes = getConfigNodes(moduleElement, moduleNamespace, readStratEntry.getKey(),
146 recognisedChildren, typeAndName);
147 AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
148 retVal.put(readStratEntry.getKey(), readElement);
151 recognisedChildren.addAll(typeAndName);
152 moduleElement.checkUnrecognisedElements(recognisedChildren);
154 // TODO: add check for conflicts between global and local edit strategy
155 String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
156 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
158 InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("") ? new InstanceConfigElementResolved(
159 retVal, defaultStrategy, providedServices) : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal, defaultStrategy, providedServices);
161 resolveConfiguration(instanceConfigElementResolved, services);
162 return instanceConfigElementResolved;
165 private List<XmlElement> getConfigNodes(XmlElement moduleElement, String moduleNamespace, String name,
166 List<XmlElement> recognisedChildren, List<XmlElement> typeAndName) throws NetconfDocumentedException {
167 List<XmlElement> foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name, moduleNamespace);
168 if (foundConfigNodes.isEmpty()) {
169 logger.debug("No config nodes {}:{} found in {}", moduleNamespace, name, moduleElement);
170 logger.debug("Trying lookup of config nodes without specified namespace");
171 foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name,
172 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
173 // In case module type or name element is not present in config it
174 // would be matched with config type or name
175 // We need to remove config type and name from available module
177 foundConfigNodes.removeAll(typeAndName);
178 logger.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
181 List<XmlElement> foundWithoutNamespaceNodes = moduleElement.getChildElementsWithinNamespace(name,
182 XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
183 foundWithoutNamespaceNodes.removeAll(typeAndName);
184 if (!foundWithoutNamespaceNodes.isEmpty()){
185 throw new NetconfDocumentedException(String.format("Element %s present multiple times with different namespaces: %s, %s", name, foundConfigNodes,
186 foundWithoutNamespaceNodes),
187 NetconfDocumentedException.ErrorType.application,
188 NetconfDocumentedException.ErrorTag.invalid_value,
189 NetconfDocumentedException.ErrorSeverity.error);
193 recognisedChildren.addAll(foundConfigNodes);
194 return foundConfigNodes;
197 private static Map<String, AttributeIfc> reverseMap(Map<String, AttributeIfc> yangNameToAttr) {
198 Map<String, AttributeIfc> reversednameToAtr = Maps.newHashMap();
200 for (Entry<String, AttributeIfc> entry : yangNameToAttr.entrySet()) {
201 reversednameToAtr.put(entry.getValue().getUpperCaseCammelCase(), entry.getValue());
204 return reversednameToAtr;