Merge "Bug 460 - Fix warning throughout netconf subsystem"
[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.ListDependenciesAttribute;
17 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
20
21 import javax.management.openmbean.ArrayType;
22 import javax.management.openmbean.CompositeType;
23 import javax.management.openmbean.OpenType;
24 import javax.management.openmbean.SimpleType;
25 import java.util.Map;
26 import java.util.Map.Entry;
27
28 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
29
30     private final ServiceRegistryWrapper dependencyTracker;
31
32     public ObjectMapper(ServiceRegistryWrapper depTracker) {
33         this.dependencyTracker = depTracker;
34     }
35
36     public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
37             Map<String, AttributeIfc> configDefinition) {
38         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
39
40         for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
41             strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
42         }
43
44         return strategies;
45     }
46
47     public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc) {
48
49         if(attributeIfc instanceof DependencyAttribute) {
50             serviceNameOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getLocalName();
51             namespaceOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getNamespace().toString();
52         }
53
54         return switchAttribute(attributeIfc);
55     }
56
57     private Map<String, String> createJmxToYangMapping(TOAttribute attributeIfc) {
58         Map<String, String> retVal = Maps.newHashMap();
59         for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
60             retVal.put(entry.getKey(), (entry.getValue()).getAttributeYangName());
61         }
62         return retVal;
63     }
64
65     @Override
66     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(SimpleType<?> openType) {
67         return new SimpleAttributeMappingStrategy(openType);
68     }
69
70     @Override
71     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(ArrayType<?> openType) {
72
73         AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
74                 (SimpleType<?>) openType.getElementOpenType());
75         return new ArrayAttributeMappingStrategy(openType, innerStrategy);
76     }
77
78     @Override
79     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(CompositeType openType) {
80         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
81
82         Map<String, String> attributeMapping = Maps.newHashMap();
83
84         for (String innerAttributeKey : openType.keySet()) {
85
86             innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
87             attributeMapping.put(innerAttributeKey, innerAttributeKey);
88         }
89
90         return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
91     }
92
93     @Override
94     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(OpenType<?> openType) {
95         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
96
97         Map<String, String> attributeMapping = Maps.newHashMap();
98
99         CompositeType compositeType = (CompositeType) openType;
100         for (String innerAttributeKey : compositeType.keySet()) {
101
102             innerStrategies.put(innerAttributeKey, caseJavaAttribute(compositeType.getType(innerAttributeKey)));
103             attributeMapping.put(innerAttributeKey, innerAttributeKey);
104         }
105
106         return new UnionCompositeAttributeMappingStrategy(compositeType, innerStrategies, attributeMapping);
107     }
108
109     private String serviceNameOfDepAttr;
110     private String namespaceOfDepAttr;
111
112     @Override
113     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
114             SimpleType<?> openType) {
115         return new ObjectNameAttributeMappingStrategy(openType,
116                 serviceNameOfDepAttr, namespaceOfDepAttr);
117     }
118
119     @Override
120     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(CompositeType openType) {
121         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
122
123         Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
124         TOAttribute lastTO = (TOAttribute) getLastAttribute();
125
126         for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute)getLastAttribute()).getJmxPropertiesToTypesMap().entrySet()) {
127             innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
128         }
129
130         return new CompositeAttributeMappingStrategy(openType, innerStrategies,
131                 createJmxToYangMapping(lastTO));
132     }
133
134     @Override
135     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ArrayType<?> openType) {
136         Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
137         return new ArrayAttributeMappingStrategy(openType,
138                 prepareStrategy(((ListAttribute) getLastAttribute()).getInnerAttribute()));
139     }
140
141     @Override
142     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(ArrayType<?> openType) {
143         Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
144         return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));
145     }
146
147 }