package com.nis.web.service.configuration; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.google.gson.Gson; import com.nis.domain.Page; import com.nis.domain.callback.InlineIp; import com.nis.domain.configuration.BaseIpCfg; import com.nis.domain.configuration.DdosIpCfg; import com.nis.domain.configuration.IpPortCfg; import com.nis.domain.maat.ToMaatResult; import com.nis.exceptions.MaatConvertException; import com.nis.util.ConfigServiceUtil; import com.nis.util.StringUtil; import com.nis.web.dao.configuration.DdosCfgDao; import com.nis.web.security.UserUtils; import com.nis.web.service.BaseService; /** * ddos配置业务 * @author dell * */ @Service public class DdosCfgService extends BaseService{ @Autowired protected DdosCfgDao ddosCfgDao; public Page findPage(Page page, DdosIpCfg entity) { entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"r")); entity.setPage(page); List list=ddosCfgDao.findPage(entity); page.setList(list); return page; } public DdosIpCfg getDdosIpCfg(Long cfgId) { return ddosCfgDao.getDdosIpCfg(cfgId); } public void saveDdosIpCfg(List ipPortCfgs) { for(BaseIpCfg cfg:ipPortCfgs) { DdosIpCfg ddosIpCfg=new DdosIpCfg(); BeanUtils.copyProperties(cfg, ddosIpCfg); saveOrUpdate(ddosIpCfg); } } @Transactional(readOnly=false,rollbackFor=RuntimeException.class) public void saveOrUpdate(DdosIpCfg entity){ Date createTime=new Date(); //设置区域运营商信息 setAreaEffectiveIds(entity); String antiddosProtocol = entity.getAntiddosProtocol(); Long bpsThreadshold = entity.getBpsThreadshold(); Long ppsThreadshold = entity.getPpsThreadshold(); if(StringUtil.isBlank(antiddosProtocol)) { entity.setAntiddosProtocol(""); } if(bpsThreadshold==null) { entity.setBpsThreadshold(0l); } if(ppsThreadshold==null) { entity.setPpsThreadshold(0l); } //新增 if(entity.getCfgId()==null){ entity.setCreatorId(UserUtils.getUser().getId()); entity.setCreateTime(createTime); entity.setIsValid(0); entity.setIsAudit(0); if(entity.getCompileId()==null||entity.getCompileId().intValue()==0) { //调用服务接口获取compileId List compileIds = new ArrayList(); try { compileIds = ConfigServiceUtil.getId(1,1); } catch (Exception e) { e.printStackTrace(); logger.info("获取编译ID出错"); throw new MaatConvertException(e.getMessage()); } if(compileIds != null && compileIds.size() >0 && compileIds.get(0) != 0){ entity.setCompileId(compileIds.get(0)); }else{ throw new MaatConvertException(""); } } if(entity.getCompileId()!=null&&entity.getCompileId().intValue()>0) { ddosCfgDao.insert(entity); }else { throw new RuntimeException("Could not get compileId!"); } //修改 }else{ Date editTime=new Date(); entity.setIsValid(0); entity.setIsAudit(0); entity.setEditorId(UserUtils.getUser().getId()); entity.setEditTime(editTime); ddosCfgDao.update(entity); } } /** * * @param isAudit * @param isValid * @param ids compileIds */ @Transactional(readOnly=false,rollbackFor=RuntimeException.class) public void delete(Integer isAudit,Integer isValid,String ids,Integer functionId){ String[] idArray = ids.split(","); for(String id :idArray){ DdosIpCfg entity = new DdosIpCfg(); entity.setCfgId(Long.valueOf(id)); entity.setFunctionId(functionId); entity.setIsAudit(isAudit); entity.setIsValid(isValid); entity.setEditorId(UserUtils.getUser().getId()); entity.setEditTime(new Date()); ddosCfgDao.update(entity); } } /** * * @param isAudit * @param isValid * @param ids cfgId * @param functionId */ @Transactional(readOnly=false,rollbackFor=RuntimeException.class) public void audit(Integer isAudit,Integer isValid,Integer functionId,String id,Date auditTime){ DdosIpCfg entity = new DdosIpCfg(); entity=ddosCfgDao.getDdosIpCfg(Long.valueOf(id)); List list = new ArrayList(); entity.setCfgId(Long.valueOf(id)); entity.setIsAudit(isAudit); entity.setIsValid(isValid); entity.setAuditorId(UserUtils.getUser().getId()); entity.setAuditTime(auditTime); ddosCfgDao.update(entity); String antiddosProtocol = entity.getAntiddosProtocol(); Long bpsThreadshold = entity.getBpsThreadshold(); Long ppsThreadshold = entity.getPpsThreadshold(); Map map= new HashMap(); if(StringUtil.isBlank(antiddosProtocol)) { antiddosProtocol=""; } if(bpsThreadshold==null) { bpsThreadshold=0l; } if(ppsThreadshold==null) { ppsThreadshold=0l; } map.put("protocol", antiddosProtocol); map.put("bps_threadshold", bpsThreadshold); map.put("pps_threadshold", ppsThreadshold); String json=""; if(entity.getIsAudit()==1){ List ipList=new ArrayList(); InlineIp resStrategy=convertCallBackIp(entity,null); resStrategy.setUserRegion(new Gson().toJson(map)); ipList.add(resStrategy); //调用服务接口下发配置数据 json=gsonToJson(ipList); logger.info("DDOS IP配置下发配置参数:"+json); //调用服务接口下发配置 try { ToMaatResult result = ConfigServiceUtil.postCallbackCfg(json); if(result!=null){ logger.info("DDOS IP配置下发响应信息:"+result.getMsg()); } } catch (Exception e) { logger.error("DDOS IP配置下发失败",e); throw e; } }else if(entity.getIsAudit()==3){ List resStrategyList=new ArrayList<>(); InlineIp resStrategy=convertCallBackIp(entity,entity.getDnsStrategyId()); resStrategy.setUserRegion(new Gson().toJson(map)); resStrategyList.add(resStrategy); //调用服务接口取消配置 json=gsonToJson(resStrategyList); logger.info("DDOS IP配置参数:"+json); //调用服务接口取消配置 try { ToMaatResult result = ConfigServiceUtil.put(json, 2); logger.info("DDOS IP配置响应信息:"+result.getMsg()); } catch (Exception e) { e.printStackTrace(); logger.info("DDOS IP配置配置失败"); throw e; } } } }