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