2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yangjmxgenerator.attribute;
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;
15 import javax.management.openmbean.ArrayType;
16 import javax.management.openmbean.OpenDataException;
17 import javax.management.openmbean.OpenType;
18 import javax.management.openmbean.SimpleType;
20 public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
22 private final Type type;
23 private final String nullableDescription, nullableDefault;
25 public JavaAttribute(LeafSchemaNode leaf,
26 TypeProviderWrapper typeProviderWrapper) {
28 this.type = typeProviderWrapper.getType(leaf);
29 this.nullableDefault = leaf.getDefault();
30 this.nullableDescription = leaf.getDescription();
33 public JavaAttribute(LeafListSchemaNode leaf,
34 TypeProviderWrapper typeProviderWrapper) {
36 this.type = typeProviderWrapper.getType(leaf);
37 this.nullableDefault = null;
38 this.nullableDescription = leaf.getDescription();
42 public Type getType() {
47 public String getNullableDescription() {
48 return nullableDescription;
52 public String getNullableDefault() {
53 return nullableDefault;
57 public boolean equals(Object o) {
60 if (o == null || getClass() != o.getClass())
65 JavaAttribute that = (JavaAttribute) o;
67 if (nullableDefault != null ? !nullableDefault
68 .equals(that.nullableDefault) : that.nullableDefault != null)
70 if (nullableDescription != null ? !nullableDescription
71 .equals(that.nullableDescription)
72 : that.nullableDescription != null)
74 if (type != null ? !type.equals(that.type) : that.type != null)
81 public int hashCode() {
82 int result = super.hashCode();
83 result = 31 * result + (type != null ? type.hashCode() : 0);
86 + (nullableDescription != null ? nullableDescription.hashCode()
89 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
94 public String toString() {
95 return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
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);
107 ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(
108 innerSimpleType, true) : new ArrayType<>(1,
111 } catch (OpenDataException e) {
112 throw new RuntimeException("Unable to create "
113 + ArrayType.class + " with inner element of type "
114 + innerSimpleType, e);
118 SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(getType());
123 private boolean isPrimitive(String innerTypeFullyQName) {
124 if (innerTypeFullyQName.contains("."))
130 private static String getInnerType(Type type) {
131 String fullyQualifiedName = type.getFullyQualifiedName();
132 return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);
135 private static boolean isArray(Type type) {
136 return type.getName().endsWith("[]");