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 java.util.Arrays;
12 import java.util.List;
13 import javax.management.openmbean.ArrayType;
14 import javax.management.openmbean.CompositeType;
15 import javax.management.openmbean.OpenDataException;
16 import javax.management.openmbean.OpenType;
17 import javax.management.openmbean.SimpleType;
18 import org.opendaylight.controller.config.api.IdentityAttributeRef;
19 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
20 import org.opendaylight.yangtools.sal.binding.model.api.Type;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
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 boolean isEnum() {
65 TypeDefinition<?> base = getBaseType(typeProviderWrapper, typeDefinition);
66 return base instanceof EnumTypeDefinition;
69 public TypeDefinition<?> getTypeDefinition() {
70 return typeDefinition;
74 * Returns the most base type
76 private TypeDefinition<?> getBaseType(TypeProviderWrapper typeProviderWrapper, TypeDefinition<?> baseType) {
77 while(baseType.getBaseType()!=null) {
78 baseType = baseType.getBaseType();
83 public String getNullableDefaultWrappedForCode() {
84 return nullableDefaultWrappedForCode;
88 public Type getType() {
93 public String getNullableDescription() {
94 return nullableDescription;
98 public String getNullableDefault() {
99 return nullableDefault;
103 public boolean equals(Object o) {
107 if (o == null || getClass() != o.getClass()) {
110 if (!super.equals(o)) {
114 JavaAttribute that = (JavaAttribute) o;
116 if (nullableDefault != null ? !nullableDefault
117 .equals(that.nullableDefault) : that.nullableDefault != null) {
120 if (nullableDescription != null ? !nullableDescription
121 .equals(that.nullableDescription)
122 : that.nullableDescription != null) {
125 if (type != null ? !type.equals(that.type) : that.type != null) {
133 public int hashCode() {
134 int result = super.hashCode();
135 result = 31 * result + (type != null ? type.hashCode() : 0);
138 + (nullableDescription != null ? nullableDescription.hashCode()
141 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
146 public String toString() {
147 return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
152 public OpenType<?> getOpenType() {
153 TypeDefinition<?> baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition);
154 Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition);
157 return getArrayType();
158 } else if (isEnum()) {
159 return getEnumType(baseTypeDefinition);
160 } else if (isUnion()) {
161 return getCompositeTypeForUnion(baseTypeDefinition);
162 } else if (isDerivedType(baseType, getType())) {
163 return getCompositeType(baseType, baseTypeDefinition);
164 } else if (isIdentityRef()) {
165 return getCompositeTypeForIdentity();
168 return getSimpleType(getType());
171 private OpenType<?> getEnumType(final TypeDefinition<?> baseType) {
172 final String fullyQualifiedName = typeProviderWrapper.getType(node, getTypeDefinition()).getFullyQualifiedName();
173 final String[] items = {"instance"};
174 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
177 return new CompositeType(fullyQualifiedName, description, items, items, new OpenType[]{SimpleType.STRING});
178 } catch (OpenDataException e) {
179 throw new RuntimeException("Unable to create enum type" + fullyQualifiedName + " as open type", e);
183 public boolean isIdentityRef() {
184 return typeDefinition instanceof IdentityrefTypeDefinition;
187 private OpenType<?> getCompositeTypeForUnion(TypeDefinition<?> baseTypeDefinition) {
188 Preconditions.checkArgument(baseTypeDefinition instanceof UnionTypeDefinition,
189 "Expected %s instance but was %s", UnionTypeDefinition.class, baseTypeDefinition);
191 List<TypeDefinition<?>> types = ((UnionTypeDefinition) baseTypeDefinition).getTypes();
193 String[] itemNames = new String[types.size()+1];
194 OpenType<?>[] itemTypes = new OpenType[itemNames.length];
196 addArtificialPropertyToUnionCompositeType(baseTypeDefinition, itemNames, itemTypes);
198 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
201 for (TypeDefinition<?> innerTypeDefinition : types) {
203 Type innerType = typeProviderWrapper.getType(innerTypeDefinition, innerTypeDefinition);
205 TypeDefinition<?> baseInnerTypeDefinition = getBaseType(typeProviderWrapper, innerTypeDefinition);
206 Type innerTypeBaseType = typeProviderWrapper.getType(baseInnerTypeDefinition, baseInnerTypeDefinition);
208 OpenType<?> innerCompositeType;
210 if(isDerivedType(innerTypeBaseType, innerType)) {
211 innerCompositeType = baseInnerTypeDefinition instanceof UnionTypeDefinition ?
212 getCompositeTypeForUnion(baseInnerTypeDefinition) :
213 getCompositeType(innerTypeBaseType, baseInnerTypeDefinition);
215 innerCompositeType = SimpleTypeResolver.getSimpleType(innerType);
218 itemNames[i] = typeProviderWrapper.getJMXParamForUnionInnerType(innerTypeDefinition);
219 itemTypes[i++] = innerCompositeType;
222 String[] descriptions = itemNames.clone();
223 descriptions[0] = DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
226 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, descriptions, itemTypes);
227 } catch (OpenDataException e) {
228 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner elements "
229 + Arrays.toString(itemTypes), e);
233 public static final Class<Character> TYPE_OF_ARTIFICIAL_UNION_PROPERTY = char.class;
235 private void addArtificialPropertyToUnionCompositeType(TypeDefinition<?> baseTypeDefinition, String[] itemNames, OpenType<?>[] itemTypes) {
236 String artificialPropertyName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
237 itemNames[0] = artificialPropertyName;
239 OpenType<?> artificialPropertyType = getArrayOpenTypeForSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName(),
240 SimpleTypeResolver.getSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName()));
241 itemTypes[0] = artificialPropertyType;
244 private OpenType<?> getSimpleType(Type type) {
245 SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
249 private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
251 SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
252 String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
254 String[] itemNames = new String[]{innerItemName};
255 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
257 OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
259 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
260 } catch (OpenDataException e) {
261 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
266 public OpenType<?> getCompositeTypeForIdentity() {
267 String[] itemNames = new String[]{IdentityAttributeRef.QNAME_ATTR_NAME};
268 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
269 OpenType<?>[] itemTypes = new OpenType[]{SimpleType.STRING};
272 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
273 } catch (OpenDataException e) {
274 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
279 private OpenType<?> getArrayType() {
280 String innerTypeFullyQName = getInnerType(getType());
281 SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
282 return getArrayOpenTypeForSimpleType(innerTypeFullyQName, innerSimpleType);
285 private OpenType<?> getArrayOpenTypeForSimpleType(String innerTypeFullyQName, SimpleType<?> innerSimpleType) {
287 ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
288 : new ArrayType<>(1, innerSimpleType);
290 } catch (OpenDataException e) {
291 throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
292 + innerSimpleType, e);
297 private boolean isPrimitive(String innerTypeFullyQName) {
298 if (innerTypeFullyQName.contains(".")) {
305 private boolean isArray() {
306 return type.getName().endsWith("[]");
309 private boolean isDerivedType(Type baseType, Type currentType) {
310 return baseType.equals(currentType) == false;
313 private static String getInnerType(Type type) {
314 String fullyQualifiedName = type.getFullyQualifiedName();
315 return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);