Esempio n. 1
0
def get_cpe(sn):
    from cpe import CPE

    cpe = CPE.get_cpe(sn)
    if (cpe is None):
        cpe = CPE.add(sn, None)

        desc = "cpe sn(%s) not online, user create." % (sn)
        log.app_info(desc)

    return cpe
Esempio n. 2
0
def on_rx_worklist_exec_start(msg, obj):
    """
    logic & physic worklist share
    1 query exist?
    2 status right?
    3 exist, update exec time    
    4 timer, wait exec finish
    """
    from cpe import CPE

    ret = ERR_FAIL  # default
    cpe = None
    msg_rsp = msg + 2  # default fail
    obj_database = obj

    for nwf in [1]:
        # check args
        if (not isinstance(obj, MsgWorklistExecStart)):
            desc = "obj is not MsgWorklistExecStart"
            log.app_err(desc)

            ret = ERR_FATAL
            break

        # 1 query exist?
        id_ = obj.id_
        #worklist_id = obj.id_
        #worklist_id = CpeUserWorklist.m_dict_desc_id[id_]
        obj_database = restore_acs_worklist(id_)
        if (obj_database is None):
            desc = "id(%s) is not exist." % id_
            log.app_err(desc)

            obj_database = obj
            obj_database.dict_ret["str_result"] = desc
            break
        log.app_info("id=%s, worklist_name=%s, sn=%s" %
                     (id_, obj_database.worklist_name, obj_database.sn))
        worklist_id = obj_database.worklist_id

        # cancel pre status timer (msg is valid for worklist id can kill timer
        # , otherwise skip)
        try:
            timer = get_worklist_timer(obj_database.id_)
            if (timer):
                pop_worklist_timer(obj_database.id_)

                timer.cancel()

        except Exception, e:
            pass

        # this msg try only 1 time, if fail, then worklist fail

        # 2 status right?
        status_expect = [WORK_LIST_STATUS_RESERVE]
        if (obj_database.status not in status_expect):
            desc = "worklist status is %s, not in (%s)" % (obj_database.status,
                                                           status_expect)
            log.app_err(desc)

            obj_database.dict_ret["str_result"] = desc
            set_worklist_status(obj_database, WORK_LIST_STATUS_FAIL, desc)
            break

        # be careful, cpe(sn)'s domain delay to here(exec start)
        sn = obj_database.sn
        cpe = CPE.get_cpe(sn)
        if (cpe is None):
            desc = "cpe(sn=%s) is not online" % sn
            log.app_err(desc)

            obj_database.dict_ret["str_result"] = desc
            set_worklist_status(obj_database, WORK_LIST_STATUS_FAIL, desc)
            break

        # 3 can update ?
        domain = cpe.cpe_property.get_cpe_domain()
        if (not domain):
            desc = "cpe(sn=%s) domain(type) is Not config" % sn
            log.app_err(desc)

            obj_database.dict_ret["str_result"] = desc
            set_worklist_status(obj_database, WORK_LIST_STATUS_FAIL, desc)
            break

        operator = cpe.cpe_property.get_cpe_operator()
        if (not operator):
            # delay to worklist server's judge, fail too
            operator = "standard"

            desc = "cpe(sn=%s) operator(type) is Not config, use default" % sn
            log.app_info(desc)

        version = cpe.cpe_property.get_cpe_interface_version()

        # 用户执行的工单需要对执行的参数和模板做匹配,系统工单暂时没有处理
        if obj_database.group and obj_database.group.lower() == "user":
            cpe_interface_version = cpe.cpe_property.get_cpe_interface_version(
            )

            # 获取工单参数模板
            worklist_template_data = get_worklist_template(
                operator, cpe_interface_version, domain,
                obj_database.worklist_name)
            if worklist_template_data is None:  # 可能工单参数为空情况 by zsj 2014-6-17
                desc = u"用户新增的%s %s %s %s工单,服务器不支持!" % (
                    operator, cpe_interface_version, domain,
                    obj_database.worklist_name)
                log.app_err(desc)

                obj_database.dict_ret["str_result"] = desc
                set_worklist_status(obj_database, WORK_LIST_STATUS_FAIL, desc)
                break

            # 用户参数和模板做匹配,重置工单参数
            obj_database.dict_data = _set_worklist_args(
                worklist_template_data, obj_database.dict_data)

            # 更新数工单参数到据库
            str_data = str(obj_database.dict_data)
            update_acs_worklist_ex_by_id(worklist_id, 'PARAMETERS', str_data)

        # 3 update
        obj_database.operator = operator
        obj_database.cpe_interface_version = version
        obj_database.domain = domain
        obj_database.status = WORK_LIST_STATUS_RUNNING
        obj_database.time_exec_start = datetime.now().strftime(
            '%Y-%m-%d %H:%M:%S')
        obj_database.rollback = cpe.cpe_property.get_cpe_worklist_rollback(
        )  # default = False

        # 4 wait exec finish
        timer = reactor.callLater(
            webservercfg.ACS_WAIT_WORKLIST_EXEC_FINISH_TIMEOUT,
            on_worklist_timeout, EV_WORKLIST_EXEC_FINISH_RQST, obj_database)
        save_worklist_timer(obj_database.id_, timer)

        # mysql
        """
        update_acs_worklist(obj_database, "OPERATOR", obj_database.operator)
        update_acs_worklist(obj_database, "CPE_DEVICE_TYPE", obj_database.domain)
        update_acs_worklist(obj_database, "STATUS", obj_database.status)
        update_acs_worklist(obj_database, "TIME_EXEC_START", obj_database.time_exec_start)
        update_acs_worklist(obj_database, "ROLLBACK", obj_database.rollback)
        update_acs_worklist(obj_database, "SN", obj_database.sn)
        """

        dict_col = {}
        dict_data = {}

        dict_col['STATUS'] = obj_database.status
        dict_col['OPERATOR'] = obj_database.operator
        dict_col['OPERATOR_VERSION'] = obj_database.cpe_interface_version
        dict_col['DOMAIN'] = obj_database.domain
        dict_col['TIME_EXEC_START'] = obj_database.time_exec_start
        dict_col['ROLLBACK'] = str(obj_database.rollback)

        if dict_col:
            # 字典的值不为空时,才更新数据库
            dict_data['columns'] = dict_col
            dict_data['condition'] = 'WORKLIST_ID=%s' % worklist_id
            operate_db('WORKLIST', 'UPDATE', dict_data)

        ret = ERR_SUCCESS
        msg_rsp = msg + 1  # response
