config-manager-facade-xml: final parameters
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / resolving / ObjectResolver.java
1 /*
2  * Copyright (c) 2015 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.resolving;
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.mapping.config.ServiceRegistryWrapper;
21 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
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 ObjectResolver extends AttributeIfcSwitchStatement<AttributeResolvingStrategy<?, ? extends OpenType<?>>> {
28
29     private final ServiceRegistryWrapper serviceTracker;
30     private EnumResolver enumResolver;
31
32     public ObjectResolver(final ServiceRegistryWrapper serviceTracker) {
33         this.serviceTracker = serviceTracker;
34     }
35
36     public Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> prepareResolving(
37             final Map<String, AttributeIfc> configDefinition, final EnumResolver enumResolver) {
38         this.enumResolver = enumResolver;
39
40         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
41
42         for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
43             strategies.put(attrEntry.getKey(),
44                     prepareStrategy(attrEntry.getValue()));
45         }
46
47         return strategies;
48     }
49
50     private AttributeResolvingStrategy<?, ? extends OpenType<?>> prepareStrategy(final AttributeIfc attributeIfc) {
51         return switchAttribute(attributeIfc);
52     }
53
54     private Map<String, String> createYangToJmxMapping(final TOAttribute attributeIfc) {
55         Map<String, String> retVal = Maps.newHashMap();
56         for (Entry<String, AttributeIfc> entry : attributeIfc.getYangPropertiesToTypesMap().entrySet()) {
57             retVal.put(entry.getKey(), (entry.getValue()).getLowerCaseCammelCase());
58         }
59         return retVal;
60     }
61
62     @Override
63     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaEnumAttribute(final OpenType<?> openType) {
64         return new EnumAttributeResolvingStrategy((CompositeType) openType, enumResolver);
65     }
66
67     @Override
68     protected AttributeResolvingStrategy<?, ? extends OpenType<?>>  caseJavaSimpleAttribute(final SimpleType<?> openType) {
69         return new SimpleAttributeResolvingStrategy(openType);
70     }
71
72     @Override
73     protected AttributeResolvingStrategy<?, ? extends OpenType<?>>  caseJavaArrayAttribute(final ArrayType<?> openType) {
74
75         SimpleType<?> innerType = (SimpleType<?>) openType.getElementOpenType();
76         AttributeResolvingStrategy<?, ? extends OpenType<?>> strat = new SimpleAttributeResolvingStrategy(innerType);
77         return new ArrayAttributeResolvingStrategy(strat, openType);
78     }
79
80     @Override
81     protected AttributeResolvingStrategy<?, ? extends OpenType<?>>  caseJavaCompositeAttribute(final CompositeType openType) {
82         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap();
83         Map<String, String> yangToJmxMapping = Maps.newHashMap();
84
85         fillMappingForComposite(openType, innerMap, yangToJmxMapping);
86         return new CompositeAttributeResolvingStrategy(innerMap, openType, yangToJmxMapping);
87     }
88
89     private void fillMappingForComposite(final CompositeType openType, final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap, final Map<String, String> yangToJmxMapping) {
90         for (String innerAttributeKey : openType.keySet()) {
91             innerMap.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
92             yangToJmxMapping.put(innerAttributeKey, innerAttributeKey);
93         }
94     }
95
96     @Override
97     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(final OpenType<?> openType) {
98
99         Preconditions.checkState(openType instanceof CompositeType, "Unexpected open type, expected %s but was %s");
100         CompositeType compositeType = (CompositeType) openType;
101
102         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap();
103         Map<String, String> yangToJmxMapping = Maps.newHashMap();
104         fillMappingForComposite(compositeType, innerMap, yangToJmxMapping);
105
106         return new UnionCompositeAttributeResolvingStrategy(innerMap, compositeType, yangToJmxMapping);
107     }
108
109     @Override
110     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
111             final SimpleType<?> openType) {
112         return new ObjectNameAttributeResolvingStrategy(serviceTracker);
113     }
114
115     @Override
116     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseTOAttribute(final CompositeType openType) {
117         Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
118         TOAttribute toAttribute = (TOAttribute) getLastAttribute();
119
120         Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap();
121
122         for (String innerName : openType.keySet()) {
123
124             AttributeIfc innerAttributeIfc = toAttribute.getJmxPropertiesToTypesMap().get(innerName);
125             innerMap.put(innerAttributeIfc.getAttributeYangName(),
126                     prepareStrategy(innerAttributeIfc));
127         }
128         return new CompositeAttributeResolvingStrategy(innerMap, openType, createYangToJmxMapping(toAttribute));
129     }
130
131     @Override
132     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseListAttribute(final ArrayType<?> openType) {
133         Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
134         AttributeIfc innerAttribute = ((ListAttribute) getLastAttribute()).getInnerAttribute();
135         return new ArrayAttributeResolvingStrategy(prepareStrategy(innerAttribute), openType);
136     }
137
138     @Override
139     protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(final ArrayType<?> openType) {
140         Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
141         return new ArrayAttributeResolvingStrategy(caseDependencyAttribute(SimpleType.OBJECTNAME), openType);
142     }
143
144 }

©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.