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