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, nullableDefaultWrappedForCode;
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.nullableDefaultWrappedForCode = leaf.getDefault() == null ? null : typeProviderWrapper.getDefault(leaf);
37 this.nullableDescription = leaf.getDescription();
40 public JavaAttribute(LeafListSchemaNode leaf,
41 TypeProviderWrapper typeProviderWrapper) {
43 this.type = typeProviderWrapper.getType(leaf);
44 this.typeDefinition = leaf.getType();
45 this.typeProviderWrapper = typeProviderWrapper;
46 this.nullableDefault = nullableDefaultWrappedForCode = null;
47 this.nullableDescription = leaf.getDescription();
50 public TypeDefinition<?> getTypeDefinition() {
51 return typeDefinition;
55 * Returns the most base type
57 private TypeDefinition<?> getBaseType(TypeProviderWrapper typeProviderWrapper, TypeDefinition<?> baseType) {
58 while(baseType.getBaseType()!=null) {
59 baseType = baseType.getBaseType();
64 public String getNullableDefaultWrappedForCode() {
65 return nullableDefaultWrappedForCode;
69 public Type getType() {
74 public String getNullableDescription() {
75 return nullableDescription;
79 public String getNullableDefault() {
80 return nullableDefault;
84 public boolean equals(Object o) {
87 if (o == null || getClass() != o.getClass())
92 JavaAttribute that = (JavaAttribute) o;
94 if (nullableDefault != null ? !nullableDefault
95 .equals(that.nullableDefault) : that.nullableDefault != null)
97 if (nullableDescription != null ? !nullableDescription
98 .equals(that.nullableDescription)
99 : that.nullableDescription != null)
101 if (type != null ? !type.equals(that.type) : that.type != null)
108 public int hashCode() {
109 int result = super.hashCode();
110 result = 31 * result + (type != null ? type.hashCode() : 0);
113 + (nullableDescription != null ? nullableDescription.hashCode()
116 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
121 public String toString() {
122 return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
127 public OpenType<?> getOpenType() {
128 TypeDefinition<?> baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition);
129 Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition);
132 return getArrayType();
133 } else if (isEnum(baseType)) {
134 return getSimpleType(baseType);
135 } else if (isDerivedType(baseType)) {
136 return getCompositeType(baseType, baseTypeDefinition);
139 return getSimpleType(getType());
142 private boolean isEnum(Type baseType) {
143 return baseType.getFullyQualifiedName().equals(Enum.class.getName());
146 private OpenType<?> getSimpleType(Type type) {
147 SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
151 private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
153 SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
154 String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
156 String[] itemNames = new String[]{innerItemName};
157 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
159 OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
161 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
162 } catch (OpenDataException e) {
163 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
169 private OpenType<?> getArrayType() {
170 String innerTypeFullyQName = getInnerType(getType());
171 SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
173 ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
174 : new ArrayType<>(1, innerSimpleType);
176 } catch (OpenDataException e) {
177 throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
178 + innerSimpleType, e);
183 private boolean isPrimitive(String innerTypeFullyQName) {
184 if (innerTypeFullyQName.contains("."))
190 private boolean isArray() {
191 return type.getName().endsWith("[]");
194 private boolean isDerivedType(Type baseType) {
195 return baseType.equals(getType()) == false;
198 private static String getInnerType(Type type) {
199 String fullyQualifiedName = type.getFullyQualifiedName();
200 return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);