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;
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;
34 * @author Sharon Aicler(saichler@gmail.com)
37 * To be removed in Nitrogen
40 public class XSQLODLUtils {
42 private static Map<Class<?>, Class<?>> types =
43 new ConcurrentHashMap<>();
46 types.put(QName.class, QName.class);
47 types.put(SchemaPath.class, SchemaPath.class);
48 types.put(Status.class, Status.class);
51 public static boolean isColumnType(final Class<?> cls) {
52 return types.containsKey(cls);
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);
67 public static String extractTableName(final SchemaPath path) {
68 List<QName> lst = path.getPath();
69 StringBuffer name = new StringBuffer();
72 name.append(q.getLocalName());
78 return name.toString();
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);
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();
102 public static List<QName> getPath(final Object odlNode) {
103 return ((DataSchemaNode) odlNode).getPath().getPath();
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();
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);
123 public static boolean isModule(final Object o) {
124 if (o instanceof Module) {
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);
137 private static void collectODL(final XSQLBluePrint bluePrint,
138 final XSQLBluePrintNode parent, final Collection<DataSchemaNode> nodes, int level) {
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) {
148 collectODL(bluePrint, bn,((ListSchemaNode) n).getChildNodes(), level);
149 Set<AugmentationSchema> s = ((ListSchemaNode)n).getAvailableAugmentations();
151 for(AugmentationSchema as:s){
152 collectODL(bluePrint, bn,as.getChildNodes(), level);
158 collectODL(bluePrint, bn,((DataNodeContainer) n).getChildNodes(), level);
159 if(n instanceof ContainerSchemaNode){
160 Set<AugmentationSchema> s = ((ContainerSchemaNode)n).getAvailableAugmentations();
162 for(AugmentationSchema as:s){
163 collectODL(bluePrint, bn,as.getChildNodes(), level);
170 if (parent != null) {
171 parent.addColumn(n, parent.getParent().getBluePrintNodeName());
173 XSQLAdapter.log("NO Parent!");
179 public static Map<String, Field> refFieldsCache =
182 public static Field findField(final Class<?> c, final String name) {
186 String cacheKey = c.getName() + name;
187 Field f = refFieldsCache.get(cacheKey);
193 f = c.getDeclaredField(name);
194 f.setAccessible(true);
195 refFieldsCache.put(cacheKey, f);
197 } catch (Exception err) {
200 Class<?> s = c.getSuperclass();
202 f = findField(s, name);
204 refFieldsCache.put(cacheKey, f);
212 private static Object get(final Object o, final String name) {
214 Class<?> c = o.getClass();
215 Field f = findField(c, name);
217 } catch (Exception err) {
218 //XSQLAdapter.log(err);
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()) {
232 public static Map<?, ?> getChildren(final Object o) {
233 return (Map<?, ?>) get(o, "children");
236 public static Collection<?> getChildrenCollection(final Object o) {
237 Object value = get(o, "children");
239 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(final Object o) {
253 return get(o, "value");
256 public static String getNodeIdentiofier(final Object o) {
258 return ((PathArgument) get(o, "nodeIdentifier")).getNodeType().toString();
259 }catch(Exception err){
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);
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;
279 if (DerivedTypes.isUint32(type) || DerivedTypes.isUint64(type)) {
281 } else if (DerivedTypes.isUint16(type)) {
283 } else if (DerivedTypes.isUint8(type)) {