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) {
102 if (o == null || getClass() != o.getClass()) {
105 if (!super.equals(o)) {
109 JavaAttribute that = (JavaAttribute) o;
111 if (nullableDefault != null ? !nullableDefault
112 .equals(that.nullableDefault) : that.nullableDefault != null) {
115 if (nullableDescription != null ? !nullableDescription
116 .equals(that.nullableDescription)
117 : that.nullableDescription != null) {
120 if (type != null ? !type.equals(that.type) : that.type != null) {
128 public int hashCode() {
129 int result = super.hashCode();
130 result = 31 * result + (type != null ? type.hashCode() : 0);
133 + (nullableDescription != null ? nullableDescription.hashCode()
136 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
141 public String toString() {
142 return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
147 public OpenType<?> getOpenType() {
148 TypeDefinition<?> baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition);
149 Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition);
152 return getArrayType();
153 } else if (isEnum(baseType)) {
154 return getSimpleType(baseType);
155 } else if (isUnion()) {
156 return getCompositeTypeForUnion(baseTypeDefinition);
157 } else if (isDerivedType(baseType, getType())) {
158 return getCompositeType(baseType, baseTypeDefinition);
159 } else if (isIdentityRef()) {
160 return getCompositeTypeForIdentity();
163 return getSimpleType(getType());
166 public boolean isIdentityRef() {
167 return typeDefinition instanceof IdentityrefTypeDefinition;
170 private OpenType<?> getCompositeTypeForUnion(TypeDefinition<?> baseTypeDefinition) {
171 Preconditions.checkArgument(baseTypeDefinition instanceof UnionTypeDefinition,
172 "Expected %s instance but was %s", UnionTypeDefinition.class, baseTypeDefinition);
174 List<TypeDefinition<?>> types = ((UnionTypeDefinition) baseTypeDefinition).getTypes();
176 String[] itemNames = new String[types.size()+1];
177 OpenType<?>[] itemTypes = new OpenType[itemNames.length];
179 addArtificialPropertyToUnionCompositeType(baseTypeDefinition, itemNames, itemTypes);
181 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
184 for (TypeDefinition<?> innerTypeDefinition : types) {
186 Type innerType = typeProviderWrapper.getType(innerTypeDefinition, innerTypeDefinition);
188 TypeDefinition<?> baseInnerTypeDefinition = getBaseType(typeProviderWrapper, innerTypeDefinition);
189 Type innerTypeBaseType = typeProviderWrapper.getType(baseInnerTypeDefinition, baseInnerTypeDefinition);
191 OpenType<?> innerCompositeType;
193 if(isDerivedType(innerTypeBaseType, innerType)) {
194 innerCompositeType = baseInnerTypeDefinition instanceof UnionTypeDefinition ?
195 getCompositeTypeForUnion(baseInnerTypeDefinition) :
196 getCompositeType(innerTypeBaseType, baseInnerTypeDefinition);
198 innerCompositeType = SimpleTypeResolver.getSimpleType(innerType);
201 itemNames[i] = typeProviderWrapper.getJMXParamForUnionInnerType(innerTypeDefinition);
202 itemTypes[i++] = innerCompositeType;
205 String[] descriptions = Arrays.copyOf(itemNames, itemNames.length);
206 descriptions[0] = DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
209 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, descriptions, itemTypes);
210 } catch (OpenDataException e) {
211 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner elements "
212 + Arrays.toString(itemTypes), e);
216 public static final Class<Character> TYPE_OF_ARTIFICIAL_UNION_PROPERTY = char.class;
218 private void addArtificialPropertyToUnionCompositeType(TypeDefinition<?> baseTypeDefinition, String[] itemNames, OpenType<?>[] itemTypes) {
219 String artificialPropertyName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
220 itemNames[0] = artificialPropertyName;
222 OpenType<?> artificialPropertyType = getArrayOpenTypeForSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName(),
223 SimpleTypeResolver.getSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName()));
224 itemTypes[0] = artificialPropertyType;
227 private boolean isEnum(Type baseType) {
228 return baseType.getFullyQualifiedName().equals(Enum.class.getName());
231 private OpenType<?> getSimpleType(Type type) {
232 SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
236 private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
238 SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
239 String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
241 String[] itemNames = new String[]{innerItemName};
242 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
244 OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
246 return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
247 } catch (OpenDataException e) {
248 throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
253 public OpenType<?> getCompositeTypeForIdentity() {
254 String[] itemNames = new String[]{IdentityAttributeRef.QNAME_ATTR_NAME};
255 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
256 OpenType<?>[] itemTypes = new OpenType[]{SimpleType.STRING};
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 private OpenType<?> getArrayType() {
267 String innerTypeFullyQName = getInnerType(getType());
268 SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
269 return getArrayOpenTypeForSimpleType(innerTypeFullyQName, innerSimpleType);
272 private OpenType<?> getArrayOpenTypeForSimpleType(String innerTypeFullyQName, SimpleType<?> innerSimpleType) {
274 ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
275 : new ArrayType<>(1, innerSimpleType);
277 } catch (OpenDataException e) {
278 throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
279 + innerSimpleType, e);
284 private boolean isPrimitive(String innerTypeFullyQName) {
285 if (innerTypeFullyQName.contains("."))
291 private boolean isArray() {
292 return type.getName().endsWith("[]");
295 private boolean isDerivedType(Type baseType, Type currentType) {
296 return baseType.equals(currentType) == false;
299 private static String getInnerType(Type type) {
300 String fullyQualifiedName = type.getFullyQualifiedName();
301 return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);