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