325ca9ee06fd1bd9d29d56a94cedc0871aac77c7
[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 org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
11 import org.opendaylight.yangtools.sal.binding.model.api.Type;
12 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
15
16 import javax.management.openmbean.ArrayType;
17 import javax.management.openmbean.CompositeType;
18 import javax.management.openmbean.OpenDataException;
19 import javax.management.openmbean.OpenType;
20 import javax.management.openmbean.SimpleType;
21
22 public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
23
24     private final Type type;
25     private final String nullableDescription, nullableDefault;
26     private final TypeProviderWrapper typeProviderWrapper;
27     private final TypeDefinition<?> typeDefinition;
28
29     public JavaAttribute(LeafSchemaNode leaf,
30             TypeProviderWrapper typeProviderWrapper) {
31         super(leaf);
32         this.type = typeProviderWrapper.getType(leaf);
33         this.typeDefinition = leaf.getType();
34         this.typeProviderWrapper = typeProviderWrapper;
35         this.nullableDefault = leaf.getDefault();
36         this.nullableDescription = leaf.getDescription();
37     }
38
39     public JavaAttribute(LeafListSchemaNode leaf,
40             TypeProviderWrapper typeProviderWrapper) {
41         super(leaf);
42         this.type = typeProviderWrapper.getType(leaf);
43         this.typeDefinition = leaf.getType();
44         this.typeProviderWrapper = typeProviderWrapper;
45         this.nullableDefault = null;
46         this.nullableDescription = leaf.getDescription();
47     }
48
49     /**
50      * Returns the most base type
51      */
52     private TypeDefinition<?> getBaseType(TypeProviderWrapper typeProviderWrapper, TypeDefinition<?> baseType) {
53         while(baseType.getBaseType()!=null) {
54             baseType = baseType.getBaseType();
55         }
56         return baseType;
57     }
58
59     @Override
60     public Type getType() {
61         return type;
62     }
63
64     @Override
65     public String getNullableDescription() {
66         return nullableDescription;
67     }
68
69     @Override
70     public String getNullableDefault() {
71         return nullableDefault;
72     }
73
74     @Override
75     public boolean equals(Object o) {
76         if (this == o)
77             return true;
78         if (o == null || getClass() != o.getClass())
79             return false;
80         if (!super.equals(o))
81             return false;
82
83         JavaAttribute that = (JavaAttribute) o;
84
85         if (nullableDefault != null ? !nullableDefault
86                 .equals(that.nullableDefault) : that.nullableDefault != null)
87             return false;
88         if (nullableDescription != null ? !nullableDescription
89                 .equals(that.nullableDescription)
90                 : that.nullableDescription != null)
91             return false;
92         if (type != null ? !type.equals(that.type) : that.type != null)
93             return false;
94
95         return true;
96     }
97
98     @Override
99     public int hashCode() {
100         int result = super.hashCode();
101         result = 31 * result + (type != null ? type.hashCode() : 0);
102         result = 31
103                 * result
104                 + (nullableDescription != null ? nullableDescription.hashCode()
105                         : 0);
106         result = 31 * result
107                 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
108         return result;
109     }
110
111     @Override
112     public String toString() {
113         return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
114                 + '}';
115     }
116
117     @Override
118     public OpenType<?> getOpenType() {
119         TypeDefinition<?> baseTypeDefinition = getBaseType(typeProviderWrapper, typeDefinition);
120         Type baseType = typeProviderWrapper.getType(baseTypeDefinition, baseTypeDefinition);
121
122         if (isArray()) {
123             return getArrayType();
124         } else if (isEnum(baseType)) {
125             return getSimpleType(baseType);
126         } else if (isDerivedType(baseType)) {
127             return getCompositeType(baseType, baseTypeDefinition);
128         }
129
130         return getSimpleType(getType());
131     }
132
133     private boolean isEnum(Type baseType) {
134         return baseType.getFullyQualifiedName().equals(Enum.class.getName());
135     }
136
137     private OpenType<?> getSimpleType(Type type) {
138         SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(type);
139         return simpleType;
140     }
141
142      private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
143
144         SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
145         String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
146
147         String[] itemNames = new String[]{innerItemName};
148         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
149
150         OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
151         try {
152             return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
153         } catch (OpenDataException e) {
154             throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
155                     + itemTypes, e);
156         }
157
158     }
159
160     private OpenType<?> getArrayType() {
161         String innerTypeFullyQName = getInnerType(getType());
162         SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
163         try {
164             ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
165                     : new ArrayType<>(1, innerSimpleType);
166             return arrayType;
167         } catch (OpenDataException e) {
168             throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
169                     + innerSimpleType, e);
170         }
171     }
172
173     // TODO verify
174     private boolean isPrimitive(String innerTypeFullyQName) {
175         if (innerTypeFullyQName.contains("."))
176             return false;
177
178         return true;
179     }
180
181     private boolean isArray() {
182         return type.getName().endsWith("[]");
183     }
184
185     private boolean isDerivedType(Type baseType) {
186         return  baseType.equals(getType()) == false;
187     }
188
189     private static String getInnerType(Type type) {
190         String fullyQualifiedName = type.getFullyQualifiedName();
191         return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);
192     }
193
194 }