Esempio n. 3
0
def on_rx_worklist_bind_physic(msg, obj):
    """
    obj = MsgWorklistBindPhysical
    1 query exist?
    2 exist, status right? 
    3 update sn + type
    """

    ret = ERR_FAIL  # default
    msg_rsp = msg + 2  # default fail
    obj_database = obj

    for nwf in [1]:
        # check args
        if (not isinstance(obj, MsgWorklistBindPhysical)):
            log.app_err("obj is not MsgWorklistBindPhysical")
            ret = ERR_FATAL
            break

        # 1 query exist?
        id_ = obj.id_
        #worklist_id = obj.id_
        #worklist_id = CpeUserWorklist.m_dict_desc_id[id_]
        """
        dict_col = dict(WORKLIST_DESC='', WORKLIST_NAME='', STATUS='')
        dict_data = {}
        
        dict_data['columns'] = dict_col
        dict_data['condition'] = 'WORKLIST_ID=%s' % worklist_id
        flag = operate_db('WORKLIST', 'SELECT', dict_data)
        if not flag:
        """
        obj_database = restore_acs_part_worklist(id_)
        if (obj_database is None):
            desc = "id(%s) is not exist." % (id_)
            log.app_err(desc)

            obj_database = obj
            obj_database.dict_ret["str_result"] = desc
            break
        """
        obj_database.worklist_name = dict_col['WORKLIST_NAME']
        obj_database.status = dict_col['STATUS']
        """
        log.app_info("id=%s, worklist_name=%s" %
                     (id_, obj_database.worklist_name))
        worklist_id = obj_database.worklist_id

        # 2 status right?
        status_expect = [WORK_LIST_STATUS_BUILD, WORK_LIST_STATUS_BIND]
        if (obj_database.status not in status_expect):
            desc = "worklist status is %s, not in (%s)" % (obj_database.status,
                                                           status_expect)
            log.app_err(desc)

            obj_database.dict_ret["str_result"] = desc
            break

        # 3 update
        obj_database.status = WORK_LIST_STATUS_BIND
        obj_database.sn = obj.sn  # be careful ,cpe(sn)'s domain delay to exec start
        obj_database.type_ = WORK_LIST_TYPE_PHISIC
        obj_database.time_bind = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        # mysql
        """
        update_acs_worklist(obj_database, "STATUS", obj_database.status)
        update_acs_worklist(obj_database, "SN", obj_database.sn)
        update_acs_worklist(obj_database, "WORKLIST_TYPE", obj_database.type_)
        update_acs_worklist(obj_database, "TIME_BIND", obj_database.time_bind)
        """

        from cpe import CPE
        cpe = CPE.get_cpe(obj_database.sn)

        dict_col = {}
        dict_data = {}

        dict_col['STATUS'] = obj_database.status
        dict_col['CPE_ID'] = cpe.get_cpe_id()
        dict_col['SN'] = obj_database.sn
        dict_col['WORKLIST_TYPE'] = obj_database.type_
        dict_col['TIME_BIND'] = obj_database.time_bind

        if dict_col:
            # 字典的值不为空时,才更新数据库
            dict_data['columns'] = dict_col
            dict_data['condition'] = 'WORKLIST_ID=%s' % worklist_id
            operate_db('WORKLIST', 'UPDATE', dict_data)

        ret = ERR_SUCCESS
        msg_rsp = msg + 1

    return ret, msg_rsp, obj_database
