301 lines
11 KiB
Java
301 lines
11 KiB
Java
package com.nis.web.service.basics;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Map.Entry;
|
||
|
||
import org.apache.commons.lang3.StringUtils;
|
||
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.nis.domain.Page;
|
||
import com.nis.domain.SysDataDictionaryItem;
|
||
import com.nis.domain.basics.AsnIpCfg;
|
||
import com.nis.domain.basics.PolicyGroupInfo;
|
||
import com.nis.domain.configuration.CfgIndexInfo;
|
||
import com.nis.domain.maat.GroupReuseAddBean;
|
||
import com.nis.domain.maat.GroupReuseCfg;
|
||
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.ToMaatResult;
|
||
import com.nis.domain.specific.ConfigGroupInfo;
|
||
import com.nis.exceptions.MaatConvertException;
|
||
import com.nis.util.ConfigServiceUtil;
|
||
import com.nis.util.Constants;
|
||
import com.nis.util.DictUtils;
|
||
import com.nis.web.dao.CrudDao;
|
||
import com.nis.web.dao.basics.AsnIpCfgDao;
|
||
import com.nis.web.dao.basics.PolicyGroupInfoDao;
|
||
import com.nis.web.dao.specific.SpecificServiceCfgDao;
|
||
import com.nis.web.security.UserUtils;
|
||
import com.nis.web.service.CrudService;
|
||
@Service
|
||
public class AsnIpCfgService extends CrudService<CrudDao<AsnIpCfg>, AsnIpCfg> {
|
||
@Autowired
|
||
private AsnIpCfgDao asnIpCfgDao;
|
||
@Autowired
|
||
private SpecificServiceCfgDao specificServiceCfgDao;
|
||
@Autowired
|
||
private PolicyGroupInfoDao policyGroupInfoDao;
|
||
/**
|
||
* @param page
|
||
* @param entity
|
||
* @return
|
||
*/
|
||
public Page<AsnIpCfg> findPage(Page<AsnIpCfg> page, AsnIpCfg entity) {
|
||
entity.getSqlMap().put("dsf", configScopeFilter(entity.getCurrentUser(),"r"));
|
||
entity.setPage(page);
|
||
List<AsnIpCfg> list=asnIpCfgDao.findPage(entity);
|
||
page.setList(list);
|
||
return page;
|
||
}
|
||
public AsnIpCfg get(Long id ) {
|
||
return asnIpCfgDao.get(id);
|
||
}
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void save(List<AsnIpCfg> entitys){
|
||
this.saveBatch(entitys, AsnIpCfgDao.class);
|
||
}
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void update(AsnIpCfg entity){
|
||
Date editTime=new Date();
|
||
entity.setEditTime(editTime);
|
||
entity.setEditorId(entity.getCurrentUser().getId());
|
||
if(entity.getAsnIpGroup()==null) {
|
||
throw new RuntimeException("asn group name not found!");
|
||
}
|
||
ConfigGroupInfo groupInfo=specificServiceCfgDao.getConfigGroupInfoByGroupId(entity.getAsnIpGroup());
|
||
if(groupInfo.getIsIssued()==0) {//未下发,可修改
|
||
asnIpCfgDao.update(entity);
|
||
}else {
|
||
throw new RuntimeException("asn group is issued, cannot update asn ip!");
|
||
}
|
||
|
||
}
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void saveAsnIpBatch(Map<String,List<AsnIpCfg>> cfgs,int total) {
|
||
List<AsnIpCfg> toDelAsnIpCfgs=Lists.newArrayList();
|
||
List<AsnIpCfg> toSaveAsnIpCfgs=Lists.newArrayList();
|
||
List<SysDataDictionaryItem> isImportAll=DictUtils.getDictList("IS_ASN_IP_IMPORT_ALL");
|
||
List<Integer> idList=ConfigServiceUtil.getId(3, total);
|
||
int ind=0;
|
||
for(Entry<String,List<AsnIpCfg>> entry:cfgs.entrySet()) {
|
||
Long asnNo=Long.parseLong(entry.getKey());
|
||
ConfigGroupInfo configGroupInfo=this.getConfigGroupInfoByAsnNo(asnNo);
|
||
if(configGroupInfo==null) {//不存在则新增
|
||
PolicyGroupInfo info=new PolicyGroupInfo();
|
||
info.setAsnNo(Long.parseLong(entry.getKey()));
|
||
info.setDescription("Create by import excel");
|
||
info.setGroupName("Import"+asnNo);
|
||
info.setGroupType(4);
|
||
configGroupInfo=this.saveAsnGroup(info);
|
||
}
|
||
for(AsnIpCfg cfg:entry.getValue()) {
|
||
cfg.setAsnIpGroup(configGroupInfo.getGroupId());
|
||
cfg.setRegionId(idList.get(ind));
|
||
if(configGroupInfo.getIsIssued()==0)
|
||
cfg.setIsValid(Constants.VALID_NO);
|
||
else {
|
||
cfg.setIsValid(Constants.VALID_YES);
|
||
}
|
||
ind++;
|
||
}
|
||
if(isImportAll.get(0).getItemCode().equals("1")) {//全量下发,删除已有的
|
||
if(configGroupInfo.getIsIssued()==1) {//已下发
|
||
List<AsnIpCfg> _toDelAsnIpCfgs=this.getByAsnGroup(configGroupInfo.getGroupId(), configGroupInfo.getAsnId());
|
||
toDelAsnIpCfgs.addAll(_toDelAsnIpCfgs);
|
||
}
|
||
this.deleteByAsnGroup(configGroupInfo.getGroupId(), configGroupInfo.getAsnId());
|
||
}
|
||
toSaveAsnIpCfgs.addAll(entry.getValue());
|
||
}
|
||
this.save(toSaveAsnIpCfgs);
|
||
if(toDelAsnIpCfgs.size()>0) {
|
||
asnIPRegionSendToMaat(toDelAsnIpCfgs,Constants.VALID_NO);
|
||
}
|
||
if(toSaveAsnIpCfgs.size()>0) {
|
||
asnIPRegionSendToMaat(toSaveAsnIpCfgs,Constants.VALID_YES);
|
||
}
|
||
}
|
||
/**
|
||
* asn ip分组复用新增
|
||
* @param asnIpCfgs
|
||
*/
|
||
public void asnIPRegionSendToMaat(List<AsnIpCfg> asnIpCfgs,Integer valid) {
|
||
GroupReuseAddBean maatBean = new GroupReuseAddBean();
|
||
List<GroupReuseCfg> groupReuseList=new ArrayList<>();
|
||
GroupReuseCfg groupReuseCfg=new GroupReuseCfg();
|
||
List<IpCfg> ipRegionList = groupReuseCfgAddRemoveConvert(asnIpCfgs,valid,null);
|
||
List<StringCfg> strRegionList = new ArrayList<>();
|
||
List<NumBoundaryCfg> numRegionList = new ArrayList<>();
|
||
groupReuseCfg.setIpRegionList(ipRegionList);
|
||
groupReuseCfg.setStrRegionList(strRegionList);
|
||
groupReuseCfg.setNumRegionList(numRegionList);
|
||
groupReuseList.add(groupReuseCfg);
|
||
maatBean.setGroupReuseCfgList(groupReuseList);
|
||
maatBean.setAuditTime(new Date());
|
||
maatBean.setCreatorName(UserUtils.getUser().getName());
|
||
maatBean.setVersion(Constants.MAAT_VERSION);
|
||
if(valid==Constants.VALID_YES) {
|
||
maatBean.setOpAction(Constants.INSERT_ACTION);
|
||
//调用服务接口下发配置数据
|
||
String json=gsonToJson(maatBean);
|
||
logger.info("asn ip复用域新增配置下发配置参数:"+json);
|
||
//调用服务接口下发配置
|
||
ToMaatResult result =ConfigServiceUtil.postGroupReuseSources(json);
|
||
logger.info("asn ip复用域新增配置响应信息:"+result.getMsg());
|
||
}else {
|
||
maatBean.setOpAction(Constants.UPDATE_ACTION);
|
||
//调用服务接口下发配置数据
|
||
String json=gsonToJson(maatBean);
|
||
logger.info("asn ip复用域删除配置下发配置参数:"+json);
|
||
//调用服务接口下发配置
|
||
ToMaatResult result = ConfigServiceUtil.put(json,3);
|
||
logger.info("asn ip复用域删除配置响应信息:"+result.getMsg());
|
||
}
|
||
}
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public ConfigGroupInfo saveAsnGroup(PolicyGroupInfo entity) throws MaatConvertException{
|
||
entity.setIsValid(1);
|
||
ConfigGroupInfo group=new ConfigGroupInfo();
|
||
//新增
|
||
Date createTime=new Date();
|
||
entity.setCreatorId(UserUtils.getUser().getId());
|
||
entity.setCreateTime(createTime);
|
||
//asn类型从综合服务获取groupId
|
||
if(4==entity.getGroupType().intValue()) {
|
||
int serviceGroupId=0;
|
||
List<Integer> groupIds= ConfigServiceUtil.getId(2,1);
|
||
if(groupIds.size()>0) {
|
||
serviceGroupId=groupIds.get(0).intValue();
|
||
}else {
|
||
throw new MaatConvertException("Get asn group id failed");
|
||
}
|
||
entity.setServiceGroupId(serviceGroupId);
|
||
//新增协议分组
|
||
group.setGroupId(serviceGroupId);
|
||
group.setGroupName(entity.getGroupName());
|
||
group.setIsIssued(0);
|
||
group.setGroupType(entity.getGroupType());
|
||
group.setAsnId(entity.getAsnNo());
|
||
specificServiceCfgDao.insertConfigGroupInfo(group);
|
||
policyGroupInfoDao.insert(entity);
|
||
}
|
||
return group;
|
||
}
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void saveAsnIpCfg(CfgIndexInfo entity) {
|
||
Date createTime=new Date();
|
||
ConfigGroupInfo groupInfo=specificServiceCfgDao.getConfigGroupInfoByGroupId(entity.getAsnIpGroup().intValue());
|
||
if(groupInfo==null) {
|
||
throw new RuntimeException("ConfigGroupInfo is null!");
|
||
}
|
||
List<Integer> idList=ConfigServiceUtil.getId(3, entity.getAsnIpCfgs().size());
|
||
int index=0;
|
||
if(groupInfo.getIsIssued()==1) {//如果已经下发,则需要下到综合服务中
|
||
if(entity.getAsnIpCfgs()!=null) {
|
||
for(AsnIpCfg cfg:entity.getAsnIpCfgs()) {
|
||
if(StringUtils.isNotBlank(entity.getCfgDesc())) {
|
||
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
|
||
}else {
|
||
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId","cfgDesc"});
|
||
}
|
||
|
||
cfg.setCreateTime(createTime);
|
||
cfg.setCreatorId(entity.getCurrentUser().getId());
|
||
cfg.setAsnIpGroup(entity.getAsnIpGroup());
|
||
cfg.setIsValid(Constants.VALID_YES);
|
||
cfg.setRegionId(idList.get(index));
|
||
index++;
|
||
}
|
||
}
|
||
this.save(entity.getAsnIpCfgs());
|
||
asnIPRegionSendToMaat(entity.getAsnIpCfgs(),Constants.VALID_YES);
|
||
}else {
|
||
if(entity.getAsnIpCfgs()!=null) {
|
||
for(AsnIpCfg cfg:entity.getAsnIpCfgs()) {
|
||
BeanUtils.copyProperties(entity, cfg, new String[]{"cfgId"});
|
||
cfg.setCreateTime(createTime);
|
||
cfg.setCreatorId(entity.getCurrentUser().getId());
|
||
cfg.setAsnIpGroup(entity.getAsnIpGroup().intValue());
|
||
cfg.setIsValid(Constants.VALID_NO);
|
||
cfg.setRegionId(idList.get(index));
|
||
index++;
|
||
}
|
||
}
|
||
this.save(entity.getAsnIpCfgs());
|
||
}
|
||
}
|
||
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
||
public void delete(Integer functionId,Integer isValid,String ids) {
|
||
for(String id:ids.split(",")) {
|
||
Long.parseLong(id);
|
||
}
|
||
List<AsnIpCfg> issuedList=new ArrayList<>();
|
||
List<AsnIpCfg> asnIpCfgList= asnIpCfgDao.getByIds(ids);
|
||
|
||
for(AsnIpCfg asnIpCfg:asnIpCfgList) {
|
||
if(asnIpCfg.getIsValid()==Constants.VALID_YES) {//代表下发过了
|
||
asnIpCfg.setIsValid(isValid);
|
||
issuedList.add(asnIpCfg);
|
||
Integer groupId=asnIpCfg.getAsnIpGroup();
|
||
List result=asnIpCfgDao.findOtherIps(groupId, asnIpCfg.getCfgId().intValue());
|
||
if(result==null||result.size()==0) {
|
||
throw new RuntimeException("Cant not delete ip from asn group, there is only one ip left in the group!");
|
||
}
|
||
}
|
||
}
|
||
if(issuedList.size()>0) {
|
||
//已经下发过的,调用分组复用配置删除接口
|
||
asnIPRegionSendToMaat(issuedList,Constants.VALID_NO);
|
||
}
|
||
asnIpCfgDao.updateValid(isValid, ids);
|
||
}
|
||
// public List<ConfigGroupInfo> findPolicyGroupInfosByType(Integer groupId) {
|
||
// // TODO Auto-generated method stub
|
||
// return asnIpCfgDao.findPolicyGroupInfosByType(groupId);
|
||
// }
|
||
/**
|
||
* 选中组中是否含有只剩一个未删除IP的组
|
||
* @param serviceGroupIds
|
||
* @return
|
||
*/
|
||
public boolean hasLastIp(String serviceGroupIds,String ids) {
|
||
// TODO Auto-generated method stub
|
||
for(String groupId:serviceGroupIds.split(",")) {
|
||
Long.parseLong(groupId);
|
||
}
|
||
List<Integer> countList=asnIpCfgDao.countValidIPs(serviceGroupIds,ids);
|
||
if(countList.size()==0) {
|
||
return true;
|
||
}
|
||
for(Integer count:countList) {
|
||
if(count==0) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
public ConfigGroupInfo getConfigGroupInfoByAsnNo(Long asnNo) {
|
||
return asnIpCfgDao.getInfoByAsnNo(asnNo);
|
||
}
|
||
public List<AsnIpCfg> getByAsnGroup(Integer groupId,Long asnNo) {
|
||
if(groupId==null||asnNo==null) {
|
||
throw new RuntimeException("groupId or asnNo is null!");
|
||
}
|
||
return asnIpCfgDao.getByAsnGroup(groupId.longValue(),asnNo.longValue());
|
||
}
|
||
public void deleteByAsnGroup(Integer groupId,Long asnNo) {
|
||
if(groupId==null||asnNo==null) {
|
||
throw new RuntimeException("groupId or asnNo is null!");
|
||
}
|
||
asnIpCfgDao.deleteByAsnGroup(groupId.longValue(),asnNo.longValue());
|
||
}
|
||
}
|