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.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;
36 * @author Sharon Aicler(saichler@gmail.com)
38 public class XSQLODLUtils {
40 private static Map<Class<?>, Class<?>> types =
41 new ConcurrentHashMap<Class<?>, Class<?>>();
44 types.put(QName.class, QName.class);
45 types.put(SchemaPath.class, SchemaPath.class);
46 types.put(Status.class, Status.class);
49 public static boolean isColumnType(Class<?> cls) {
50 return types.containsKey(cls);
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);
65 public static String extractTableName(SchemaPath path) {
66 List<QName> lst = path.getPath();
67 StringBuffer name = new StringBuffer();
70 name.append(q.getLocalName());
76 return name.toString();
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);
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();
100 public static List<QName> getPath(Object odlNode) {
101 return ((DataSchemaNode) odlNode).getPath().getPath();
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();
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);
121 public static boolean isModule(Object o) {
122 if (o instanceof Module) {
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);
135 private static void collectODL(XSQLBluePrint bluePrint,
136 XSQLBluePrintNode parent, Collection<DataSchemaNode> nodes, int level) {
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) {
146 collectODL(bluePrint, bn,((ListSchemaNode) n).getChildNodes(), level);
147 Set<AugmentationSchema> s = ((ListSchemaNode)n).getAvailableAugmentations();
149 for(AugmentationSchema as:s){
150 collectODL(bluePrint, bn,as.getChildNodes(), level);
156 collectODL(bluePrint, bn,((DataNodeContainer) n).getChildNodes(), level);
157 if(n instanceof ContainerSchemaNode){
158 Set<AugmentationSchema> s = ((ContainerSchemaNode)n).getAvailableAugmentations();
160 for(AugmentationSchema as:s){
161 collectODL(bluePrint, bn,as.getChildNodes(), level);
168 if (parent != null) {
169 parent.addColumn(n, parent.getParent().getBluePrintNodeName());
171 XSQLAdapter.log("NO Parent!");
177 public static Map<String, Field> refFieldsCache =
178 new HashMap<String, Field>();
180 public static Field findField(Class<?> c, String name) {
184 String cacheKey = c.getName() + name;
185 Field f = refFieldsCache.get(cacheKey);
191 f = c.getDeclaredField(name);
192 f.setAccessible(true);
193 refFieldsCache.put(cacheKey, f);
195 } catch (Exception err) {
198 Class<?> s = c.getSuperclass();
200 f = findField(s, name);
202 refFieldsCache.put(cacheKey, f);
210 public static Object get(Object o, String name) {
212 Class<?> c = o.getClass();
213 Field f = findField(c, name);
215 } catch (Exception err) {
216 //XSQLAdapter.log(err);
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);
230 public static Map<?, ?> getChildren(Object o) {
231 return (Map<?, ?>) get(o, "children");
234 public static Collection<?> getChildrenCollection(Object o) {
235 Object value = get(o, "children");
237 return Collections.emptyList();
238 if(value instanceof Map)
239 return ((Map<?,?>)value).values();
241 if(value instanceof Collection){
242 return (Collection<?>)value;
244 XSQLAdapter.log("Unknown Child Value Type="+value.getClass().getName());
245 return new ArrayList();
249 public static Object getValue(Object o) {
250 return get(o, "value");
253 public static String getNodeIdentiofier(Object o) {
255 return ((PathArgument) get(o, "nodeIdentifier")).getNodeType().toString();
256 }catch(Exception err){
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);
271 public static Class<?> getTypeForODLColumn(Object odlNode){
272 Object type = get(odlNode,"type");
273 if(type instanceof Uint32 || type instanceof Uint64){
276 if(type instanceof Uint16){
279 if(type instanceof Uint8){