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