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