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 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;
25 import javax.management.openmbean.CompositeType;
26 import javax.management.openmbean.OpenDataException;
27 import javax.management.openmbean.OpenType;
28 import java.util.HashMap;
30 import java.util.Map.Entry;
33 public class TOAttribute extends AbstractAttribute implements TypedAttribute {
35 private final String nullableDescription, nullableDefault;
36 private final Map<String, AttributeIfc> yangNameToAttributeMap;
37 private final Map<String, String> attributeNameMap;
38 private final String packageName;
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, String packageName) {
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, packageName));
61 } catch (IllegalArgumentException e) {
62 throw new IllegalStateException("Unable to create TO", e);
65 return new TOAttribute(containerSchemaNode, map, attributeNameMap,
66 containerSchemaNode.getDescription(), packageName);
69 private static AttributeIfc createInnerAttribute(
70 DataSchemaNode dataSchemaNode,
71 TypeProviderWrapper typeProviderWrapper, String packageName) {
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,
79 typeProviderWrapper, packageName);
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,
85 typeProviderWrapper, packageName);
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, String packageName) {
105 yangNameToAttributeMap = transferObject;
106 this.attributeNameMap = attributeNameMap;
107 this.nullableDescription = nullableDescription;
108 nullableDefault = null;
109 this.packageName = packageName;
112 public Map<String, String> getAttributeNameMap() {
113 return attributeNameMap;
116 public Map<String, AttributeIfc> getCapitalizedPropertiesToTypesMap() {
117 Map<String, AttributeIfc> capitalizedPropertiesToTypesMap = Maps
119 for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
122 capitalizedPropertiesToTypesMap.put(
123 TypeProviderWrapper.convertToJavaName(entry.getKey(), true),
126 return capitalizedPropertiesToTypesMap;
129 public Map<String, AttributeIfc> getJmxPropertiesToTypesMap() {
130 Map<String, AttributeIfc> jmxPropertiesToTypesMap = Maps.newHashMap();
131 for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
134 jmxPropertiesToTypesMap.put(
135 TypeProviderWrapper.convertToJavaName(entry.getKey(), false),
138 return jmxPropertiesToTypesMap;
141 public Map<String, AttributeIfc> getYangPropertiesToTypesMap() {
142 return yangNameToAttributeMap;
146 public String getNullableDescription() {
147 return nullableDescription;
151 public String getNullableDefault() {
152 return nullableDefault;
156 public boolean equals(Object o) {
159 if (o == null || getClass() != o.getClass())
161 if (!super.equals(o))
164 TOAttribute that = (TOAttribute) o;
166 if (nullableDefault != null ? !nullableDefault
167 .equals(that.nullableDefault) : that.nullableDefault != null)
169 if (nullableDescription != null ? !nullableDescription
170 .equals(that.nullableDescription)
171 : that.nullableDescription != null)
173 if (yangNameToAttributeMap != null ? !yangNameToAttributeMap
174 .equals(that.yangNameToAttributeMap)
175 : that.yangNameToAttributeMap != null)
182 public int hashCode() {
183 int result = super.hashCode();
186 + (nullableDescription != null ? nullableDescription.hashCode()
189 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
192 + (yangNameToAttributeMap != null ? yangNameToAttributeMap
198 public String toString() {
199 return "TOAttribute{" + getAttributeYangName() + "," + "to="
200 + yangNameToAttributeMap + '}';
204 public Type getType() {
205 // TODO: ReferencedTypeImpl from Types
206 return new ReferencedTypeImpl(packageName, getUpperCaseCammelCase());
210 public CompositeType getOpenType() {
211 String description = getNullableDescription() == null ? getAttributeYangName()
212 : getNullableDescription();
213 final String[] itemNames = new String[yangNameToAttributeMap.keySet()
215 String[] itemDescriptions = itemNames;
216 FunctionImpl functionImpl = new FunctionImpl(itemNames);
217 Map<String, AttributeIfc> jmxPropertiesToTypesMap = getJmxPropertiesToTypesMap();
218 OpenType<?>[] itemTypes = Collections2.transform(
219 jmxPropertiesToTypesMap.entrySet(), functionImpl).toArray(
220 new OpenType<?>[] {});
222 // TODO add package name to create fully qualified name for this
224 CompositeType compositeType = new CompositeType(
225 getUpperCaseCammelCase(), description, itemNames,
226 itemDescriptions, itemTypes);
227 return compositeType;
228 } catch (OpenDataException e) {
229 throw new RuntimeException("Unable to create CompositeType for "
234 public String getPackageName() {
238 private static final class FunctionImpl implements
239 Function<Entry<String, AttributeIfc>, OpenType<?>> {
240 private final String[] itemNames;
243 private FunctionImpl(String[] itemNames) {
244 this.itemNames = itemNames;
248 public OpenType<?> apply(Entry<String, AttributeIfc> input) {
249 AttributeIfc innerType = input.getValue();
250 itemNames[i++] = input.getKey();
251 return innerType.getOpenType();