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