Esempio n. 4
0
def get_url(sn, url, username, password):
    """
    """
    from cpe import CPE

    ret = AUTHENTICATE_FAIL
    ret_api = None
    err_messsage = ""
    soap_id = 0

    desc = "begin get url(url=%s, username=%s, password=%s)." % (url, username,
                                                                 password)
    log.app_info(desc)
    if (not sn):
        err_messsage = "The sn is not exist"
        log.app_info(err_messsage)

        return AUTHENTICATE_FAIL, err_messsage

    if (not url):
        err_messsage = "The url is not exist(need inform?)"
        log.app_info(err_messsage)

        return AUTHENTICATE_FAIL, err_messsage

    # nwf 2013-06-09; retry 3times if error(10060 or 10065)
    for i in [1, 2]:

        try:
            #conn = httplib2.Http(timeout = 60)
            conn = MyHttp(sn, timeout=60)  # Alter by lizn 2014-05-30
            conn.add_credentials(username, password)

            # mysql ; first
            cpe = CPE.get_cpe(sn)

            time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            cpe.cpe_soap.time_s1_start = time
            # Alter by lizn 2014-03-11

            # mysql ------------- out
            content = "username=%s; password=%s; url=%s" % (username, password,
                                                            url)
            #insert_acs_soap("connection request", "OUT", sn, content)   by lizn 2014-05-30

            ret_api, data = conn.request(url)

            # mysql -------------- in
            content = str(ret_api) + "\n\n" + data
            #soap_id = insert_acs_soap("connection request", "IN", sn, content)  by lizn 2014-05-30

            # mysql ; first end
            time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            cpe.cpe_soap.time_s1_finish = time
            # Alter by lizn 2014-03-11

            status = ret_api.status
            conn.close()
            if status == AUTHENTICTATE_SUCCEED:
                err_messsage = "Get cpe url(url=%s, username=%s, password=%s) success." % (
                    url, username, password)
                log.app_info(err_messsage)

                ret = AUTHENTICTATE_SUCCEED
                break
            else:
                err_messsage = "Get cpe url(url=%s, username=%s, password=%s) not pass." % (
                    url, username, password)
                log.app_info(err_messsage)

                ret = AUTHENTICATE_FAIL
                break

        except Exception, e:
            err_messsage = "Get cpe url(url=%s, username=%s, password=%s) fail:%s." % (
                url, username, password, e)
            log.app_err(err_messsage)

            # friendly tip
            err_messsage = "Get cpe url(url=%s, username=%s, password=%s) fail:connect to cpe fail." % (
                url, username, password)

            try:
                # retry
                if ((e.errno == 10060) or (e.errno == 10065)):
                    continue
            except Exception:
                pass

            # other error, fail
            ret = AUTHENTICATE_FAIL
            break