73b557e291c03385823fb214fb0a96054d2b6610
[controller.git] / opendaylight / config / yang-jmx-generator / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / attribute / ListAttribute.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.binding.generator.util.Types;
12 import org.opendaylight.yangtools.sal.binding.model.api.Type;
13 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
16
17 import javax.management.openmbean.ArrayType;
18 import javax.management.openmbean.OpenDataException;
19 import javax.management.openmbean.OpenType;
20 import java.util.List;
21
22 public class ListAttribute extends AbstractAttribute implements TypedAttribute {
23
24     private final String nullableDescription, nullableDefault;
25     private final TypedAttribute innerAttribute;
26
27     public static ListAttribute create(ListSchemaNode node,
28             TypeProviderWrapper typeProvider, String packageName) {
29
30         TOAttribute innerAttribute = TOAttribute.create(node, typeProvider, packageName);
31
32         return new ListAttribute(node, innerAttribute, node.getDescription());
33     }
34
35     public static ListAttribute create(LeafListSchemaNode node,
36             TypeProviderWrapper typeProvider) {
37
38         JavaAttribute innerAttribute = new JavaAttribute(node, typeProvider);
39
40         return new ListAttribute(node, innerAttribute, node.getDescription());
41     }
42
43     ListAttribute(DataSchemaNode attrNode, TypedAttribute innerAttribute,
44             String description) {
45         super(attrNode);
46         this.nullableDescription = description;
47         this.innerAttribute = innerAttribute;
48         this.nullableDefault = null;
49     }
50
51     @Override
52     public String getNullableDescription() {
53         return nullableDescription;
54     }
55
56     @Override
57     public String getNullableDefault() {
58         return nullableDefault;
59     }
60
61     public AttributeIfc getInnerAttribute() {
62         return innerAttribute;
63     }
64
65     @Override
66     public String toString() {
67         return "ListAttribute{" + getAttributeYangName() + "," + "to="
68                 + innerAttribute + '}';
69     }
70
71     @Override
72     public int hashCode() {
73         int result = super.hashCode();
74         result = 31
75                 * result
76                 + (nullableDescription != null ? nullableDescription.hashCode()
77                         : 0);
78         result = 31 * result
79                 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
80         return result;
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         ListAttribute that = (ListAttribute) 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
102         return true;
103     }
104
105
106     @Override
107     public Type getType() {
108         return Types.parameterizedTypeFor(Types.typeForClass(List.class), innerAttribute.getType());
109     }
110
111     @Override
112     public ArrayType<?> getOpenType() {
113         OpenType<?> innerOpenType = innerAttribute.getOpenType();
114         return constructArrayType(innerOpenType);
115     }
116
117     static ArrayType<?> constructArrayType(OpenType<?> innerOpenType){
118         try {
119             return new ArrayType<>(1, innerOpenType);
120         } catch (OpenDataException e) {
121             throw new RuntimeException("Unable to create " + ArrayType.class
122                     + " with inner element of type " + innerOpenType, e);
123         }
124     }
125
126 }