286 lines
9.5 KiB
Java
286 lines
9.5 KiB
Java
|
|
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 javax.servlet.http.HttpServletRequest;
|
|||
|
|
import javax.servlet.http.HttpServletResponse;
|
|||
|
|
|
|||
|
|
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.beust.jcommander.internal.Lists;
|
|||
|
|
import com.google.gson.Gson;
|
|||
|
|
import com.google.gson.GsonBuilder;
|
|||
|
|
import com.nis.domain.Page;
|
|||
|
|
import com.nis.domain.callback.InlineIp;
|
|||
|
|
import com.nis.domain.callback.IpAddrPool;
|
|||
|
|
import com.nis.domain.configuration.BaseIpCfg;
|
|||
|
|
import com.nis.domain.configuration.IpAddrPoolCfg;
|
|||
|
|
import com.nis.domain.configuration.IpMultiplexPoolCfg;
|
|||
|
|
import com.nis.domain.configuration.IpPortCfg;
|
|||
|
|
import com.nis.domain.maat.ToMaatResult;
|
|||
|
|
import com.nis.domain.maat.MaatCfg.IpCfg;
|
|||
|
|
import com.nis.exceptions.MaatConvertException;
|
|||
|
|
import com.nis.util.ConfigServiceUtil;
|
|||
|
|
import com.nis.util.StringUtils;
|
|||
|
|
import com.nis.util.httpclient.HttpClientUtil;
|
|||
|
|
import com.nis.web.dao.configuration.IpAddrPoolCfgDao;
|
|||
|
|
import com.nis.web.security.UserUtils;
|
|||
|
|
import com.nis.web.service.BaseService;
|
|||
|
|
import com.nis.web.service.SpringContextHolder;
|
|||
|
|
import com.nis.web.service.basics.PolicyGroupInfoService;
|
|||
|
|
|
|||
|
|
import jersey.repackaged.com.google.common.collect.Maps;
|
|||
|
|
|
|||
|
|
@Service
|
|||
|
|
public class IpAddrPoolCfgService extends BaseService{
|
|||
|
|
|
|||
|
|
@Autowired
|
|||
|
|
private IpAddrPoolCfgDao ipAddrPoolCfgDao;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 列表分页查询
|
|||
|
|
* @param page
|
|||
|
|
* @param entity
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public Page<IpAddrPoolCfg> findPage(Page<IpAddrPoolCfg> page, IpAddrPoolCfg entity) {
|
|||
|
|
entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"r"));
|
|||
|
|
entity.setPage(page);
|
|||
|
|
List<IpAddrPoolCfg> list=ipAddrPoolCfgDao.findPage(entity);
|
|||
|
|
page.setList(list);
|
|||
|
|
return page;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取数据跳转表单
|
|||
|
|
* @param id
|
|||
|
|
* @param isValid
|
|||
|
|
* @return
|
|||
|
|
*/
|
|||
|
|
public IpAddrPoolCfg getIpAddrPoolCfg(Long id, Integer isValid) {
|
|||
|
|
// 1.获取地址池配置信息
|
|||
|
|
List<IpAddrPoolCfg> list=ipAddrPoolCfgDao.findList(id,isValid,null);
|
|||
|
|
IpAddrPoolCfg addrPoolCfg=null;
|
|||
|
|
if(list != null && list.size()>0){
|
|||
|
|
addrPoolCfg=list.get(0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 2.获取地址池内IP信息
|
|||
|
|
List<BaseIpCfg> ipCfgs = ipAddrPoolCfgDao.getReuseIpCfgs(addrPoolCfg.getAddrPoolId());
|
|||
|
|
if(ipCfgs != null && ipCfgs.size()>0){
|
|||
|
|
addrPoolCfg.setIpCfgs(ipCfgs);
|
|||
|
|
}
|
|||
|
|
return addrPoolCfg;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 更新配置数据
|
|||
|
|
* @param res
|
|||
|
|
* @param req
|
|||
|
|
* @param cfg
|
|||
|
|
*/
|
|||
|
|
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
|||
|
|
public void saveOrUpdate(IpAddrPoolCfg entity, HttpServletRequest req, HttpServletResponse res) {
|
|||
|
|
|
|||
|
|
// 可用IP数(需调用CGI接口查询)
|
|||
|
|
/*Map<String, Object> params = new HashMap<String, Object>();
|
|||
|
|
params.put("cmd", "IpNumGet");
|
|||
|
|
params.put("addr_pool_id", entity.getAddrPoolId());
|
|||
|
|
try {
|
|||
|
|
String resJson = HttpClientUtil.getMsg("", params, req);
|
|||
|
|
} catch (Exception e1) {
|
|||
|
|
e1.printStackTrace();
|
|||
|
|
}*/
|
|||
|
|
|
|||
|
|
// 1.更新配置信息
|
|||
|
|
Date time = new Date();
|
|||
|
|
entity.setIsValid(0);
|
|||
|
|
entity.setIsAudit(0);
|
|||
|
|
setAreaEffectiveIds(entity);
|
|||
|
|
entity.setIpTotal(entity.getIpCfgs().size());
|
|||
|
|
if(entity.getCfgId()==null){
|
|||
|
|
entity.initDefaultValue();
|
|||
|
|
entity.setCreateTime(time);
|
|||
|
|
entity.setCreatorId(UserUtils.getUser().getId());
|
|||
|
|
//调用服务接口获取compileId
|
|||
|
|
try {
|
|||
|
|
List<Integer> compileIds = ConfigServiceUtil.getId(1,1);
|
|||
|
|
if(compileIds != null && compileIds.size() > 0 ){
|
|||
|
|
entity.setCompileId(compileIds.get(0));
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
logger.info("获取编译ID出错");
|
|||
|
|
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>:"+e.getMessage());
|
|||
|
|
}
|
|||
|
|
ipAddrPoolCfgDao.saveAddrPoolCfg(entity);
|
|||
|
|
|
|||
|
|
}else{
|
|||
|
|
entity.setEditorId(UserUtils.getUser().getId());
|
|||
|
|
entity.setEditTime(time);
|
|||
|
|
ipAddrPoolCfgDao.updateAddrPoolCfg(entity);
|
|||
|
|
entity.setCreateTime(new Date());
|
|||
|
|
entity.setCreatorId(entity.getCurrentUser().getId());
|
|||
|
|
}
|
|||
|
|
ipAddrPoolCfgDao.deleteReuseIpCfgs(entity.getAddrPoolId());
|
|||
|
|
|
|||
|
|
// 2.保存IP信息 -> ip_reuse_ip_cfg(该表user_region1字段 存放 地址池ID)
|
|||
|
|
for (BaseIpCfg IpCfg : entity.getIpCfgs()) {
|
|||
|
|
BeanUtils.copyProperties(entity, IpCfg);
|
|||
|
|
IpCfg.setUserRegion1(entity.getAddrPoolId()+"");
|
|||
|
|
ipAddrPoolCfgDao.saveReuseIpCfgs(IpCfg);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
|||
|
|
public void delete(Integer isAudit, Integer isValid, String ids, Integer functionId) {
|
|||
|
|
String[] idArray = ids.split(",");
|
|||
|
|
String groupIds = "";
|
|||
|
|
for(String id :idArray){
|
|||
|
|
IpAddrPoolCfg entity = new IpAddrPoolCfg();
|
|||
|
|
entity.setCfgId(Long.valueOf(id));
|
|||
|
|
entity.setFunctionId(functionId);
|
|||
|
|
entity.setIsAudit(isAudit);
|
|||
|
|
entity.setIsValid(isValid);
|
|||
|
|
entity.setEditorId(UserUtils.getUser().getId());
|
|||
|
|
entity.setEditTime(new Date());
|
|||
|
|
Integer addrPoolId = ipAddrPoolCfgDao.getAddrPoolId(entity.getCfgId());
|
|||
|
|
groupIds += addrPoolId+",";
|
|||
|
|
ipAddrPoolCfgDao.deleteReuseIpCfgs(addrPoolId);
|
|||
|
|
|
|||
|
|
ipAddrPoolCfgDao.updateAddrPoolCfg(entity);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
PolicyGroupInfoService groupInfoService = SpringContextHolder.getBean(PolicyGroupInfoService.class);
|
|||
|
|
groupInfoService.deldete(groupIds, isValid);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 配置审核
|
|||
|
|
* @param isAudit
|
|||
|
|
* @param isValid
|
|||
|
|
* @param functionId
|
|||
|
|
* @param id
|
|||
|
|
* @param auditTime
|
|||
|
|
*/
|
|||
|
|
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
|||
|
|
public void audit(Integer isAudit, Integer isValid, Integer functionId, String id, Date auditTime) {
|
|||
|
|
IpAddrPoolCfg cfg=new IpAddrPoolCfg();
|
|||
|
|
cfg.setCfgId(Long.valueOf(id));
|
|||
|
|
cfg.setIsValid(isValid);
|
|||
|
|
cfg.setIsAudit(isAudit);
|
|||
|
|
cfg.setAuditorId(UserUtils.getUser().getId());
|
|||
|
|
cfg.setAuditTime(auditTime);
|
|||
|
|
ipAddrPoolCfgDao.updateAddrPoolCfg(cfg);// 更新配置审核状态
|
|||
|
|
|
|||
|
|
cfg=getIpAddrPoolCfg(cfg.getCfgId(), null);
|
|||
|
|
String json="";
|
|||
|
|
if(cfg.getIsAudit()==1){
|
|||
|
|
List<IpAddrPool> resStrategyList=new ArrayList<IpAddrPool>();
|
|||
|
|
for(BaseIpCfg ipcfg : cfg.getIpCfgs()) {
|
|||
|
|
IpAddrPool ip = new IpAddrPool();
|
|||
|
|
BeanUtils.copyProperties(cfg, ipcfg, new String[]{"cfgId"});
|
|||
|
|
List<IpCfg> cfgs = BaseService.ipConvert(new IpCfg(), ipcfg);
|
|||
|
|
if (cfgs.size() > 1) {
|
|||
|
|
throw new RuntimeException("CallBack IP did not support IP range!");
|
|||
|
|
}
|
|||
|
|
IpCfg c = cfgs.get(0);
|
|||
|
|
ip.setRegionId(ipcfg.getCompileId());
|
|||
|
|
//ip.setGroupId(ipcfg.getCompileId());
|
|||
|
|
|
|||
|
|
ip.setAddrType(ipcfg.getIpType());
|
|||
|
|
ip.setSrcIp(c.getSrcIp());
|
|||
|
|
ip.setMaskSrcIp(c.getSrcIpMask());
|
|||
|
|
ip.setSrcPort(c.getSrcPort());
|
|||
|
|
ip.setMaskSrcPort(c.getSrcPortMask());
|
|||
|
|
ip.setDstIp(c.getDstIp());
|
|||
|
|
ip.setMaskDstIp(c.getDstIpMask());
|
|||
|
|
ip.setDstPort(c.getDstPort());
|
|||
|
|
ip.setMaskDstPort(c.getDstPortMask());
|
|||
|
|
ip.setProtocol(ipcfg.getProtocol());
|
|||
|
|
ip.setDirection(ipcfg.getDirection());
|
|||
|
|
|
|||
|
|
ip.setIsValid(cfg.getIsValid());
|
|||
|
|
ip.setAction(ipcfg.getAction());
|
|||
|
|
ip.setService(ipcfg.getServiceId());
|
|||
|
|
ip.setAddrPoolId(cfg.getAddrPoolId());
|
|||
|
|
ip.setOpTime(auditTime);
|
|||
|
|
ip.setAreaEffectiveIds(cfg.getAreaEffectiveIds());//添加区域管控
|
|||
|
|
|
|||
|
|
resStrategyList.add(ip);
|
|||
|
|
}
|
|||
|
|
//调用服务接口下发配置数据
|
|||
|
|
json=gsonToJson(resStrategyList);
|
|||
|
|
logger.info("SNAT地址池配置下发配置参数:"+json);
|
|||
|
|
//调用服务接口下发配置
|
|||
|
|
try {
|
|||
|
|
ToMaatResult result = ConfigServiceUtil.postCallbackCfg(json);
|
|||
|
|
if(result!=null){
|
|||
|
|
logger.info("SNAT地址池配置下发响应信息:"+result.getMsg());
|
|||
|
|
}
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
logger.error("SNAT地址池配置下发失败",e);
|
|||
|
|
throw e;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}else if(cfg.getIsAudit()==3){
|
|||
|
|
List<IpAddrPool> resStrategyList=new ArrayList<IpAddrPool>();
|
|||
|
|
for(BaseIpCfg ipcfg : cfg.getIpCfgs()) {
|
|||
|
|
IpAddrPool ip = new IpAddrPool();
|
|||
|
|
BeanUtils.copyProperties(cfg, ipcfg, new String[]{"cfgId"});
|
|||
|
|
List<IpCfg> cfgs = BaseService.ipConvert(new IpCfg(), ipcfg);
|
|||
|
|
if (cfgs.size() > 1) {
|
|||
|
|
throw new RuntimeException("CallBack IP did not support IP range!");
|
|||
|
|
}
|
|||
|
|
IpCfg c = cfgs.get(0);
|
|||
|
|
ip.setRegionId(ipcfg.getCompileId());
|
|||
|
|
ip.setAddrType(ipcfg.getIpType());
|
|||
|
|
ip.setSrcIp(c.getSrcIp());
|
|||
|
|
ip.setMaskSrcIp(c.getSrcIpMask());
|
|||
|
|
ip.setSrcPort(c.getSrcPort());
|
|||
|
|
ip.setMaskSrcPort(c.getSrcPortMask());
|
|||
|
|
ip.setDstIp(c.getDstIp());
|
|||
|
|
ip.setMaskDstIp(c.getDstIpMask());
|
|||
|
|
ip.setDstPort(c.getDstPort());
|
|||
|
|
ip.setMaskDstPort(c.getDstPortMask());
|
|||
|
|
ip.setProtocol(ipcfg.getProtocol());
|
|||
|
|
ip.setDirection(ipcfg.getDirection());
|
|||
|
|
ip.setIsValid(cfg.getIsValid());
|
|||
|
|
ip.setAction(ipcfg.getAction());
|
|||
|
|
ip.setService(ipcfg.getServiceId());
|
|||
|
|
ip.setAddrPoolId(cfg.getAddrPoolId());
|
|||
|
|
ip.setOpTime(auditTime);
|
|||
|
|
ip.setAreaEffectiveIds(cfg.getAreaEffectiveIds());
|
|||
|
|
resStrategyList.add(ip);
|
|||
|
|
}
|
|||
|
|
//调用服务接口取消配置
|
|||
|
|
json=gsonToJson(resStrategyList);
|
|||
|
|
logger.info("SNAT地址池配置配置参数:"+json);
|
|||
|
|
//调用服务接口取消配置
|
|||
|
|
try {
|
|||
|
|
ToMaatResult result = ConfigServiceUtil.put(json, 2);
|
|||
|
|
logger.info("SNAT地址池配置响应信息:"+result.getMsg());
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
logger.info("SNAT地址池配置配置失败");
|
|||
|
|
throw e;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public IpAddrPoolCfg getCfgInfo(IpAddrPoolCfg cfg) {
|
|||
|
|
return ipAddrPoolCfgDao.getCfgInfo(cfg);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|