Merge changes Ia268965a,Iefa79f99
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / mapping / ObjectMapper.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.attributes.mapping;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
14 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
17 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
19
20 import javax.management.openmbean.ArrayType;
21 import javax.management.openmbean.CompositeType;
22 import javax.management.openmbean.OpenType;
23 import javax.management.openmbean.SimpleType;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
28
29     private final Services dependencyTracker;
30
31     public ObjectMapper(Services depTracker) {
32         this.dependencyTracker = depTracker;
33     }
34
35     public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
36             Map<String, AttributeIfc> configDefinition) {
37         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
38
39         for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
40             strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
41         }
42
43         return strategies;
44     }
45
46     public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc) {
47
48         if(attributeIfc instanceof DependencyAttribute) {
49             serviceNameOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getLocalName();
50             namespaceOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getNamespace().toString();
51         }
52
53         return switchAttribute(attributeIfc);
54     }
55
56     private Map<String, String> createJmxToYangMapping(TOAttribute attributeIfc) {
57         Map<String, String> retVal = Maps.newHashMap();
58         for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
59             retVal.put(entry.getKey(), (entry.getValue()).getAttributeYangName());
60         }
61         return retVal;
62     }
63
64     @Override
65     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(SimpleType<?> openType) {
66         return new SimpleAttributeMappingStrategy(openType);
67     }
68
69     @Override
70     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(ArrayType<?> openType) {
71
72         AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
73                 (SimpleType<?>) openType.getElementOpenType());
74         return new ArrayAttributeMappingStrategy(openType, innerStrategy);
75     }
76
77     @Override
78     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(CompositeType openType) {
79         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
80
81         Map<String, String> attributeMapping = Maps.newHashMap();
82
83         for (String innerAttributeKey : openType.keySet()) {
84
85             innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
86             attributeMapping.put(innerAttributeKey, innerAttributeKey);
87         }
88
89         return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
90     }
91
92     private String serviceNameOfDepAttr;
93     private String namespaceOfDepAttr;
94
95     @Override
96     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
97             SimpleType<?> openType) {
98         return new ObjectNameAttributeMappingStrategy(openType, dependencyTracker,
99                 serviceNameOfDepAttr, namespaceOfDepAttr);
100     }
101
102     @Override
103     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(CompositeType openType) {
104         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
105
106         Preconditions.checkState(lastAttribute instanceof TOAttribute);
107         TOAttribute lastTO = (TOAttribute) lastAttribute;
108
109         for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute)lastAttribute).getJmxPropertiesToTypesMap().entrySet()) {
110             innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
111         }
112
113         return new CompositeAttributeMappingStrategy(openType, innerStrategies,
114                 createJmxToYangMapping(lastTO));
115     }
116
117     @Override
118     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ArrayType<?> openType) {
119         Preconditions.checkState(lastAttribute instanceof ListAttribute);
120         return new ArrayAttributeMappingStrategy(openType,
121                 prepareStrategy(((ListAttribute) lastAttribute).getInnerAttribute()));
122     }
123
124 }