8f516ef1812f0a929e73dc42708850fd026fe9e9
[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 javax.management.openmbean.ArrayType;
11 import javax.management.openmbean.OpenDataException;
12 import javax.management.openmbean.OpenType;
13 import javax.management.openmbean.SimpleType;
14
15 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
16 import org.opendaylight.yangtools.sal.binding.model.api.Type;
17 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19
20 public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
21     private final Type type;
22     private final String nullableDescription, nullableDefault;
23
24     public JavaAttribute(LeafSchemaNode leaf,
25             TypeProviderWrapper typeProviderWrapper) {
26         super(leaf);
27         this.type = typeProviderWrapper.getType(leaf);
28         this.nullableDefault = leaf.getDefault();
29         this.nullableDescription = leaf.getDescription();
30     }
31
32     public JavaAttribute(LeafListSchemaNode leaf,
33             TypeProviderWrapper typeProviderWrapper) {
34         super(leaf);
35         this.type = typeProviderWrapper.getType(leaf);
36         this.nullableDefault = null;
37         this.nullableDescription = leaf.getDescription();
38     }
39
40     @Override
41     public Type getType() {
42         return type;
43     }
44
45     @Override
46     public String getNullableDescription() {
47         return nullableDescription;
48     }
49
50     @Override
51     public String getNullableDefault() {
52         return nullableDefault;
53     }
54
55     @Override
56     public boolean equals(Object o) {
57         if (this == o)
58             return true;
59         if (o == null || getClass() != o.getClass())
60             return false;
61         if (!super.equals(o))
62             return false;
63
64         JavaAttribute that = (JavaAttribute) o;
65
66         if (nullableDefault != null ? !nullableDefault
67                 .equals(that.nullableDefault) : that.nullableDefault != null)
68             return false;
69         if (nullableDescription != null ? !nullableDescription
70                 .equals(that.nullableDescription)
71                 : that.nullableDescription != null)
72             return false;
73         if (type != null ? !type.equals(that.type) : that.type != null)
74             return false;
75
76         return true;
77     }
78
79     @Override
80     public int hashCode() {
81         int result = super.hashCode();
82         result = 31 * result + (type != null ? type.hashCode() : 0);
83         result = 31
84                 * result
85                 + (nullableDescription != null ? nullableDescription.hashCode()
86                         : 0);
87         result = 31 * result
88                 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
89         return result;
90     }
91
92     @Override
93     public String toString() {
94         return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
95                 + '}';
96     }
97
98     @Override
99     public OpenType<?> getOpenType() {
100         // If is array => arrayType
101         if (isArray(getType())) {
102             String innerTypeFullyQName = getInnerType(getType());
103             SimpleType<?> innerSimpleType = SimpleTypeResolver
104                     .getSimpleType(innerTypeFullyQName);
105             try {
106                 ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(
107                         innerSimpleType, true) : new ArrayType<>(1,
108                         innerSimpleType);
109                 return arrayType;
110             } catch (OpenDataException e) {
111                 throw new RuntimeException("Unable to create "
112                         + ArrayType.class + " with inner element of type "
113                         + innerSimpleType, e);
114             }
115         }
116         // else simple type
117         SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(getType());
118         return simpleType;
119     }
120
121     // TODO verify
122     private boolean isPrimitive(String innerTypeFullyQName) {
123         if (innerTypeFullyQName.contains("."))
124             return false;
125
126         return true;
127     }
128
129     private static String getInnerType(Type type) {
130         String fullyQualifiedName = type.getFullyQualifiedName();
131         return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);
132     }
133
134     private static boolean isArray(Type type) {
135         return type.getName().endsWith("[]");
136     }
137
138 }

©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.