16a33b380bb4e54a09ee6e70025e45e8ab1de263
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLODLUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.xsql;
9
10 import java.lang.reflect.Field;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashMap;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentHashMap;
20
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.opendaylight.yangtools.yang.model.api.Status;
31 import org.opendaylight.yangtools.yang.model.util.Uint16;
32 import org.opendaylight.yangtools.yang.model.util.Uint32;
33 import org.opendaylight.yangtools.yang.model.util.Uint64;
34 import org.opendaylight.yangtools.yang.model.util.Uint8;
35 /**
36  * @author Sharon Aicler(saichler@gmail.com)
37  **/
38 public class XSQLODLUtils {
39
40     private static Map<Class<?>, Class<?>> types =
41         new ConcurrentHashMap<Class<?>, Class<?>>();
42
43     static {
44         types.put(QName.class, QName.class);
45         types.put(SchemaPath.class, SchemaPath.class);
46         types.put(Status.class, Status.class);
47     }
48
49     public static boolean isColumnType(Class<?> cls) {
50         return types.containsKey(cls);
51     }
52
53     public static String getTableName(Object odlNode) {
54         if (odlNode instanceof Module) {
55             return ((Module) odlNode).getNamespace().toString();
56         } else if (odlNode instanceof DataSchemaNode) {
57             SchemaPath p = ((DataSchemaNode) odlNode).getPath();
58             return extractTableName(p);
59         } else {
60             int i = 0;
61         }
62         return null;
63     }
64
65     public static String extractTableName(SchemaPath path) {
66         List<QName> lst = path.getPath();
67         StringBuffer name = new StringBuffer();
68         int i = 0;
69         for (QName q : lst) {
70             name.append(q.getLocalName());
71             i++;
72             if (i < lst.size()) {
73                 name.append("/");
74             }
75         }
76         return name.toString();
77     }
78
79     public static String getBluePrintName(Object odlNode){
80         if (odlNode instanceof Module) {
81             return ((Module) odlNode).getNamespace().toString();
82         } else if (odlNode instanceof DataSchemaNode) {
83             SchemaPath p = ((DataSchemaNode) odlNode).getPath();
84             return extractTableName(p);
85         }
86         return null;
87     }
88
89     public static String getODLNodeName(Object odlNode) {
90         if (odlNode instanceof Module) {
91             return ((Module) odlNode).getNamespace().toString();
92         } else if (odlNode instanceof DataSchemaNode) {
93             SchemaPath p = ((DataSchemaNode) odlNode).getPath();
94             List<QName> lst = p.getPath();
95             return lst.get(lst.size() - 1).toString();
96         }
97         return null;
98     }
99
100     public static List<QName> getPath(Object odlNode) {
101         return ((DataSchemaNode) odlNode).getPath().getPath();
102     }
103
104
105     public static String getODLTableName(Object odlNode) {
106         if (odlNode instanceof Module) {
107             return ((Module) odlNode).getNamespace().toString();
108         } else if (odlNode instanceof DataSchemaNode) {
109             return ((DataSchemaNode) odlNode).getPath().toString();
110         }
111         return null;
112     }
113
114     public static String getNodeNameFromDSN(Object o) {
115         DataSchemaNode node = (DataSchemaNode) o;
116         String nodeName = node.getQName().toString();
117         int index = nodeName.lastIndexOf(")");
118         return nodeName.substring(index + 1);
119     }
120
121     public static boolean isModule(Object o) {
122         if (o instanceof Module) {
123             return true;
124         }
125         return false;
126     }
127
128     public static boolean createOpenDaylightCache(XSQLBluePrint bluePrint,Object module) {
129         XSQLBluePrintNode node = new XSQLBluePrintNode(module, 0,null);
130         bluePrint.addToBluePrintCache(node,null);
131         collectODL(bluePrint, node, ((Module) module).getChildNodes(), 1);
132         return true;
133     }
134
135     private static void collectODL(XSQLBluePrint bluePrint,
136         XSQLBluePrintNode parent, Collection<DataSchemaNode> nodes, int level) {
137         if (nodes == null) {
138             return;
139         }
140         for (DataSchemaNode n : nodes) {
141             if (n instanceof DataNodeContainer) {
142                 XSQLBluePrintNode bn = new XSQLBluePrintNode(n, level,parent);
143                 bn = bluePrint.addToBluePrintCache(bn,parent);
144                 if (n instanceof ListSchemaNode) {
145                     level++;
146                     collectODL(bluePrint, bn,((ListSchemaNode) n).getChildNodes(), level);
147                     Set<AugmentationSchema> s = ((ListSchemaNode)n).getAvailableAugmentations();
148                     if(s!=null){
149                         for(AugmentationSchema as:s){
150                             collectODL(bluePrint, bn,as.getChildNodes(), level);
151                         }
152                     }
153                     level--;
154                 }else{
155                     level++;
156                     collectODL(bluePrint, bn,((DataNodeContainer) n).getChildNodes(), level);
157                     if(n instanceof ContainerSchemaNode){
158                        Set<AugmentationSchema> s = ((ContainerSchemaNode)n).getAvailableAugmentations();
159                        if(s!=null){
160                            for(AugmentationSchema as:s){
161                                collectODL(bluePrint, bn,as.getChildNodes(), level);
162                            }
163                        }
164                     }
165                     level--;
166                 }
167             } else {
168                 if (parent != null) {
169                     parent.addColumn(n, parent.getParent().getBluePrintNodeName());
170                 } else {
171                     XSQLAdapter.log("NO Parent!");
172                 }
173             }
174         }
175     }
176
177     public static Map<String, Field> refFieldsCache =
178         new HashMap<String, Field>();
179
180     public static Field findField(Class<?> c, String name) {
181         if (c == null) {
182             return null;
183         }
184         String cacheKey = c.getName() + name;
185         Field f = refFieldsCache.get(cacheKey);
186         if (f != null) {
187             return f;
188         }
189
190         try {
191             f = c.getDeclaredField(name);
192             f.setAccessible(true);
193             refFieldsCache.put(cacheKey, f);
194             return f;
195         } catch (Exception err) {
196         }
197
198         Class<?> s = c.getSuperclass();
199         if (s != null) {
200             f = findField(s, name);
201             if (f != null) {
202                 refFieldsCache.put(cacheKey, f);
203             }
204             return f;
205         }
206         return null;
207     }
208
209
210     public static Object get(Object o, String name) {
211         try {
212             Class<?> c = o.getClass();
213             Field f = findField(c, name);
214             return f.get(o);
215         } catch (Exception err) {
216             //XSQLAdapter.log(err);
217         }
218         return null;
219     }
220
221     public static List<Object> getMChildren(Object o) {
222         Map<?, ?> children = getChildren(o);
223         List<Object> result = new LinkedList<Object>();
224         for (Object val : children.values()) {
225             result.add((Object) val);
226         }
227         return result;
228     }
229
230     public static Map<?, ?> getChildren(Object o) {
231         return (Map<?, ?>) get(o, "children");
232     }
233
234     public static Collection<?> getChildrenCollection(Object o) {
235         Object value = get(o, "children");
236         if(value==null)
237             return Collections.emptyList();
238         if(value instanceof Map)
239             return ((Map<?,?>)value).values();
240         else
241         if(value instanceof Collection){
242             return (Collection<?>)value;
243         }else{
244             XSQLAdapter.log("Unknown Child Value Type="+value.getClass().getName());
245             return new ArrayList();
246         }
247     }
248
249     public static Object getValue(Object o) {
250         return get(o, "value");
251     }
252
253     public static String getNodeIdentiofier(Object o) {
254         try{
255             return ((PathArgument) get(o, "nodeIdentifier")).getNodeType().toString();
256         }catch(Exception err){
257             return null;
258         }
259     }
260
261     public static String getNodeName(Object o) {
262         Object nodeID = get(o, "nodeIdentifier");
263         if (nodeID != null) {
264             String nodeName = nodeID.toString();
265             int index = nodeName.lastIndexOf(")");
266             return nodeName.substring(index + 1);
267         }
268         return "NULL";
269     }
270
271     public static Class<?> getTypeForODLColumn(Object odlNode){
272         Object type = get(odlNode,"type");
273         if(type instanceof Uint32 || type instanceof Uint64){
274             return long.class;
275         }else
276         if(type instanceof Uint16){
277             return int.class;
278         }else
279         if(type instanceof Uint8){
280             return byte.class;
281         }
282         return String.class;
283     }
284
285 }