Index: /branches/rel_avx_2_7_5/src/webui/webui/htdocs/new/src/avx/models/va/instance/__init__.py
===================================================================
--- /branches/rel_avx_2_7_5/src/webui/webui/htdocs/new/src/avx/models/va/instance/__init__.py	(revision 9228)
+++ /branches/rel_avx_2_7_5/src/webui/webui/htdocs/new/src/avx/models/va/instance/__init__.py	(working copy)
@@ -1,13 +1,14 @@
 from hive.imports.model import *
 from django.utils.translation import ugettext_lazy as _
-from hive.utils import andebug
+from hive.utils import andebug, aninfo, _thread_locals
 from hive.model.query import mark_expire_all
 from hive.model.loading import get_model
 from hive.utils import get_current_session
 from hive.model.legacycli import cli_parse, RegexParser, MATCHALL
-from djproject.an_settings import VA_RUN_CONFIG_INFO_PATH, VA_BOOT_TIME_INFO_PATH, VA_CONFIG_XML_PATH
+from djproject.an_settings import VA_RUN_CONFIG_INFO_PATH, VA_BOOT_TIME_INFO_PATH, VA_CONFIG_XML_PATH, FAST_CLI_CMD
 from datetime import datetime
 from avx.company import *
+import threading
 import cgi
 try:
     import xml.etree.cElementTree as ET
@@ -143,21 +144,246 @@
 
     class Manager(CLIManager):
         def _get_query_set(self):
