463e98100c2578138932340df7470d9df27935d1
[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 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.EnumResolver;
26
27 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
28
29     private EnumResolver enumResolver;
30
31     public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
32             Map<String, AttributeIfc> configDefinition, EnumResolver enumResolver) {
33         this.enumResolver = Preconditions.checkNotNull(enumResolver);
34         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
35
36         for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
37             strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
38         }
39
40         return strategies;
41     }
42
43     public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc) {
44
45         if(attributeIfc instanceof DependencyAttribute) {
46             serviceNameOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getLocalName();
47             namespaceOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getNamespace().toString();
48         } else if (attributeIfc instanceof ListDependenciesAttribute) {
49             serviceNameOfDepAttr = ((ListDependenciesAttribute)attributeIfc).getDependency().getSie().getQName().getLocalName();
50             namespaceOfDepAttr = ((ListDependenciesAttribute)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<?>> caseJavaEnumAttribute(final OpenType<?> openType) {
71         return new EnumAttributeMappingStrategy(((CompositeType) openType), enumResolver);
72     }
73
74     @Override
75     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(ArrayType<?> openType) {
76
77         AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
78                 (SimpleType<?>) openType.getElementOpenType());
79         return new ArrayAttributeMappingStrategy(openType, innerStrategy);
80     }
81
82     @Override
83     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(CompositeType openType) {
84         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
85
86         Map<String, String> attributeMapping = Maps.newHashMap();
87
88         for (String innerAttributeKey : openType.keySet()) {
89
90             innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
91             attributeMapping.put(innerAttributeKey, innerAttributeKey);
92         }
93
94         return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
95     }
96
97     @Override
98     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(OpenType<?> openType) {
99         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
100
101         Map<String, String> attributeMapping = Maps.newHashMap();
102
103         CompositeType compositeType = (CompositeType) openType;
104         for (String innerAttributeKey : compositeType.keySet()) {
105
106             innerStrategies.put(innerAttributeKey, caseJavaAttribute(compositeType.getType(innerAttributeKey)));
107             attributeMapping.put(innerAttributeKey, innerAttributeKey);
108         }
109
110         return new UnionCompositeAttributeMappingStrategy(compositeType, innerStrategies, attributeMapping);
111     }
112
113     private String serviceNameOfDepAttr;
114     private String namespaceOfDepAttr;
115
116     @Override
117     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
118             SimpleType<?> openType) {
119         return new ObjectNameAttributeMappingStrategy(openType,
120                 serviceNameOfDepAttr, namespaceOfDepAttr);
121     }
122
123     @Override
124     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(CompositeType openType) {
125         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
126
127         Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
128         TOAttribute lastTO = (TOAttribute) getLastAttribute();
129
130         for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute)getLastAttribute()).getJmxPropertiesToTypesMap().entrySet()) {
131             innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
132         }
133
134         return new CompositeAttributeMappingStrategy(openType, innerStrategies,
135                 createJmxToYangMapping(lastTO));
136     }
137
138     @Override
139     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ArrayType<?> openType) {
140         Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
141         return new ArrayAttributeMappingStrategy(openType,
142                 prepareStrategy(((ListAttribute) getLastAttribute()).getInnerAttribute()));
143     }
144
145     @Override
146     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(ArrayType<?> openType) {
147         Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
148         return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));
149     }
150
151 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.