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
8 package org.opendaylight.controller.config.yangjmxgenerator.attribute;
10 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
11 import org.opendaylight.yangtools.sal.binding.model.api.Type;
12 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import javax.management.openmbean.ArrayType;
17 import javax.management.openmbean.CompositeType;
18 import javax.management.openmbean.OpenDataException;
19 import javax.management.openmbean.OpenType;
20 import javax.management.openmbean.SimpleType;
22 public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
24 private final Type type;
25 private final String nullableDescription, nullableDefault;
26 private final TypeProviderWrapper typeProviderWrapper;
27 private final TypeDefinition<?> typeDefinition;
29 public JavaAttribute(LeafSchemaNode leaf,
30 TypeProviderWrapper typeProviderWrapper) {
32 this.type = typeProviderWrapper.getType(leaf);
33 this.typeDefinition = leaf.getType();
34 this.typeProviderWrapper = typeProviderWrapper;
35 this.nullableDefault = leaf.getDefault();
36 this.nullableDescription = leaf.getDescription();
39 public JavaAttribute(LeafListSchemaNode leaf,
40 TypeProviderWrapper typeProviderWrapper) {
42 this.type = typeProviderWrapper.getType(leaf);
43 this.typeDefinition = leaf.getType();
44 this.typeProviderWrapper = typeProviderWrapper;
45 this.nullableDefault = null;
46 this.nullableDescription = leaf.getDescription();
50 * Returns the most base type
52 private TypeDefinition<?> getBaseType(TypeProviderWrapper typeProviderWrapper, TypeDefinition<?> baseType) {
53 while(baseType.getBaseType()!=null) {
54 baseType = baseType.getBaseType();
60 public Type getType() {
65 public String getNullableDescription() {
66 return nullableDescription;
70 public String getNullableDefault() {
71 return nullableDefault;
75 public boolean equals(Object o) {
78 if (o == null || getClass() != o.getClass())
83 JavaAttribute that = (JavaAttribute) o;
85 if (nullableDefault != null ? !nullableDefault
86 .equals(that.nullableDefault) : that.nullableDefault != null)
88 if (nullableDescription != null ? !nullableDescription
89 .equals(that.nullableDescription)
90 : that.nullableDescription != null)
92 if (type != null ? !type.equals(that.type) : that.type != null)
99 public int hashCode() {
100 int result = super.hashCode();
101 result = 31 * result + (type != null ? type.hashCode() : 0);
104 + (nullableDescription != null ? nullableDescription.hashCode()
107 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
112 public String toString() {
113 return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
118 public OpenType<?> getOpenType() {
119 TypeDefinition<?> baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition);
120 Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition);
123 return getArrayType();
124 } else if (isEnum(baseType)) {
125 return getSimpleType(baseType);
126 } else if (isDerivedType(baseType)) {
127 return getCompositeType(baseType, baseTypeDefinition);
130 return getSimpleType(getType());
133 private boolean isEnum(Type baseType) {
134 return baseType.getFullyQualifiedName().equals(Enum.class.getName());
137 private OpenType<?> getSimpleType(Type type) {
138 SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
142 private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
144 SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
145 String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
147 String[] itemNames = new String[]{innerItemName};
148 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
150 OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
152 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
153 } catch (OpenDataException e) {
154 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
160 private OpenType<?> getArrayType() {
161 String innerTypeFullyQName = getInnerType(getType());
162 SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
164 ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
165 : new ArrayType<>(1, innerSimpleType);
167 } catch (OpenDataException e) {
168 throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
169 + innerSimpleType, e);
174 private boolean isPrimitive(String innerTypeFullyQName) {
175 if (innerTypeFullyQName.contains("."))
181 private boolean isArray() {
182 return type.getName().endsWith("[]");
185 private boolean isDerivedType(Type baseType) {
186 return baseType.equals(getType()) == false;
189 private static String getInnerType(Type type) {
190 String fullyQualifiedName = type.getFullyQualifiedName();
191 return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);