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 java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.Map.Entry;
20 import javax.management.openmbean.CompositeType;
21 import javax.management.openmbean.OpenDataException;
22 import javax.management.openmbean.OpenType;
23 import org.opendaylight.controller.config.yangjmxgenerator.TypeProviderWrapper;
24 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
25 import org.opendaylight.yangtools.sal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 public class TOAttribute extends AbstractAttribute implements TypedAttribute {
36 private final String nullableDescription, nullableDefault;
37 private final Map<String, AttributeIfc> yangNameToAttributeMap;
38 private final Map<String, String> attributeNameMap;
39 private final String packageName;
41 private static final Set<Class<? extends DataSchemaNode>> ALLOWED_CHILDREN = Sets
44 ALLOWED_CHILDREN.add(LeafListSchemaNode.class);
45 ALLOWED_CHILDREN.add(ListSchemaNode.class);
46 ALLOWED_CHILDREN.add(LeafSchemaNode.class);
47 ALLOWED_CHILDREN.add(ContainerSchemaNode.class);
50 public static <T extends DataNodeContainer & AugmentationTarget & DataSchemaNode> TOAttribute create(
51 T containerSchemaNode, TypeProviderWrapper typeProviderWrapper, String packageName) {
52 // Transfer Object: get the leaves
53 Map<String, AttributeIfc> map = new HashMap<>();
54 Map<String, String> attributeNameMap = new HashMap<>();
55 for (DataSchemaNode dataSchemaNode : containerSchemaNode
58 String yangName = dataSchemaNode.getQName().getLocalName();
60 createInnerAttribute(dataSchemaNode,
61 typeProviderWrapper, packageName));
62 } catch (IllegalArgumentException e) {
63 throw new IllegalStateException("Unable to create TO", e);
66 return new TOAttribute(containerSchemaNode, map, attributeNameMap,
67 containerSchemaNode.getDescription(), packageName);
70 private static AttributeIfc createInnerAttribute(
71 DataSchemaNode dataSchemaNode,
72 TypeProviderWrapper typeProviderWrapper, String packageName) {
73 Class<? extends DataSchemaNode> type = isAllowedType(dataSchemaNode);
75 if (type.equals(LeafSchemaNode.class)) {
76 return new JavaAttribute((LeafSchemaNode) dataSchemaNode,
78 } else if (type.equals(ListSchemaNode.class)) {
79 return ListAttribute.create((ListSchemaNode) dataSchemaNode,
80 typeProviderWrapper, packageName);
81 } else if (type.equals(LeafListSchemaNode.class)) {
82 return ListAttribute.create((LeafListSchemaNode) dataSchemaNode,
84 } else if (type.equals(ContainerSchemaNode.class)) {
85 return TOAttribute.create((ContainerSchemaNode) dataSchemaNode,
86 typeProviderWrapper, packageName);
89 throw new IllegalStateException("This should never happen");
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) {
99 throw new IllegalArgumentException("Illegal child node for TO: "
100 + dataSchemaNode.getClass() + " allowed node types: "
104 private TOAttribute(DataSchemaNode attrNode,
105 Map<String, AttributeIfc> transferObject,
106 Map<String, String> attributeNameMap, String nullableDescription, String packageName) {
108 yangNameToAttributeMap = transferObject;
109 this.attributeNameMap = attributeNameMap;
110 this.nullableDescription = nullableDescription;
111 nullableDefault = null;
112 this.packageName = packageName;
115 public Map<String, String> getAttributeNameMap() {
116 return attributeNameMap;
119 public Map<String, AttributeIfc> getCapitalizedPropertiesToTypesMap() {
120 Map<String, AttributeIfc> capitalizedPropertiesToTypesMap = Maps
122 for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
125 capitalizedPropertiesToTypesMap.put(
126 TypeProviderWrapper.convertToJavaName(entry.getKey(), true),
129 return capitalizedPropertiesToTypesMap;
132 public Map<String, AttributeIfc> getJmxPropertiesToTypesMap() {
133 Map<String, AttributeIfc> jmxPropertiesToTypesMap = Maps.newHashMap();
134 for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
137 jmxPropertiesToTypesMap.put(
138 TypeProviderWrapper.convertToJavaName(entry.getKey(), false),
141 return jmxPropertiesToTypesMap;
144 public Map<String, AttributeIfc> getYangPropertiesToTypesMap() {
145 return yangNameToAttributeMap;
149 public String getNullableDescription() {
150 return nullableDescription;
154 public String getNullableDefault() {
155 return nullableDefault;
159 public boolean equals(Object o) {
163 if (o == null || getClass() != o.getClass()) {
166 if (!super.equals(o)) {
170 TOAttribute that = (TOAttribute) o;
172 if (nullableDefault != null ? !nullableDefault
173 .equals(that.nullableDefault) : that.nullableDefault != null) {
176 if (nullableDescription != null ? !nullableDescription
177 .equals(that.nullableDescription)
178 : that.nullableDescription != null) {
181 if (yangNameToAttributeMap != null ? !yangNameToAttributeMap
182 .equals(that.yangNameToAttributeMap)
183 : that.yangNameToAttributeMap != null) {
191 public int hashCode() {
192 int result = super.hashCode();
195 + (nullableDescription != null ? nullableDescription.hashCode()
198 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
201 + (yangNameToAttributeMap != null ? yangNameToAttributeMap
207 public String toString() {
208 return "TOAttribute{" + getAttributeYangName() + "," + "to="
209 + yangNameToAttributeMap + '}';
213 public Type getType() {
214 // TODO: ReferencedTypeImpl from Types
215 return new ReferencedTypeImpl(packageName, getUpperCaseCammelCase());
219 public CompositeType getOpenType() {
220 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
222 FunctionImpl functionImpl = new FunctionImpl();
223 Map<String, AttributeIfc> jmxPropertiesToTypesMap = getJmxPropertiesToTypesMap();
224 OpenType<?>[] itemTypes = Collections2.transform(
225 jmxPropertiesToTypesMap.entrySet(), functionImpl).toArray(
226 new OpenType<?>[] {});
227 String[] itemNames = functionImpl.getItemNames();
229 // TODO add package name to create fully qualified name for this
231 CompositeType compositeType = new CompositeType(
232 getUpperCaseCammelCase(), description, itemNames,
233 itemNames, itemTypes);
234 return compositeType;
235 } catch (OpenDataException e) {
236 throw new RuntimeException("Unable to create CompositeType for "
241 public String getPackageName() {
247 class FunctionImpl implements
248 Function<Entry<String, AttributeIfc>, OpenType<?>> {
249 private final List<String> itemNames = new ArrayList<>();
252 public OpenType<?> apply(Entry<String, AttributeIfc> input) {
253 AttributeIfc innerType = input.getValue();
254 itemNames.add(input.getKey());
255 return innerType.getOpenType();
258 public String[] getItemNames(){
259 return itemNames.toArray(new String[itemNames.size()]);