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