2 * Copyright (c) 2013 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.netconf.confignetconfconnector.mapping.attributes.resolving;
12 import java.util.Map.Entry;
14 import javax.management.openmbean.ArrayType;
15 import javax.management.openmbean.CompositeType;
16 import javax.management.openmbean.OpenType;
17 import javax.management.openmbean.SimpleType;
19 import org.opendaylight.controller.config.yangjmxgenerator.attribute.*;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
21 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.Services;
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.Maps;
26 public class ObjectResolver extends AttributeIfcSwitchStatement<AttributeResolvingStrategy<?, ? extends OpenType<?>>> {
28 private final Services serviceTracker;
29 private OpenType<?> openType;
31 public ObjectResolver(Services serviceTracker) {
32 this.serviceTracker = serviceTracker;
35 public Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> prepareResolving(
36 Map<String, AttributeIfc> configDefinition) {
37 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
39 for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
40 strategies.put(attrEntry.getKey(),
41 prepareStrategy(attrEntry.getValue(), attrEntry.getValue().getOpenType()));
47 private AttributeResolvingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc,
48 OpenType<?> openType) {
50 this.openType = openType;
51 return switchAttribute(attributeIfc);
54 private Map<String, String> createYangToJmxMapping(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());
63 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaAttribute(JavaAttribute attributeIfc) {
64 if (attributeIfc.getOpenType() instanceof SimpleType<?>)
65 return new SimpleAttributeResolvingStrategy((SimpleType<?>) openType);
66 else if (attributeIfc.getOpenType() instanceof ArrayType<?>) {
67 ArrayType<?> arrayType = (ArrayType<?>) openType;
68 SimpleType<?> innerType = (SimpleType<?>) arrayType.getElementOpenType();
69 AttributeResolvingStrategy<?, ? extends OpenType<?>> strat = new SimpleAttributeResolvingStrategy(innerType);
70 return new ArrayAttributeResolvingStrategy(strat, arrayType);
72 throw new IllegalStateException(JavaAttribute.class + " can only provide open type " + SimpleType.class
73 + " or " + ArrayType.class);
77 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
78 DependencyAttribute attributeIfc) {
79 return new ObjectNameAttributeResolvingStrategy(serviceTracker);
83 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseTOAttribute(TOAttribute attributeIfc) {
84 CompositeType compositeType = (CompositeType) openType;
85 Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap();
86 for (String innerName : compositeType.keySet()) {
87 Preconditions.checkState(attributeIfc instanceof TOAttribute, "Unexpected state, " + attributeIfc
88 + " should be instance of " + TOAttribute.class.getName());
89 AttributeIfc innerAttributeIfc = attributeIfc.getJmxPropertiesToTypesMap().get(innerName);
90 innerMap.put(innerAttributeIfc.getAttributeYangName(),
91 prepareStrategy(innerAttributeIfc, compositeType.getType(innerName)));
93 return new CompositeAttributeResolvingStrategy(innerMap, compositeType, createYangToJmxMapping(attributeIfc));
97 protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseListAttribute(ListAttribute attributeIfc) {
98 ArrayType<?> arrayType = (ArrayType<?>) openType;
99 OpenType<?> innerType = arrayType.getElementOpenType();
100 AttributeIfc inner = attributeIfc.getInnerAttribute();
101 return new ArrayAttributeResolvingStrategy(prepareStrategy(inner, innerType), arrayType);