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.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Map.Entry;
35 public class TOAttribute extends AbstractAttribute implements TypedAttribute {
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;
42 private static final Set<Class<? extends DataSchemaNode>> ALLOWED_CHILDREN = Sets
45 ALLOWED_CHILDREN.add(LeafListSchemaNode.class);
46 ALLOWED_CHILDREN.add(ListSchemaNode.class);
47 ALLOWED_CHILDREN.add(LeafSchemaNode.class);
48 ALLOWED_CHILDREN.add(ContainerSchemaNode.class);
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
59 String yangName = dataSchemaNode.getQName().getLocalName();
61 createInnerAttribute(dataSchemaNode,
62 typeProviderWrapper, packageName));
63 } catch (IllegalArgumentException e) {
64 throw new IllegalStateException("Unable to create TO", e);
67 return new TOAttribute(containerSchemaNode, map, attributeNameMap,
68 containerSchemaNode.getDescription(), packageName);
71 private static AttributeIfc createInnerAttribute(
72 DataSchemaNode dataSchemaNode,
73 TypeProviderWrapper typeProviderWrapper, String packageName) {
74 Class<? extends DataSchemaNode> type = isAllowedType(dataSchemaNode);
76 if (type.equals(LeafSchemaNode.class))
77 return new JavaAttribute((LeafSchemaNode) dataSchemaNode,
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,
85 else if (type.equals(ContainerSchemaNode.class))
86 return TOAttribute.create((ContainerSchemaNode) dataSchemaNode,
87 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)
98 throw new IllegalArgumentException("Illegal child node for TO: "
99 + dataSchemaNode.getClass() + " allowed node types: "
103 private TOAttribute(DataSchemaNode attrNode,
104 Map<String, AttributeIfc> transferObject,
105 Map<String, String> attributeNameMap, String nullableDescription, String packageName) {
107 yangNameToAttributeMap = transferObject;
108 this.attributeNameMap = attributeNameMap;
109 this.nullableDescription = nullableDescription;
110 nullableDefault = null;
111 this.packageName = packageName;
114 public Map<String, String> getAttributeNameMap() {
115 return attributeNameMap;
118 public Map<String, AttributeIfc> getCapitalizedPropertiesToTypesMap() {
119 Map<String, AttributeIfc> capitalizedPropertiesToTypesMap = Maps
121 for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
124 capitalizedPropertiesToTypesMap.put(
125 TypeProviderWrapper.convertToJavaName(entry.getKey(), true),
128 return capitalizedPropertiesToTypesMap;
131 public Map<String, AttributeIfc> getJmxPropertiesToTypesMap() {
132 Map<String, AttributeIfc> jmxPropertiesToTypesMap = Maps.newHashMap();
133 for (Entry<String, AttributeIfc> entry : yangNameToAttributeMap
136 jmxPropertiesToTypesMap.put(
137 TypeProviderWrapper.convertToJavaName(entry.getKey(), false),
140 return jmxPropertiesToTypesMap;
143 public Map<String, AttributeIfc> getYangPropertiesToTypesMap() {
144 return yangNameToAttributeMap;
148 public String getNullableDescription() {
149 return nullableDescription;
153 public String getNullableDefault() {
154 return nullableDefault;
158 public boolean equals(Object o) {
161 if (o == null || getClass() != o.getClass())
163 if (!super.equals(o))
166 TOAttribute that = (TOAttribute) o;
168 if (nullableDefault != null ? !nullableDefault
169 .equals(that.nullableDefault) : that.nullableDefault != null)
171 if (nullableDescription != null ? !nullableDescription
172 .equals(that.nullableDescription)
173 : that.nullableDescription != null)
175 if (yangNameToAttributeMap != null ? !yangNameToAttributeMap
176 .equals(that.yangNameToAttributeMap)
177 : that.yangNameToAttributeMap != null)
184 public int hashCode() {
185 int result = super.hashCode();
188 + (nullableDescription != null ? nullableDescription.hashCode()
191 + (nullableDefault != null ? nullableDefault.hashCode() : 0);
194 + (yangNameToAttributeMap != null ? yangNameToAttributeMap
200 public String toString() {
201 return "TOAttribute{" + getAttributeYangName() + "," + "to="
202 + yangNameToAttributeMap + '}';
206 public Type getType() {
207 // TODO: ReferencedTypeImpl from Types
208 return new ReferencedTypeImpl(packageName, getUpperCaseCammelCase());
212 public CompositeType getOpenType() {
213 String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription();
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();
222 // TODO add package name to create fully qualified name for this
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 "
234 public String getPackageName() {
240 class FunctionImpl implements
241 Function<Entry<String, AttributeIfc>, OpenType<?>> {
242 private final List<String> itemNames = new ArrayList<>();
245 public OpenType<?> apply(Entry<String, AttributeIfc> input) {
246 AttributeIfc innerType = input.getValue();
247 itemNames.add(input.getKey());
248 return innerType.getOpenType();
251 public String[] getItemNames(){
252 return itemNames.toArray(new String[itemNames.size()]);