17

文件名:foreground_interface.py
包含类:无
功能:提供四川电力N-1推演设备同步功能的外部接口函数,实现同步的逻辑总体框架
"""
import os;
from user_param_manager import *;
from dev_label_manager import *;
from dev_sync_manager import *;
from conf_manager_huawei import *;
from remote_manager import *;
from if_status_manager import *;
from hcl_manager import *;
from data_manager import *;
from RouteTrafficInterface import *;
from deduction.CommonConfig import *;
from dev_cfg_manager import *;
from rest_tools import *;


import socket
import shutil;
from imc_oper_manager import *;


#字符集设置为utf-8
default_encoding = 'utf-8';
if sys.getdefaultencoding() != default_encoding:
    stdout = sys.stdout;
    stdin = sys.stdin;
    reload(sys);
    sys.stdout = stdout;
    sys.stdin = stdin;
    sys.setdefaultencoding(default_encoding);


rest_tool = rest_tools();


def IMCOper(p_hcl_view_id,bTest = False):
    if (bTest):
        return oErrorBase.base_succeed;
    iResult = oErrorBase.base_succeed;
    list_ip = [];
    dict_id_ip = {};
    iResult,list_ip = imc_oper_manager.GetInstance().GetSimuDevIPList(param_tool.GetInstance().param_db_real.m_str_db_topo_name);
    if (oErrorBase.base_succeed != iResult):
        log_manager.GetInstance().CoutLog('fail to GetSimuDevIPList',Com_foreground_interface);
        return iResult;
    rest_tool.SetRestInfo(param_tool.GetInstance().m_str_simu_imc_server_user,param_tool.GetInstance().m_str_simu_imc_server_pw,param_tool.GetInstance().m_str_simu_imc_server_ip + ':8080');
    imc_oper_manager.GetInstance().SetRestParam(param_tool.GetInstance().m_str_simu_imc_server_ip,param_tool.GetInstance().m_str_simu_imc_server_user,param_tool.GetInstance().m_str_simu_imc_server_pw)
    iResult = imc_oper_manager.GetInstance().AddDevsToiMC(list_ip,ReadParamFromXml('AddDevsToiMC'));
    if (oErrorBase.base_succeed != iResult):
        log_manager.GetInstance().CoutLog('fail to AddDevsToiMC',Com_foreground_interface);
        return iResult;


    iResult,dict_id_ip = imc_oper_manager.GetInstance().GetSimuIDIPDict(param_tool.GetInstance().param_db_real.m_str_db_topo_name);
    if (oErrorBase.base_succeed != iResult):
        log_manager.GetInstance().CoutLog('fail to GetSimuIDIPDict',Com_foreground_interface);
        return iResult;


    #iResult = imc_oper_manager.GetInstance().CreateViewWithDevs(param_tool.GetInstance().m_str_real_topo_name.decode('GBK'),dict_id_ip);
    iResult = imc_oper_manager.GetInstance().CreateViewWithDevs(p_hcl_view_id,param_tool.GetInstance().param_db_real.m_str_db_topo_name.decode('GBK'),dict_id_ip,ReadParamFromXml('CreateViewWithDevs'));
    if (oErrorBase.base_succeed != iResult):
        log_manager.GetInstance().CoutLog('fail to CreateViewWithDevs',Com_foreground_interface);
        return iResult;


    print 'auto add dev ok';
    return oErrorBase.base_succeed;




#获取真实设备的配置文件
def GetRealDevConfByTopo(p_str_topo_name,bTest = False):
    if (bTest):
        return oErrorBase.base_succeed;
    rest_tool.SetRestInfo(param_tool.GetInstance().m_str_real_imc_server_user,param_tool.GetInstance().m_str_real_imc_server_pw,param_tool.GetInstance().m_str_real_imc_server_ip + ':8080');
    result = oErrorBase.base_succeed;
    result = dev_cfg_manager.GetInstance().GetRealDevCfgByTopoName(param_tool.GetInstance().param_db_real.m_str_db_topo_name);
    return  result;


 #配置转换
def DevConfSwitch(bTest = False):
    if (bTest):
        return oErrorBase.base_succeed;
    link_manager.GetInstance().SetDBInfo(param_tool.GetInstance().param_db_real.m_str_db_ip,param_tool.GetInstance().param_db_real.m_str_db_user,param_tool.GetInstance().param_db_real.m_str_db_pw);#设置数据库连接信息
    list_src_file = os.listdir(dir.src_config_file_dir);#从该读取目录读取设备原始配置文件,修改后的配置文件放在des_config_file目录下
    for elem in list_src_file:
        match = re.search("(\d+\.\d+\.\d+\.\d+).cfg", elem);
        if match:
            ipaddr = match.group(1);
        else:
            log_manager.GetInstance().CoutLog('Error name of config file',Com_foreground_interface);
            continue;
        my_conf_manager = None;
        real_db_tool = db_tools();
        real_db_tool.SetDBInfo(param_tool.GetInstance().param_db_real.m_str_db_ip,param_tool.GetInstance().param_db_real.m_str_db_user,param_tool.GetInstance().param_db_real.m_str_db_pw);
        real_db_tool.OpenDBCon('config_db');
        dev_vendor = real_db_tool.getDevVendor(ipaddr);
        if (dev_vendor == 'Huawei'):
            my_conf_manager = conf_manager_huawei(elem,param_tool.GetInstance().param_db_real.m_str_db_topo_name);
        else:
            my_conf_manager = conf_manager(elem,param_tool.GetInstance().param_db_real.m_str_db_topo_name);
        if (None != my_conf_manager):
            my_conf_manager.execTrans(elem,param_tool.GetInstance().m_str_real_topo_name);
            my_conf_manager.CreateNewConfFile(param_tool.GetInstance().m_str_real_topo_name,elem);
        del my_conf_manager;
    return oErrorBase.base_succeed;


#hcl文件转换
def HclProjectSwitch(bTest = False):
    if (bTest):
        return oErrorBase.base_succeed;
    iResult = oErrorBase.base_succeed;
    log_manager.GetInstance().CoutLog('begin to run run_hcl_conf_modify.py',Com_foreground_interface);
    link_manager.GetInstance().LoadMIPInfo(param_tool.GetInstance().m_str_real_topo_name.decode('UTF-8').encode('iso8859-1') + 'm_port_info.cfg');
    link_manager.GetInstance().SetDBInfo(param_tool.GetInstance().param_db_real.m_str_db_ip,param_tool.GetInstance().param_db_real.m_str_db_user,param_tool.GetInstance().param_db_real.m_str_db_pw);#设置数据库连接信息
    link_manager.GetInstance().GetTopoDevIPFromDB(param_tool.GetInstance().param_db_real.m_str_db_topo_name);#设置拓扑名称
    for elem in param_tool.GetInstance().param_hcl.m_list_hcl_param:
        str_name = '';
        pa_dir = re.compile('.*/');
        ma_dir = pa_dir.search(elem.m_str_hcl_project_dir_file);
        if ma_dir:
            str_dir = ma_dir.group();
            str_name = elem.m_str_hcl_project_dir_file.replace(str_dir,'');
        else:
            log_manager.GetInstance().CoutLog('get name failed',Com_foreground_interface);
            log_manager.GetInstance().CoutLog('hcl_project_dir_file:' + elem.m_str_hcl_project_dir_file,Com_foreground_interface);
            return oErrorBase.base_failed;
        hmm = hcl_modify_manager();
        iResult = hmm.LoadNetFile(str_name);#设置原始HCL工程文件名,新HCL文件名为new_hcl.net,二者均放在user_config目录下
        if (oErrorBase.base_succeed != iResult):
            log_manager.GetInstance().CoutLog('LoadNetFile failed:' + elem.m_str_project_name,Com_foreground_interface);
            return iResult;
        iResult = hmm.ModifDevInfoListA(param_tool.GetInstance().param_db_real.m_str_db_topo_name);#拓扑名称应与上面一致
        if (oErrorBase.base_succeed != iResult):
            return iResult;
        remote_manager.GetInstance().list_h_m_m.append(hmm);
    link_manager.GetInstance().WriteDevLabelFile();
    remote_manager.GetInstance().SetRemoteInfo();
    for hmm in remote_manager.GetInstance().list_h_m_m:
        hmm.CreateNewHclFile();
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页