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