Merge "BUG 1839 - HTTP delete of non existing data"
[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         }
88         if (o == null || getClass() != o.getClass()) {
89             return false;
90         }
91         if (!super.equals(o)) {
92             return false;
93         }
94
95         ListAttribute that = (ListAttribute) o;
96
97         if (nullableDefault != null ? !nullableDefault
98                 .equals(that.nullableDefault) : that.nullableDefault != null) {
99             return false;
100         }
101         if (nullableDescription != null ? !nullableDescription
102                 .equals(that.nullableDescription)
103                 : that.nullableDescription != null) {
104             return false;
105         }
106
107         return true;
108     }
109
110
111     @Override
112     public Type getType() {
113         return Types.parameterizedTypeFor(Types.typeForClass(List.class), innerAttribute.getType());
114     }
115
116     @Override
117     public ArrayType<?> getOpenType() {
118         OpenType<?> innerOpenType = innerAttribute.getOpenType();
119         return constructArrayType(innerOpenType);
120     }
121
122     static ArrayType<?> constructArrayType(OpenType<?> innerOpenType){
123         try {
124             return new ArrayType<>(1, innerOpenType);
125         } catch (OpenDataException e) {
126             throw new RuntimeException("Unable to create " + ArrayType.class
127                     + " with inner element of type " + innerOpenType, e);
128         }
129     }
130
131 }