-            vaHandler = VAInstanceHandle(self.cli)
-            current_time = int(time.time())
-            query_type = ""
-            if getattr(self, 'query_type', None):
-                query_type = getattr(self, 'query_type')
-            counter = SessionCounter(vaHandler.get_id(), query_type)
-            counter_data = counter.get_counter_data()
-            if counter_data['timestamp'] <= current_time: # check newest request
-                request_id = str(uuid.uuid4())
-                counter.cleanup_old_results(request_id)
-                counter.set_counter_data(counter_data['counter'] + 1, request_id, current_time)
-                vaHandler.get_data_from_cli(request_id, counter)
-            else:
-                counter.wait_for_result(counter_data['uuid'])
-            rst = counter.read_result_file(counter.get_counter_data()['uuid'])
+            self.cli.set_enable()
+            result = self.cli.cmd('show va instance',
+                                  EasyParser('va instance', ['?va_name', '?va_size', '?starting_port', '?domain_id', '?tgt_name']),
+                                  EasyParser('va custinstance', ['?va_name', '?cpu_thread', '?memory', '?domain_id', '?tgt_name']))
+
+            # --- Run all independent CLI queries in parallel threads ---
+            # Each cli.cmd() call creates its own TCP connection (connect/disconnect),
+            # and uses _thread_locals for socket storage, so threads don't conflict.
+            _pdata = {}       # key -> result
+            _perrs = []       # list of (key, exception)
+            _plock = threading.Lock()
+            _parent_tid = getattr(_thread_locals, '_hive_thread_id', 0)
+            _cli_ref = self.cli
+
+            def _par_worker(key, tidx, cmd_tuple):
+                """Worker thread: init thread-local state, set enable, run CLI command."""
+                try:
+                    _thread_locals._hive_thread_id = _parent_tid * 1000 + tidx
+                    if FAST_CLI_CMD:
+                        _thread_locals._is_set_enable = True
+                    else:
+                        _cli_ref.set_enable()
+                    _pdata[key] = _cli_ref.cmd(*cmd_tuple)
+                except Exception as e:
+                    with _plock:
+                        _perrs.append((key, e))
+
+            # Define all independent queries (key, thread_index, cmd_tuple)
+            _queries = [
+                ('cpu', 1, ('show va resource', RegexParser('\s+(?P<va_name>[^\n]+?)\s+(?P<cpu_thread>[0-9]+(\sshared)?)\s+(?P<memory>[0-9]+)G\s+[0-9]+\s+dedicated VF\s+', MATCHALL, reflags=re.S))),
+                ('status', 2, ('show va status', RegexParser('(?P<va_name>\S+)\s+(?P<status>running|shutoff)\n', MATCHALL, reflags=re.S))),
+                ('vnc', 3, ('show vncport', RegexParser('(?P<va_name>\S+):\s*(?P<vnc_port>[0-9]+)\n', MATCHALL, reflags=re.S))),
+                ('mgmtip', 4, ('show va mgmtip', RegexParser('va mgmtip (?P<va_name>\S+) (?P<ip>\S+)\n', MATCHALL, reflags=re.S))),
+                ('vlan', 5, ('show va mgmtvlan', RegexParser('va mgmtvlan (?P<va_name>\S+) (?P<tag>\S+)\n', MATCHALL, reflags=re.S))),
+                ('nic_type', 6, ('show va mgmttype', RegexParser('(?P<va_name>\S+)\s+(?P<nic_type>\S+)\n', MATCHALL, reflags=re.S))),
+                ('disk', 7, ('show va disk',)),
+            ]
+            _tidx = 8
+            if COMPANY_KEYWORD == "Array":
+                for ltype in ['apv', 'ag', 'netgate', 'netiag']:
+                    _queries.append(('lic_' + ltype, _tidx, ('show va license ' + ltype, EasyParser('', ['?va_name', '?type']))))
+                    _tidx += 1
+            if COMPANY_KEYWORD == "Infosec":
+                for ltype in ['nsae', 'netgate', 'netopti', 'apv', 'ag', 'netiag']:
+                    _queries.append(('lic_' + ltype, _tidx, ('show va license ' + ltype, EasyParser('', ['?va_name', '?type']))))
+                    _tidx += 1
+
+            _threads = []
+            for key, tidx, cmd_tuple in _queries:
+                t = threading.Thread(target=_par_worker, args=(key, tidx, cmd_tuple))
+                t.daemon = True
+                t.start()
+                _threads.append(t)
+
+            for t in _threads:
+                t.join(timeout=60.0)
+
+            if _perrs:
+                raise _perrs[0][1]
+
+            # --- Extract parallel results ---
+            cpu_thread_result = _pdata['cpu']
+            va_status_result = _pdata['status']
+            vnc_result = _pdata['vnc']
+            mgmtip_result = _pdata['mgmtip']
+            vlan_result = _pdata['vlan']
+            nic_type_result = _pdata['nic_type']
+            raw_disk_output = _pdata['disk']
+
+            # --- Parse disk output (plain string split, no regex) ---
+            sections = raw_disk_output.strip().split('\n\n')
+            res = []
+            for section in sections:
+                lines = section.strip().split('\n')
+                if not lines:
+                    continue
+                header = lines[0].strip()
+                if not header.endswith(':'):
+                    continue
+                v_name = header[:-1]
+                for item in lines[1:]:
+                    if not item.strip():
+                        continue
+                    parts = item.split()
+                    if len(parts) < 3:
+                        continue
+                    if parts[0] in ["Disk", "reserved"]:
+                        continue
+                    res.append({
+                        "disk_name": parts[0],
+                        "va_name": v_name,
+                        "disk_size": parts[1].split("(")[0],
+                        "bus_type": parts[2]
+                    })
+
+            # --- Build O(1) lookup dicts ---
+            cpu_map = {}
+            for item in cpu_thread_result:
+                cpu_map[item["va_name"]] = item
+
+            vnc_map = {}
+            for item in vnc_result:
+                vnc_map[item["va_name"]] = item
+
+            mgmtip_map = {}
+            for item in mgmtip_result:
+                mgmtip_map[item["va_name"]] = item
+
+            vlan_map = {}
+            for item in vlan_result:
+                vlan_map[item["va_name"]] = item
+
+            nic_type_map = {}
+            for item in nic_type_result:
+                nic_type_map[item["va_name"]] = item
+
+            status_map = {}
+            for item in va_status_result:
+                status_map[item["va_name"]] = item
+
+            disk_map = {}
+            for item in res:
+                disk_map.setdefault(item["va_name"], []).append(item)
+
+            # License: merge all license sources into one dict (first match wins per VA)
+            license_map = {}
+            if COMPANY_KEYWORD == "Array":
+                for ltype in ['apv', 'ag', 'netgate', 'netiag']:
+                    for item in _pdata.get('lic_' + ltype, []):
+                        if "va_name" in item and item["va_name"] not in license_map:
+                            license_map[item["va_name"]] = item["type"]
+            if COMPANY_KEYWORD == "Infosec":
+                for ltype in ['nsae', 'netopti', 'netgate', 'apv', 'ag', 'netiag']:
+                    for item in _pdata.get('lic_' + ltype, []):
+                        if "va_name" in item and item["va_name"] not in license_map:
+                            license_map[item["va_name"]] = item["type"]
+
+            # Image info: query once, build lookup to avoid N+1 queries
+            sess = get_current_session()
+            image_model = get_model('avx', ['va', 'image', 'VAImageManagement'])
+            image_manager = image_model.get_manager(sess)
+            all_images = image_manager.all()
+            image_map = {}
+            for img in all_images:
+                image_map[img.image_name] = img
+
+            rst = result[0] + result[1]
+            for each in rst:
+                # for normal va instance
+                if "starting_port" in each and each["starting_port"] == 'null':
+                    del each["starting_port"]
+                # for custom va instance
+                if "va_size" not in each:
+                    each["va_size"] = "custom"
+                    each["cpu_thread_shared"] = False
+
+                # init the field
+                name = each["va_name"]
+                each["ip"] = ""
+                each["va_vncport"] = ""
+                each["vlan_tag"] = ""
+                each["nic_type"] = "e1000"
+                each["type"] = ""
+                each["attached_disk"] = []
+
+                # memory & cpu_thread & cpu_thread_shared (O(1) lookup)
+                cpu_info = cpu_map.get(name)
+                if cpu_info:
+                    each["memory"] = cpu_info["memory"]
+                    if "shared" in cpu_info["cpu_thread"]:
+                        cpu_thread = int(cpu_info["cpu_thread"].split("shared")[0])
+                        cpu_thread_shared = True
+                    else:
+                        cpu_thread = int(cpu_info["cpu_thread"])
+                        cpu_thread_shared = False
+                else:
+                    cpu_thread = 0
+                    cpu_thread_shared = False
+                each["cpu_thread"] = cpu_thread
+                each["cpu_thread_shared"] = cpu_thread_shared
+
+                # vnc (O(1) lookup)
+                vnc_info = vnc_map.get(name)
+                if vnc_info:
+                    each["va_vncport"] = vnc_info["vnc_port"]
+
+                # mgmt_ip (O(1) lookup)
+                ip_info = mgmtip_map.get(name)
+                if ip_info:
+                    each["ip"] = ip_info["ip"]
+
+                # mgmtvlan (O(1) lookup)
+                vlan_info = vlan_map.get(name)
+                if vlan_info:
+                    each["vlan_tag"] = vlan_info["tag"]
+
+                # mgmttype (O(1) lookup)
+                nic_info = nic_type_map.get(name)
+                if nic_info:
+                    each["nic_type"] = nic_info["nic_type"]
+
+                # mgmtexdisk (O(1) lookup)
+                for item in disk_map.get(name, []):
+                    each_disk = {
+                        "disk_name": item["disk_name"],
+                        "va_name": item["va_name"],
+                        "disk_size": item["disk_size"],
+                        "bus_type": item["bus_type"]
+                    }
+                    each["attached_disk"].append(each_disk)
+
+                # license type (O(1) lookup)
+                if name in license_map:
+                    each["type"] = license_map[name]
+
+                self._model._meta.mark_delay_query(each)
+
+                # query va image info (O(1) lookup from pre-fetched cache)
+                image = each["tgt_name"]
+                img_instance = image_map.get(image)
+                if img_instance:
+                    product_category = img_instance.product_category
+                    if product_category == "Other":
+                        product_category = img_instance.other_category
+                    product_name = img_instance.product_name
+                    vendor = img_instance.vendor
+                else:
+                    product_category = ''
+                    product_name = ''
+                    vendor = ''
+                each["product_category"] = product_category
+                each["product_name"] = product_name
+                each["vendor"] = vendor
+
+                # status (O(1) lookup)
+                each["status"] = False
+                status_info = status_map.get(name)
+                if status_info:
+                    each["status"] = True if status_info["status"] == "running" else False
+
             return QuerySet(self._model, rst)
 
         def _get_boottime(self, pk_dict):
