Merge "Add support for identity-ref config attributes to config/netconf subsystem"
[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 = getCompositeType(innerTypeBaseType, baseInnerTypeDefinition);
189             } else {
190                 innerCompositeType = SimpleTypeResolver.getSimpleType(innerType);
191             }
192
193             itemNames[i] = typeProviderWrapper.getJMXParamForUnionInnerType(innerTypeDefinition);
194             itemTypes[i++] = innerCompositeType;
195         }
196
197         String[] descriptions = Arrays.copyOf(itemNames, itemNames.length);
198         descriptions[0] = DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION;
199
200         try {
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);
205         }
206     }
207
208     public static final Class<Character> TYPE_OF_ARTIFICIAL_UNION_PROPERTY = char.class;
209
210     private void addArtificialPropertyToUnionCompositeType(TypeDefinition<?> baseTypeDefinition, String[] itemNames, OpenType<?>[] itemTypes) {
211         String artificialPropertyName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
212         itemNames[0] = artificialPropertyName;
213
214         OpenType<?> artificialPropertyType = getArrayOpenTypeForSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName(),
215                 SimpleTypeResolver.getSimpleType(TYPE_OF_ARTIFICIAL_UNION_PROPERTY.getName()));
216         itemTypes[0] = artificialPropertyType;
217     }
218
219     private boolean isEnum(Type baseType) {
220         return baseType.getFullyQualifiedName().equals(Enum.class.getName());
221     }
222
223     private OpenType<?> getSimpleType(Type type) {
224         SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
225         return simpleType;
226     }
227
228      private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
229
230         SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
231         String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
232
233         String[] itemNames = new String[]{innerItemName};
234         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
235
236         OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
237         try {
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 "
241                     + itemTypes, e);
242         }
243     }
244
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};
249
250         try {
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 "
254                     + itemTypes, e);
255         }
256     }
257
258     private OpenType<?> getArrayType() {
259         String innerTypeFullyQName = getInnerType(getType());
260         SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
261         return getArrayOpenTypeForSimpleType(innerTypeFullyQName, innerSimpleType);
262     }
263
264     private OpenType<?> getArrayOpenTypeForSimpleType(String innerTypeFullyQName, SimpleType<?> innerSimpleType) {
265         try {
266             ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
267                     : new ArrayType<>(1, innerSimpleType);
268             return arrayType;
269         } catch (OpenDataException e) {
270             throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
271                     + innerSimpleType, e);
272         }
273     }
274
275     // TODO verify
276     private boolean isPrimitive(String innerTypeFullyQName) {
277         if (innerTypeFullyQName.contains("."))
278             return false;
279
280         return true;
281     }
282
283     private boolean isArray() {
284         return type.getName().endsWith("[]");
285     }
286
287     private boolean isDerivedType(Type baseType, Type currentType) {
288         return baseType.equals(currentType) == false;
289     }
290
291     private static String getInnerType(Type type) {
292         String fullyQualifiedName = type.getFullyQualifiedName();
293         return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);
294     }
295
296 }