from ..bf_gbl_env.var_env import *


def _Exception():
    exc_type, exc_obj, tb = sys.exc_info()
    f = tb.tb_frame
    lineno = tb.tb_lineno
    filename = f.f_code.co_filename
    linecache.checkcache(filename)
    line = linecache.getline(filename, lineno, f.f_globals)
    return 'Exception in ({},\n line {} "{}"): {}'.format(
        filename, lineno, line.strip(), exc_obj
    )


class BfNshCounter(Thread):
    def __init__(
        self,
        threadID,
        name,
        bfgc,
        sck_file,
        pipe_name,
        nsh_counter_interval=30,
    ):
        self.class_name = type(self).__name__
        Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.bfgc = bfgc
        self.file = sck_file
        self.die = False
        self.nsh_counter_interval = nsh_counter_interval
        self.pipe_name = pipe_name

    def run(self):
        try:
            logger.debug("%s - main" % (self.class_name))
            while not self.die:
                logger.debug("%s - Sending nsh counters ..." % self.name)
                logger.debug("%s - %s thread loop" % (self.class_name, self.name))
                self.getReadSwitchNshCounter()
                sleep(self.nsh_counter_interval)

        except Exception as e:
            e = sys.exc_info()[0]
            logger.warning("%s - %s" % (self.class_name, _Exception()))
            self.tearDown()

    def getReadSwitchNshCounter(self):
        tbl_global_path = "%s.ig_ctl" % self.pipe_name
        tbl_nsh_name = "%s.ig_ctl_nsh.tbl_nsh" % (tbl_global_path)

        try:
            key_fields = {}
            data_fields = {}
            data = ""
            tbl_nsh = self.bfgc.bfrt_info.table_get(tbl_nsh_name)
            # tbl_nsh.operations_execute(
            #    self.bfgc.target, "Sync", p4_name=self.bfgc.p4_name
            # )
            # logger.debug("%s - %s counters synced !" % (self.class_name, tbl_nsh_name))
            resp = tbl_nsh.entry_get(
                self.bfgc.target, [], {"from_hw": True}, p4_name=self.bfgc.p4_name
            )

            for d, k in resp:
                key_fields = k.to_dict()
                data_fields = d.to_dict()

                if not (key_fields == {}):
                    nsh_sp = key_fields["hdr.nsh.sp"]["value"]
                    nsh_si = key_fields["hdr.nsh.si"]["value"]
                    nsh_pkt_cnt = data_fields["$COUNTER_SPEC_PKTS"]
                    nsh_byte_cnt = data_fields["$COUNTER_SPEC_BYTES"]

                    data = "nsh_cnt %s %s %s %s \n" % (
                        nsh_sp,
                        nsh_si,
                        nsh_pkt_cnt,
                        nsh_byte_cnt,
                    )

                    logger.debug("tx: %s" % data.split(" "))

                    self.file.write(data)
                    self.file.flush()
            else:
                logger.debug("%s - no nsh entry" % self.class_name)

        except:
            logger.warning(_Exception())
            logger.warning("%s - %s request problem" % (self.class_name, tbl_nsh_name))

    def tearDown(self):
        os._exit(0)
        self.die = True