@@ -208,21 +434,30 @@
 
         def _get_attached_disk(self, pk_dict):
             self.cli.set_enable(force=True)
-            result = self.cli.cmd('show va disk "%s"' % pk_dict["va_name"],
-                                  RegexParser('(?P<va_name>\S+):\n(?P<disk_name>([\S]+\s*?[\S]*?\s*?[\S]*?\n)+)\n', MATCHALL, reflags=re.S))
+            raw_disk_output = self.cli.cmd('show va disk "%s"' % pk_dict["va_name"])
+            sections = raw_disk_output.strip().split('\n\n')
             res = []
-            for each in result:
-                each["_asso_idx"] = 0
-                each["va"] = []
-                disks = each["disk_name"].split("\n")
-                for item in disks:
-                    if item and item.split()[0] != "reserved":
-                        each_disk = {
-                            "disk_name": item.split()[0],
-                            "va": [],
-                            "disk_size": item.split()[-1].split("(")[0]
-                        }
-                        res.append(each_disk)
+            for section in sections:
+                lines = section.strip().split('\n')
+                if not lines:
+                    continue
+                header = lines[0].strip()
+                if not header.endswith(':'):
+                    continue
+
+                for line in lines[1:]:
+                    parts = line.split()
+                    if len(parts) < 3:
+                        continue
+                    if parts[0] in ("Disk", "reserved"):
+                        continue
+                    each_disk = {
+                        "disk_name": parts[0],
+                        "va": [],
+                        "disk_size": parts[1].split("(")[0],
+                        "_asso_idx": 0
+                    }
+                    res.append(each_disk)
             return res
 
         def _update_va_port(self, instance, old_values):
