Merge "Add reverse() method in Edge and Path classes"
[controller.git] / opendaylight / config / yang-jmx-generator / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / attribute / TOAttribute.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 com.google.common.base.Function;
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.Maps;
13 import com.google.common.collect.Sets;
14 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
15 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
16 import org.opendaylight.yangtools.sal.binding.model.api.Type;
17 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
18 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
24
25 import javax.management.openmbean.CompositeType;
26 import javax.management.openmbean.OpenDataException;
27 import javax.management.openmbean.OpenType;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.Set;
34
35 public class TOAttribute extends AbstractAttribute implements TypedAttribute {
36
37     private final String nullableDescription, nullableDefault;
38     private final Map<String, AttributeIfc> yangNameToAttributeMap;
39     private final Map<String, String> attributeNameMap;
40     private final String packageName;
41
42     private static final Set<Class<? extends DataSchemaNode>> ALLOWED_CHILDREN = Sets
43             .newHashSet();
44     static {
45         ALLOWED_CHILDREN.add(LeafListSchemaNode.class);
46         ALLOWED_CHILDREN.add(ListSchemaNode.class);
47         ALLOWED_CHILDREN.add(LeafSchemaNode.class);
48         ALLOWED_CHILDREN.add(ContainerSchemaNode.class);
49     }
50
51     public static <T extends DataNodeContainer & AugmentationTarget & DataSchemaNode> TOAttribute create(
52             T containerSchemaNode, TypeProviderWrapper typeProviderWrapper, String packageName) {
53         // Transfer Object: get the leaves
54         Map<String, AttributeIfc> map = new HashMap<>();
55         Map<String, String> attributeNameMap = new HashMap<>();
56         for (DataSchemaNode dataSchemaNode : containerSchemaNode
57                 .getChildNodes()) {
58             try {
59                 String yangName = dataSchemaNode.getQName().getLocalName();
60                 map.put(yangName,
61                         createInnerAttribute(dataSchemaNode,
62                                 typeProviderWrapper, packageName));
63             } catch (IllegalArgumentException e) {
64                 throw new IllegalStateException("Unable to create TO", e);
65             }
66         }
67         return new TOAttribute(containerSchemaNode, map, attributeNameMap,
68                 containerSchemaNode.getDescription(), packageName);
69     }
70
71     private static AttributeIfc createInnerAttribute(
72             DataSchemaNode dataSchemaNode,
73             TypeProviderWrapper typeProviderWrapper, String packageName) {
74         Class<? extends DataSchemaNode> type = isAllowedType(dataSchemaNode);
75
76         if (type.equals(LeafSchemaNode.class))
77             return new JavaAttribute((LeafSchemaNode) dataSchemaNode,
78                     typeProviderWrapper);
79         else if (type.equals(ListSchemaNode.class))
80             return ListAttribute.create((ListSchemaNode) dataSchemaNode,
81                     typeProviderWrapper, packageName);
82         else if (type.equals(LeafListSchemaNode.class))
83             return ListAttribute.create((LeafListSchemaNode) dataSchemaNode,
84                     typeProviderWrapper);
85         else if (type.equals(ContainerSchemaNode.class))
86             return TOAttribute.create((ContainerSchemaNode) dataSchemaNode,
87                     typeProviderWrapper, packageName);
88
89         throw new IllegalStateException("This should never happen");
90     }
91
92     private static Class<? extends DataSchemaNode> isAllowedType(
93             DataSchemaNode dataSchemaNode) {
94         for (Class<? extends DataSchemaNode> allowedType : ALLOWED_CHILDREN) {
95             if (allowedType.isAssignableFrom(dataSchemaNode.getClass()) == true)
96                 return allowedType;
97         }
98         throw new IllegalArgumentException("Illegal child node for TO: "
99                 + dataSchemaNode.getClass() + " allowed node types: "
100                 + ALLOWED_CHILDREN);
101     }
102
103     private TOAttribute(DataSchemaNode attrNode,
104             Map<String, AttributeIfc> transferObject,
105             Map<String, String> attributeNameMap, String nullableDescription, String packageName) {
106         super(attrNode);
107         yangNameToAttributeMap = transferObject;
108         this.attributeNameMap = attributeNameMap;
109         this.nullableDescription = nullableDescription;
110         nullableDefault = null;
111         this.packageName = packageName;
112     }
113
114     public Map<String, String> getAttributeNameMap() {
115         return attributeNameMap;
116     }
117
118     public Map<String, AttributeIfc> getCapitalizedPropertiesToTypesMap() {
119         Map<String, AttributeIfc> capitalizedPropertiesToTypesMap = Maps
120                 .newHashMap();
121         for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
122                 .entrySet()) {
123
124             capitalizedPropertiesToTypesMap.put(
125                     TypeProviderWrapper.convertToJavaName(entry.getKey(), true),
126                     entry.getValue());
127         }
128         return capitalizedPropertiesToTypesMap;
129     }
130
131     public Map<String, AttributeIfc> getJmxPropertiesToTypesMap() {
132         Map<String, AttributeIfc> jmxPropertiesToTypesMap = Maps.newHashMap();
133         for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
134                 .entrySet()) {
135
136             jmxPropertiesToTypesMap.put(
137                     TypeProviderWrapper.convertToJavaName(entry.getKey(), false),
138                     entry.getValue());
139         }
140         return jmxPropertiesToTypesMap;
141     }
142
143     public Map<String, AttributeIfc> getYangPropertiesToTypesMap() {
144         return yangNameToAttributeMap;
145     }
146
147     @Override
148     public String getNullableDescription() {
149         return nullableDescription;
150     }
151
152     @Override
153     public String getNullableDefault() {
154         return nullableDefault;
155     }
156
157     @Override
158     public boolean equals(Object o) {
159         if (this == o)
160             return true;
161         if (o == null || getClass() != o.getClass())
162             return false;
163         if (!super.equals(o))
164             return false;
165
166         TOAttribute that = (TOAttribute) o;
167
168         if (nullableDefault != null ? !nullableDefault
169                 .equals(that.nullableDefault) : that.nullableDefault != null)
170             return false;
171         if (nullableDescription != null ? !nullableDescription
172                 .equals(that.nullableDescription)
173                 : that.nullableDescription != null)
174             return false;
175         if (yangNameToAttributeMap != null ? !yangNameToAttributeMap
176                 .equals(that.yangNameToAttributeMap)
177                 : that.yangNameToAttributeMap != null)
178             return false;
179
180         return true;
181     }
182
183     @Override
184     public int hashCode() {
185         int result = super.hashCode();
186         result = 31
187                 * result
188                 + (nullableDescription != null ? nullableDescription.hashCode()
189                         : 0);
190         result = 31 * result
191                 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
192         result = 31
193                 * result
194                 + (yangNameToAttributeMap != null ? yangNameToAttributeMap
195                         .hashCode() : 0);
196         return result;
197     }
198
199     @Override
200     public String toString() {
201         return "TOAttribute{" + getAttributeYangName() + "," + "to="
202                 + yangNameToAttributeMap + '}';
203     }
204
205     @Override
206     public Type getType() {
207         // TODO: ReferencedTypeImpl from Types
208         return new ReferencedTypeImpl(packageName, getUpperCaseCammelCase());
209     }
210
211     @Override
212     public CompositeType getOpenType() {
213         String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
214
215         FunctionImpl functionImpl = new FunctionImpl();
216         Map<String, AttributeIfc> jmxPropertiesToTypesMap = getJmxPropertiesToTypesMap();
217         OpenType<?>[] itemTypes = Collections2.transform(
218                 jmxPropertiesToTypesMap.entrySet(), functionImpl).toArray(
219                 new OpenType<?>[] {});
220         String[] itemNames = functionImpl.getItemNames();
221         try {
222             // TODO add package name to create fully qualified name for this
223             // type
224             CompositeType compositeType = new CompositeType(
225                     getUpperCaseCammelCase(), description, itemNames,
226                     itemNames, itemTypes);
227             return compositeType;
228         } catch (OpenDataException e) {
229             throw new RuntimeException("Unable to create CompositeType for "
230                     + this, e);
231         }
232     }
233
234     public String getPackageName() {
235         return packageName;
236     }
237
238 }
239
240 class FunctionImpl implements
241         Function<Entry<String, AttributeIfc>, OpenType<?>> {
242     private final List<String> itemNames = new ArrayList<>();
243
244     @Override
245     public OpenType<?> apply(Entry<String, AttributeIfc> input) {
246         AttributeIfc innerType = input.getValue();
247         itemNames.add(input.getKey());
248         return innerType.getOpenType();
249     }
250
251     public String[] getItemNames(){
252         return itemNames.toArray(new String[itemNames.size()]);
253     }
254 }