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