73c7e227be8abc03aed2bb7a5b3dc8204fe240f8
[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 (isDerivedType(baseType)) {
125             return getCompositeType(baseType, baseTypeDefinition);
126         }
127
128         return getSimpleType();
129     }
130
131     private OpenType<?> getSimpleType() {
132         SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(getType());
133         return simpleType;
134     }
135
136      private OpenType<?> getCompositeType(Type baseType, TypeDefinition<?> baseTypeDefinition) {
137
138         SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType);
139         String innerItemName = typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition);
140
141         String[] itemNames = new String[]{innerItemName};
142         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
143
144         OpenType<?>[] itemTypes = new OpenType[]{innerItemType};
145         try {
146             return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes);
147         } catch (OpenDataException e) {
148             throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type "
149                     + itemTypes, e);
150         }
151
152     }
153
154     private OpenType<?> getArrayType() {
155         String innerTypeFullyQName = getInnerType(getType());
156         SimpleType<?> innerSimpleType = SimpleTypeResolver.getSimpleType(innerTypeFullyQName);
157         try {
158             ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(innerSimpleType, true)
159                     : new ArrayType<>(1, innerSimpleType);
160             return arrayType;
161         } catch (OpenDataException e) {
162             throw new RuntimeException("Unable to create " + ArrayType.class + " with inner element of type "
163                     + innerSimpleType, e);
164         }
165     }
166
167     // TODO verify
168     private boolean isPrimitive(String innerTypeFullyQName) {
169         if (innerTypeFullyQName.contains("."))
170             return false;
171
172         return true;
173     }
174
175     private boolean isArray() {
176         return type.getName().endsWith("[]");
177     }
178
179     private boolean isDerivedType(Type baseType) {
180         return  baseType.equals(getType()) == false;
181     }
182
183     private static String getInnerType(Type type) {
184         String fullyQualifiedName = type.getFullyQualifiedName();
185         return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);
186     }
187
188 }