BUG-5222: offload XSQLBluePrint creation to first access
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLODLUtils.java
index 17b8ae5f291e7a12f425dd20a96010ddb165af20..a42e47ba005cd9ee3451bad18491a6cff714e6f6 100644 (file)
@@ -1,30 +1,46 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.controller.md.sal.dom.xsql;
 
 import java.lang.reflect.Field;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
-
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.Status;
-import org.opendaylight.yangtools.yang.model.util.Uint16;
-import org.opendaylight.yangtools.yang.model.util.Uint32;
-import org.opendaylight.yangtools.yang.model.util.Uint64;
-import org.opendaylight.yangtools.yang.model.util.Uint8;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes;
 
+/**
+ * @author Sharon Aicler(saichler@gmail.com)
+ **/
+/**
+ * To be removed in Nitrogen
+ */
+@Deprecated
 public class XSQLODLUtils {
 
     private static Map<Class<?>, Class<?>> types =
-        new ConcurrentHashMap<Class<?>, Class<?>>();
+            new ConcurrentHashMap<>();
 
     static {
         types.put(QName.class, QName.class);
@@ -32,11 +48,11 @@ public class XSQLODLUtils {
         types.put(Status.class, Status.class);
     }
 
-    public static boolean isColumnType(Class<?> cls) {
+    public static boolean isColumnType(final Class<?> cls) {
         return types.containsKey(cls);
     }
 
-    public static String getTableName(Object odlNode) {
+    public static String getTableName(final Object odlNode) {
         if (odlNode instanceof Module) {
             return ((Module) odlNode).getNamespace().toString();
         } else if (odlNode instanceof DataSchemaNode) {
@@ -48,7 +64,7 @@ public class XSQLODLUtils {
         return null;
     }
 
-    public static String extractTableName(SchemaPath path) {
+    public static String extractTableName(final SchemaPath path) {
         List<QName> lst = path.getPath();
         StringBuffer name = new StringBuffer();
         int i = 0;
@@ -62,7 +78,7 @@ public class XSQLODLUtils {
         return name.toString();
     }
 
-    public static String getBluePrintName(Object odlNode){
+    public static String getBluePrintName(final Object odlNode){
         if (odlNode instanceof Module) {
             return ((Module) odlNode).getNamespace().toString();
         } else if (odlNode instanceof DataSchemaNode) {
@@ -72,7 +88,7 @@ public class XSQLODLUtils {
         return null;
     }
 
-    public static String getODLNodeName(Object odlNode) {
+    public static String getODLNodeName(final Object odlNode) {
         if (odlNode instanceof Module) {
             return ((Module) odlNode).getNamespace().toString();
         } else if (odlNode instanceof DataSchemaNode) {
@@ -83,12 +99,12 @@ public class XSQLODLUtils {
         return null;
     }
 
-    public static List<QName> getPath(Object odlNode) {
+    public static List<QName> getPath(final Object odlNode) {
         return ((DataSchemaNode) odlNode).getPath().getPath();
     }
 
 
-    public static String getODLTableName(Object odlNode) {
+    public static String getODLTableName(final Object odlNode) {
         if (odlNode instanceof Module) {
             return ((Module) odlNode).getNamespace().toString();
         } else if (odlNode instanceof DataSchemaNode) {
@@ -97,47 +113,57 @@ public class XSQLODLUtils {
         return null;
     }
 
-    public static String getNodeNameFromDSN(Object o) {
+    public static String getNodeNameFromDSN(final Object o) {
         DataSchemaNode node = (DataSchemaNode) o;
         String nodeName = node.getQName().toString();
         int index = nodeName.lastIndexOf(")");
         return nodeName.substring(index + 1);
     }
 
-    public static boolean isModule(Object o) {
+    public static boolean isModule(final Object o) {
         if (o instanceof Module) {
             return true;
         }
         return false;
     }
 
-    public static boolean createOpenDaylightCache(XSQLBluePrint bluePrint,Object module) {
+    public static boolean createOpenDaylightCache(final XSQLBluePrint bluePrint, final Module module) {
         XSQLBluePrintNode node = new XSQLBluePrintNode(module, 0,null);
-        bluePrint.addToBluePrintCache(node);
-        collectODL(bluePrint, node, ((Module) module).getChildNodes(), 1);
+        bluePrint.addToBluePrintCache(node,null);
+        collectODL(bluePrint, node, module.getChildNodes(), 1);
         return true;
     }
 
-    private static void collectODL(XSQLBluePrint bluePrint,
-        XSQLBluePrintNode parent, Collection<DataSchemaNode> nodes, int level) {
+    private static void collectODL(final XSQLBluePrint bluePrint,
+        final XSQLBluePrintNode parent, final Collection<DataSchemaNode> nodes, int level) {
         if (nodes == null) {
             return;
         }
         for (DataSchemaNode n : nodes) {
-            if (n instanceof DataNodeContainer /*|| n instanceof LeafListSchemaNode*/
-                || n instanceof ListSchemaNode) {
+            if (n instanceof DataNodeContainer) {
                 XSQLBluePrintNode bn = new XSQLBluePrintNode(n, level,parent);
-                bluePrint.addToBluePrintCache(bn);
-                parent.AddChild(bn);
-                if (n instanceof DataNodeContainer) {
+                bn = bluePrint.addToBluePrintCache(bn,parent);
+                if (n instanceof ListSchemaNode) {
                     level++;
-                    collectODL(bluePrint, bn,
-                        ((DataNodeContainer) n).getChildNodes(), level);
+                    collectODL(bluePrint, bn,((ListSchemaNode) n).getChildNodes(), level);
+                    Set<AugmentationSchema> s = ((ListSchemaNode)n).getAvailableAugmentations();
+                    if(s!=null){
+                        for(AugmentationSchema as:s){
+                            collectODL(bluePrint, bn,as.getChildNodes(), level);
+                        }
+                    }
                     level--;
-                } else if (n instanceof ListSchemaNode) {
+                }else{
                     level++;
-                    collectODL(bluePrint, bn,
-                        ((ListSchemaNode) n).getChildNodes(), level);
+                    collectODL(bluePrint, bn,((DataNodeContainer) n).getChildNodes(), level);
+                    if(n instanceof ContainerSchemaNode){
+                       Set<AugmentationSchema> s = ((ContainerSchemaNode)n).getAvailableAugmentations();
+                       if(s!=null){
+                           for(AugmentationSchema as:s){
+                               collectODL(bluePrint, bn,as.getChildNodes(), level);
+                           }
+                       }
+                    }
                     level--;
                 }
             } else {
@@ -151,9 +177,9 @@ public class XSQLODLUtils {
     }
 
     public static Map<String, Field> refFieldsCache =
-        new HashMap<String, Field>();
+            new HashMap<>();
 
-    public static Field findField(Class<?> c, String name) {
+    public static Field findField(final Class<?> c, final String name) {
         if (c == null) {
             return null;
         }
@@ -183,35 +209,51 @@ public class XSQLODLUtils {
     }
 
 
-    public static Object get(Object o, String name) {
+    public static Object get(final Object o, final String name) {
         try {
             Class<?> c = o.getClass();
             Field f = findField(c, name);
             return f.get(o);
         } catch (Exception err) {
-            XSQLAdapter.log(err);
+            //XSQLAdapter.log(err);
         }
         return null;
     }
 
-    public static List<Object> getMChildren(Object o) {
+    public static List<Object> getMChildren(final Object o) {
         Map<?, ?> children = getChildren(o);
-        List<Object> result = new LinkedList<Object>();
+        List<Object> result = new LinkedList<>();
         for (Object val : children.values()) {
-            result.add((Object) val);
+            result.add(val);
         }
         return result;
     }
 
-    public static Map<?, ?> getChildren(Object o) {
+    public static Map<?, ?> getChildren(final Object o) {
         return (Map<?, ?>) get(o, "children");
     }
 
-    public static Object getValue(Object o) {
+    public static Collection<?> getChildrenCollection(final Object o) {
+        Object value = get(o, "children");
+        if(value==null) {
+            return Collections.emptyList();
+        }
+        if(value instanceof Map) {
+            return ((Map<?,?>)value).values();
+        } else
+        if(value instanceof Collection){
+            return (Collection<?>)value;
+        }else{
+            XSQLAdapter.log("Unknown Child Value Type="+value.getClass().getName());
+            return new ArrayList();
+        }
+    }
+
+    public static Object getValue(final Object o) {
         return get(o, "value");
     }
 
-    public static String getNodeIdentiofier(Object o) {
+    public static String getNodeIdentiofier(final Object o) {
         try{
             return ((PathArgument) get(o, "nodeIdentifier")).getNodeType().toString();
         }catch(Exception err){
@@ -219,7 +261,7 @@ public class XSQLODLUtils {
         }
     }
 
-    public static String getNodeName(Object o) {
+    public static String getNodeName(final Object o) {
         Object nodeID = get(o, "nodeIdentifier");
         if (nodeID != null) {
             String nodeName = nodeID.toString();
@@ -229,17 +271,20 @@ public class XSQLODLUtils {
         return "NULL";
     }
 
-    public static Class<?> getTypeForODLColumn(Object odlNode){
-        Object type = get(odlNode,"type");
-        if(type instanceof Uint32 || type instanceof Uint64){
-            return long.class;
-        }else
-        if(type instanceof Uint16){
-            return int.class;
-        }else
-        if(type instanceof Uint8){
-            return byte.class;
+    public static Class<?> getTypeForODLColumn(final Object odlNode){
+        final Object o = get(odlNode,"type");
+        if (o instanceof TypeDefinition) {
+            final TypeDefinition<?> type = (TypeDefinition<?>)o;
+
+            if (DerivedTypes.isUint32(type) || DerivedTypes.isUint64(type)) {
+                return long.class;
+            } else if (DerivedTypes.isUint16(type)) {
+                return int.class;
+            } else if (DerivedTypes.isUint8(type)) {
+                return byte.class;
+            }
         }
+
         return String.class;
     }