Merge "Changed maximumEntries to correct int rather than long"
[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.Services;
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 Services dependencyTracker;
31
32     public ObjectMapper(Services 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     private String serviceNameOfDepAttr;
94     private String namespaceOfDepAttr;
95
96     @Override
97     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
98             SimpleType<?> openType) {
99         return new ObjectNameAttributeMappingStrategy(openType, dependencyTracker,
100                 serviceNameOfDepAttr, namespaceOfDepAttr);
101     }
102
103     @Override
104     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(CompositeType openType) {
105         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
106
107         Preconditions.checkState(lastAttribute instanceof TOAttribute);
108         TOAttribute lastTO = (TOAttribute) lastAttribute;
109
110         for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute)lastAttribute).getJmxPropertiesToTypesMap().entrySet()) {
111             innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
112         }
113
114         return new CompositeAttributeMappingStrategy(openType, innerStrategies,
115                 createJmxToYangMapping(lastTO));
116     }
117
118     @Override
119     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ArrayType<?> openType) {
120         Preconditions.checkState(lastAttribute instanceof ListAttribute);
121         return new ArrayAttributeMappingStrategy(openType,
122                 prepareStrategy(((ListAttribute) lastAttribute).getInnerAttribute()));
123     }
124
125     @Override
126     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(ArrayType<?> openType) {
127         Preconditions.checkState(lastAttribute instanceof ListDependenciesAttribute);
128         return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));
129     }
130
131 }