2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.facade.xml.mapping.attributes.resolving;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
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;
27 public class ObjectResolver extends AttributeIfcSwitchStatement<AttributeResolvingStrategy<?, ? extends OpenType<?>>> {
29 private final ServiceRegistryWrapper serviceTracker;
30 private EnumResolver enumResolver;
32 public ObjectResolver(final ServiceRegistryWrapper serviceTracker) {
33 this.serviceTracker = serviceTracker;
36 public Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> prepareResolving(
37 final Map<String, AttributeIfc> configDefinition, final EnumResolver enumResolver) {
38 this.enumResolver = enumResolver;
40 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
42 for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
43 strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
49 private AttributeResolvingStrategy<?, ? extends OpenType<?>> prepareStrategy(final AttributeIfc attributeIfc) {
50 return switchAttribute(attributeIfc);
53 private Map<String, String> createYangToJmxMapping(final TOAttribute attributeIfc) {
54 Map<String, String> retVal = Maps.newHashMap();
55 for (Entry<String, AttributeIfc> entry : attributeIfc.getYangPropertiesToTypesMap().entrySet()) {
56 retVal.put(entry.getKey(), entry.getValue().getLowerCaseCammelCase());
62 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaEnumAttribute(final OpenType<?> openType) {
63 return new EnumAttributeResolvingStrategy((CompositeType) openType, enumResolver);
67 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(
68 final SimpleType<?> openType) {
69 return new SimpleAttributeResolvingStrategy(openType);
73 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(final ArrayType<?> openType) {
75 SimpleType<?> innerType = (SimpleType<?>) openType.getElementOpenType();
76 AttributeResolvingStrategy<?, ? extends OpenType<?>> strat = new SimpleAttributeResolvingStrategy(innerType);
77 return new ArrayAttributeResolvingStrategy(strat, openType);
81 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(
82 final CompositeType openType) {
83 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap();
84 Map<String, String> yangToJmxMapping = Maps.newHashMap();
86 fillMappingForComposite(openType, innerMap, yangToJmxMapping);
87 return new CompositeAttributeResolvingStrategy(innerMap, openType, yangToJmxMapping);
90 private void fillMappingForComposite(final CompositeType openType,
91 final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap,
92 final Map<String, String> yangToJmxMapping) {
93 for (String innerAttributeKey : openType.keySet()) {
94 innerMap.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
95 yangToJmxMapping.put(innerAttributeKey, innerAttributeKey);
100 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(final OpenType<?> openType) {
102 Preconditions.checkState(openType instanceof CompositeType, "Unexpected open type, expected %s but was %s");
103 CompositeType compositeType = (CompositeType) openType;
105 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap();
106 Map<String, String> yangToJmxMapping = Maps.newHashMap();
107 fillMappingForComposite(compositeType, innerMap, yangToJmxMapping);
109 return new UnionCompositeAttributeResolvingStrategy(innerMap, compositeType, yangToJmxMapping);
113 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
114 final SimpleType<?> openType) {
115 return new ObjectNameAttributeResolvingStrategy(serviceTracker);
119 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseTOAttribute(final CompositeType openType) {
120 Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
121 TOAttribute toAttribute = (TOAttribute) getLastAttribute();
123 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap();
125 for (String innerName : openType.keySet()) {
127 AttributeIfc innerAttributeIfc = toAttribute.getJmxPropertiesToTypesMap().get(innerName);
128 innerMap.put(innerAttributeIfc.getAttributeYangName(), prepareStrategy(innerAttributeIfc));
130 return new CompositeAttributeResolvingStrategy(innerMap, openType, createYangToJmxMapping(toAttribute));
134 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseListAttribute(final ArrayType<?> openType) {
135 Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
136 AttributeIfc innerAttribute = ((ListAttribute) getLastAttribute()).getInnerAttribute();
137 return new ArrayAttributeResolvingStrategy(prepareStrategy(innerAttribute), openType);
141 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(
142 final ArrayType<?> openType) {
143 Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
144 return new ArrayAttributeResolvingStrategy(caseDependencyAttribute(SimpleType.OBJECTNAME), openType);