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 javax.management.openmbean.ArrayType;
11 import javax.management.openmbean.OpenDataException;
12 import javax.management.openmbean.OpenType;
13 import javax.management.openmbean.SimpleType;
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;
20 public class JavaAttribute extends AbstractAttribute implements TypedAttribute {
21 private final Type type;
22 private final String nullableDescription, nullableDefault;
24 public JavaAttribute(LeafSchemaNode leaf,
25 TypeProviderWrapper typeProviderWrapper) {
27 this.type = typeProviderWrapper.getType(leaf);
28 this.nullableDefault = leaf.getDefault();
29 this.nullableDescription = leaf.getDescription();
32 public JavaAttribute(LeafListSchemaNode leaf,
33 TypeProviderWrapper typeProviderWrapper) {
35 this.type = typeProviderWrapper.getType(leaf);
36 this.nullableDefault = null;
37 this.nullableDescription = leaf.getDescription();
41 public Type getType() {
46 public String getNullableDescription() {
47 return nullableDescription;
51 public String getNullableDefault() {
52 return nullableDefault;
56 public boolean equals(Object o) {
59 if (o == null || getClass() != o.getClass())
64 JavaAttribute that = (JavaAttribute) o;
66 if (nullableDefault != null ? !nullableDefault
67 .equals(that.nullableDefault) : that.nullableDefault != null)
69 if (nullableDescription != null ? !nullableDescription
70 .equals(that.nullableDescription)
71 : that.nullableDescription != null)
73 if (type != null ? !type.equals(that.type) : that.type != null)
80 public int hashCode() {
81 int result = super.hashCode();
82 result = 31 * result + (type != null ? type.hashCode() : 0);
85 + (nullableDescription != null ? nullableDescription.hashCode()
88 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
93 public String toString() {
94 return "JavaAttribute{" + getAttributeYangName() + "," + "type=" + type
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);
106 ArrayType<Object> arrayType = isPrimitive(innerTypeFullyQName) ? new ArrayType<>(
107 innerSimpleType, true) : new ArrayType<>(1,
110 } catch (OpenDataException e) {
111 throw new RuntimeException("Unable to create "
112 + ArrayType.class + " with inner element of type "
113 + innerSimpleType, e);
117 SimpleType<?> simpleType = SimpleTypeResolver.getSimpleType(getType());
122 private boolean isPrimitive(String innerTypeFullyQName) {
123 if (innerTypeFullyQName.contains("."))
129 private static String getInnerType(Type type) {
130 String fullyQualifiedName = type.getFullyQualifiedName();
131 return fullyQualifiedName.substring(0, fullyQualifiedName.length() - 2);
134 private static boolean isArray(Type type) {
135 return type.getName().endsWith("[]");