@@ -493,37 +728,39 @@
             return self._filter(pk_dict).get(pk_dict)
 
         def _filter(self, pk_list):
-            rst = []
-            if pk_list:
-                va_name = pk_list['va'][0]['va_name']
-                self.cli.set_enable()
-                result = self.cli.cmd('show va disk',
-                                    RegexParser('(?P<va_name>\S+):\n(?P<disk_name>([\S]+\s*?[\S]*?\s*?[\S]*?\n)+)\n', MATCHALL, reflags=re.S))
-                for each in result:
-                    disks = each["disk_name"].split("\n")
-                    for item in disks:
-                        if item and item.split()[0] != "reserved" and va_name == each["va_name"]:
-                            each_disk = {
-                                "disk_name": item.split()[0],
-                                "va": each["va_name"],
-                                "disk_size": item.split()[-1].split("(")[0]
-                            }
-                            res.append(each_disk)
-            else:
-                self.cli.set_enable()
-                result = self.cli.cmd('show va disk',
-                                    RegexParser('(?P<va_name>\S+):\n(?P<disk_name>([\S]+\s*?[\S]*?\s*?[\S]*?\n)+)\n', MATCHALL, reflags=re.S))
-                for each in result:
-                    disks = each["disk_name"].split("\n")
-                    for item in disks:
-                        if item and item.split()[0] != "reserved":
-                            each_disk = {
-                                "disk_name": item.split()[0],
-                                "va": each["va_name"],
-                                "disk_size": item.split()[-1].split("(")[0]
-                            }
-                            res.append(each_disk)
-            return QuerySet(self._model, rst)
+            res = []
+            filter_va = pk_list['va'][0]['va_name'] if pk_list else None
+
+            self.cli.set_enable()
+            raw_disk_output = self.cli.cmd('show va disk')
+            sections = raw_disk_output.strip().split('\n\n')
+
+            for section in sections:
+                lines = section.strip().split('\n')
+                if not lines:
+                    continue
+                header = lines[0].strip()
+                if not header.endswith(':'):
+                    continue
+                section_va_name = header[:-1]
+
+                if filter_va and section_va_name != filter_va:
+                    continue
+
+                for line in lines[1:]:
+                    parts = line.split()
+                    if len(parts) < 3:
+                        continue
+                    if parts[0] in ("Disk", "reserved"):
+                        continue
+                    each_disk = {
+                        "disk_name": parts[0],
+                        "va": section_va_name,
+                        "disk_size": parts[1].split("(")[0]
+                    }
+                    res.append(each_disk)
+
+            return QuerySet(self._model, res)
 
         def _insert(self, instance):
             self.cli.set_config()
@@ -835,39 +1072,32 @@
             return QuerySet(self._model, res)
 
         def _handle_output(self, result):
-            mapping = {}
-            split = result.split("\n")
-            va_name = ""
-            for ind in range(len(split)):
-                each = split[ind]
-                if not each:
-                    continue
-                if "libvirt:  error : internal error: could not initialize domain event timer" in each:
-                    continue
-                if ":" in each:
-                    va_name = each.split(":")[0]
-                    if va_name not in mapping:
-                        mapping[va_name] = []
-                    continue
-                if "Disk" in each and "Size" in each and "Bus type" in each:
-                    # skip title row
-                    continue
-                row = each.split()
-                if va_name:
-                    mapping[va_name] = row
+            sections = result.strip().split('\n\n')
             res = []
-            for va_name, each in mapping.items():
-                if len(each) < 3:
+            for section in sections:
+                lines = section.strip().split('\n')
+                if not lines:
                     continue
-                if each[0] == "reserved":
+                header = lines[0].strip()
+                if not header.endswith(':'):
                     continue
-                each_disk = {
-                    "disk_name": each[0],
-                    "va_name": va_name,
-                    "disk_size": each[1],
-                    "bus_type": each[2],
-                }
-                res.append(each_disk)
+                va_name = header[:-1]
+                for line in lines[1:]:
+                    if not line.strip():
+                        continue
+                    if "libvirt:  error : internal error: could not initialize domain event timer" in line:
+                        continue
+                    parts = line.split()
+                    if len(parts) < 3:
+                        continue
+                    if parts[0] in ("Disk", "reserved"):
+                        continue
+                    res.append({
+                        "disk_name": parts[0],
+                        "va_name": va_name,
+                        "disk_size": parts[1],
+                        "bus_type": parts[2],
+                    })
             return res
 
 class ShowVASSL(ANModel):
@@ -1359,4 +1589,4 @@
         #     for each in result:
         #         each['cpu_usage'] = float(each['cpu_usage'])
 
