Fix checkstyle reported by odlparent-3.0.0
[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     @SuppressWarnings("checkstyle:hiddenField")
32     public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
33             final Map<String, AttributeIfc> configDefinition, final EnumResolver enumResolver) {
34         this.enumResolver = Preconditions.checkNotNull(enumResolver);
35         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
36
37         for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
38             strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
39         }
40         return strategies;
41     }
42
43     public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(final AttributeIfc attributeIfc) {
44
45         if (attributeIfc instanceof DependencyAttribute) {
46             namespaceOfDepAttr = ((DependencyAttribute) attributeIfc).getDependency().getSie().getQName().getNamespace()
47                     .toString();
48         } else if (attributeIfc instanceof ListDependenciesAttribute) {
49             namespaceOfDepAttr = ((ListDependenciesAttribute) attributeIfc).getDependency().getSie().getQName()
50                     .getNamespace().toString();
51         }
52         return switchAttribute(attributeIfc);
53     }
54
55     private Map<String, String> createJmxToYangMapping(final TOAttribute attributeIfc) {
56         Map<String, String> retVal = Maps.newHashMap();
57         for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
58             retVal.put(entry.getKey(), entry.getValue().getAttributeYangName());
59         }
60         return retVal;
61     }
62
63     @Override
64     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(final SimpleType<?> openType) {
65         return new SimpleAttributeMappingStrategy(openType);
66     }
67
68     @Override
69     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaEnumAttribute(final OpenType<?> openType) {
70         return new EnumAttributeMappingStrategy((CompositeType) openType, enumResolver);
71     }
72
73     @Override
74     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(final ArrayType<?> openType) {
75
76         AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
77                 (SimpleType<?>) openType.getElementOpenType());
78         return new ArrayAttributeMappingStrategy(openType, innerStrategy);
79     }
80
81     @Override
82     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(
83             final 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         return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
94     }
95
96     @Override
97     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(final OpenType<?> openType) {
98         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
99
100         Map<String, String> attributeMapping = Maps.newHashMap();
101
102         CompositeType compositeType = (CompositeType) openType;
103         for (String innerAttributeKey : compositeType.keySet()) {
104
105             innerStrategies.put(innerAttributeKey, caseJavaAttribute(compositeType.getType(innerAttributeKey)));
106             attributeMapping.put(innerAttributeKey, innerAttributeKey);
107         }
108         return new UnionCompositeAttributeMappingStrategy(compositeType, innerStrategies, attributeMapping);
109     }
110
111     private String namespaceOfDepAttr;
112
113     @Override
114     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(final SimpleType<?> openType) {
115         return new ObjectNameAttributeMappingStrategy(openType, namespaceOfDepAttr);
116     }
117
118     @Override
119     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(final CompositeType openType) {
120         Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
121
122         Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
123         TOAttribute lastTO = (TOAttribute) getLastAttribute();
124
125         for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute) getLastAttribute())
126                 .getJmxPropertiesToTypesMap().entrySet()) {
127             innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
128         }
129
130         return new CompositeAttributeMappingStrategy(openType, innerStrategies, createJmxToYangMapping(lastTO));
131     }
132
133     @Override
134     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(final ArrayType<?> openType) {
135         Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
136         return new ArrayAttributeMappingStrategy(openType,
137                 prepareStrategy(((ListAttribute) getLastAttribute()).getInnerAttribute()));
138     }
139
140     @Override
141     protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(
142             final ArrayType<?> openType) {
143         Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
144         return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));
145     }
146 }