package com.nis.web.service.configuration; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.apache.poi.ss.formula.functions.T; import org.dom4j.Node; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import antlr.StringUtils; import com.nis.domain.FunctionServiceDict; import com.nis.domain.Page; import com.nis.domain.configuration.AppPolicyCfg; import com.nis.domain.configuration.AvFileSampleCfg; import com.nis.domain.configuration.BaseCfg; import com.nis.domain.configuration.BaseStringCfg; import com.nis.domain.configuration.CfgIndexInfo; import com.nis.domain.configuration.ComplexkeywordCfg; import com.nis.domain.configuration.DdosIpCfg; import com.nis.domain.configuration.DnsIpCfg; import com.nis.domain.configuration.DnsResStrategy; import com.nis.domain.configuration.FileDigestCfg; import com.nis.domain.configuration.IpPortCfg; import com.nis.domain.configuration.PxyObjKeyring; import com.nis.domain.configuration.PxyObjTrustedCaCert; import com.nis.domain.configuration.PxyObjTrustedCaCrl; import com.nis.domain.maat.MaatCfg; import com.nis.domain.maat.ToMaatBean; import com.nis.domain.maat.ToMaatResult; import com.nis.domain.maat.MaatCfg.DigestCfg; import com.nis.domain.maat.MaatCfg.GroupCfg; import com.nis.domain.maat.MaatCfg.IpCfg; import com.nis.domain.maat.MaatCfg.NumBoundaryCfg; import com.nis.domain.maat.MaatCfg.StringCfg; import com.nis.util.ConfigServiceUtil; import com.nis.util.Constants; import com.nis.util.DateUtils; import com.nis.util.DictUtils; import com.nis.util.ServiceConfigTemplateUtil; import com.nis.util.StringUtil; import com.nis.web.dao.configuration.ConfigSynchronizationDao; import com.nis.web.security.UserUtils; import com.nis.web.service.BaseService; /** * 配置全量同步事务类 * @author zhangwei * */ @Service public class ConfigSynchronizationService extends BaseService{ @Autowired protected ConfigSynchronizationDao configSynchronizationDao; /** * 配置全量更新下发 * @param request * @param response * @throws IllegalAccessException * @throws IllegalArgumentException * @throws SecurityException * @throws NoSuchFieldException * @throws ClassNotFoundException */ public void send(HttpServletRequest request,HttpServletResponse response,BaseCfg cfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException { long start = System.currentTimeMillis(); ServiceConfigTemplateUtil serviceTemplate = new ServiceConfigTemplateUtil(); if(cfg==null){ List> serviceList = serviceTemplate.getServiceList(); for(Map service:serviceList){ String tableName = service.get("tableName").toString(); String serviceType = service.get("serviceType").toString(); String className = service.get("className").toString(); String serviceId = service.get("id").toString(); BaseCfg entity = new BaseCfg(); entity.setServiceId(Integer.valueOf(serviceId)); entity.setIsAudit(1); entity.setIsValid(1); if("1".equals(serviceType)){//maat类配置 List> cfgList = (List>) service.get("cfgList"); List> userRegionList = (List>) service.get("userRegionList"); if("cfg_index_info".equals(tableName)){ Page page=new Page(request,response,Constants.MAAT_JSON_SEND_SIZE,"a"); handleNtcMaatData(cfgList,userRegionList,page,entity,request,response,false); }else if("ddos_ip_cfg".equals(tableName)){ Page page=new Page(request,response,Constants.MAAT_JSON_SEND_SIZE,"a"); handleDdosMaatData(cfgList,userRegionList,page,entity,request,response,false); } }else if("2".equals(serviceType)){//回调类配置 entity.setTableName(tableName); entity.setServiceId(Integer.valueOf(serviceId)); Page page=new Page(request,response,Constants.MAAT_JSON_SEND_SIZE,"a"); handleCallbackData(className,page,entity,request,response,false); } } long end = System.currentTimeMillis(); logger.info("本次配置全量同步,开始时间:"+start +",结束时间:"+end +",共计花费"+((end-start)/1000+"秒")); }else{ } } /** * 处理ntc业务maat类配置 * @param serviceId * @param request * @param response * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */ public void handleNtcMaatData(List>cfgList,List>userRegionList, Page page,BaseCfg entity,HttpServletRequest request,HttpServletResponse response, boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ boolean hasData = true; while(hasData){ entity.setPage(page); List list = configSynchronizationDao.getCfgIndexList(entity); if(!StringUtil.isEmpty(list)){ hasData=auditNtcMaatData(cfgList,userRegionList,page,entity,list,hasData,isUpdateCfg); if(hasData) { page.setPageNo(page.getNext()); } }else{ hasData = false; } } } /** * MAAT配置批量下发 * @param cfgList * @param userRegionList * @param page * @param entity * @param list * @param hasData * @param isUpdateCfg 业务配置全部生效时需同步更新库表配置状态 * @return * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalArgumentException * @throws IllegalAccessException */ public boolean auditNtcMaatData(List> cfgList, List> userRegionList, Page page, BaseCfg entity, List list, boolean hasData, boolean isUpdateCfg)throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ ToMaatBean maatBean; MaatCfg maatCfg; List configCompileList; List groupRelationList; List ipRegionList; List strRegionList; List numRegionList; List digestRegionList; List areaIpRegionList; List ipList = new ArrayList(); List strList = new ArrayList(); List complexStrList = new ArrayList(); List numList = new ArrayList(); List fileList = new ArrayList(); maatBean = new ToMaatBean(); configCompileList = new ArrayList(); List compileIds = new ArrayList(); for(CfgIndexInfo cfg:list){ compileIds.add(cfg.getCompileId()); } if(cfgList!=null){ for(Map m:cfgList){ String tableName = m.get("tableName").toString(); if("1".equals(m.get("cfgType"))){ if(tableName.equals("asn_ip_cfg")){ }else{ ipList.addAll(configSynchronizationDao.getIpPortList(tableName, compileIds)); } }else if("2".equals(m.get("cfgType"))){ strList.addAll(configSynchronizationDao.getStrList(tableName,compileIds)); }else if("3".equals(m.get("cfgType"))){ complexStrList.addAll(configSynchronizationDao.getComplexStrList(tableName,compileIds)); }else if("4".equals(m.get("cfgType"))){ }else if("5".equals(m.get("cfgType"))){ fileList.addAll(configSynchronizationDao.getFileDigestList(tableName, compileIds)); } } } for(CfgIndexInfo cfg:list){ maatCfg = new MaatCfg(); maatCfg.initDefaultValue(); groupRelationList = new ArrayList(); ipRegionList = new ArrayList(); strRegionList = new ArrayList(); numRegionList = new ArrayList(); digestRegionList = new ArrayList(); areaIpRegionList = new ArrayList(); List list1 = new ArrayList(); List list2 = new ArrayList(); List list3 = new ArrayList(); List list4 = new ArrayList(); List list5 = new ArrayList(); String userRegion = ""; //处理自定义域 if(userRegionList!=null){ for(Map n:userRegionList){ Object userRegionPosition = n.get("userRegionPosition"); if(userRegionPosition!=null && (userRegionPosition.toString().equals("1")||userRegionPosition.toString().equals("0"))){ //通过反射机制获取自定义域字段值 String regionColumn = n.get("regionColumn").toString(); Class aClass = null; if(userRegionPosition.toString().equals(("0"))){ aClass = BaseCfg.class; }else{ aClass = CfgIndexInfo.class; } Object value = ""; if(entity.getServiceId().equals(517)||entity.getServiceId().equals(560)){//代理替换策略 String[] regionArray = regionColumn.split(","); for(int r=0;r0){ ipList.removeAll(list1); Map map = cfgConvert(ipRegionList,list1,1,cfg,groupRelationList); groupRelationList=map.get("groupList"); ipRegionList=map.get("dstList"); if(map.get("numRegionList")!=null){ numRegionList.addAll(map.get("numRegionList")); } } if(list2.size()>0){ strList.removeAll(list2); Map map = cfgConvert(strRegionList,list2,2,cfg,groupRelationList); groupRelationList=map.get("groupList"); strRegionList=map.get("dstList"); } if(list3.size()>0){ complexStrList.removeAll(list3); Map map = cfgConvert(strRegionList,list3,3,cfg,groupRelationList); groupRelationList=map.get("groupList"); strRegionList=map.get("dstList"); } if(list4.size()>0){ numList.removeAll(list4); Map map = cfgConvert(numRegionList,list4,4,cfg,groupRelationList); groupRelationList=map.get("groupList"); numRegionList=map.get("dstList"); } if(list5.size()>0){ fileList.removeAll(list5); Map map = cfgConvert(digestRegionList,list5,5,cfg,groupRelationList); groupRelationList=map.get("groupList"); digestRegionList=map.get("dstList"); } BeanUtils.copyProperties(cfg, maatCfg); maatCfg.setAction(cfg.getAction()); maatCfg.setAuditTime(cfg.getAuditTime()); maatCfg.setIpRegionList(ipRegionList); maatCfg.setStrRegionList(strRegionList); maatCfg.setNumRegionList(numRegionList); maatCfg.setDigestRegionList(digestRegionList); maatCfg.setGroupRelationList(groupRelationList); maatCfg.setGroupNum(groupRelationList.size()); maatCfg.setAreaIpRegionList(areaIpRegionList); maatCfg.setIsValid(cfg.getIsValid()); if(!StringUtil.isEmpty(userRegion)){ maatCfg.setUserRegion(userRegion); } configCompileList.add(maatCfg); } page.setList(list); if(page.isLastPage()){ hasData = false; } if(!StringUtil.isEmpty(configCompileList.size())){ maatBean.setConfigCompileList(configCompileList); maatBean.setAuditTime(new Date()); maatBean.setCreatorName(UserUtils.getUser().getName()); maatBean.setVersion(Constants.MAAT_VERSION); maatBean.setOpAction(Constants.INSERT_ACTION); String json=gsonToJson(maatBean); //调用服务接口配置全量更新 ToMaatResult result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,(hasData?null:"FINISHED")); } return hasData; } /** * 处理ddos配置 * @param serviceId * @param request * @param response * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */ public void handleDdosMaatData(List>cfgList,List>userRegionList, Page page,BaseCfg entity,HttpServletRequest request,HttpServletResponse response, boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ boolean hasData = true; while(hasData){ entity.setPage(page); List list = configSynchronizationDao.getDdosIpCfgList(entity); if(!StringUtil.isEmpty(list)){ hasData=auditDdosMaatData(cfgList,userRegionList, page,entity,list,hasData,isUpdateCfg); if(hasData) { page.setPageNo(page.getNext()); } }else{ hasData = false; } } } /** * ddos配置批量下发 * @param cfgList * @param userRegionList * @param page * @param entity * @param list * @param hasData * @param isUpdateCfg 业务配置全部生效时需同步更新库表配置状态 * @return * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalArgumentException * @throws IllegalAccessException */ public boolean auditDdosMaatData(List> cfgList, List>userRegionList, Page page, BaseCfg entity, List list, boolean hasData, boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{ ToMaatBean maatBean; MaatCfg maatCfg; List configCompileList; List groupRelationList; List ipRegionList; List strRegionList; List numRegionList; List digestRegionList; List areaIpRegionList; List ipList = new ArrayList(); maatBean = new ToMaatBean(); configCompileList = new ArrayList(); for(DdosIpCfg cfg:list){ maatCfg = new MaatCfg(); maatCfg.initDefaultValue(); groupRelationList = new ArrayList(); ipRegionList = new ArrayList(); strRegionList = new ArrayList(); numRegionList = new ArrayList(); digestRegionList = new ArrayList(); areaIpRegionList = new ArrayList(); List list1 = new ArrayList(); String userRegion = ""; //处理自定义域 if(userRegionList!=null){ for(Map n:userRegionList){ Object userRegionPosition = n.get("userRegionPosition"); if(userRegionPosition!=null && (userRegionPosition.toString().equals("1")||userRegionPosition.toString().equals("0"))){ //通过反射机制获取自定义域字段值 String regionColumn = n.get("regionColumn").toString(); Class aClass = null; if(userRegionPosition.toString().equals(("0"))){ aClass = BaseCfg.class; }else{ aClass = DdosIpCfg.class; } Object value = ""; Field field = aClass.getDeclaredField(regionColumn); field.setAccessible(true); value = field.get(cfg); if(!StringUtil.isEmpty(value)){ if(StringUtil.isEmpty(n.get("regionKey"))){ userRegion = value.toString(); }else{ userRegion += n.get("regionKey")+"="+value+Constants.USER_REGION_SPLIT; } } } } if(userRegion.endsWith(Constants.USER_REGION_SPLIT)){ userRegion = userRegion.substring(0, userRegion.length()-1); } } list1.add(cfg); if(list1.size()>0){ ipList.removeAll(list1); Map map = cfgConvert(ipRegionList,list1,1,cfg,groupRelationList); groupRelationList=map.get("groupList"); ipRegionList=map.get("dstList"); if(map.get("numRegionList")!=null){ numRegionList.addAll(map.get("numRegionList")); } } BeanUtils.copyProperties(cfg, maatCfg); maatCfg.setAction(cfg.getAction()); maatCfg.setAuditTime(cfg.getAuditTime()); maatCfg.setIpRegionList(ipRegionList); maatCfg.setStrRegionList(strRegionList); maatCfg.setNumRegionList(numRegionList); maatCfg.setDigestRegionList(digestRegionList); maatCfg.setGroupRelationList(groupRelationList); maatCfg.setGroupNum(groupRelationList.size()); maatCfg.setAreaIpRegionList(areaIpRegionList); maatCfg.setIsValid(cfg.getIsValid()); if(!StringUtil.isEmpty(userRegion)){ maatCfg.setUserRegion(userRegion); } configCompileList.add(maatCfg); } page.setList(list); if(page.isLastPage()){ hasData = false; } if(!StringUtil.isEmpty(configCompileList.size())){ maatBean.setConfigCompileList(configCompileList); maatBean.setAuditTime(new Date()); maatBean.setCreatorName(UserUtils.getUser().getName()); maatBean.setVersion(Constants.MAAT_VERSION); maatBean.setOpAction(Constants.INSERT_ACTION); String json=gsonToJson(maatBean); //调用服务接口配置全量更新 ToMaatResult result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,(hasData?null:"FINISHED")); } return hasData; } /** * 处理app业务maat类配置 * @param serviceId * @param request * @param response */ public void handleAppMaatData(List>cfgList,List>userRegionList, Page page,Object entity,HttpServletRequest request,HttpServletResponse response){ /*if(entity instanceof AppPolicyCfg){ AppPolicyCfg newEntity = entity; entity.setPage(page); } boolean hasData = true; while(hasData){ List list = configSynchronizationDao.getCfgIndexList(entity); page.setList(list); if(page.isLastPage()){ hasData = false; } }*/ } /** * 处理回调类业务配置 * @param serviceId * @param request * @param response * @throws ClassNotFoundException */ public void handleCallbackData(String className, Page page,BaseCfg entity,HttpServletRequest request,HttpServletResponse response, boolean isUpdateCfg) throws ClassNotFoundException{ boolean hasData = true; while(hasData){ entity.setPage(page); hasData=auditCallbackData(className,page,entity,hasData,isUpdateCfg); if(hasData){ page.setPageNo(page.getNext()); } } } /** * 回调类配置下发 * @param className * @param page * @param entity * @param hasData * @param isUpdateCfg * @throws ClassNotFoundException */ public boolean auditCallbackData(String className, Page page,BaseCfg entity, boolean hasData, boolean isUpdateCfg) throws ClassNotFoundException{ ToMaatBean maatBean; MaatCfg maatCfg; List configCompileList; List groupRelationList; List ipRegionList; List strRegionList; List numRegionList; List digestRegionList; List areaIpRegionList; maatBean = new ToMaatBean(); configCompileList = new ArrayList(); List list = new ArrayList(); List newList = new ArrayList(); if(className.equals("AvFileSampleCfg")){ list = configSynchronizationDao.getAvFileCfgList(entity); }else if(className.equals("PxyObjKeyring")){ list = configSynchronizationDao.getPxyObjKeyringCfgList(entity); for(int i=0;i