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 com.google.common.base.Preconditions;
11 import org.opendaylight.controller.config.api.IdentityAttributeRef;
12 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
13 import org.opendaylight.yangtools.sal.binding.model.api.Type;
14 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
20 import javax.management.openmbean.ArrayType;
21 import javax.management.openmbean.CompositeType;
22 import javax.management.openmbean.OpenDataException;
23 import javax.management.openmbean.OpenType;
24 import javax.management.openmbean.SimpleType;
25 import java.util.Arrays;
26 import java.util.List;
28 public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
30 public static final String DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION = "valueOfArtificialUnionProperty";
32 private final Type type;
33 private final String nullableDescription, nullableDefault, nullableDefaultWrappedForCode;
34 private final TypeProviderWrapper typeProviderWrapper;
35 private final TypeDefinition<?> typeDefinition;
37 public JavaAttribute(LeafSchemaNode leaf,
38 TypeProviderWrapper typeProviderWrapper) {
40 this.type = typeProviderWrapper.getType(leaf);
42 this.typeDefinition = leaf.getType();
43 this.typeProviderWrapper = typeProviderWrapper;
44 this.nullableDefault = leaf.getDefault();
45 this.nullableDefaultWrappedForCode = leaf.getDefault() == null ? null : typeProviderWrapper.getDefault(leaf);
46 this.nullableDescription = leaf.getDescription();
49 public JavaAttribute(LeafListSchemaNode leaf,
50 TypeProviderWrapper typeProviderWrapper) {
52 this.type = typeProviderWrapper.getType(leaf);
53 this.typeDefinition = leaf.getType();
54 this.typeProviderWrapper = typeProviderWrapper;
55 this.nullableDefault = nullableDefaultWrappedForCode = null;
56 this.nullableDescription = leaf.getDescription();
59 public boolean isUnion() {
60 TypeDefinition<?> base = getBaseType(typeProviderWrapper, typeDefinition);
61 return base instanceof UnionTypeDefinition;
64 public TypeDefinition<?> getTypeDefinition() {
65 return typeDefinition;
69 * Returns the most base type
71 private TypeDefinition<?> getBaseType(TypeProviderWrapper typeProviderWrapper, TypeDefinition<?> baseType) {
72 while(baseType.getBaseType()!=null) {
73 baseType = baseType.getBaseType();
78 public String getNullableDefaultWrappedForCode() {
79 return nullableDefaultWrappedForCode;
83 public Type getType() {
88 public String getNullableDescription() {
89 return nullableDescription;
93 public String getNullableDefault() {
94 return nullableDefault;
98 public boolean equals(Object o) {
101 if (o == null || getClass() != o.getClass())
103 if (!super.equals(o))
106 JavaAttribute that = (JavaAttribute) o;
108 if (nullableDefault != null ? !nullableDefault
109 .equals(that.nullableDefault) : that.nullableDefault != null)
111 if (nullableDescription != null ? !nullableDescription
112 .equals(that.nullableDescription)
113 : that.nullableDescription != null)
115 if (type != null ? !type.equals(that.type) : that.type != null)
122 public int hashCode() {
123 int result = super.hashCode();
124 result = 31 * result + (type != null ? type.hashCode() : 0);
127 + (nullableDescription != null ? nullableDescription.hashCode()
130 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
135 public String toString() {
136 return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
141 public OpenType<?> getOpenType() {
142 TypeDefinition<?> baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition);
143 Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition);
146 return getArrayType();
147 } else if (isEnum(baseType)) {
148 return getSimpleType(baseType);
149 } else if (isUnion()) {
150 return getCompositeTypeForUnion(baseTypeDefinition);
151 } else if (isDerivedType(baseType, getType())) {
152 return getCompositeType(baseType, baseTypeDefinition);
153 } else if (isIdentityRef()) {
154 return getCompositeTypeForIdentity();
157 return getSimpleType(getType());
160 public boolean isIdentityRef() {
161 return typeDefinition instanceof IdentityrefTypeDefinition;
164 private OpenType<?> getCompositeTypeForUnion(TypeDefinition<?> baseTypeDefinition) {
165 Preconditions.checkArgument(baseTypeDefinition instanceof UnionTypeDefinition,
166 "Expected %s instance but was %s", UnionTypeDefinition.class, baseTypeDefinition);
168 List<TypeDefinition<?>> types = ((UnionTypeDefinition) baseTypeDefinition).getTypes();
170 String[] itemNames = new String[types.size()+1];
171 OpenType<?>[] itemTypes = new OpenType[itemNames.length];
173 addArtificialPropertyToUnionCompositeType(baseTypeDefinition, itemNames, itemTypes);
175 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
178 for (TypeDefinition<?> innerTypeDefinition : types) {
180 Type innerType = typeProviderWrapper.getType(innerTypeDefinition, innerTypeDefinition);
182 TypeDefinition<?> baseInnerTypeDefinition = getBaseType(typeProviderWrapper, innerTypeDefinition);
183 Type innerTypeBaseType = typeProviderWrapper.getType(baseInnerTypeDefinition, baseInnerTypeDefinition);
185 OpenType<?> innerCompositeType;
187 if(isDerivedType(innerTypeBaseType, innerType)) {
188 innerCompositeType = getCompositeType(innerTypeBaseType, baseInnerTypeDefinition);
190 innerCompositeType = SimpleTypeResolver.getSimpleType(innerType);
193 itemNames[i] = typeProviderWrapper.getJMXParamForUnionInnerType(innerTypeDefinition);
194 itemTypes[i++] = innerCompositeType;
197 String[] descriptions = Arrays.copyOf(itemNames, itemNames.length);
198 descriptions[0] = DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
201 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, descriptions, itemTypes);
202 } catch (OpenDataException e) {
203 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner elements "
204 + Arrays.toString(itemTypes), e);
208 public static final Class<Character> TYPE_OF_ARTIFICIAL_UNION_PROPERTY = char.class;
210 private void addArtificialPropertyToUnionCompositeType(TypeDefinition<?> baseTypeDefinition, String[] itemNames, OpenType<?>[] itemTypes) {
211 String artificialPropertyName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
212 itemNames[0] = artificialPropertyName;
214 OpenType<?> artificialPropertyType = getArrayOpenTypeForSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName(),
215 SimpleTypeResolver.getSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName()));
216 itemTypes[0] = artificialPropertyType;
219 private boolean isEnum(Type baseType) {
220 return baseType.getFullyQualifiedName().equals(Enum.class.getName());
223 private OpenType<?> getSimpleType(Type type) {
224 SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
228 private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
230 SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
231 String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
233 String[] itemNames = new String[]{innerItemName};
234 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
236 OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
238 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
239 } catch (OpenDataException e) {
240 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
245 public OpenType<?> getCompositeTypeForIdentity() {
246 String[] itemNames = new String[]{IdentityAttributeRef.QNAME_ATTR_NAME};
247 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
248 OpenType<?>[] itemTypes = new OpenType[]{SimpleType.STRING};
251 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
252 } catch (OpenDataException e) {
253 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
258 private OpenType<?> getArrayType() {
259 String innerTypeFullyQName = getInnerType(getType());
260 SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
261 return getArrayOpenTypeForSimpleType(innerTypeFullyQName, innerSimpleType);
264 private OpenType<?> getArrayOpenTypeForSimpleType(String innerTypeFullyQName, SimpleType<?> innerSimpleType) {
266 ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
267 : new ArrayType<>(1, innerSimpleType);
269 } catch (OpenDataException e) {
270 throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
271 + innerSimpleType, e);
276 private boolean isPrimitive(String innerTypeFullyQName) {
277 if (innerTypeFullyQName.contains("."))
283 private boolean isArray() {
284 return type.getName().endsWith("[]");
287 private boolean isDerivedType(Type baseType, Type currentType) {
288 return baseType.equals(currentType) == false;
291 private static String getInnerType(Type type) {
292 String fullyQualifiedName = type.getFullyQualifiedName();
293 return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);