131 lines
4.7 KiB
Java
131 lines
4.7 KiB
Java
package com.nis.web.task;
|
||
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.Iterator;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.Map.Entry;
|
||
|
||
import org.apache.ibatis.session.ExecutorType;
|
||
import org.apache.ibatis.session.SqlSession;
|
||
import org.apache.ibatis.session.SqlSessionFactory;
|
||
import org.apache.log4j.Logger;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
||
import com.nis.domain.basics.AsnGroupInfo;
|
||
import com.nis.domain.basics.AsnIpCfg;
|
||
import com.nis.util.IPUtil;
|
||
import com.nis.util.StringUtil;
|
||
import com.nis.web.dao.basics.AsnGroupInfoDao;
|
||
import com.nis.web.dao.basics.AsnIpCfgDao;
|
||
import com.nis.web.service.SpringContextHolder;
|
||
|
||
import net.sf.json.JSONObject;
|
||
/**
|
||
* 定时统计ASN 下IPv4和IPV6的个数
|
||
* @author intraUser
|
||
*
|
||
*/
|
||
public class StatisticASNIpNumer {
|
||
|
||
private Logger logger = Logger.getLogger(this.getClass());
|
||
|
||
@Autowired
|
||
protected AsnIpCfgDao asnIpCfgDao;
|
||
public void calculateASNIp() {
|
||
logger.info("定时统计ASN IP个数开始。。。。。。。。。。。。。。");
|
||
long start=System.currentTimeMillis();
|
||
Map<Integer,Map<String,Long>> asnNumerMap=new HashMap<>();
|
||
List<Object[]> asnIpList=new ArrayList<>();
|
||
asnIpList=asnIpCfgDao.findAllAsnIpCfgList();
|
||
|
||
getAllASNIPNumber(asnIpList, asnNumerMap);
|
||
|
||
updateAllASNIPNumber(asnNumerMap);
|
||
|
||
long end=System.currentTimeMillis();
|
||
logger.info("定时统计ASN IP个数结束:"+(end-start)+"。。。。。。。。。。。。。。");
|
||
}
|
||
public void getAllASNIPNumber(List<Object[]> asnIpList,Map<Integer,Map<String,Long>> asnNumerMap){
|
||
logger.info("计算ASN IP开始:asn ip size:"+asnIpList.size()+"。。。。。。。。。。。。。。");
|
||
long start=System.currentTimeMillis();
|
||
if(!StringUtil.isEmpty(asnIpList)){
|
||
try {
|
||
for (Iterator iterator = asnIpList.iterator(); iterator.hasNext();) {
|
||
Map asnIpMap=(Map) iterator.next();
|
||
//groupId
|
||
Integer asnGroupId = (Integer)asnIpMap.get("asn_ip_group");
|
||
//ipType(4:v4,6:v6)
|
||
Integer ipType= (Integer)asnIpMap.get("ip_type");
|
||
//(1:mask,2:range,3:ip)
|
||
//Integer ipPattern= (Integer)asnIpMap.get("ip_pattern");
|
||
//dest_ip_address
|
||
String ipAddress= (String)asnIpMap.get("dest_ip_address");
|
||
long IPNumber=0;
|
||
if(ipType.equals(4)){
|
||
if(ipAddress.indexOf("/") > -1){
|
||
Integer mask=Integer.parseInt(ipAddress.split("/")[1]);
|
||
ipAddress=ipAddress.split("/")[0];
|
||
IPNumber=IPUtil.getIpNum(ipAddress, mask);
|
||
}else{
|
||
IPNumber=1;
|
||
}
|
||
//判断组是否已经存在
|
||
if(asnNumerMap.keySet().contains(asnGroupId)){
|
||
asnNumerMap.get(asnGroupId).put("v4", asnNumerMap.get(asnGroupId).get("v4")+IPNumber);
|
||
asnNumerMap.put(asnGroupId, asnNumerMap.get(asnGroupId));
|
||
}else{
|
||
Map<String, Long> map=new HashMap<>();
|
||
map.put("v4", IPNumber);
|
||
map.put("v6", 0l);
|
||
asnNumerMap.put(asnGroupId, map);
|
||
}
|
||
}else{
|
||
IPNumber=1;
|
||
//判断组是否已经存在
|
||
if(asnNumerMap.keySet().contains(asnGroupId)){
|
||
asnNumerMap.get(asnGroupId).put("v6", asnNumerMap.get(asnGroupId).get("v6")+IPNumber);
|
||
asnNumerMap.put(asnGroupId, asnNumerMap.get(asnGroupId));
|
||
}else{
|
||
Map<String, Long> map=new HashMap<>();
|
||
map.put("v4", 0l);
|
||
map.put("v6", IPNumber);
|
||
asnNumerMap.put(asnGroupId, map);
|
||
}
|
||
}
|
||
}
|
||
} catch (Exception e) {
|
||
logger.error("计算ASN IP个数失败",e);
|
||
}
|
||
}
|
||
|
||
long end=System.currentTimeMillis();
|
||
logger.info("计算ASN IP个数结束:"+(end-start)+"。。。。。。。。。。。。。。");
|
||
}
|
||
public void updateAllASNIPNumber(Map<Integer,Map<String,Long>> asnNumerMap) {
|
||
logger.info("修改ASN IP个数开始:asn size:"+asnNumerMap.size()+"。。。。。。。。。。。。。。");
|
||
long start=System.currentTimeMillis();
|
||
if(!StringUtil.isEmpty(asnNumerMap)){
|
||
int index=0;
|
||
SqlSessionFactory sqlSessionFactory=SpringContextHolder.getBean(SqlSessionFactory.class);
|
||
SqlSession batchSqlSession = null;
|
||
try {
|
||
batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, true);
|
||
for(Entry<Integer,Map<String,Long>> e: asnNumerMap.entrySet()) {
|
||
((AsnGroupInfoDao) batchSqlSession.getMapper(AsnGroupInfoDao.class)).updateIpNum(e.getValue().get("v4"),e.getValue().get("v6"),e.getKey());
|
||
index++;
|
||
}
|
||
}catch (Exception e) {
|
||
logger.error("修改ASN IP个数失败", e);
|
||
} finally {
|
||
if(batchSqlSession != null){
|
||
batchSqlSession.close();
|
||
}
|
||
}
|
||
}
|
||
long end=System.currentTimeMillis();
|
||
logger.info("修改ASN IP个数结束:"+(end-start)+"。。。。。。。。。。。。。。");
|
||
}
|
||
}
|