This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
k18-ntcs-web-ntc/src/main/java/com/nis/web/service/configuration/ConfigSynchronizationService.java

2579 lines
106 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.nis.web.service.configuration;
import java.lang.reflect.Field;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nis.domain.basics.*;
import com.nis.domain.configuration.*;
import com.nis.util.*;
import com.nis.web.dao.basics.*;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
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 com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.nis.domain.FunctionServiceDict;
import com.nis.domain.Page;
import com.nis.domain.maat.GroupReuseAddBean;
import com.nis.domain.maat.GroupReuseCfg;
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.domain.maat.ManipulatActionParam;
import com.nis.domain.specific.ConfigGroupInfo;
import com.nis.domain.specific.SpecificServiceCfg;
import com.nis.web.dao.configuration.AppCfgDao;
import com.nis.web.dao.configuration.CommonPolicyDao;
import com.nis.web.dao.configuration.ConfigSynchronizationDao;
import com.nis.web.dao.configuration.PxyObjSpoofingIpPoolDao;
import com.nis.web.dao.specific.ConfigGroupInfoDao;
import com.nis.web.dao.specific.SpecificServiceCfgDao;
import com.nis.web.security.UserUtils;
import com.nis.web.service.BaseService;
/**
* 配置全量同步事务类
* @author zhangwei
*
*/
@Service
public class ConfigSynchronizationService extends BaseService{
@Autowired
protected ConfigSynchronizationDao configSynchronizationDao;
@Autowired
protected CommonPolicyDao commonPolicyDao;
@Autowired
protected SpecificServiceCfgDao specificServiceCfgDao;
@Autowired
protected ConfigGroupInfoDao configGroupInfoDao;
@Autowired
protected AsnGroupInfoDao asnGroupInfoDao;
@Autowired
protected AppCfgDao appCfgDao;
@Autowired
protected PxyObjSpoofingIpPoolDao pxyObjSpoofingIpPoolDao;
@Autowired
protected IpCommGroupCfgDao ipCommGroupCfgDao;
@Autowired
protected ScriberIdCommGroupDao scriberIdCommGroupDao;
@Autowired
protected UrlCommGroupDao urlCommGroupDao;
@Autowired
protected DomainCommGroupDao domainCommGroupDao;
private boolean lastServiceTag = false;//标识是否是最后一个同步业务
private boolean isFinished = false;
/**
* 配置全量更新下发
* @param serviceIdCondition 界面传入的全量同步某一个业务
* @param request
* @param response
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws NoSuchFieldException
* @throws ClassNotFoundException
*/
public void send(Integer serviceIdCondition,HttpServletRequest request,HttpServletResponse response,BaseCfg cfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
long start = System.currentTimeMillis();
ServiceConfigTemplateUtil serviceTemplate = new ServiceConfigTemplateUtil();
if(cfg==null){
List<Map<String,Object>> serviceList =new ArrayList<>();
if(StringUtil.isEmpty(serviceIdCondition)) {
serviceList= serviceTemplate.getServiceList();
}else {
serviceList=serviceTemplate.getServiceListByServiceId(serviceIdCondition);
}
int lastService = 0;
lastServiceTag = false;
for(Map<String,Object> service:serviceList){
lastService ++;
if(lastService==serviceList.size()){
lastServiceTag = true;
}
String tableName = service.get("tableName").toString();
String serviceType = service.get("serviceType").toString();
String className = service.get("className").toString();
String serviceId = service.get("id").toString();
String serviceIds=service.get("serviceIds").toString();
BaseCfg entity = new BaseCfg();
entity.setServiceId(Integer.valueOf(serviceId));
entity.setIsAudit(1);
entity.setIsValid(1);
entity.setTableName(tableName);
if("1".equals(serviceType)){//maat类配置
if(StringUtils.isNotBlank(serviceIds)){//公共分组
Map<Integer,List<Map<String,Object>>> childrenCfgMap=new HashMap<>();
Map<Integer,List<Map<String,Object>>> childrenUserRegionMap=new HashMap<>();
for(String _serviceId:serviceIds.split(",")){
if(StringUtils.isNotBlank(_serviceId)){
Map<String,Object> subService=serviceTemplate.getServiceListByServiceId(Integer.parseInt(_serviceId)).get(0);
childrenCfgMap.put(Integer.parseInt(_serviceId),(List<Map<String,Object>>)subService.get("cfgList"));
}
}
if(childrenCfgMap.size()>0){
entity.setFunctionId(Integer.parseInt(service.get("functionId").toString()));
Page page=new Page(request,response,Constants.MAAT_JSON_SEND_SIZE,"a");
handleObjGroupListMaatData(childrenCfgMap,childrenUserRegionMap,page,entity,request,response,false,tableName);
}
}else{
List<Map<String,Object>> cfgList = (List<Map<String, Object>>) service.get("cfgList");
List<Map<String,Object>> userRegionList = (List<Map<String, Object>>) service.get("userRegionList");
if(cfgList.size()>0){
Page page=new Page(request,response,Constants.MAAT_JSON_SEND_SIZE,"a");
handleNtcMaatData(cfgList,userRegionList,page,entity,request,response,false,tableName);
}else{
int cfgType = Integer.parseInt(service.get("cfgType").toString());
if("ddos_ip_cfg".equals(tableName)){
Page<DdosIpCfg> page=new Page<DdosIpCfg>(request,response,Constants.MAAT_JSON_SEND_SIZE,"a");
handleDdosMaatData(cfgList,userRegionList,page,entity,request,response,false);
}else{
Page<T> page=new Page<T>(request,response,Constants.MAAT_JSON_SEND_SIZE,"a");
handleSingleMaatData(cfgType,userRegionList,page,entity,request,response,false);
}
}
}
}else if("2".equals(serviceType)){//回调类配置
entity.setTableName(tableName);
entity.setServiceId(Integer.valueOf(serviceId));
Page<BaseCfg> page=new Page<BaseCfg>(request,response,Constants.MAAT_JSON_SEND_SIZE,"a");
handleCallbackData(className,page,entity,request,response,false);
}
//最后一个业务,发送一个结束标识
if(lastServiceTag){
String json = "{}";
//如果是所有业务全量同步需要发送一个service=-1的请求有助于服务端删除分组复用配置
if(StringUtil.isEmpty(serviceIdCondition)){
entity.setServiceId(-1);
}
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(finish data).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,Integer.parseInt(serviceType),entity.getServiceId(),entity.getTableName(),"FINISHED");
logger.info("全量下发响应信息【finish】"+result.toString());
}
}
long end = System.currentTimeMillis();
logger.info("本次配置全量同步,开始时间:"+start
+",结束时间:"+end
+",共计花费"+((end-start)/1000+""));
}else{
}
}
/**
* 处理ntc业务maat类配置
* @param cfgList
* @param userRegionList
* @param page
* @param entity
* @param request
* @param response
* @param isUpdateCfg
* @param tableName
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public void handleNtcMaatData(List<Map<String,Object>>cfgList,List<Map<String,Object>>userRegionList,
Page<T> page,BaseCfg entity,HttpServletRequest request,HttpServletResponse response,
boolean isUpdateCfg,String tableName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
boolean hasData = true;
int index=0;
while(hasData){
entity.setPage(page);
List list = Lists.newArrayList();
if("cfg_index_info".equals(tableName)){
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;
}
}else if("file_digest_cfg".equals(tableName)){
list = configSynchronizationDao.getFileDigestListByService(entity);
if(!StringUtil.isEmpty(list)){
hasData=auditFileDigestData(cfgList,userRegionList,page,entity,list,hasData,isUpdateCfg);
if(hasData) {
page.setPageNo(page.getNext());
}
}else{
hasData = false;
}
}else if("app_policy_cfg".equals(tableName)){
list = configSynchronizationDao.getAppPolicyList(entity);
if(!StringUtil.isEmpty(list)){
hasData=auditAppPolicyData(cfgList,userRegionList,page,entity,list,hasData,isUpdateCfg);
if(hasData) {
page.setPageNo(page.getNext());
}
}else{
hasData = false;
}
}else if("app_feature_index".equals(tableName)){
list = configSynchronizationDao.getAppFeatureIndexList(entity);
if(!StringUtil.isEmpty(list)){
hasData=auditAppFeatureData(cfgList,userRegionList,page,entity,list,hasData,isUpdateCfg);
if(hasData) {
page.setPageNo(page.getNext());
}
}else{
hasData = false;
}
}else{
logger.info("全量同步未知业务");
hasData = false;
}
//此业务无数据需同步,也许向服务端发送一个{}串
if(index ==0 && StringUtil.isEmpty(list) && !isUpdateCfg){
String json = "{}";
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(m nodata).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
index++;
}
}
/**
* 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<Map<String,Object>> cfgList,
List<Map<String,Object>> userRegionList,
Page page,
BaseCfg entity,
List<CfgIndexInfo> list,
boolean hasData,
boolean isUpdateCfg)throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
List<IpPortCfg> ipList = new ArrayList();
List<AsnIpCfg> asnIpList = new ArrayList();
List<BaseStringCfg> strList = new ArrayList();
List<ComplexkeywordCfg> complexStrList = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> numList = new ArrayList();
List<FileDigestCfg> fileList = new ArrayList();
maatBean = new ToMaatBean();
configCompileList = new ArrayList();
List<Integer> compileIds = new ArrayList();
List<String> asnGroupIds = new ArrayList();
List<String> spoofingPoolCfgIds = new ArrayList();
for(CfgIndexInfo cfg:list){
compileIds.add(cfg.getCompileId());
if(entity.getServiceId().equals(518)){ // IP_Spoofing
spoofingPoolCfgIds.add(cfg.getUserRegion3());
}
/*if(entity.getServiceId().equals(37)||entity.getServiceId().equals(149)){
asnGroupIds.add(cfg.getUserRegion4());
}*/
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(compileIds) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( entity.getTableName(), entity,compileIds,null);
}
}
//用于存储自定义域为关键字的业务key=compileIdvalue=keyword.
Map<Integer, BaseStringCfg> keywordMap=new HashMap<>();
if(cfgList!=null){
for(Map<String,Object> m:cfgList){
String tableName = m.get("tableName").toString();
if("1".equals(m.get("cfgType"))){
ipList.addAll(configSynchronizationDao.getIpPortList(tableName, compileIds));
}else if("2".equals(m.get("cfgType"))){
List<BaseStringCfg> strDataList=configSynchronizationDao.getStrList(tableName,compileIds);
strList.addAll(strDataList);
//512/521/514/1026业务的用户自定义域取DOMAIN_STR取自Keyword
if(/*entity.getServiceId()==512
||*/entity.getServiceId()==521
||entity.getServiceId()==514
||entity.getServiceId()==1026) {
keywordMap=configSynchronizationDao.getCompileIdWithKeyword(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));
}
if(isUpdateCfg) {
if(tableName.equals("asn_ip_cfg")){
if(!StringUtil.isEmpty(asnGroupIds)) {
commonPolicyDao.auditCfgBatch(tableName, entity,asnGroupIds,null);
}
}else if(tableName.equals("pxy_obj_spoofing_ip_pool")) {
if(!StringUtil.isEmpty(spoofingPoolCfgIds)) {
commonPolicyDao.auditCfgBatch(tableName, entity,spoofingPoolCfgIds,null);
}
}else{
commonPolicyDao.auditCfgBatch( tableName, entity,compileIds,null);
}
}
}
}
if(entity.getServiceId().equals(518)){// IP仿冒策略
List<PxyObjSpoofingIpPool> poolList = new ArrayList<PxyObjSpoofingIpPool>();
String json="";
for (String cfgId : spoofingPoolCfgIds) {
PxyObjSpoofingIpPool pool = new PxyObjSpoofingIpPool();
pool = pxyObjSpoofingIpPoolDao.getPxyObjSpoofingIpPool(Long.parseLong(cfgId));
pool.setAreaEffectiveIds("0");
pool.setGroupId(pool.getCfgId().intValue());// 界面端的配置ID作为策略分组ID
poolList.add(pool);
}
// 调用服务接口下发配置数据
json=gsonToJson(poolList);
// logger.info("欺骗IP池配置批量下发配置参数"+json);
// 调用服务接口下发配置
try {
ToMaatResult result = ConfigServiceUtil.postCallbackCfg(json);
if(result!=null){
logger.info("欺骗IP池配置批量下发响应信息"+result.getMsg());
}
} catch (Exception e) {
logger.error("欺骗IP池配置批量下发失败",e);
throw e;
}
}
//批量获取regionId,groupId(相同编译下的IP类配置多条ip只获取一个组号分组复用的域配置不需要重新获取regionId,groupId
List<Integer> regionIds = ConfigServiceUtil.getId(3, ipList.size()+strList.size()+complexStrList.size()+numList.size()+fileList.size());
List<Integer> groupIds = ConfigServiceUtil.getId(2, ipList.size()+strList.size()+complexStrList.size()+numList.size()+fileList.size());
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<BaseStringCfg> list2 = new ArrayList();
List<ComplexkeywordCfg> list3 = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> list4 = new ArrayList();
List<FileDigestCfg> list5 = new ArrayList();
String userRegion = "";
//处理自定义域
if(userRegionList!=null){
for(Map<String,Object> n:userRegionList){
Object userRegionPosition = n.get("userRegionPosition");
if(userRegionPosition!=null && (userRegionPosition.toString().equals("1")||userRegionPosition.toString().equals("0"))){
//通过反射机制获取自定义域字段值
String regionColumn = n.get("regionColumn").toString();
String regionKey = n.get("regionKey").toString();
Object value = "";
Class aClass = null;
if(userRegionPosition.toString().equals(("0"))){
aClass = BaseCfg.class;
}else{
aClass = CfgIndexInfo.class;
}
if(entity.getServiceId().equals(517)||entity.getServiceId().equals(560)){//代理替换策略
String[] regionArray = regionColumn.split(",");
for(int r=0;r<regionArray.length;r++){
Field field = aClass.getDeclaredField(regionArray[r]);
field.setAccessible(true);
if(n.get("regionKey").equals("substitute")){
value += "/"+replaceContentEscape(field.get(cfg)==null?"":field.get(cfg).toString());//自定义域以/分割时的处理
}else{
value += replaceContentEscape(field.get(cfg)==null?"":field.get(cfg).toString());
}
}
userRegion += n.get("regionKey")+"="+value+Constants.USER_REGION_SPLIT;
}else if(entity.getServiceId().equals(512)) {
if(!StringUtil.isEmpty(cfg.getUserRegion1())&&cfg.getUserRegion1().startsWith("{")) {
userRegion=cfg.getUserRegion1();
}
}else{
/*if(regionKey.equals("keyring_id") && StringUtil.isEmpty(value)){
value = "0";
}else */if(regionKey.equals("DOMAIN_STR")) {
if(!StringUtil.isEmpty(keywordMap) && !StringUtil.isEmpty(keywordMap.get(cfg.getCompileId()))) {
value = keywordMap.get(cfg.getCompileId()).getCfgKeywords();
}
}else if(regionKey.equals("DOMAIN_ID")) {
if(!StringUtil.isEmpty(keywordMap) && !StringUtil.isEmpty(keywordMap.get(cfg.getCompileId()))) {
value = cfg.getCompileId();
}
}else {
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);
}
}
// Proxy Block --xml无法描述的user_region
if(entity.getServiceId().equals(576)) {
ManipulatActionParam actionParam = new ManipulatActionParam();
actionParam.setMethod("block");
actionParam.setCode(Integer.valueOf(cfg.getUserRegion1()));
actionParam.setMessage(!StringUtil.isBlank(cfg.getUserRegion2()) ? cfg.getUserRegion2():"");
actionParam.setHtml_profile(Integer.valueOf(!StringUtil.isBlank(cfg.getUserRegion3()) ? cfg.getUserRegion3():"0"));
userRegion = StringEscapeUtils.unescapeJson(gsonToJson(actionParam));
}
// Proxy Manipulation
if(entity.getServiceId().equals(656)) {
userRegion = BaseService.setUserRegionOfMantipulateCfg(cfg, userRegion);
}
//Intercept Policy、http(s) 监测 、http(s) 白名单
else if(entity.getServiceId().equals(521) || entity.getServiceId().equals(592)
|| entity.getServiceId().equals(750)) {
userRegion="{}";
}
for(AsnIpCfg ip:asnIpList){//asn不需要再次获取regionId,groupId
if(ip.getAsnIpGroup().toString().equals(cfg.getUserRegion4())){//根据组号对应
ip.setIsValid(entity.getIsValid());
ip.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
list1.add(ip);
}
}
if(list1.size()>0){
GroupCfg groupCfg=new GroupCfg();
groupCfg.setCompileId(cfg.getCompileId());
groupCfg.setGroupId(Integer.parseInt(cfg.getUserRegion4()));
groupCfg.setIsValid(entity.getIsValid());
groupCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
groupRelationList.add(groupCfg);
ipRegionList.addAll(groupReuseCfgAddRemoveConvert(list1,Constants.VALID_YES,null));
}else if(!StringUtil.isEmpty(asnGroupIds)) {
GroupCfg groupCfg=new GroupCfg();
groupCfg.setCompileId(cfg.getCompileId());
groupCfg.setGroupId(Integer.parseInt(cfg.getUserRegion4()));
groupCfg.setIsValid(entity.getIsValid());
groupCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
groupRelationList.add(groupCfg);
ipRegionList.addAll(ipRegionList);
}
//配置域转换
userRegion = configCovert(ipList,strList,complexStrList,
numList,fileList,regionIds,groupIds,entity,cfg.getCompileId(),
StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime(),
groupRelationList,ipRegionList,strRegionList,numRegionList,digestRegionList,areaIpRegionList,userRegion);
BeanUtils.copyProperties(cfg, maatCfg);
maatCfg.setAction(cfg.getAction());
maatCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.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(entity.getIsValid());
if(!StringUtil.isEmpty(userRegion)){
maatCfg.setUserRegion(userRegion);
}
configCompileList.add(maatCfg);
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
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);
//调用服务接口下发配置数据
if(isUpdateCfg) {
//logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* 文件摘要配置批量下发
* @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 auditFileDigestData(List<Map<String,Object>> cfgList,
List<Map<String,Object>> userRegionList,
Page page,
BaseCfg entity,
List<FileDigestCfg> list,
boolean hasData,
boolean isUpdateCfg)throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
List<IpPortCfg> ipList = new ArrayList();
List<BaseStringCfg> strList = new ArrayList();
List<ComplexkeywordCfg> complexStrList = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> numList = new ArrayList();
List<FileDigestCfg> fileList = new ArrayList();
maatBean = new ToMaatBean();
configCompileList = new ArrayList();
List<Integer> compileIds = new ArrayList();
for(FileDigestCfg cfg:list){
compileIds.add(cfg.getCompileId());
if(!StringUtil.isEmpty(cfg.getCfdsLevel()) && (cfg.getCfdsLevel() > 10)){
cfg.setCfdsLevel((int)(cfg.getCfdsLevel()/10));//文件摘要置信度界面显示为70,80,90,100下发为7,8,9,10
}
fileList.add(cfg);
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(compileIds) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( entity.getTableName(), entity,compileIds,null);
}
}
if(cfgList!=null){
for(Map<String,Object> m:cfgList){
String tableName = m.get("tableName").toString();
if("1".equals(m.get("cfgType"))){
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));
}
if(isUpdateCfg) {
commonPolicyDao.auditCfgBatch( tableName, entity,compileIds,null);
}
}
}
//批量获取regionId,groupId(相同编译下的IP类配置多条ip只获取一个组号分组复用的域配置不需要重新获取regionId,groupId
List<Integer> regionIds = ConfigServiceUtil.getId(3, ipList.size()+strList.size()+complexStrList.size()+numList.size()+fileList.size());
List<Integer> groupIds = ConfigServiceUtil.getId(2, ipList.size()+strList.size()+complexStrList.size()+numList.size()+fileList.size());
for(FileDigestCfg 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<BaseStringCfg> list2 = new ArrayList();
List<ComplexkeywordCfg> list3 = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> list4 = new ArrayList();
List<FileDigestCfg> list5 = new ArrayList();
String userRegion = "";
//处理自定义域
if(userRegionList!=null){
for(Map<String,Object> 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 = FileDigestCfg.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;
}
}
}
// System.out.println("userRegionList:"+n.get("regionKey")+","+n.get("regionColumn")+","+n.get("handleType"));
}
if(userRegion.endsWith(Constants.USER_REGION_SPLIT)){
userRegion = userRegion.substring(0, userRegion.length()-1);
}
}
//配置域转换
configCovert(ipList,strList,complexStrList,
numList,fileList,regionIds,groupIds,entity,cfg.getCompileId(),
StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime(),
groupRelationList,ipRegionList,strRegionList,numRegionList,digestRegionList,areaIpRegionList,userRegion);
BeanUtils.copyProperties(cfg, maatCfg);
maatCfg.setAction(cfg.getAction());
maatCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.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(entity.getIsValid());
if(!StringUtil.isEmpty(userRegion)){
maatCfg.setUserRegion(userRegion);
}
configCompileList.add(maatCfg);
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
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);
//调用服务接口下发配置数据
if(isUpdateCfg) {
// logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* APP策略配置批量下发
* @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 auditAppPolicyData(List<Map<String,Object>> cfgList,
List<Map<String,Object>> userRegionList,
Page page,
BaseCfg entity,
List<AppPolicyCfg> list,
boolean hasData,
boolean isUpdateCfg)throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
List<IpPortCfg> ipList = new ArrayList();
List<BaseStringCfg> strList = new ArrayList();
List<ComplexkeywordCfg> complexStrList = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> numList = new ArrayList();
List<FileDigestCfg> fileList = new ArrayList();
maatBean = new ToMaatBean();
configCompileList = new ArrayList();
List<Integer> compileIds = new ArrayList();
List<String> asnGroupIds = new ArrayList();
for(AppPolicyCfg cfg:list){
compileIds.add(cfg.getCompileId());
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(compileIds) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( entity.getTableName(), entity,compileIds,null);
}
}
if(cfgList!=null){
for(Map<String,Object> m:cfgList){
String tableName = m.get("tableName").toString();
if("1".equals(m.get("cfgType"))){
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));
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(compileIds) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( tableName, entity,compileIds,null);
}
}
}
}
//批量获取regionId,groupId(相同编译下的IP类配置多条ip只获取一个组号compileIds.size()表示app策略本身有一个字符串域
List<Integer> regionIds = ConfigServiceUtil.getId(3, ipList.size()+strList.size()+compileIds.size()+complexStrList.size()+numList.size()+fileList.size());
List<Integer> groupIds = ConfigServiceUtil.getId(2, ipList.size()+strList.size()+compileIds.size()+complexStrList.size()+numList.size()+fileList.size());
for(AppPolicyCfg 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<BaseStringCfg> list2 = new ArrayList();
List<ComplexkeywordCfg> list3 = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> list4 = new ArrayList();
List<FileDigestCfg> list5 = new ArrayList();
String userRegion = "";
//处理自定义域
if(userRegionList!=null){
for(Map<String,Object> 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 = AppPolicyCfg.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);
}
}
//将app码与行为码设置为字符串域
StringBuffer cfgKeywords = new StringBuffer();
BaseStringCfg strCfg = new BaseStringCfg();
if(cfg.getServiceId().equals(35)||cfg.getServiceId().equals(147)||cfg.getServiceId().equals(1059)){//基础协议
cfgKeywords.append(Constants.PROTO_ID_REGION + "=" + cfg.getAppCode());
}else if(cfg.getServiceId().equals(33)||cfg.getServiceId().equals(145)||cfg.getServiceId().equals(1056)){
cfgKeywords.append(Constants.APP_ID_REGION + "=" + cfg.getAppCode());
if(cfg.getBehavCode()!=null){
cfgKeywords.append(Constants.KEYWORD_EXPR);
cfgKeywords.append(Constants.BEHAV_ID_REGION + "=" + cfg.getBehavCode());
}
}else if(cfg.getServiceId().equals(36)||cfg.getServiceId().equals(148)||cfg.getServiceId().equals(1060)){//隧道加密协议
cfgKeywords.append(Constants.PROTO_ID_REGION + "=" + cfg.getAppCode());
if(cfg.getBehavCode()!=null){
cfgKeywords.append(Constants.KEYWORD_EXPR);
cfgKeywords.append(Constants.BEHAV_ID_REGION + "=" + cfg.getBehavCode());
}
}
cfg.setCfgKeywords(cfgKeywords.toString());
BeanUtils.copyProperties(cfg, strCfg);
strList.add(strCfg);
//配置域转换
configCovert(ipList,strList,complexStrList,
numList,fileList,regionIds,groupIds,entity,cfg.getCompileId(),
StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime(),
groupRelationList,ipRegionList,strRegionList,numRegionList,digestRegionList,areaIpRegionList,userRegion);
BeanUtils.copyProperties(cfg, maatCfg);
maatCfg.setAction(cfg.getAction());
maatCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.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(entity.getIsValid());
if(!StringUtil.isEmpty(userRegion)){
maatCfg.setUserRegion(userRegion);
}
configCompileList.add(maatCfg);
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
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);
//调用服务接口下发配置数据
if(isUpdateCfg) {
// logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* APP特征批量下发
* @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 auditAppFeatureData(List<Map<String,Object>> cfgList,
List<Map<String,Object>> userRegionList,
Page page,
BaseCfg entity,
List<AppFeatureIndex> list,
boolean hasData,
boolean isUpdateCfg)throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
List<IpPortCfg> ipList = new ArrayList();
List<BaseStringCfg> strList = new ArrayList();
List<ComplexkeywordCfg> complexStrList = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> numList = new ArrayList();
List<FileDigestCfg> fileList = new ArrayList();
maatBean = new ToMaatBean();
configCompileList = new ArrayList();
List<Integer> compileIds = new ArrayList();
List<String> asnGroupIds = new ArrayList();
for(AppFeatureIndex cfg:list){
compileIds.add(cfg.getCompileId());
if(entity.getServiceId().equals(37)||entity.getServiceId().equals(149)){
asnGroupIds.add(cfg.getUserRegion4());
}
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(compileIds) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( entity.getTableName(), entity,compileIds,null);
}
}
if(cfgList!=null){
for(Map<String,Object> m:cfgList){
String tableName = m.get("tableName").toString();
if("1".equals(m.get("cfgType"))){
ipList.addAll(configSynchronizationDao.getAppIpPortList(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));
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(compileIds) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( tableName, entity,compileIds,null);
}
}
}
}
//批量获取regionId,groupId(相同编译下的IP类配置多条ip只获取一个组号分组复用的域配置不需要重新获取regionId,groupId
List<Integer> regionIds = ConfigServiceUtil.getId(3, ipList.size()+strList.size()+complexStrList.size()+numList.size()+fileList.size());
List<Integer> groupIds = ConfigServiceUtil.getId(2, ipList.size()+strList.size()+complexStrList.size()+numList.size()+fileList.size());
for(AppFeatureIndex 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<BaseStringCfg> list2 = new ArrayList();
List<ComplexkeywordCfg> list3 = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> list4 = new ArrayList();
List<FileDigestCfg> list5 = new ArrayList();
String userRegion = "";
//处理自定义域
if(userRegionList!=null){
for(Map<String,Object> 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 = AppFeatureIndex.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);
}
}
//配置域转换
configCovert(ipList,strList,complexStrList,
numList,fileList,regionIds,groupIds,entity,cfg.getCompileId(),
StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime(),
groupRelationList,ipRegionList,strRegionList,numRegionList,digestRegionList,areaIpRegionList,userRegion);
BeanUtils.copyProperties(cfg, maatCfg);
maatCfg.setAction(cfg.getAction());
maatCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.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(entity.getIsValid());
if(!StringUtil.isEmpty(userRegion)){
maatCfg.setUserRegion(userRegion);
}
configCompileList.add(maatCfg);
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
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);
//调用服务接口下发配置数据
if(isUpdateCfg) {
// logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* 处理ddos配置
* @param serviceId
* @param request
* @param response
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public void handleDdosMaatData(List<Map<String,Object>>cfgList,List<Map<String,Object>>userRegionList,
Page<DdosIpCfg> page,BaseCfg entity,HttpServletRequest request,HttpServletResponse response,
boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
boolean hasData = true;
int index=0;
while(hasData){
entity.setPage(page);
List<DdosIpCfg> 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;
}
//此业务无数据需同步,也许向服务端发送一个{}串
if(index ==0 && StringUtil.isEmpty(list) && !isUpdateCfg){
String json = "{}";
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(d nodata).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
index++;
}
}
/**
* 处理单域maat配置
* @param serviceId
* @param request
* @param response
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public void handleSingleMaatData(int cfgType,List<Map<String,Object>>userRegionList,
Page<T> page,BaseCfg entity,HttpServletRequest request,HttpServletResponse response,
boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
boolean hasData = true;
//APP IP按配置Id批量下发[服务端只支持maat和回调类格式的json串]
List<ConfigGroupInfo> groupInfos=new ArrayList<>();
//ASN IP按配置Id全量下发[服务端只支持maat和回调类格式的json串]
List<AsnGroupInfo> asnGroupInfos=new ArrayList<>();
if(entity.getServiceId().equals(1028)){
ConfigGroupInfo groupInfo=new ConfigGroupInfo();
groupInfo.setIsIssued(1);//全量下发只用获取已下发的group信息
groupInfo.setGroupType(1);
groupInfos=configGroupInfoDao.findAllList(groupInfo);//每一批次下发都取一次最新的group_info信息
if(StringUtil.isEmpty(groupInfos)){
hasData=false;
}
}
if(entity.getServiceId().equals(400)){
AsnGroupInfo groupInfo=new AsnGroupInfo();
groupInfo.setIsValid(1);//全量下发只用获取已下发的group信息
asnGroupInfos=asnGroupInfoDao.findAsnGroupInfoList(groupInfo);//每一批次下发都取一次最新的group_info信息
if(StringUtil.isEmpty(asnGroupInfos)){
hasData=false;
}
}
//此业务无数据需同步,也许向服务端发送一个{}串
if(!hasData && !isUpdateCfg){
String json = "{}";
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(s asn or app nodata).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
int index=0;
List listData = Lists.newArrayList();
while(hasData){
entity.setPage(page);
List list = Lists.newArrayList();
if(cfgType==1){
if(entity.getServiceId().equals(1028)){
page.setPageSize(-1);
page.setPageNo(1);
page.setLastPage(true);
entity.setPage(page);
entity.setCompileId(groupInfos.get(0).getCompileId());
entity.setGroupId(groupInfos.get(0).getGroupId());
list = configSynchronizationDao.getAppIpFeatureList(entity);
listData.addAll(list);
}else if(entity.getServiceId().equals(400)){
page.setPageSize(-1);
page.setPageNo(1);
page.setLastPage(true);
entity.setPage(page);
entity.setCompileId(asnGroupInfos.get(0).getCompileId());
entity.setGroupId(asnGroupInfos.get(0).getGroupId());
list = configSynchronizationDao.getAsnIpList(entity);
listData.addAll(list);
}else{
list = configSynchronizationDao.getIpPortListByService(entity);
}
}else if(cfgType==2){
list = configSynchronizationDao.getStrListByService(entity);
}else if(cfgType==3){
list = configSynchronizationDao.getComplexStrListByService(entity);
}
if(!StringUtil.isEmpty(list)){
if(entity.getServiceId().equals(1028)){
hasData=auditAppIpData(userRegionList,
page,entity,list,hasData,isUpdateCfg);
}else if(entity.getServiceId().equals(400)){
hasData=auditAsnIpData(userRegionList,
page,entity,list,hasData,isUpdateCfg);
}else{
hasData=auditSingleMaatData(cfgType,userRegionList,
page,entity,list,hasData,isUpdateCfg);
}
if(hasData) {
page.setPageNo(page.getNext());
}
}else{
hasData = false;
}
//ASN IP
if(entity.getServiceId().equals(400)){
//处理完成一个compile删除一个Group
if(!StringUtil.isEmpty(asnGroupInfos)){
asnGroupInfos.remove(0);
}
if(asnGroupInfos.size() > 0) {
hasData=true;
}else {
hasData=false;
if(StringUtil.isEmpty(listData) && !isUpdateCfg){
String json = "{}";
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(asn nodata).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
}
//TODO 待处理
}else if(entity.getServiceId().equals(1028)){
//APP IP
//处理完成一个compile删除一个Group
if(!StringUtil.isEmpty(groupInfos)){
groupInfos.remove(0);
}
if(groupInfos.size() > 0) {
hasData=true;
}else {
hasData=false;
if(StringUtil.isEmpty(listData) && !isUpdateCfg){
String json = "{}";
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(app nodata).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
}
}else{
//此业务无数据需同步,也许向服务端发送一个{}串
if(index ==0 && StringUtil.isEmpty(list) && !isUpdateCfg){
String json = "{}";
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(s nodata).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
}
index++;
}
}
/**
* 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<Map<String,Object>> cfgList,
List<Map<String,Object>>userRegionList,
Page<DdosIpCfg> page,
BaseCfg entity,
List<DdosIpCfg> list,
boolean hasData,
boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
maatBean = new ToMaatBean();
configCompileList = new ArrayList();
List<Integer> compileIds=new ArrayList();
//批量获取regionId,groupId
List<Integer> regionIds = ConfigServiceUtil.getId(3, list.size());
List<Integer> groupIds = ConfigServiceUtil.getId(2, list.size());
for(int i=0;i<list.size();i++){
DdosIpCfg cfg=list.get(i);
cfg.setRegionId(regionIds.get(i));
cfg.setGroupId(groupIds.get(i));
cfg.setIsValid(entity.getIsValid());
cfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
maatCfg = new MaatCfg();
maatCfg.initDefaultValue();
groupRelationList = new ArrayList();
ipRegionList = new ArrayList();
strRegionList = new ArrayList();
numRegionList = new ArrayList();
digestRegionList = new ArrayList();
areaIpRegionList = new ArrayList();
List<DdosIpCfg> list1 = new ArrayList();
String userRegion = "";
//处理自定义域【DDOS自定义域组成json结构】
Map ddosUserRegion= new HashMap();
if(userRegionList!=null){
for(Map<String,Object> 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;
ddosUserRegion.put(n.get("regionKey"), value);
}
}else{
ddosUserRegion.put(n.get("regionKey"), "");
}
}
}
if(userRegion.endsWith(Constants.USER_REGION_SPLIT)){
userRegion = userRegion.substring(0, userRegion.length()-1);
}else if(!StringUtil.isEmpty(ddosUserRegion)){
userRegion = new Gson().toJson(ddosUserRegion);
}
}
if(isUpdateCfg && !StringUtil.isEmpty(cfg) && !StringUtil.isEmpty(cfg.getCompileId())) {
compileIds.add(cfg.getCompileId());
}
list1.add(cfg);
if(list1.size()>0){
Map<String,List> map = cfgToMaatConvert(ipRegionList,list1,1,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(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.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(entity.getIsValid());
if(!StringUtil.isEmpty(userRegion)){
maatCfg.setUserRegion(userRegion);
}
configCompileList.add(maatCfg);
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(compileIds) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( entity.getTableName(), entity,compileIds,null);
}
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
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);
//调用服务接口下发配置数据
if(isUpdateCfg) {
// logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* 单域配置批量下发
* @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 auditSingleMaatData(int cfgType,
List<Map<String,Object>>userRegionList,
Page<T> page,
BaseCfg entity,
List<T> list,
boolean hasData,
boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
maatBean = new ToMaatBean();
configCompileList = new ArrayList();
List<Integer> ids = new ArrayList();
//批量获取regionId,groupId
List<Integer> regionIds = ConfigServiceUtil.getId(3, list.size());
List<Integer> groupIds = ConfigServiceUtil.getId(2, list.size());
IpPortCfg ipCfg = new IpPortCfg();
BaseStringCfg strCfg = new BaseStringCfg();
ComplexkeywordCfg complexStrCfg = new ComplexkeywordCfg();
for(int i=0;i<list.size();i++){
BaseCfg cfg = new BaseCfg();
if(cfgType==1){
BeanUtils.copyProperties(list.get(i), ipCfg);
}else if(cfgType==2){
BeanUtils.copyProperties(list.get(i), strCfg);
}else if(cfgType==3){
BeanUtils.copyProperties(list.get(i), complexStrCfg);
}
BeanUtils.copyProperties(list.get(i), cfg);
cfg.setIsValid(entity.getIsValid());
cfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
ids.add(cfg.getCompileId());
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<String,Object> n:userRegionList){
if(entity.getTableName().equals("app_topic_domain_cfg")){
String domain = strCfg.getCfgKeywords();
WebsiteDomainTopic websiteDomainTopic = new WebsiteDomainTopic();
websiteDomainTopic.setDomain(domain);
//查询domainId
List<WebsiteDomainTopic> domainDict = appCfgDao.getDomainDict(websiteDomainTopic);
Long domainId = domainDict.get(0).getId();
userRegion += n.get("regionKey")+"="+domainId+Constants.USER_REGION_SPLIT;
}else{
Object userRegionPosition = n.get("userRegionPosition");
if(userRegionPosition!=null){
//通过反射机制获取自定义域字段值
String regionColumn = n.get("regionColumn").toString();
Class aClass = null;
if(userRegionPosition.toString().equals(("0"))){
aClass = BaseCfg.class;
}else{
if(cfgType==1){
aClass = IpPortCfg.class;
}else if(cfgType==2){
aClass = BaseStringCfg.class;
}else if(cfgType==3){
aClass = ComplexkeywordCfg.class;
}
}
Object value = "";
Field field = aClass.getDeclaredField(regionColumn);
field.setAccessible(true);
if(cfgType==1){
value = field.get(ipCfg);
}else if(cfgType==2){
value = field.get(strCfg);
}else if(cfgType==3){
value = field.get(complexStrCfg);
}
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);
}
}
if(cfgType==1){
ipCfg.setRegionId(regionIds.get(0));
regionIds.remove(0);
ipCfg.setGroupId(groupIds.get(0));
ipCfg.setIsValid(entity.getIsValid());
ipCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
groupIds.remove(0);
list1.add(ipCfg);
}else if(cfgType==2){
strCfg.setRegionId(regionIds.get(0));
regionIds.remove(0);
strCfg.setGroupId(groupIds.get(0));
strCfg.setIsValid(entity.getIsValid());
strCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
groupIds.remove(0);
list1.add(strCfg);
}else if(cfgType==3){
complexStrCfg.setRegionId(regionIds.get(0));
regionIds.remove(0);
complexStrCfg.setGroupId(groupIds.get(0));
complexStrCfg.setIsValid(entity.getIsValid());
complexStrCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
groupIds.remove(0);
list1.add(complexStrCfg);
}
if(list1.size()>0){
if(cfgType==1){
Map<String,List> map = cfgToMaatConvert(ipRegionList,list1,1,groupRelationList);
groupRelationList=map.get("groupList");
ipRegionList=map.get("dstList");
if(map.get("numRegionList")!=null){
numRegionList.addAll(map.get("numRegionList"));
}
}else if(cfgType==2||cfgType==3){
Map<String,List> map = cfgToMaatConvert(strRegionList,list1,2,groupRelationList);
groupRelationList=map.get("groupList");
strRegionList=map.get("dstList");
}
}
BeanUtils.copyProperties(cfg, maatCfg);
maatCfg.setAction(cfg.getAction());
maatCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.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(entity.getIsValid());
if(!StringUtil.isEmpty(userRegion)){
maatCfg.setUserRegion(userRegion);
}
configCompileList.add(maatCfg);
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(ids) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( entity.getTableName(), entity,ids,null);
}
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
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);
//调用服务接口下发配置数据
if(isUpdateCfg) {
// logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* 处理app ip特征分组复用配置
* 全量下发(全量下发接口只支持maat和回调类的json字符串格式)
* 批量下发【按照config group每组每批次下发一次之下发一种格式的配置maat/reuse,CompileIsIssued标识】
* @param serviceId
* @param request
* @param response
*/
public boolean auditAppIpData(List<Map<String,Object>>userRegionList,
Page<T> page,
BaseCfg entity,
List<T> list,
boolean hasData,
boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
GroupReuseAddBean reuseMaatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
maatBean = new ToMaatBean();
reuseMaatBean = new GroupReuseAddBean();
configCompileList = new ArrayList();
List<Integer> regionIds= new ArrayList();//存储app ip域id
List<Integer> compileIds= new ArrayList();//存储app compileId
Integer isIssued=entity.getCompileIsIssued();//批量下发时使用,需要用来判断是否走分组复用接口
//同一编译IP的分组
Map<Integer,List<AppIpCfg>> dataMap=new HashMap<>();
Integer compileId=entity.getCompileId();
Integer groupId=entity.getGroupId();
compileIds.add(compileId);
List<AppIpCfg> ipList=new ArrayList<>();
for(int i=0;i<list.size();i++){
AppIpCfg cfg = new AppIpCfg();
BeanUtils.copyProperties(list.get(i), cfg);
cfg.setIsValid(entity.getIsValid());
cfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
cfg.setGroupId(groupId);
ipList.add(cfg);
regionIds.add(Integer.parseInt(cfg.getUserRegion1()));
}
dataMap.put(compileId, ipList);
for(List<AppIpCfg> entitys:dataMap.values()) {
maatCfg = new MaatCfg();
reuseMaatBean = new GroupReuseAddBean();
maatCfg.initDefaultValue();
groupRelationList = new ArrayList();
ipRegionList = new ArrayList();
strRegionList = new ArrayList();
numRegionList = new ArrayList();
digestRegionList = new ArrayList();
areaIpRegionList = new ArrayList();
if(isUpdateCfg && isIssued.equals(1)) {
List<GroupReuseCfg> groupReuseList=new ArrayList<>();
GroupReuseCfg groupReuseCfg=new GroupReuseCfg();
/*groupReuseCfg.setUserRegion(userRegion);*/
groupReuseCfg.setServiceId(entitys.get(0).getServiceId());
ipRegionList.addAll(groupReuseCfgAddRemoveConvert(entitys,entity.getIsValid(),entitys.get(0).getGroupId()));
groupReuseCfg.setIpRegionList(ipRegionList);
groupReuseCfg.setStrRegionList(strRegionList);
groupReuseCfg.setNumRegionList(numRegionList);
groupReuseList.add(groupReuseCfg);
reuseMaatBean.setGroupReuseCfgList(groupReuseList);
reuseMaatBean.setAuditTime(StringUtil.isEmpty(entity.getAuditTime())?entitys.get(0).getAuditTime():entity.getAuditTime());
reuseMaatBean.setCreatorName(entitys.get(0).getCurrentUser().getName());
reuseMaatBean.setVersion(Constants.MAAT_VERSION);
reuseMaatBean.setOpAction(Constants.INSERT_ACTION);
}else {
GroupCfg groupCfg=new GroupCfg();
groupCfg.setCompileId(entitys.get(0).getCompileId());
groupCfg.setGroupId(entitys.get(0).getGroupId());
groupCfg.setIsValid(Constants.VALID_YES);
groupCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime())?entitys.get(0).getAuditTime():entity.getAuditTime());
groupRelationList.add(groupCfg);
BeanUtils.copyProperties(entitys.get(0), maatCfg);
ipRegionList.addAll(groupReuseCfgAddRemoveConvert(entitys,Constants.VALID_YES,entitys.get(0).getGroupId()));
maatCfg.setAction(entitys.get(0).getAction());
maatCfg.setAuditTime(entitys.get(0).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(entitys.get(0).getIsValid());
// 设置APP自定义域
String userRegion = "APP_ID=" + entitys.get(0).getAppCode()/*+Constants.USER_REGION_SPLIT+"ASEV_ID="+businessType*/;
maatCfg.setUserRegion(userRegion);
configCompileList.add(maatCfg);
maatBean.setConfigCompileList(configCompileList);
maatBean.setAuditTime(entitys.get(0).getAuditTime());
maatBean.setCreatorName(entitys.get(0).getCurrentUser().getName());
maatBean.setVersion(Constants.MAAT_VERSION);
maatBean.setOpAction(Constants.INSERT_ACTION);
}
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(regionIds)) {
commonPolicyDao.auditCfgBatch(entity.getTableName(), entity, regionIds, null);
}
if(isIssued.equals(0)) {//group第一次下发需要修改状态为已下发
if(!StringUtil.isEmpty(compileIds)) {
commonPolicyDao.auditCfgBatch("config_group_info", entity, compileIds, null);
}
}
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
hasData = false;
}
if(!StringUtil.isEmpty(configCompileList.size())){
String json=gsonToJson(maatBean);
//调用服务接口下发配置数据
if(isUpdateCfg) {
if(isIssued.equals(1)) {//已下发过的compile或配置取消直接走ip复用接口
json=gsonToJson(reuseMaatBean);
//logger.info("app ip批量生效"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postGroupReuseSources(json);
if(result!=null){
logger.info("app ip批量生效响应信息"+result.getMsg());
}
}else {//未下发过的compile直接走正常maat配置接口
//logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+compileId+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* 处理ASN ip配置
* 全量下发(全量下发接口只支持maat和回调类的json字符串格式)
* 批量下发【按照asn group每组每批次下发一次之下发一种格式的配置maat/reuse,CompileIsIssued标识】
* @param serviceId
* @param request
* @param response
*/
public boolean auditAsnIpData(List<Map<String,Object>>userRegionList,
Page<T> page,
BaseCfg entity,
List<T> list,
boolean hasData,
boolean isUpdateCfg) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
GroupReuseAddBean reuseMaatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
maatBean = new ToMaatBean();
reuseMaatBean = new GroupReuseAddBean();
configCompileList = new ArrayList();
List<Integer> regionIds= new ArrayList();//存储app ip域id
List<Integer> compileIds= new ArrayList();//存储app ip域id
Integer isIssued=entity.getCompileIsIssued();//批量下发时使用,需要用来判断是否走分组复用接口
//同一编译IP的分组
Map<Integer,List<AsnIpCfg>> dataMap=new HashMap<>();
Integer compileId=entity.getCompileId();
Integer groupId=entity.getGroupId();
compileIds.add(compileId);
List<AsnIpCfg> ipList=new ArrayList<>();
for(int i=0;i<list.size();i++){
AsnIpCfg cfg = new AsnIpCfg();
BeanUtils.copyProperties(list.get(i), cfg);
cfg.setIsValid(entity.getIsValid());
cfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.getAuditTime());
cfg.setGroupId(groupId);
regionIds.add(cfg.getRegionId());
ipList.add(cfg);
}
dataMap.put(compileId, ipList);
for(List<AsnIpCfg> entitys:dataMap.values()) {
maatCfg = new MaatCfg();
reuseMaatBean = new GroupReuseAddBean();
maatCfg.initDefaultValue();
groupRelationList = new ArrayList();
ipRegionList = new ArrayList();
strRegionList = new ArrayList();
numRegionList = new ArrayList();
digestRegionList = new ArrayList();
areaIpRegionList = new ArrayList();
//批量配置生效,当前组已下发过
if(isUpdateCfg && isIssued.equals(1)) {
List<GroupReuseCfg> groupReuseList=new ArrayList<>();
GroupReuseCfg groupReuseCfg=new GroupReuseCfg();
/*groupReuseCfg.setUserRegion(userRegion);*/
groupReuseCfg.setServiceId(entitys.get(0).getServiceId());
ipRegionList.addAll(groupReuseCfgAddRemoveConvert(entitys,entity.getIsValid(),entitys.get(0).getGroupId()));
groupReuseCfg.setIpRegionList(ipRegionList);
groupReuseCfg.setStrRegionList(strRegionList);
groupReuseCfg.setNumRegionList(numRegionList);
groupReuseList.add(groupReuseCfg);
reuseMaatBean.setGroupReuseCfgList(groupReuseList);
reuseMaatBean.setAuditTime(StringUtil.isEmpty(entity.getAuditTime())?entitys.get(0).getAuditTime():entity.getAuditTime());
reuseMaatBean.setCreatorName(entitys.get(0).getCurrentUser().getName());
reuseMaatBean.setVersion(Constants.MAAT_VERSION);
reuseMaatBean.setOpAction(Constants.INSERT_ACTION);
}else {
GroupCfg groupCfg=new GroupCfg();
groupCfg.setCompileId(entitys.get(0).getCompileId());
groupCfg.setGroupId(entitys.get(0).getGroupId());
groupCfg.setIsValid(Constants.VALID_YES);
groupCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime())?entitys.get(0).getAuditTime():entity.getAuditTime());
groupRelationList.add(groupCfg);
BeanUtils.copyProperties(entitys.get(0), maatCfg);
ipRegionList.addAll(groupReuseCfgAddRemoveConvert(entitys,Constants.VALID_YES,entitys.get(0).getGroupId()));
maatCfg.setAction(entitys.get(0).getAction());
maatCfg.setAuditTime(entitys.get(0).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(entitys.get(0).getIsValid());
// 设置asn自定义域
String userRegion = "ASN_ID=AS" + entitys.get(0).getUserRegion1()/*+Constants.USER_REGION_SPLIT+"ASEV_ID="+businessType*/;
maatCfg.setUserRegion(userRegion);
configCompileList.add(maatCfg);
maatBean.setConfigCompileList(configCompileList);
maatBean.setAuditTime(entitys.get(0).getAuditTime());
maatBean.setCreatorName(entitys.get(0).getCurrentUser().getName());
maatBean.setVersion(Constants.MAAT_VERSION);
maatBean.setOpAction(Constants.INSERT_ACTION);
}
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(regionIds)) {
commonPolicyDao.auditCfgBatch(entity.getTableName(), entity, regionIds, null);
}
if(isIssued.equals(0)) {//group第一次下发需要修改状态为已下发
if(!StringUtil.isEmpty(compileIds)) {
commonPolicyDao.auditCfgBatch("asn_group_info", entity, compileIds, null);
}
}
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
hasData = false;
}
if(!StringUtil.isEmpty(configCompileList.size())){
String json=gsonToJson(maatBean);
//调用服务接口下发配置数据
if(isUpdateCfg) {
if(isIssued.equals(1)) {//已下发过的compile或配置取消直接走ip复用接口
json=gsonToJson(reuseMaatBean);
//logger.info("app ip批量生效"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postGroupReuseSources(json);
if(result!=null){
logger.info("app ip批量生效响应信息"+result.getMsg());
}
}else {//未下发过的compile直接走正常maat配置接口
//logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+compileId+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* 处理回调类业务配置
* @param serviceId
* @param request
* @param response
* @throws ClassNotFoundException
*/
public void handleCallbackData(String className,
Page<BaseCfg> page,BaseCfg entity,HttpServletRequest request,HttpServletResponse response,
boolean isUpdateCfg) throws ClassNotFoundException{
boolean hasData = true;
int index=0;
while(hasData){
entity.setPage(page);
hasData=auditCallbackData(className,page,entity,hasData,isUpdateCfg);
if(hasData){
page.setPageNo(page.getNext());
}
//此业务无数据需同步,也许向服务端发送一个{}串
if(index ==0 && StringUtil.isEmpty(page.getList()) && !isUpdateCfg){
String json = "{}";
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(h nodata).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,2,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
index++;
}
}
/**
* 回调类配置下发
* @param className
* @param page
* @param entity
* @param hasData
* @param isUpdateCfg
* @throws ClassNotFoundException
*/
public boolean auditCallbackData(String className,
Page<BaseCfg> page,BaseCfg entity,
boolean hasData,
boolean isUpdateCfg) throws ClassNotFoundException{
ToMaatBean maatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
maatBean = new ToMaatBean();
configCompileList = new ArrayList();
List list = new ArrayList();
List newList = new ArrayList();
List ids = new ArrayList();
if(entity.getServiceId().equals(3)){//ip drop回调类配置用了主表和子表关系
list = configSynchronizationDao.getCfgIndexList(entity);
if(!StringUtil.isEmpty(list)){
List<Integer> compileIds = Lists.newArrayList();
for(int i=0;i<list.size();i++){
CfgIndexInfo cfg = (CfgIndexInfo) list.get(i);
compileIds.add(cfg.getCompileId());
}
List subList = configSynchronizationDao.getIpDropList("ip_port_cfg", compileIds);
for(int i=0;i<subList.size();i++){
IpPortCfg cfg = (IpPortCfg) subList.get(i);
newList.add(convertCallBackIp(cfg,cfg.getGroupId()));
}
}
}else{
if(className.equals("AvFileSampleCfg")){
list = configSynchronizationDao.getAvFileCfgList(entity);
newList.addAll(list);
}else if(className.equals("AvSignSampleCfg")){
list = configSynchronizationDao.getAvSignCfgList(entity);
newList.addAll(list);
}else if(className.equals("PxyObjKeyring")){
list = configSynchronizationDao.getPxyObjKeyringCfgList(entity);
for(int i=0;i<list.size();i++){
PxyObjKeyring cfg = (PxyObjKeyring) list.get(i);
newList.add(convertCallBackProxyObjKeyring(cfg));
}
}else if(className.equals("PxyObjTrustedCaCert")){
//下发cert配置时需绑定下发crl配置
list = configSynchronizationDao.getPxyObjTrustedCertCfgList(entity);
for(int i=0;i<list.size();i++){
PxyObjTrustedCaCert cfg = (PxyObjTrustedCaCert) list.get(i);
newList.addAll(convertCallBackProxyObjTrustedCa(cfg,null));
ids.add(cfg.getCompileId());
}
if(!StringUtil.isEmpty(ids)){
list=new ArrayList<>();
list=configSynchronizationDao.getPxyObjTrustedCrlCfgListByCertId(ids);
for(int i=0;i<list.size();i++){
PxyObjTrustedCaCrl cfg = (PxyObjTrustedCaCrl) list.get(i);
newList.addAll(convertCallBackProxyObjTrustedCa(null,cfg));
}
}
}else if(className.equals("PxyObjTrustedCaCrl")){
ids=new ArrayList<>();
//只允许单独下发certId为空或0的crl配置
list = configSynchronizationDao.getPxyObjTrustedCrlCfgList(entity);
for(int i=0;i<list.size();i++){
PxyObjTrustedCaCrl cfg = (PxyObjTrustedCaCrl) list.get(i);
newList.addAll(convertCallBackProxyObjTrustedCa(null,cfg));
}
}else if(className.equals("DnsResStrategy")){
list = configSynchronizationDao.getDnsStrategyList(entity);
for(int i=0;i<list.size();i++){
DnsResStrategy cfg = (DnsResStrategy) list.get(i);
newList.add(convertCallBackDnsResStrategy(cfg));
}
}else if(className.equals("DnsIpCfg")){
list = configSynchronizationDao.getDnsIpCfgList(entity);
for(int i=0;i<list.size();i++){
DnsIpCfg cfg = (DnsIpCfg) list.get(i);
newList.add(convertCallBackIp(cfg,cfg.getDnsStrategyId()));
}
}else if(className.equals("IpPortCfg")){
list = configSynchronizationDao.getIpPortListByService(entity);
for(int i=0;i<list.size();i++){
IpPortCfg cfg = (IpPortCfg) list.get(i);
newList.add(convertCallBackIp(cfg,cfg.getGroupId()));
}
}else if(className.equals("PxyObjSpoofingIpPool")){
list = configSynchronizationDao.getSpoofingIpPoolList(entity);
newList.addAll(list);
}
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
hasData = false;
}
if(newList.size()>0){
//调用服务接口下发配置数据
String json=gsonToJson(newList);
if(isUpdateCfg) {
// logger.info("配置批量下发:"+json);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}else {
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
//调用服务接口同步回调类配置
isFinished = ((!hasData)&&lastServiceTag)?true:false;
JSONObject result = ConfigServiceUtil.configSync(json,2,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
/**
* 配置域转换
* @param ipList
* @param strList
* @param complexStrList
* @param numList
* @param fileList
* @param regionIds
* @param groupIds
* @param entity
* @param compileId
* @param auditTime
* @param groupRelationList
* @param ipRegionList
* @param strRegionList
* @param numRegionList
* @param digestRegionList
* @param areaIpRegionList
* @param userRegion
*/
public String configCovert(List<IpPortCfg> ipList,List<BaseStringCfg> strList,List<ComplexkeywordCfg> complexStrList,
List<com.nis.domain.configuration.NumBoundaryCfg > numList,
List<FileDigestCfg> fileList,List<Integer> regionIds,List<Integer> groupIds,BaseCfg entity,
Integer compileId,Date auditTime,List<GroupCfg> groupRelationList,
List<IpCfg> ipRegionList,
List<StringCfg> strRegionList,
List<NumBoundaryCfg> numRegionList,
List<DigestCfg> digestRegionList,
List<IpCfg> areaIpRegionList,String userRegion){
List list1 = new ArrayList();
List<BaseStringCfg> list2 = new ArrayList();
List<ComplexkeywordCfg> list3 = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> list4 = new ArrayList();
List<FileDigestCfg> list5 = new ArrayList();
if(ipList.size()>0){
for(int index=0;index<ipList.size();index++){
IpPortCfg ip = ipList.get(index);
if(ip.getCompileId().equals(compileId)){
ip.setRegionId(regionIds.get(0));
regionIds.remove(0);
ip.setGroupId(groupIds.get(0));
groupIds.remove(0);
ip.setIsValid(entity.getIsValid());
ip.setAuditTime(auditTime);
list1.add(ip);
}
}
}
for(int index=0;index<strList.size();index++){
BaseStringCfg str = strList.get(index);
if(str.getCompileId().equals(compileId)){
str.setRegionId(regionIds.get(0));
regionIds.remove(0);
str.setGroupId(groupIds.get(0));
groupIds.remove(0);
str.setIsValid(entity.getIsValid());
str.setAuditTime(auditTime);
list2.add(str);
if(entity.getServiceId().equals(513)||entity.getServiceId().equals(515)){
if(userRegion.equals("")){
userRegion += Constants.USERREGION_DOMAIN_STR+"="+str.getCfgKeywords();
}else{
userRegion += Constants.USER_REGION_SPLIT+Constants.USERREGION_DOMAIN_STR+"="+str.getCfgKeywords();
}
}
}
}
for(int index=0;index<complexStrList.size();index++){
ComplexkeywordCfg str = complexStrList.get(index);
if(str.getCompileId().equals(compileId)){
str.setRegionId(regionIds.get(0));
regionIds.remove(0);
str.setGroupId(groupIds.get(0));
groupIds.remove(0);
str.setIsValid(entity.getIsValid());
str.setAuditTime(auditTime);
list3.add(str);
if(entity.getServiceId().equals(129) && str.getDistrict()!=null){//http监测
String dictValue = DictUtils.getDictCode(Constants.HTTP_HEADER_DICT_MODULE, str.getDistrict());
if(StringUtil.isEmpty(dictValue) || dictValue.equals("默认")){
if(userRegion.equals("")){
userRegion += Constants.HTTP_HEADER_USER_REGION_KEY+"="+str.getDistrict();
}else{
userRegion += Constants.USER_REGION_SPLIT+Constants.HTTP_HEADER_USER_REGION_KEY+"="+str.getDistrict();
}
}
}
}
}
for(int index=0;index<numList.size();index++){
com.nis.domain.configuration.NumBoundaryCfg num = numList.get(index);
if(num.getCompileId().equals(compileId)){
num.setRegionId(regionIds.get(0));
regionIds.remove(0);
num.setGroupId(groupIds.get(0));
groupIds.remove(0);
num.setIsValid(entity.getIsValid());
num.setAuditTime(auditTime);
list4.add(num);
}
}
for(int index=0;index<fileList.size();index++){
FileDigestCfg file = fileList.get(index);
if(file.getCompileId().equals(compileId)){
file.setRegionId(regionIds.get(0));
regionIds.remove(0);
file.setGroupId(groupIds.get(0));
groupIds.remove(0);
file.setIsValid(entity.getIsValid());
file.setAuditTime(auditTime);
list5.add(file);
}
}
if(list1.size()>0){
ipList.removeAll(list1);
Map<String,List> map = cfgToMaatConvert(ipRegionList,list1,1,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<String,List> map = cfgToMaatConvert(strRegionList,list2,2,groupRelationList);
groupRelationList=map.get("groupList");
strRegionList=map.get("dstList");
}
if(list3.size()>0){
complexStrList.removeAll(list3);
Map<String,List> map = cfgToMaatConvert(strRegionList,list3,3,groupRelationList);
groupRelationList=map.get("groupList");
strRegionList=map.get("dstList");
}
if(list4.size()>0){
numList.removeAll(list4);
Map<String,List> map = cfgToMaatConvert(numRegionList,list4,4,groupRelationList);
groupRelationList=map.get("groupList");
numRegionList=map.get("dstList");
}
if(list5.size()>0){
fileList.removeAll(list5);
Map<String,List> map = cfgToMaatConvert(digestRegionList,list5,5,groupRelationList);
groupRelationList=map.get("groupList");
digestRegionList=map.get("dstList");
}
return userRegion;
}
/**
* 处理Obj group list
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public void handleObjGroupListMaatData(Map<Integer,List<Map<String,Object>>> cfgMap,Map<Integer,List<Map<String,Object>>> userRegionMap,
Page<T> page,BaseCfg entity,HttpServletRequest request,HttpServletResponse response,
boolean isUpdateCfg,String tableName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
boolean hasData = true;
int index=0;
while(hasData){
entity.setPage(page);
List list = Lists.newArrayList();
if("cfg_index_info".equals(tableName)){
int ind=0;
for(Map.Entry e:cfgMap.entrySet()){
entity.setServiceId((Integer) e.getKey());
list = configSynchronizationDao.getCfgIndexList(entity);
if(CollectionUtils.isNotEmpty(list)){
hasData=auditObjGroupListMaatData(cfgMap,userRegionMap,page,entity,list,hasData,isUpdateCfg);
if(hasData) {
page.setPageNo(page.getNext());
}
}else{
hasData = false;
}
ind++;
}
}else{
logger.info("全量同步未知业务");
hasData = false;
}
//此业务无数据需同步,也许向服务端发送一个{}串
if(index ==0 && StringUtil.isEmpty(list) && !isUpdateCfg){
String json = "{}";
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+System.currentTimeMillis()+"(m nodata).json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),entity.getTableName(),null);
logger.info("全量下发响应信息:"+result.toString());
}
index++;
}
}
/**
* Obj Group List 配置批量下发
* @param cfgMap
* @param userRegionMap
* @param page
* @param entity
* @param list
* @param hasData
* @param isUpdateCfg 业务配置全部生效时需同步更新库表配置状态
* @return
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public boolean auditObjGroupListMaatData(Map<Integer,List<Map<String,Object>>> cfgMap,
Map<Integer,List<Map<String,Object>>> userRegionMap,
Page page,
BaseCfg entity,
List<CfgIndexInfo> list,
boolean hasData,
boolean isUpdateCfg)throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
ToMaatBean maatBean;
MaatCfg maatCfg;
List<MaatCfg> configCompileList;
List<GroupCfg> groupRelationList;
List<IpCfg> ipRegionList;
List<StringCfg> strRegionList;
List<NumBoundaryCfg> numRegionList;
List<DigestCfg> digestRegionList;
List<IpCfg> areaIpRegionList;
List<IpPortCfg> ipList = new ArrayList();
List<AsnIpCfg> asnIpList = new ArrayList();
List<BaseStringCfg> strList = new ArrayList();
List<ComplexkeywordCfg> complexStrList = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> numList = new ArrayList();
List<FileDigestCfg> fileList = new ArrayList();
maatBean = new ToMaatBean();
configCompileList = new ArrayList();
List<Integer> compileIds = new ArrayList();
List<IpCommCfg> ipCommCfgList=new ArrayList<>();
List<ScriberIdCommCfg> scriberIdCommCfgList=new ArrayList<>();
List<UrlCommCfg> urlCommCfgList=new ArrayList<>();
List<DomainCommCfg> domainCommCfgList=new ArrayList<>();
for(CfgIndexInfo cfg:list){
//查询子配置
String commonGroupIds=cfg.getCommonGroupIds();
if(StringUtils.isNotBlank(commonGroupIds)){
Map<String,Object> groupMap=ConfigConvertUtil.gsonFromJson(commonGroupIds,Map.class);
if(groupMap.containsKey("ipGroup")){
IpCommCfg commCfg=new IpCommCfg();
commCfg.setCommonGroupIds(groupMap.get("ipGroup").toString().substring(1,groupMap.get("ipGroup").toString().length()-1));
List<IpCommCfg> commIps=ipCommGroupCfgDao.findAllList(commCfg);
if(CollectionUtils.isNotEmpty(commIps)){
cfg.setIpCommGroupCfgList(commIps);
}
}
if(groupMap.containsKey("subscribeIdGroup")){
ScriberIdCommCfg commCfg=new ScriberIdCommCfg();
commCfg.setCommonGroupIds(groupMap.get("subscribeIdGroup").toString().substring(1,groupMap.get("subscribeIdGroup").toString().length()-1));
List<ScriberIdCommCfg> commIds=scriberIdCommGroupDao.findAllList(commCfg);
if(CollectionUtils.isNotEmpty(commIds)){
cfg.setScriberIdCommGroupList(commIds);
}
}
if(groupMap.containsKey("urlGroup")){
UrlCommCfg commCfg=new UrlCommCfg();
commCfg.setCommonGroupIds(groupMap.get("urlGroup").toString().substring(1,groupMap.get("urlGroup").toString().length()-1));
List<UrlCommCfg> commIds=urlCommGroupDao.findAllList(commCfg);
if(CollectionUtils.isNotEmpty(commIds)){
cfg.setUrlCommGroupList(commIds);
}
}
if(groupMap.containsKey("domainGroup")){
DomainCommCfg commCfg=new DomainCommCfg();
commCfg.setCommonGroupIds(groupMap.get("domainGroup").toString().substring(1,groupMap.get("domainGroup").toString().length()-1));
List<DomainCommCfg> commIds=domainCommGroupDao.findAllList(commCfg);
if(CollectionUtils.isNotEmpty(commIds)){
cfg.setDomainCommGroupList(commIds);
}
}
compileIds.add(cfg.getCompileId());
}
}
if(isUpdateCfg) {
if(!StringUtil.isEmpty(compileIds) && !StringUtil.isEmpty(entity.getTableName())) {
commonPolicyDao.auditCfgBatch( entity.getTableName(), entity,compileIds,null);
}
}
Map<Integer,FunctionServiceDict> serviceDictMap=ConfigConvertUtil.objGetServiceDict(entity.getFunctionId());
//批量获取regionId,groupId(相同编译下的IP类配置多条ip只获取一个组号分组复用的域配置不需要重新获取regionId,groupId
// List<Integer> regionIds = ConfigServiceUtil.getId(3, ipList.size()+strList.size()+complexStrList.size()+numList.size()+fileList.size());
// List<Integer> groupIds = ConfigServiceUtil.getId(2, ipList.size()+strList.size()+complexStrList.size()+numList.size()+fileList.size());
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<BaseStringCfg> list2 = new ArrayList();
List<ComplexkeywordCfg> list3 = new ArrayList();
List<com.nis.domain.configuration.NumBoundaryCfg> list4 = new ArrayList();
List<FileDigestCfg> list5 = new ArrayList();
StringBuffer userRegion = new StringBuffer();
if(StringUtils.isBlank(cfg.getCommonGroupIds())){
continue;
}
//处理自定义域
if(userRegionMap.containsKey(cfg.getServiceId())){
List<Map<String,Object>> userRegionList=userRegionMap.get(cfg.getServiceId());
userRegion.append(ConfigConvertUtil.generateCommonGroupDefaultUserRegion(null,cfg.getServiceId()));
}
//Intercept Policy、http(s) 监测 、http(s) 白名单
if("HTTPS".equalsIgnoreCase(cfg.getUserRegion1())||"INTERCEPT".equalsIgnoreCase(cfg.getUserRegion1())&&userRegion.toString().length()==0) {
userRegion.append("{}");
}
//子配置
if(cfgMap.containsKey(cfg.getServiceId())){
List<Map<String,Object>> cfgList=(List<Map<String,Object>>)cfgMap.get(cfg.getServiceId());
Map<String,Object> maatTableMap=ConfigConvertUtil.convertCommonGroupMaatTable(cfg,cfgList);
Set<Integer> groupIdSet=new HashSet<>();
//多线程转换配置
ObjGroupCfgConvert ipConvert=new ObjGroupCfgConvert("ipGroup",cfg,1,serviceDictMap,maatTableMap,groupIdSet);
ipConvert.setGroupRelationList(groupRelationList);
ipConvert.setIpRegionList(ipRegionList);
ipConvert.setNumRegionList(numRegionList);
ipConvert.setStrRegionList(strRegionList);
ipConvert.setIsValid(entity.getIsValid());
ipConvert.setAuditTime(entity.getAuditTime());
ObjGroupCfgConvert subscribeIdConvert=new ObjGroupCfgConvert("subscribeIdGroup",cfg,1,serviceDictMap,maatTableMap,groupIdSet);
subscribeIdConvert.setGroupRelationList(groupRelationList);
subscribeIdConvert.setIpRegionList(ipRegionList);
subscribeIdConvert.setNumRegionList(numRegionList);
subscribeIdConvert.setStrRegionList(strRegionList);
subscribeIdConvert.setIsValid(entity.getIsValid());
subscribeIdConvert.setAuditTime(entity.getAuditTime());
ObjGroupCfgConvert urlConvert=new ObjGroupCfgConvert("urlGroup",cfg,1,serviceDictMap,maatTableMap,groupIdSet);
urlConvert.setGroupRelationList(groupRelationList);
urlConvert.setIpRegionList(ipRegionList);
urlConvert.setNumRegionList(numRegionList);
urlConvert.setStrRegionList(strRegionList);
urlConvert.setIsValid(entity.getIsValid());
urlConvert.setAuditTime(entity.getAuditTime());
ObjGroupCfgConvert domainConvert=new ObjGroupCfgConvert("domainGroup",cfg,1,serviceDictMap,maatTableMap,groupIdSet);
domainConvert.setGroupRelationList(groupRelationList);
domainConvert.setIpRegionList(ipRegionList);
domainConvert.setNumRegionList(numRegionList);
domainConvert.setStrRegionList(strRegionList);
domainConvert.setIsValid(entity.getIsValid());
domainConvert.setAuditTime(entity.getAuditTime());
Thread t1=new Thread(ipConvert);
Thread t2=new Thread(subscribeIdConvert);
Thread t3=new Thread(urlConvert);
Thread t4=new Thread(domainConvert);
t1.start();
t2.start();
t3.start();
t4.start();
try {
t1.join();
t2.join();
t3.join();
t4.join();
} catch (InterruptedException e) {
logger.error("多线程join异常",e);
}
//IP公共分组
// if(CollectionUtils.isNotEmpty(cfg.getIpCommGroupCfgList())){
// IpPortCfg _cfg = new IpPortCfg();
// BeanUtils.copyProperties(cfg, _cfg, new String[]{"cfgId"});
// _cfg.setIsValid(entity.getIsValid());
// _cfg.setIsAudit(entity.getIsAudit());
// _cfg.setAuditTime(entity.getAuditTime());
// Map<String,List> map = ConfigConvertUtil.objGroupCfgConvert(serviceDictMap,ipRegionList,cfg.getIpCommGroupCfgList(),1,_cfg,groupRelationList,maatTableMap,groupIdSet);
// groupRelationList=map.get("groupList");
// ipRegionList=map.get("dstList");
// if(map.get("numRegionList")!=null){
// numRegionList.addAll(map.get("numRegionList"));
// }
//
// }
//URL公共分组
// if(CollectionUtils.isNotEmpty(cfg.getUrlCommGroupList())){
// CommonStringCfg _cfg = new CommonStringCfg();
// BeanUtils.copyProperties(cfg, _cfg, new String[]{"cfgId"});
// _cfg.setIsValid(entity.getIsValid());
// _cfg.setIsAudit(entity.getIsAudit());
// _cfg.setAuditTime(entity.getAuditTime());
// Map<String,List> map =ConfigConvertUtil.objGroupCfgConvert(serviceDictMap,strRegionList,cfg.getUrlCommGroupList(),2,_cfg,groupRelationList,maatTableMap,groupIdSet);
// groupRelationList=map.get("groupList");
// strRegionList=map.get("dstList");
// }
//账号公共分组
// if(CollectionUtils.isNotEmpty(cfg.getScriberIdCommGroupList())){
// CommonStringCfg _cfg = new CommonStringCfg();
// BeanUtils.copyProperties(cfg, _cfg, new String[]{"cfgId"});
// _cfg.setIsValid(entity.getIsValid());
// _cfg.setIsAudit(entity.getIsAudit());
// _cfg.setAuditTime(entity.getAuditTime());
// Map<String,List> map =ConfigConvertUtil.objGroupCfgConvert(serviceDictMap,strRegionList,cfg.getScriberIdCommGroupList(),2,_cfg,groupRelationList,maatTableMap,groupIdSet);
// groupRelationList=map.get("groupList");
// strRegionList=map.get("dstList");
//
// }
//域名公共分组
// if(CollectionUtils.isNotEmpty(cfg.getDomainCommGroupList())){
// CommonStringCfg _cfg = new CommonStringCfg();
// BeanUtils.copyProperties(cfg, _cfg, new String[]{"cfgId",});
// _cfg.setIsValid(entity.getIsValid());
// _cfg.setIsAudit(entity.getIsAudit());
// _cfg.setAuditTime(entity.getAuditTime());
// Map<String,List> map =ConfigConvertUtil.objGroupCfgConvert(serviceDictMap,strRegionList,cfg.getDomainCommGroupList(),2,_cfg,groupRelationList,maatTableMap,groupIdSet);
// groupRelationList=map.get("groupList");
// strRegionList=map.get("dstList");
// }
BeanUtils.copyProperties(cfg, maatCfg);
if(cfg.getServiceId().equals(512)){
maatCfg.setAction(2);
}else{
maatCfg.setAction(cfg.getAction());
}
maatCfg.setAuditTime(StringUtil.isEmpty(entity.getAuditTime()) ? cfg.getAuditTime():entity.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(entity.getIsValid());
if(!StringUtil.isEmpty(userRegion.toString())){
maatCfg.setUserRegion(userRegion.toString());
}
configCompileList.add(maatCfg);
}
}
page.setList(list);
if(page.getLast()==page.getPageNo()){
hasData = false;
}
if(CollectionUtils.isNotEmpty(configCompileList)){
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);
//调用服务接口下发配置数据
if(isUpdateCfg) {
//logger.info("配置批量下发:"+json);
// FileUtils.writeToFile("D:\\send.json",json,false);
//调用服务接口同步回调类配置
ToMaatResult result = ConfigServiceUtil.postMaatCfg(json);
if(result!=null){
logger.info("配置批量下发响应信息:"+result.getMsg());
}
}else {
//调用服务接口配置全量更新
isFinished = ((!hasData)&&lastServiceTag)?true:false;
FileUtils.writeToFile("/home/ceiec/configSync/"+DateUtils.getDate("yyyy-MM-dd")+"/"+entity.getServiceId()+"_"+page.getPageNo()+"_"+System.currentTimeMillis()+".json", json, false);
JSONObject result = ConfigServiceUtil.configSync(json,1,entity.getServiceId(),null,null);
logger.info("全量下发响应信息:"+result.toString());
}
}
return hasData;
}
}