Merge "Fixed unneeded wrapping of guava for karaf"
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLODLUtils.java
1 package org.opendaylight.controller.md.sal.dom.xsql;
2
3 import java.lang.reflect.Field;
4 import java.util.HashMap;
5 import java.util.LinkedList;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.concurrent.ConcurrentHashMap;
10
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.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;
23
24 public class XSQLODLUtils {
25
26     private static Map<Class<?>, Class<?>> types =
27         new ConcurrentHashMap<Class<?>, Class<?>>();
28
29     static {
30         types.put(QName.class, QName.class);
31         types.put(SchemaPath.class, SchemaPath.class);
32         types.put(Status.class, Status.class);
33     }
34
35     public static boolean isColumnType(Class cls) {
36         return types.containsKey(cls);
37     }
38
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);
45         } else {
46             int i = 0;
47         }
48         return null;
49     }
50
51     public static String extractTableName(SchemaPath path) {
52         List<QName> lst = path.getPath();
53         StringBuffer name = new StringBuffer();
54         int i = 0;
55         for (QName q : lst) {
56             name.append(q.getLocalName());
57             i++;
58             if (i < lst.size()) {
59                 name.append("/");
60             }
61         }
62         return name.toString();
63     }
64
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);
71         }
72         return null;
73     }
74
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();
82         }
83         return null;
84     }
85
86     public static List<QName> getPath(Object odlNode) {
87         return ((DataSchemaNode) odlNode).getPath().getPath();
88     }
89
90
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();
96         }
97         return null;
98     }
99
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);
105     }
106
107     public static boolean isModule(Object o) {
108         if (o instanceof Module) {
109             return true;
110         }
111         return false;
112     }
113
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);
118         return true;
119     }
120
121     private static void collectODL(XSQLBluePrint bluePrint,
122         XSQLBluePrintNode parent, Set<DataSchemaNode> nodes, int level) {
123         if (nodes == null) {
124             return;
125         }
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);
131                 parent.AddChild(bn);
132                 if (n instanceof DataNodeContainer) {
133                     level++;
134                     collectODL(bluePrint, bn,
135                         ((DataNodeContainer) n).getChildNodes(), level);
136                     level--;
137                 } else if (n instanceof ListSchemaNode) {
138                     level++;
139                     collectODL(bluePrint, bn,
140                         ((ListSchemaNode) n).getChildNodes(), level);
141                     level--;
142                 }
143             } else {
144                 if (parent != null) {
145                     parent.addColumn(n, parent.getParent().getBluePrintNodeName());
146                 } else {
147                     XSQLAdapter.log("NO Parent!");
148                 }
149             }
150         }
151     }
152
153     public static Map<String, Field> refFieldsCache =
154         new HashMap<String, Field>();
155
156     public static Field findField(Class<?> c, String name) {
157         if (c == null) {
158             return null;
159         }
160         String cacheKey = c.getName() + name;
161         Field f = refFieldsCache.get(cacheKey);
162         if (f != null) {
163             return f;
164         }
165
166         try {
167             f = c.getDeclaredField(name);
168             f.setAccessible(true);
169             refFieldsCache.put(cacheKey, f);
170             return f;
171         } catch (Exception err) {
172         }
173
174         Class<?> s = c.getSuperclass();
175         if (s != null) {
176             f = findField(s, name);
177             if (f != null) {
178                 refFieldsCache.put(cacheKey, f);
179             }
180             return f;
181         }
182         return null;
183     }
184
185
186     public static Object get(Object o, String name) {
187         try {
188             Class<?> c = o.getClass();
189             Field f = findField(c, name);
190             return f.get(o);
191         } catch (Exception err) {
192             XSQLAdapter.log(err);
193         }
194         return null;
195     }
196
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);
202         }
203         return result;
204     }
205
206     public static Map<?, ?> getChildren(Object o) {
207         return (Map<?, ?>) get(o, "children");
208     }
209
210     public static Object getValue(Object o) {
211         return get(o, "value");
212     }
213
214     public static String getNodeIdentiofier(Object o) {
215         try{
216             return ((PathArgument) get(o, "nodeIdentifier")).getNodeType().toString();
217         }catch(Exception err){
218             return null;
219         }
220     }
221
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);
228         }
229         return "NULL";
230     }
231
232     public static Class getTypeForODLColumn(Object odlNode){
233         Object type = get(odlNode,"type");
234         if(type instanceof Uint32 || type instanceof Uint64){
235             return long.class;
236         }else
237         if(type instanceof Uint16){
238             return int.class;
239         }else
240         if(type instanceof Uint8){
241             return byte.class;
242         }
243         return String.class;
244     }
245
246 }