Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / config / InstanceConfig.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.config;
10
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.util.xml.XmlElement;
28 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33
34 import javax.management.ObjectName;
35 import javax.management.openmbean.OpenType;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Map.Entry;
39
40 public final class InstanceConfig {
41     private static final Logger logger = LoggerFactory.getLogger(InstanceConfig.class);
42
43     private final Map<String, AttributeIfc> yangToAttrConfig;
44     private final Map<String, AttributeIfc> jmxToAttrConfig;
45     private final ConfigRegistryClient configRegistryClient;
46
47     public InstanceConfig(ConfigRegistryClient configRegistryClient, Map<String, AttributeIfc> yangNamesToAttributes) {
48         this.yangToAttrConfig = yangNamesToAttributes;
49         this.jmxToAttrConfig = reverseMap(yangNamesToAttributes);
50         this.configRegistryClient = configRegistryClient;
51     }
52
53     private Map<String, Object> getMappedConfiguration(ObjectName on, Services depTracker) {
54
55         // TODO make field, mappingStrategies can be instantiated only once
56         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> mappingStrategies = new ObjectMapper(depTracker)
57                 .prepareMapping(jmxToAttrConfig);
58
59         Map<String, Object> toXml = Maps.newHashMap();
60
61         for (Entry<String, AttributeIfc> configDefEntry : jmxToAttrConfig.entrySet()) {
62
63             // Skip children runtime beans as they are mapped by InstanceRuntime
64             if (configDefEntry.getValue() instanceof RuntimeBeanEntry)
65                 continue;
66
67             Object value = configRegistryClient.getAttributeCurrentValue(on, configDefEntry.getKey());
68             try {
69                 AttributeMappingStrategy<?, ? extends OpenType<?>> attributeMappingStrategy = mappingStrategies
70                         .get(configDefEntry.getKey());
71                 Optional<?> a = attributeMappingStrategy.mapAttribute(value);
72                 if (a.isPresent() == false)
73                     continue;
74
75                 toXml.put(configDefEntry.getValue().getAttributeYangName(), a.get());
76             } catch (Exception e) {
77                 throw new IllegalStateException("Unable to map value " + value + " to attribute "
78                         + configDefEntry.getKey(), e);
79             }
80         }
81
82         return toXml;
83     }
84
85     public Element toXml(ObjectName on, Services depTracker, String namespace, Document document, Element rootElement) {
86
87         Element cfgElement = rootElement;
88
89         Map<String, AttributeWritingStrategy> strats = new ObjectXmlWriter().prepareWriting(yangToAttrConfig, document);
90
91         Map<String, Object> mappedConfig = getMappedConfiguration(on, depTracker);
92
93         for (Entry<String, ?> mappingEntry : mappedConfig.entrySet()) {
94             try {
95                 strats.get(mappingEntry.getKey()).writeElement(cfgElement, namespace, mappingEntry.getValue());
96             } catch (Exception e) {
97                 throw new IllegalStateException("Unable to write value " + mappingEntry.getValue() + " for attribute "
98                         + mappingEntry.getValue(), e);
99             }
100         }
101
102         return cfgElement;
103     }
104
105     private void resolveConfiguration(InstanceConfigElementResolved mappedConfig, Services depTracker) {
106
107         // TODO make field, resolvingStrategies can be instantiated only once
108         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> resolvingStrategies = new ObjectResolver(
109                 depTracker).prepareResolving(yangToAttrConfig);
110
111         for (Entry<String, AttributeConfigElement> configDefEntry : mappedConfig.getConfiguration().entrySet()) {
112             try {
113
114                 AttributeResolvingStrategy<?, ? extends OpenType<?>> attributeResolvingStrategy = resolvingStrategies
115                         .get(configDefEntry.getKey());
116
117                 configDefEntry.getValue().resolveValue(attributeResolvingStrategy, configDefEntry.getKey());
118                 configDefEntry.getValue().setJmxName(
119                         yangToAttrConfig.get(configDefEntry.getKey()).getUpperCaseCammelCase());
120             } catch (Exception e) {
121                 throw new IllegalStateException("Unable to resolve value " + configDefEntry.getValue()
122                         + " to attribute " + configDefEntry.getKey(), e);
123             }
124         }
125     }
126
127     public InstanceConfigElementResolved fromXml(XmlElement moduleElement, Services services, String moduleNamespace) {
128         Map<String, AttributeConfigElement> retVal = Maps.newHashMap();
129
130         Map<String, AttributeReadingStrategy> strats = new ObjectXmlReader().prepareReading(yangToAttrConfig);
131         List<XmlElement> recognisedChildren = Lists.newArrayList();
132
133         XmlElement type = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.TYPE_KEY);
134         XmlElement name = moduleElement.getOnlyChildElementWithSameNamespace(XmlNetconfConstants.NAME_KEY);
135         List<XmlElement> typeAndName = Lists.newArrayList(type, name);
136
137         for (Entry<String, AttributeReadingStrategy> readStratEntry : strats.entrySet()) {
138             List<XmlElement> configNodes = getConfigNodes(moduleElement, moduleNamespace, readStratEntry.getKey(),
139                     recognisedChildren, typeAndName);
140             AttributeConfigElement readElement = readStratEntry.getValue().readElement(configNodes);
141             retVal.put(readStratEntry.getKey(), readElement);
142         }
143
144         recognisedChildren.addAll(typeAndName);
145         moduleElement.checkUnrecognisedElements(recognisedChildren);
146
147         // TODO: add check for conflicts between global and local edit strategy
148         String perInstanceEditStrategy = moduleElement.getAttribute(XmlNetconfConstants.OPERATION_ATTR_KEY,
149                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
150
151         InstanceConfigElementResolved instanceConfigElementResolved = perInstanceEditStrategy.equals("") ? new InstanceConfigElementResolved(
152                 retVal) : new InstanceConfigElementResolved(perInstanceEditStrategy, retVal);
153
154         resolveConfiguration(instanceConfigElementResolved, services);
155         return instanceConfigElementResolved;
156     }
157
158     private List<XmlElement> getConfigNodes(XmlElement moduleElement, String moduleNamespace, String name,
159             List<XmlElement> recognisedChildren, List<XmlElement> typeAndName) {
160         List<XmlElement> foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name, moduleNamespace);
161         if (foundConfigNodes.isEmpty()) {
162             logger.debug("No config nodes {}:{} found in {}", moduleNamespace, name, moduleElement);
163             logger.debug("Trying lookup of config nodes without specified namespace");
164             foundConfigNodes = moduleElement.getChildElementsWithinNamespace(name,
165                     XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
166             // In case module type or name element is not present in config it
167             // would be matched with config type or name
168             // We need to remove config type and name from available module
169             // config elements
170             foundConfigNodes.removeAll(typeAndName);
171             logger.debug("Found {} config nodes {} without specified namespace in {}", foundConfigNodes.size(), name,
172                     moduleElement);
173         } else {
174             List<XmlElement> foundWithoutNamespaceNodes = moduleElement.getChildElementsWithinNamespace(name,
175                     XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
176             foundWithoutNamespaceNodes.removeAll(typeAndName);
177             Preconditions.checkState(foundWithoutNamespaceNodes.isEmpty(),
178                     "Element %s present multiple times with different namespaces: %s, %s", name, foundConfigNodes,
179                     foundWithoutNamespaceNodes);
180         }
181
182         recognisedChildren.addAll(foundConfigNodes);
183         return foundConfigNodes;
184     }
185
186     private static Map<String, AttributeIfc> reverseMap(Map<String, AttributeIfc> yangNameToAttr) {
187         Map<String, AttributeIfc> reversednameToAtr = Maps.newHashMap();
188
189         for (Entry<String, AttributeIfc> entry : yangNameToAttr.entrySet()) {
190             reversednameToAtr.put(entry.getValue().getUpperCaseCammelCase(), entry.getValue());
191         }
192
193         return reversednameToAtr;
194     }
195
196 }