2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yangjmxgenerator.attribute;
10 import java.util.HashMap;
12 import java.util.Map.Entry;
15 import javax.management.openmbean.CompositeType;
16 import javax.management.openmbean.OpenDataException;
17 import javax.management.openmbean.OpenType;
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;
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;
34 public class TOAttribute extends AbstractAttribute {
36 private final String nullableDescription, nullableDefault;
37 private final Map<String, AttributeIfc> yangNameToAttributeMap;
38 private final Map<String, String> attributeNameMap;
40 private static final Set<Class<? extends DataSchemaNode>> ALLOWED_CHILDREN = Sets
43 ALLOWED_CHILDREN.add(LeafListSchemaNode.class);
44 ALLOWED_CHILDREN.add(ListSchemaNode.class);
45 ALLOWED_CHILDREN.add(LeafSchemaNode.class);
46 ALLOWED_CHILDREN.add(ContainerSchemaNode.class);
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
57 String yangName = dataSchemaNode.getQName().getLocalName();
59 createInnerAttribute(dataSchemaNode,
60 typeProviderWrapper));
61 } catch (IllegalArgumentException e) {
62 throw new IllegalStateException("Unable to create TO");
65 return new TOAttribute(containerSchemaNode, map, attributeNameMap,
66 containerSchemaNode.getDescription());
69 private static AttributeIfc createInnerAttribute(
70 DataSchemaNode dataSchemaNode,
71 TypeProviderWrapper typeProviderWrapper) {
72 Class<? extends DataSchemaNode> type = isAllowedType(dataSchemaNode);
74 if (type.equals(LeafSchemaNode.class))
75 return new JavaAttribute((LeafSchemaNode) dataSchemaNode,
77 else if (type.equals(ListSchemaNode.class))
78 return ListAttribute.create((ListSchemaNode) dataSchemaNode,
80 else if (type.equals(LeafListSchemaNode.class))
81 return ListAttribute.create((LeafListSchemaNode) dataSchemaNode,
83 else if (type.equals(ContainerSchemaNode.class))
84 return TOAttribute.create((ContainerSchemaNode) dataSchemaNode,
87 throw new IllegalStateException("This should never happen");
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)
96 throw new IllegalArgumentException("Illegal child node for TO: "
97 + dataSchemaNode.getClass() + " allowed node types: "
101 private TOAttribute(DataSchemaNode attrNode,
102 Map<String, AttributeIfc> transferObject,
103 Map<String, String> attributeNameMap, String nullableDescription) {
105 yangNameToAttributeMap = transferObject;
106 this.attributeNameMap = attributeNameMap;
107 this.nullableDescription = nullableDescription;
108 nullableDefault = null;
111 public Map<String, String> getAttributeNameMap() {
112 return attributeNameMap;
115 public Map<String, AttributeIfc> getCapitalizedPropertiesToTypesMap() {
116 Map<String, AttributeIfc> capitalizedPropertiesToTypesMap = Maps
118 for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
121 capitalizedPropertiesToTypesMap.put(
122 ModuleMXBeanEntry.convertToJavaName(entry.getKey(), true),
125 return capitalizedPropertiesToTypesMap;
128 public Map<String, AttributeIfc> getJmxPropertiesToTypesMap() {
129 Map<String, AttributeIfc> jmxPropertiesToTypesMap = Maps.newHashMap();
130 for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
133 jmxPropertiesToTypesMap.put(
134 ModuleMXBeanEntry.convertToJavaName(entry.getKey(), false),
137 return jmxPropertiesToTypesMap;
140 public Map<String, AttributeIfc> getYangPropertiesToTypesMap() {
141 return yangNameToAttributeMap;
145 public String getNullableDescription() {
146 return nullableDescription;
150 public String getNullableDefault() {
151 return nullableDefault;
155 public boolean equals(Object o) {
158 if (o == null || getClass() != o.getClass())
160 if (!super.equals(o))
163 TOAttribute that = (TOAttribute) o;
165 if (nullableDefault != null ? !nullableDefault
166 .equals(that.nullableDefault) : that.nullableDefault != null)
168 if (nullableDescription != null ? !nullableDescription
169 .equals(that.nullableDescription)
170 : that.nullableDescription != null)
172 if (yangNameToAttributeMap != null ? !yangNameToAttributeMap
173 .equals(that.yangNameToAttributeMap)
174 : that.yangNameToAttributeMap != null)
181 public int hashCode() {
182 int result = super.hashCode();
185 + (nullableDescription != null ? nullableDescription.hashCode()
188 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
191 + (yangNameToAttributeMap != null ? yangNameToAttributeMap
197 public String toString() {
198 return "TOAttribute{" + getAttributeYangName() + "," + "to="
199 + yangNameToAttributeMap + '}';
203 public OpenType<?> getOpenType() {
204 String description = getNullableDescription() == null ? getAttributeYangName()
205 : getNullableDescription();
206 final String[] itemNames = new String[yangNameToAttributeMap.keySet()
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<?>[] {});
215 // TODO add package name to create fully qualified name for this
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 "
227 private static final class FunctionImpl implements
228 Function<Entry<String, AttributeIfc>, OpenType<?>> {
229 private final String[] itemNames;
232 private FunctionImpl(String[] itemNames) {
233 this.itemNames = itemNames;
237 public OpenType<?> apply(Entry<String, AttributeIfc> input) {
238 AttributeIfc innerType = input.getValue();
239 itemNames[i++] = input.getKey();
240 return innerType.getOpenType();