2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.xsql;
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;
19 import java.util.concurrent.ConcurrentHashMap;
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;
35 * @author Sharon Aicler(saichler@gmail.com)
38 * To be removed in Nitrogen
41 public class XSQLODLUtils {
43 private static Map<Class<?>, Class<?>> types =
44 new ConcurrentHashMap<>();
47 types.put(QName.class, QName.class);
48 types.put(SchemaPath.class, SchemaPath.class);
49 types.put(Status.class, Status.class);
52 public static boolean isColumnType(Class<?> cls) {
53 return types.containsKey(cls);
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);
68 public static String extractTableName(SchemaPath path) {
69 List<QName> lst = path.getPath();
70 StringBuffer name = new StringBuffer();
73 name.append(q.getLocalName());
79 return name.toString();
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);
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();
103 public static List<QName> getPath(Object odlNode) {
104 return ((DataSchemaNode) odlNode).getPath().getPath();
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();
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);
124 public static boolean isModule(Object o) {
125 if (o instanceof Module) {
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);
138 private static void collectODL(XSQLBluePrint bluePrint,
139 XSQLBluePrintNode parent, Collection<DataSchemaNode> nodes, int level) {
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) {
149 collectODL(bluePrint, bn,((ListSchemaNode) n).getChildNodes(), level);
150 Set<AugmentationSchema> s = ((ListSchemaNode)n).getAvailableAugmentations();
152 for(AugmentationSchema as:s){
153 collectODL(bluePrint, bn,as.getChildNodes(), level);
159 collectODL(bluePrint, bn,((DataNodeContainer) n).getChildNodes(), level);
160 if(n instanceof ContainerSchemaNode){
161 Set<AugmentationSchema> s = ((ContainerSchemaNode)n).getAvailableAugmentations();
163 for(AugmentationSchema as:s){
164 collectODL(bluePrint, bn,as.getChildNodes(), level);
171 if (parent != null) {
172 parent.addColumn(n, parent.getParent().getBluePrintNodeName());
174 XSQLAdapter.log("NO Parent!");
180 public static Map<String, Field> refFieldsCache =
183 public static Field findField(Class<?> c, String name) {
187 String cacheKey = c.getName() + name;
188 Field f = refFieldsCache.get(cacheKey);
194 f = c.getDeclaredField(name);
195 f.setAccessible(true);
196 refFieldsCache.put(cacheKey, f);
198 } catch (Exception err) {
201 Class<?> s = c.getSuperclass();
203 f = findField(s, name);
205 refFieldsCache.put(cacheKey, f);
213 public static Object get(Object o, String name) {
215 Class<?> c = o.getClass();
216 Field f = findField(c, name);
218 } catch (Exception err) {
219 //XSQLAdapter.log(err);
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);
233 public static Map<?, ?> getChildren(Object o) {
234 return (Map<?, ?>) get(o, "children");
237 public static Collection<?> getChildrenCollection(Object o) {
238 Object value = get(o, "children");
240 return Collections.emptyList();
241 if(value instanceof Map)
242 return ((Map<?,?>)value).values();
244 if(value instanceof Collection){
245 return (Collection<?>)value;
247 XSQLAdapter.log("Unknown Child Value Type="+value.getClass().getName());
248 return new ArrayList();
252 public static Object getValue(Object o) {
253 return get(o, "value");
256 public static String getNodeIdentiofier(Object o) {
258 return ((PathArgument) get(o, "nodeIdentifier")).getNodeType().toString();
259 }catch(Exception err){
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);
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;
279 if (DerivedTypes.isUint32(type) || DerivedTypes.isUint64(type)) {
281 } else if (DerivedTypes.isUint16(type)) {
283 } else if (DerivedTypes.isUint8(type)) {