Merge "BUG 1129:Removed hard coded version from netconf project as part of the automa...
[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 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;
19
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;
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 TypeDefinition<?> getTypeDefinition() {
65         return typeDefinition;
66     }
67
68     /**
69      * Returns the most base type
70      */
71     private TypeDefinition<?> getBaseType(TypeProviderWrapper typeProviderWrapper, TypeDefinition<?> baseType) {
72         while(baseType.getBaseType()!=null) {
73             baseType = baseType.getBaseType();
74         }
75         return baseType;
76     }
77
78     public String getNullableDefaultWrappedForCode() {
79         return nullableDefaultWrappedForCode;
80     }
81
82     @Override
83     public Type getType() {
84         return type;
85     }
86
87     @Override
88     public String getNullableDescription() {
89         return nullableDescription;
90     }
91
92     @Override
93     public String getNullableDefault() {
94         return nullableDefault;
95     }
96
97     @Override
98     public boolean equals(Object o) {
99         if (this == o)
100             return true;
101         if (o == null || getClass() != o.getClass())
102             return false;
103         if (!super.equals(o))
104             return false;
105
106         JavaAttribute that = (JavaAttribute) o;
107
108         if (nullableDefault != null ? !nullableDefault
109                 .equals(that.nullableDefault) : that.nullableDefault != null)
110             return false;
111         if (nullableDescription != null ? !nullableDescription
112                 .equals(that.nullableDescription)
113                 : that.nullableDescription != null)
114             return false;
115         if (type != null ? !type.equals(that.type) : that.type != null)
116             return false;
117
118         return true;
119     }
120
121     @Override
122     public int hashCode() {
123         int result = super.hashCode();
124         result = 31 * result + (type != null ? type.hashCode() : 0);
125         result = 31
126                 * result
127                 + (nullableDescription != null ? nullableDescription.hashCode()
128                         : 0);
129         result = 31 * result
130                 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
131         return result;
132     }
133
134     @Override
135     public String toString() {
136         return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
137                 + '}';
138     }
139
140     @Override
141     public OpenType<?> getOpenType() {
142         TypeDefinition<?> baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition);
143         Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition);
144
145         if (isArray()) {
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();
155         }
156
157         return getSimpleType(getType());
158     }
159
160     public boolean isIdentityRef() {
161         return typeDefinition instanceof IdentityrefTypeDefinition;
162     }
163
164     private OpenType<?> getCompositeTypeForUnion(TypeDefinition<?> baseTypeDefinition) {
165         Preconditions.checkArgument(baseTypeDefinition instanceof UnionTypeDefinition,
166                 "Expected %s instance but was %s", UnionTypeDefinition.class, baseTypeDefinition);
167
168         List<TypeDefinition<?>> types = ((UnionTypeDefinition) baseTypeDefinition).getTypes();
169
170         String[] itemNames = new String[types.size()+1];
171         OpenType<?>[] itemTypes = new OpenType[itemNames.length];
172
173         addArtificialPropertyToUnionCompositeType(baseTypeDefinition, itemNames, itemTypes);
174
175         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
176
177         int i = 1;
178         for (TypeDefinition<?> innerTypeDefinition : types) {
179
180             Type innerType = typeProviderWrapper.getType(innerTypeDefinition, innerTypeDefinition);
181
182             TypeDefinition<?> baseInnerTypeDefinition = getBaseType(typeProviderWrapper, innerTypeDefinition);
183             Type innerTypeBaseType = typeProviderWrapper.getType(baseInnerTypeDefinition, baseInnerTypeDefinition);
184
185             OpenType<?> innerCompositeType;
186
187             if(isDerivedType(innerTypeBaseType, innerType)) {
188                 innerCompositeType = baseInnerTypeDefinition instanceof UnionTypeDefinition ?
189                         getCompositeTypeForUnion(baseInnerTypeDefinition) :
190                         getCompositeType(innerTypeBaseType, baseInnerTypeDefinition);
191             } else {
192                 innerCompositeType = SimpleTypeResolver.getSimpleType(innerType);
193             }
194
195             itemNames[i] = typeProviderWrapper.getJMXParamForUnionInnerType(innerTypeDefinition);
196             itemTypes[i++] = innerCompositeType;
197         }
198
199         String[] descriptions = Arrays.copyOf(itemNames, itemNames.length);
200         descriptions[0] = DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
201
202         try {
203             return new CompositeType(getUpperCaseCammelCase(), description, itemNames, descriptions, itemTypes);
204         } catch (OpenDataException e) {
205             throw new RuntimeException("Unable to create " + CompositeType.class + " with inner elements "
206                     + Arrays.toString(itemTypes), e);
207         }
208     }
209
210     public static final Class<Character> TYPE_OF_ARTIFICIAL_UNION_PROPERTY = char.class;
211
212     private void addArtificialPropertyToUnionCompositeType(TypeDefinition<?> baseTypeDefinition, String[] itemNames, OpenType<?>[] itemTypes) {
213         String artificialPropertyName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
214         itemNames[0] = artificialPropertyName;
215
216         OpenType<?> artificialPropertyType = getArrayOpenTypeForSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName(),
217                 SimpleTypeResolver.getSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName()));
218         itemTypes[0] = artificialPropertyType;
219     }
220
221     private boolean isEnum(Type baseType) {
222         return baseType.getFullyQualifiedName().equals(Enum.class.getName());
223     }
224
225     private OpenType<?> getSimpleType(Type type) {
226         SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
227         return simpleType;
228     }
229
230      private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
231
232         SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
233         String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
234
235         String[] itemNames = new String[]{innerItemName};
236         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
237
238         OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
239         try {
240             return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
241         } catch (OpenDataException e) {
242             throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
243                     + itemTypes, e);
244         }
245     }
246
247     public OpenType<?> getCompositeTypeForIdentity() {
248         String[] itemNames = new String[]{IdentityAttributeRef.QNAME_ATTR_NAME};
249         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
250         OpenType<?>[] itemTypes = new OpenType[]{SimpleType.STRING};
251
252         try {
253             return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
254         } catch (OpenDataException e) {
255             throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
256                     + itemTypes, e);
257         }
258     }
259
260     private OpenType<?> getArrayType() {
261         String innerTypeFullyQName = getInnerType(getType());
262         SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
263         return getArrayOpenTypeForSimpleType(innerTypeFullyQName, innerSimpleType);
264     }
265
266     private OpenType<?> getArrayOpenTypeForSimpleType(String innerTypeFullyQName, SimpleType<?> innerSimpleType) {
267         try {
268             ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
269                     : new ArrayType<>(1, innerSimpleType);
270             return arrayType;
271         } catch (OpenDataException e) {
272             throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
273                     + innerSimpleType, e);
274         }
275     }
276
277     // TODO verify
278     private boolean isPrimitive(String innerTypeFullyQName) {
279         if (innerTypeFullyQName.contains("."))
280             return false;
281
282         return true;
283     }
284
285     private boolean isArray() {
286         return type.getName().endsWith("[]");
287     }
288
289     private boolean isDerivedType(Type baseType, Type currentType) {
290         return baseType.equals(currentType) == false;
291     }
292
293     private static String getInnerType(Type type) {
294         String fullyQualifiedName = type.getFullyQualifiedName();
295         return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);
296     }
297
298 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.