1 package org.opendaylight.controller.md.sal.dom.xsql;
3 import java.lang.reflect.Field;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.LinkedList;
9 import java.util.concurrent.ConcurrentHashMap;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
13 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.util.Uint16;
20 import org.opendaylight.yangtools.yang.model.util.Uint32;
21 import org.opendaylight.yangtools.yang.model.util.Uint64;
22 import org.opendaylight.yangtools.yang.model.util.Uint8;
24 public class XSQLODLUtils {
26 private static Map<Class<?>, Class<?>> types =
27 new ConcurrentHashMap<Class<?>, Class<?>>();
30 types.put(QName.class, QName.class);
31 types.put(SchemaPath.class, SchemaPath.class);
32 types.put(Status.class, Status.class);
35 public static boolean isColumnType(Class cls) {
36 return types.containsKey(cls);
39 public static String getTableName(Object odlNode) {
40 if (odlNode instanceof Module) {
41 return ((Module) odlNode).getNamespace().toString();
42 } else if (odlNode instanceof DataSchemaNode) {
43 SchemaPath p = ((DataSchemaNode) odlNode).getPath();
44 return extractTableName(p);
51 public static String extractTableName(SchemaPath path) {
52 List<QName> lst = path.getPath();
53 StringBuffer name = new StringBuffer();
56 name.append(q.getLocalName());
62 return name.toString();
65 public static String getBluePrintName(Object odlNode){
66 if (odlNode instanceof Module) {
67 return ((Module) odlNode).getNamespace().toString();
68 } else if (odlNode instanceof DataSchemaNode) {
69 SchemaPath p = ((DataSchemaNode) odlNode).getPath();
70 return extractTableName(p);
75 public static String getODLNodeName(Object odlNode) {
76 if (odlNode instanceof Module) {
77 return ((Module) odlNode).getNamespace().toString();
78 } else if (odlNode instanceof DataSchemaNode) {
79 SchemaPath p = ((DataSchemaNode) odlNode).getPath();
80 List<QName> lst = p.getPath();
81 return lst.get(lst.size() - 1).toString();
86 public static List<QName> getPath(Object odlNode) {
87 return ((DataSchemaNode) odlNode).getPath().getPath();
91 public static String getODLTableName(Object odlNode) {
92 if (odlNode instanceof Module) {
93 return ((Module) odlNode).getNamespace().toString();
94 } else if (odlNode instanceof DataSchemaNode) {
95 return ((DataSchemaNode) odlNode).getPath().toString();
100 public static String getNodeNameFromDSN(Object o) {
101 DataSchemaNode node = (DataSchemaNode) o;
102 String nodeName = node.getQName().toString();
103 int index = nodeName.lastIndexOf(")");
104 return nodeName.substring(index + 1);
107 public static boolean isModule(Object o) {
108 if (o instanceof Module) {
114 public static boolean createOpenDaylightCache(XSQLBluePrint bluePrint,Object module) {
115 XSQLBluePrintNode node = new XSQLBluePrintNode(module, 0,null);
116 bluePrint.addToBluePrintCache(node);
117 collectODL(bluePrint, node, ((Module) module).getChildNodes(), 1);
121 private static void collectODL(XSQLBluePrint bluePrint,
122 XSQLBluePrintNode parent, Collection<DataSchemaNode> nodes, int level) {
126 for (DataSchemaNode n : nodes) {
127 if (n instanceof DataNodeContainer /*|| n instanceof LeafListSchemaNode*/
128 || n instanceof ListSchemaNode) {
129 XSQLBluePrintNode bn = new XSQLBluePrintNode(n, level,parent);
130 bluePrint.addToBluePrintCache(bn);
132 if (n instanceof DataNodeContainer) {
134 collectODL(bluePrint, bn,
135 ((DataNodeContainer) n).getChildNodes(), level);
137 } else if (n instanceof ListSchemaNode) {
139 collectODL(bluePrint, bn,
140 ((ListSchemaNode) n).getChildNodes(), level);
144 if (parent != null) {
145 parent.addColumn(n, parent.getParent().getBluePrintNodeName());
147 XSQLAdapter.log("NO Parent!");
153 public static Map<String, Field> refFieldsCache =
154 new HashMap<String, Field>();
156 public static Field findField(Class<?> c, String name) {
160 String cacheKey = c.getName() + name;
161 Field f = refFieldsCache.get(cacheKey);
167 f = c.getDeclaredField(name);
168 f.setAccessible(true);
169 refFieldsCache.put(cacheKey, f);
171 } catch (Exception err) {
174 Class<?> s = c.getSuperclass();
176 f = findField(s, name);
178 refFieldsCache.put(cacheKey, f);
186 public static Object get(Object o, String name) {
188 Class<?> c = o.getClass();
189 Field f = findField(c, name);
191 } catch (Exception err) {
192 XSQLAdapter.log(err);
197 public static List<Object> getMChildren(Object o) {
198 Map<?, ?> children = getChildren(o);
199 List<Object> result = new LinkedList<Object>();
200 for (Object val : children.values()) {
201 result.add((Object) val);
206 public static Map<?, ?> getChildren(Object o) {
207 return (Map<?, ?>) get(o, "children");
210 public static Object getValue(Object o) {
211 return get(o, "value");
214 public static String getNodeIdentiofier(Object o) {
216 return ((PathArgument) get(o, "nodeIdentifier")).getNodeType().toString();
217 }catch(Exception err){
222 public static String getNodeName(Object o) {
223 Object nodeID = get(o, "nodeIdentifier");
224 if (nodeID != null) {
225 String nodeName = nodeID.toString();
226 int index = nodeName.lastIndexOf(")");
227 return nodeName.substring(index + 1);
232 public static Class getTypeForODLColumn(Object odlNode){
233 Object type = get(odlNode,"type");
234 if(type instanceof Uint32 || type instanceof Uint64){
237 if(type instanceof Uint16){
240 if(type instanceof Uint8){