d830e97678db5d7f4f5b26c8db70daf212472d48
[controller.git] / opendaylight / config / yang-jmx-generator / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / attribute / JavaAttribute.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yangjmxgenerator.attribute;
9
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;
27
28 public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
29
30     public static final String DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION = "valueOfArtificialUnionProperty";
31
32     private final Type type;
33     private final String nullableDescription, nullableDefault, nullableDefaultWrappedForCode;
34     private final TypeProviderWrapper typeProviderWrapper;
35     private final TypeDefinition<?> typeDefinition;
36
37     public JavaAttribute(LeafSchemaNode leaf,
38             TypeProviderWrapper typeProviderWrapper) {
39         super(leaf);
40         this.type = typeProviderWrapper.getType(leaf);
41
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();
47     }
48
49     public JavaAttribute(LeafListSchemaNode leaf,
50             TypeProviderWrapper typeProviderWrapper) {
51         super(leaf);
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();
57     }
58
59     public boolean isUnion() {
60         TypeDefinition<?> base = getBaseType(typeProviderWrapper, typeDefinition);
61         return base instanceof UnionTypeDefinition;
62     }
63
64     public boolean isEnum() {
65         TypeDefinition<?> base = getBaseType(typeProviderWrapper, typeDefinition);
66         return base instanceof EnumTypeDefinition;
67     }
68
69     public TypeDefinition<?> getTypeDefinition() {
70         return typeDefinition;
71     }
72
73     /**
74      * Returns the most base type
75      */
76     private TypeDefinition<?> getBaseType(TypeProviderWrapper typeProviderWrapper, TypeDefinition<?> baseType) {
77         while(baseType.getBaseType()!=null) {
78             baseType = baseType.getBaseType();
79         }
80         return baseType;
81     }
82
83     public String getNullableDefaultWrappedForCode() {
84         return nullableDefaultWrappedForCode;
85     }
86
87     @Override
88     public Type getType() {
89         return type;
90     }
91
92     @Override
93     public String getNullableDescription() {
94         return nullableDescription;
95     }
96
97     @Override
98     public String getNullableDefault() {
99         return nullableDefault;
100     }
101
102     @Override
103     public boolean equals(Object o) {
104         if (this == o) {
105             return true;
106         }
107         if (o == null || getClass() != o.getClass()) {
108             return false;
109         }
110         if (!super.equals(o)) {
111             return false;
112         }
113
114         JavaAttribute that = (JavaAttribute) o;
115
116         if (nullableDefault != null ? !nullableDefault
117                 .equals(that.nullableDefault) : that.nullableDefault != null) {
118             return false;
119         }
120         if (nullableDescription != null ? !nullableDescription
121                 .equals(that.nullableDescription)
122                 : that.nullableDescription != null) {
123             return false;
124         }
125         if (type != null ? !type.equals(that.type) : that.type != null) {
126             return false;
127         }
128
129         return true;
130     }
131
132     @Override
133     public int hashCode() {
134         int result = super.hashCode();
135         result = 31 * result + (type != null ? type.hashCode() : 0);
136         result = 31
137                 * result
138                 + (nullableDescription != null ? nullableDescription.hashCode()
139                         : 0);
140         result = 31 * result
141                 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
142         return result;
143     }
144
145     @Override
146     public String toString() {
147         return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
148                 + '}';
149     }
150
151     @Override
152     public OpenType<?> getOpenType() {
153         TypeDefinition<?> baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition);
154         Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition);
155
156         if (isArray()) {
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();
166         }
167
168         return getSimpleType(getType());
169     }
170
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();
175
176         try {
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);
180         }
181     }
182
183     public boolean isIdentityRef() {
184         return typeDefinition instanceof IdentityrefTypeDefinition;
185     }
186
187     private OpenType<?> getCompositeTypeForUnion(TypeDefinition<?> baseTypeDefinition) {
188         Preconditions.checkArgument(baseTypeDefinition instanceof UnionTypeDefinition,
189                 "Expected %s instance but was %s", UnionTypeDefinition.class, baseTypeDefinition);
190
191         List<TypeDefinition<?>> types = ((UnionTypeDefinition) baseTypeDefinition).getTypes();
192
193         String[] itemNames = new String[types.size()+1];
194         OpenType<?>[] itemTypes = new OpenType[itemNames.length];
195
196         addArtificialPropertyToUnionCompositeType(baseTypeDefinition, itemNames, itemTypes);
197
198         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
199
200         int i = 1;
201         for (TypeDefinition<?> innerTypeDefinition : types) {
202
203             Type innerType = typeProviderWrapper.getType(innerTypeDefinition, innerTypeDefinition);
204
205             TypeDefinition<?> baseInnerTypeDefinition = getBaseType(typeProviderWrapper, innerTypeDefinition);
206             Type innerTypeBaseType = typeProviderWrapper.getType(baseInnerTypeDefinition, baseInnerTypeDefinition);
207
208             OpenType<?> innerCompositeType;
209
210             if(isDerivedType(innerTypeBaseType, innerType)) {
211                 innerCompositeType = baseInnerTypeDefinition instanceof UnionTypeDefinition ?
212                         getCompositeTypeForUnion(baseInnerTypeDefinition) :
213                         getCompositeType(innerTypeBaseType, baseInnerTypeDefinition);
214             } else {
215                 innerCompositeType = SimpleTypeResolver.getSimpleType(innerType);
216             }
217
218             itemNames[i] = typeProviderWrapper.getJMXParamForUnionInnerType(innerTypeDefinition);
219             itemTypes[i++] = innerCompositeType;
220         }
221
222         String[] descriptions = itemNames.clone();
223         descriptions[0] = DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
224
225         try {
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);
230         }
231     }
232
233     public static final Class<Character> TYPE_OF_ARTIFICIAL_UNION_PROPERTY = char.class;
234
235     private void addArtificialPropertyToUnionCompositeType(TypeDefinition<?> baseTypeDefinition, String[] itemNames, OpenType<?>[] itemTypes) {
236         String artificialPropertyName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
237         itemNames[0] = artificialPropertyName;
238
239         OpenType<?> artificialPropertyType = getArrayOpenTypeForSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName(),
240                 SimpleTypeResolver.getSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName()));
241         itemTypes[0] = artificialPropertyType;
242     }
243
244     private OpenType<?> getSimpleType(Type type) {
245         SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
246         return simpleType;
247     }
248
249     private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
250
251         SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
252         String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
253
254         String[] itemNames = new String[]{innerItemName};
255         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
256
257         OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
258         try {
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 "
262                     + itemTypes, e);
263         }
264     }
265
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};
270
271         try {
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 "
275                     + itemTypes, e);
276         }
277     }
278
279     private OpenType<?> getArrayType() {
280         String innerTypeFullyQName = getInnerType(getType());
281         SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
282         return getArrayOpenTypeForSimpleType(innerTypeFullyQName, innerSimpleType);
283     }
284
285     private OpenType<?> getArrayOpenTypeForSimpleType(String innerTypeFullyQName, SimpleType<?> innerSimpleType) {
286         try {
287             ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
288                     : new ArrayType<>(1, innerSimpleType);
289             return arrayType;
290         } catch (OpenDataException e) {
291             throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
292                     + innerSimpleType, e);
293         }
294     }
295
296     // TODO verify
297     private boolean isPrimitive(String innerTypeFullyQName) {
298         if (innerTypeFullyQName.contains(".")) {
299             return false;
300         }
301
302         return true;
303     }
304
305     private boolean isArray() {
306         return type.getName().endsWith("[]");
307     }
308
309     private boolean isDerivedType(Type baseType, Type currentType) {
310         return baseType.equals(currentType) == false;
311     }
312
313     private static String getInnerType(Type type) {
314         String fullyQualifiedName = type.getFullyQualifiedName();
315         return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);
316     }
317
318 }