-        #     return QuerySet(self._model, result)
+        #     return QuerySet(self._model, result)
\ No newline at end of file
Index: /branches/rel_avx_2_7_5/src/webui/webui/htdocs/new/src/avx/models/va/instance/handler.py
===================================================================
--- /branches/rel_avx_2_7_5/src/webui/webui/htdocs/new/src/avx/models/va/instance/handler.py	(revision 9228)
+++ /branches/rel_avx_2_7_5/src/webui/webui/htdocs/new/src/avx/models/va/instance/handler.py	(working copy)
@@ -1,473 +0,0 @@
-"""
-This file is for counter of API:
-Get list of VA Instance.
-It will use file to save counter setting,
-and check which request is latest by uuid and timestamp.
-keep latest data is returned.
-"""
-from hive.imports.model import *
-from hive.model.loading import get_model
-from hive.utils import get_current_session, aninfo
-from hive.model.legacycli import RegexParser, MATCHALL
-from djproject.an_settings import VA_BOOT_TIME_INFO_PATH
-from datetime import datetime
-from avx.company import *
-import json
-import time
-import os
-import fcntl
-import shutil
-import errno
-
-TEMP_DIR = '/tmp'
-VA_INSTANCE_COUNTER_DIR = '/va_instance_counter'
-VA_INSTANCE_COUNTER_FILE = 'config.json'
-EXPIRY_SECONDS = 600  # 10 min
-QUERY_TYPE_MAP_FILE = 'query_type_map.json'
-
-class SessionCounter():
-    def __init__(self, session_id, query_type=None):
-        self.session_id = session_id
-        self.counter_dir = TEMP_DIR + VA_INSTANCE_COUNTER_DIR
-        self.session_dir = self.counter_dir + "/%s/" % self.session_id
-        self.query_type_file = self.session_dir + QUERY_TYPE_MAP_FILE
-        try:
-            if not os.path.exists(self.counter_dir):
-                os.makedirs(self.counter_dir)
-            if not os.path.exists(self.session_dir):
-                os.makedirs(self.session_dir)
-        except OSError as e:
-            if e.errno != errno.EEXIST:
-                raise
-        exist_type_id = self.get_type_id_by_query_type(query_type)
-        if exist_type_id:
-            type_id = exist_type_id
-        else:
-            type_id = self.get_type_id()
-            if query_type:
-                self.set_type_id(type_id, query_type)
-            else:
-                self.set_type_id(type_id, "")
-        self.result_dir = self.session_dir + "{type_id}/".format(type_id=type_id)
-        self.counter_file = self.result_dir + VA_INSTANCE_COUNTER_FILE
-
-    def get_result_file(self, uuid):
-        return (self.result_dir + 'result_%s.json' % uuid)
-
-    def get_counter_data(self):
-        # structure: { "counter": 0, "uuid": "xxxx", "timestamp": 123456 }
-        if not os.path.exists(self.counter_file):
-            return {"counter": 0, "uuid": "", "timestamp": 0}
-        with open(self.counter_file, 'r') as f:
-            fcntl.flock(f, fcntl.LOCK_SH)
-            data = json.load(f)
-            fcntl.flock(f, fcntl.LOCK_UN)
-        return data
-
-    def set_counter_data(self, counter, uuid, timestamp):
-        parent_dir = os.path.dirname(self.counter_file)
-        if not os.path.exists(parent_dir):
-            os.makedirs(parent_dir)
-        with open(self.counter_file, 'w') as f:
-            fcntl.flock(f, fcntl.LOCK_EX)
-            json.dump({"counter": counter, "uuid": uuid, "timestamp": timestamp}, f)
-            fcntl.flock(f, fcntl.LOCK_UN)
-
-    def write_result_file(self, uuid, result):
-        final_path = self.get_result_file(uuid)
-        with tempfile.NamedTemporaryFile(mode='w', dir=self.result_dir, delete=False) as tmp_file:
-            json.dump(result, tmp_file)
-            tmp_file_path = tmp_file.name
-        os.rename(tmp_file_path, final_path)
-
-    def read_result_file(self, uuid):
-        path = self.get_result_file(uuid)
-        if not os.path.exists(path):
-            return []
-        with open(path, 'r') as f:
-            return json.load(f)
-
-    def wait_for_result(self, uuid, timeout=10):
-        path = self.get_result_file(uuid)
-        waited = 0
-        while not os.path.exists(path) and waited < timeout:
-            time.sleep(0.5)
-            waited += 0.5
-
-    def cleanup_old_results(self, current_uuid):
-        now = time.time()
-        for fname in os.listdir(self.counter_dir):
-            dir_path = os.path.join(self.counter_dir, fname)
-            if os.path.isdir(dir_path) and fname != self.session_id:
-                dir_mtime = os.path.getmtime(dir_path)
-                if now - dir_mtime > EXPIRY_SECONDS:
-                    try:
-                        shutil.rmtree(dir_path)
-                    except:
-                        aninfo('hive.debug', 'There is error occurred when cleanup old results directory for VA Instance list')
-                continue
-            if not fname.startswith('result_') or not fname.endswith('.json'):
-                continue
-            uuid = fname[7:-5]
-            if uuid == current_uuid:
-                continue
-            full_path = os.path.join(self.result_dir, fname)
-            try:
-                mtime = os.path.getmtime(full_path)
-                if now - mtime > EXPIRY_SECONDS:
-                    os.remove(full_path)
-            except:
-                aninfo('hive.debug', 'There is error occurred when cleanup old results file for VA Instance list')
-
-    def get_type_id(self):
-        """
-        Get the new type id for this session counter.
-        This is used to identify the type of query being made.
-        file content ex: [{"type": 1, "query_type": ""}]
-        :return last type id + 1
-        1 is the first type id.
-        """
-        type_id = 1
-        content = []
-        if os.path.exists(self.query_type_file):
-            with open(self.query_type_file, 'r') as f:
-                fcntl.flock(f, fcntl.LOCK_SH)
-                content = json.load(f)
-                fcntl.flock(f, fcntl.LOCK_UN)
-        if content:
-            last_type = max(item["type"] for item in content)
-            type_id = last_type + 1
-        return type_id
-
-    def set_type_id(self, type_id, query_type):
-        """
-        Set the type id and query type to the file.
-        :param type_id: The new type id.
-        :param query_type: The query type.
-        """
-        if not os.path.exists(self.query_type_file):
-            with open(self.query_type_file, 'w') as f:
-                json.dump([], f)
-        parent_dir = os.path.dirname(self.query_type_file)
-        if not os.path.exists(parent_dir):
-            os.makedirs(parent_dir)
-        try:
-            with open(self.query_type_file, 'r+') as f:
-                fcntl.flock(f, fcntl.LOCK_EX)
-                content = json.load(f)
-                content.append({"type": type_id, "query_type": query_type})
-                f.seek(0)
-                json.dump(content, f)
-                f.truncate()
-                fcntl.flock(f, fcntl.LOCK_UN)
-        except Exception as e:
-            import traceback
-            aninfo("hive.debug", "set_type_id error: %s\n%s" % (e, traceback.format_exc()))
-
-    def get_type_id_by_query_type(self, query_type):
-        """
-        Get the type id by query type.
-        :param query_type: The query type.
-        :return: The type id if found, otherwise None.
-        """
-        if not os.path.exists(self.query_type_file):
-            return None
-        with open(self.query_type_file, 'r') as f:
-            fcntl.flock(f, fcntl.LOCK_EX)
-            content = json.load(f)
-            norm_query_type = self.normalize_query_type(query_type)
-            for item in content:
-                item_norm = self.normalize_query_type(item["query_type"])
-                if item_norm == norm_query_type:
-                    fcntl.flock(f, fcntl.LOCK_UN)
-                    return item["type"]
-            fcntl.flock(f, fcntl.LOCK_UN)
-        return None
-
-    def normalize_query_type(self, query_type):
-        """
-        Normalize the query type to a standard format.
-        :param query_type: The query type to normalize.
-        :return: The normalized query type.
-        """
-        if not query_type:
-            return ""
-        qt = dict(query_type)
-        if "key" in qt:
-            if isinstance(qt["key"], str):
-                qt["key"] = json.loads(qt["key"])
-            elif isinstance(qt["key"], unicode):
-                qt["key"] = json.loads(qt["key"])
-        if "key" in qt and isinstance(qt["key"], list):
-            qt["key"] = sorted(qt["key"])
-        return json.dumps(qt, sort_keys=True)
-
-class VAInstanceHandle():
-    def __init__(self, cli):
-        self.sess = get_current_session()
-        self.cli = cli
-
-    def get_id(self):
-        return self.sess._sessid
-
-    def get_instance(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va instance',
-                                EasyParser('va instance', ['?va_name', '?va_size', '?starting_port', '?domain_id', '?tgt_name']),
-                                EasyParser('va custinstance', ['?va_name', '?cpu_thread', '?memory', '?domain_id', '?tgt_name']))
-
-    def get_resource(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va resource', RegexParser('\s+(?P<va_name>[^\n]+?)\s+(?P<cpu_thread>[0-9]+(\sshared)?)\s+(?P<memory>[0-9]+)G\s+[0-9]+\s+dedicated VF\s+', MATCHALL, reflags=re.S))
-
-    def get_status(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va status', RegexParser('(?P<va_name>\S+)\s+(?P<status>running|shutoff)\n', MATCHALL, reflags=re.S))
-
-    def get_vncport(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show vncport', RegexParser('(?P<va_name>\S+):\s*(?P<vnc_port>[0-9]+)\n', MATCHALL, reflags=re.S))
-
-    def get_mgmtip(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va mgmtip', RegexParser('va mgmtip (?P<va_name>\S+) (?P<ip>\S+)\n', MATCHALL, reflags=re.S))
-
-    def get_vlan(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va mgmtvlan' , RegexParser('va mgmtvlan (?P<va_name>\S+) (?P<tag>\S+)\n', MATCHALL, reflags=re.S))
-
-    def get_nic_type(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va mgmttype', RegexParser('(?P<va_name>\S+)\s+(?P<nic_type>\S+)\n', MATCHALL, reflags=re.S))
-
-    def get_disk(self):
-        self.cli.set_enable()
-        result = self.cli.cmd('show va disk',
-                            RegexParser('(?P<va_name>\S+):\n(?P<disk_name>([\S]+\s*?[\S]*?\s*?[\S]*?\s*?[\S]*?\n)+)\n', MATCHALL, reflags=re.S))
-        res = []
-        for each in result:
-            disks = each["disk_name"].split("\n")
-            del disks[0]
-            for item in disks:
-                if item and item.split()[0] != "reserved":
-                    each_disk = {
-                        "disk_name": item.split()[0],
-                        "va_name": each["va_name"],
-                        "disk_size": item.split()[-2].split("(")[0],
-                        "bus_type": item.split()[-1]
-                    }
-                    res.append(each_disk)
-        return res
-
-    def get_apv_license(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va license apv', EasyParser('', ['?va_name', '?type']))
-
-    def get_ag_license(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va license ag', EasyParser('', ['?va_name', '?type']))
-
-    def get_netgate_license(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va license netgate', EasyParser('', ['?va_name', '?type']))
-
-    def get_netiag_license(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va license netiag', EasyParser('', ['?va_name', '?type']))
-
-    def get_nsae_license(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va license nsae', EasyParser('', ['?va_name', '?type']))
-
-    def get_netopti_license(self):
-        self.cli.set_enable()
-        return self.cli.cmd('show va license netopti', EasyParser('', ['?va_name', '?type']))
-
-    def get_boottime(self, va_name):
-        boottime = 'N/A'
-        boottime_file = VA_BOOT_TIME_INFO_PATH + va_name +'/boottime'
-        if os.path.exists(boottime_file):
-            try:
-                destination = open(boottime_file)
-                date_str = destination.read()
-                destination.close()
-            except Exception as e:
-                if destination:
-                    destination.close()
-            #here has a  %Z  bug of python 2.7.5 datetime
-            l = date_str.strip().split()
-            l = l[:4] + [l[-1]]
-            date = datetime.strptime(' '.join(l), '%a %b %d %H:%M:%S %Y')
-            boottime =  date.isoformat()
-        return boottime
-
-    def handle_parameter(self, done):
-        rst = done['get_instance'][0] + done['get_instance'][1]
-        image_model = get_model('avx', ['va', 'image', 'VAImageManagement'])
-        image_manager = image_model.get_manager(self.sess)
-        for each in rst:
-            # for normal va instance
-            if "starting_port" in each and each["starting_port"] == 'null':
-                del each["starting_port"]
-            # for custom va instance
-            if "va_size" not in each:
-                each["va_size"] = "custom"
-                each["cpu_thread_shared"] = False
-
-            # init the field
-            name = each["va_name"]
-            each["ip"] = ""
-            each["va_vncport"] = ""
-            each["vlan_tag"] = ""
-            each["nic_type"] = "e1000"
-            each["type"] = ""
-            each["attached_disk"] = []
-
-            # memory & cpu_thread & cpu_thread_shared
-            for item in done['get_resource']:
-                if item["va_name"] == name:
-                    each["memory"] = item["memory"]
-                    if "shared" in item["cpu_thread"]:
-                        cpu_thread = int(item["cpu_thread"].split("shared")[0])
-                        cpu_thread_shared = True
-                    else:
-                        cpu_thread = int(item["cpu_thread"])
-                        cpu_thread_shared = False
-                    break
-            each["cpu_thread"] = cpu_thread
-            each["cpu_thread_shared"] = cpu_thread_shared
-            # vnc
-            for item in done['get_vncport']:
-                if item["va_name"] == name:
-                    each["va_vncport"] = item["vnc_port"]
-                    break
-            # mgmt_ip
-            for item in done['get_mgmtip']:
-                if item["va_name"] == name:
-                    each["ip"] = item["ip"]
-                    break
-            # mgmtvlan
-            for item in done['get_vlan']:
-                if item["va_name"] == name:
-                    each["vlan_tag"] = item["tag"]
-                    break
-            # mgmttype
-            for item in done['get_nic_type']:
-                if item["va_name"] == name:
-                    each["nic_type"] = item["nic_type"]
-                    break
-            # mgmtexdisk
-
-            for item in done['get_disk']:
-                if item["va_name"] == name:
-                    each_disk = {
-                        "disk_name": item["disk_name"],
-                        "va_name": item["va_name"],
-                        "disk_size": item["disk_size"],
-                        "bus_type": item["bus_type"]
-                    }
-                    each["attached_disk"].append(each_disk)
-
-            if COMPANY_KEYWORD == "Array":
-                # ag/apv license type
-                for item in done['get_apv_license']:
-                    if "va_name" in item and item["va_name"] == name:
-                        each["type"] = item["type"]
-                        break
-                for item in done['get_ag_license']:
-                    if "va_name" in item and item["va_name"] == name:
-                        each["type"] = item["type"]
-                        break
-                for item in done['get_netgate_license']:
-                    if "va_name" in item and item["va_name"] == name:
-                        each["type"] = item["type"]
-                        break
-                for item in done['get_netiag_license']:
-                    if "va_name" in item and item["va_name"] == name:
-                        each["type"] = item["type"]
-                        break
-            if COMPANY_KEYWORD == "Infosec":
-                    # nsae/netopti/netgate license type
-                    for item in done['get_nsae_license']:
-                        if "va_name" in item and item["va_name"] == name:
-                            each["type"] = item["type"]
-                            break
-                    for item in done['get_netopti_license']:
-                        if "va_name" in item and item["va_name"] == name:
-                            each["type"] = item["type"]
-                            break
-                    for item in done['get_netgate_license']:
-                        if "va_name" in item and item["va_name"] == name:
-                            each["type"] = item["type"]
-                            break
-                    for item in done['get_apv_license']:
-                        if "va_name" in item and item["va_name"] == name:
-                            each["type"] = item["type"]
-                            break
-                    for item in done['get_ag_license']:
-                        if "va_name" in item and item["va_name"] == name:
-                            each["type"] = item["type"]
-                            break
-                    for item in done['get_netiag_license']:
-                        if "va_name" in item and item["va_name"] == name:
-                            each["type"] = item["type"]
-                            break
-
-            # query va image info for each va instance
-            image = each["tgt_name"]
-            image_instance = image_manager.all().filter({"image_name": image})
-            product_category = image_instance[0].product_category if image_instance else ''
-            if product_category == "Other":
-                product_category = image_instance[0].other_category
-            product_name = image_instance[0].product_name if image_instance else ''
-            vendor = image_instance[0].vendor if image_instance else ''
-            each["product_category"] = product_category
-            each["product_name"] = product_name
-            each["vendor"] = vendor
-            each["status"] = False
-            each['boottime'] = self.get_boottime(each["va_name"])
-            for item in done['get_status']:
-                if item["va_name"] == each["va_name"]:
-                    each["status"] = True if item["status"] == "running" else False
-                    break
-        return rst
-
-    def get_data_from_cli(self, uuid, counter):
-        function_map = {
-            'get_instance': (self.get_instance, []),
-            'get_resource': (self.get_resource, []),
-            'get_status': (self.get_status, []),
-            'get_vncport': (self.get_vncport, []),
-            'get_mgmtip': (self.get_mgmtip, []),
-            'get_vlan': (self.get_vlan, []),
-            'get_nic_type': (self.get_nic_type, []),
-            'get_disk': (self.get_disk, []),
-        }
-        if COMPANY_KEYWORD == "Array":
-            function_map['get_apv_license'] = (self.get_apv_license, [])
-            function_map['get_ag_license'] = (self.get_ag_license, [])
-            function_map['get_netgate_license'] = (self.get_netgate_license, [])
-            function_map['get_netiag_license'] = (self.get_netiag_license, [])
-        if COMPANY_KEYWORD == "Infosec":
-            function_map['get_nsae_license'] = (self.get_nsae_license, [])
-            function_map['get_netgate_license'] = (self.get_netgate_license, [])
-            function_map['get_netopti_license'] = (self.get_netopti_license, [])
-            function_map['get_apv_license'] = (self.get_apv_license, [])
-            function_map['get_ag_license'] = (self.get_ag_license, [])
-            function_map['get_netiag_license'] = (self.get_netiag_license, [])
-        done = {}
-        for key in function_map.keys():
-            current = counter.get_counter_data()
-            if current['uuid'] != uuid:
-                break
-            try:
-                func, args = function_map[key]  # call real function
-                result = func(*args)
-                done[key] = result
-            except Exception as e:
-                done[key] = "ERROR: %s" % str(e)
-        current = counter.get_counter_data()
-        if current['uuid'] != uuid:
-            return []
-        result = self.handle_parameter(done)
-        counter.write_result_file(uuid, result)
