Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / mapping / ObjectMapper.java
1 /*
2  * Copyright (c) 2015, 2017 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.config.facade.xml.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.facade.xml.mapping.attributes.AttributeIfcSwitchStatement;
20 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
23 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
24 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
25 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
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             final Map<String, AttributeIfc> configDefinition, final 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         return strategies;
40     }
41
42     public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(final AttributeIfc attributeIfc) {
43
44         if (attributeIfc instanceof DependencyAttribute) {
45             namespaceOfDepAttr = ((DependencyAttribute) attributeIfc).getDependency().getSie().getQName().getNamespace()
46                     .toString();
47         } else if (attributeIfc instanceof ListDependenciesAttribute) {
48             namespaceOfDepAttr = ((ListDependenciesAttribute) attributeIfc).getDependency().getSie().getQName()
49                     .getNamespace().toString();
50         }
51         return switchAttribute(attributeIfc);
52     }
53
54     private Map<String, String> createJmxToYangMapping(final TOAttribute attributeIfc) {
55         Map<String, String> retVal = Maps.newHashMap();
56         for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
57             retVal.put(entry.getKey(), entry.getValue().getAttributeYangName());
58         }
59         return retVal;
60     }
61
62     @Override
63     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(final SimpleType<?> openType) {
64         return new SimpleAttributeMappingStrategy(openType);
65     }
66
67     @Override
68     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaEnumAttribute(final OpenType<?> openType) {
69         return new EnumAttributeMappingStrategy((CompositeType) openType, enumResolver);
70     }
71
72     @Override
73     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(final ArrayType<?> openType) {
74
75         AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
76                 (SimpleType<?>) openType.getElementOpenType());
77         return new ArrayAttributeMappingStrategy(openType, innerStrategy);
78     }
79
80     @Override
81     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(
82             final CompositeType openType) {
83         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
84
85         Map<String, String> attributeMapping = Maps.newHashMap();
86
87         for (String innerAttributeKey : openType.keySet()) {
88
89             innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
90             attributeMapping.put(innerAttributeKey, innerAttributeKey);
91         }
92         return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
93     }
94
95     @Override
96     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(final OpenType<?> openType) {
97         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
98
99         Map<String, String> attributeMapping = Maps.newHashMap();
100
101         CompositeType compositeType = (CompositeType) openType;
102         for (String innerAttributeKey : compositeType.keySet()) {
103
104             innerStrategies.put(innerAttributeKey, caseJavaAttribute(compositeType.getType(innerAttributeKey)));
105             attributeMapping.put(innerAttributeKey, innerAttributeKey);
106         }
107         return new UnionCompositeAttributeMappingStrategy(compositeType, innerStrategies, attributeMapping);
108     }
109
110     private String namespaceOfDepAttr;
111
112     @Override
113     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(final SimpleType<?> openType) {
114         return new ObjectNameAttributeMappingStrategy(openType, namespaceOfDepAttr);
115     }
116
117     @Override
118     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(final CompositeType openType) {
119         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
120
121         Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
122         TOAttribute lastTO = (TOAttribute) getLastAttribute();
123
124         for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute) getLastAttribute())
125                 .getJmxPropertiesToTypesMap().entrySet()) {
126             innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
127         }
128
129         return new CompositeAttributeMappingStrategy(openType, innerStrategies, createJmxToYangMapping(lastTO));
130     }
131
132     @Override
133     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(final ArrayType<?> openType) {
134         Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
135         return new ArrayAttributeMappingStrategy(openType,
136                 prepareStrategy(((ListAttribute) getLastAttribute()).getInnerAttribute()));
137     }
138
139     @Override
140     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(
141             final ArrayType<?> openType) {
142         Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
143         return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));
144     }
145 }