2018-03-22 14:41:10 +08:00
|
|
|
package com.nis.web.service.configuration;
|
|
|
|
|
|
2018-03-26 14:58:47 +08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2018-03-27 19:00:57 +08:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2018-03-28 10:16:27 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2018-03-22 14:41:10 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
2018-03-27 19:00:57 +08:00
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
2018-03-22 14:41:10 +08:00
|
|
|
|
2018-03-26 14:58:47 +08:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import com.google.gson.GsonBuilder;
|
2018-03-22 14:41:10 +08:00
|
|
|
import com.nis.domain.configuration.DnsIpCfg;
|
2018-03-26 14:58:47 +08:00
|
|
|
import com.nis.domain.maat.DnsIpRecvData;
|
|
|
|
|
import com.nis.domain.maat.FromMaatBean;
|
|
|
|
|
import com.nis.domain.maat.MaatDnsIpCfg;
|
|
|
|
|
import com.nis.domain.maat.ToMaatDnsIpBean;
|
|
|
|
|
import com.nis.main.ConvertTool;
|
|
|
|
|
import com.nis.util.Collections3;
|
2018-03-22 14:41:10 +08:00
|
|
|
import com.nis.web.dao.configuration.DnsIpCfgDao;
|
|
|
|
|
import com.nis.web.service.CrudService;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class DnsIpCfgService extends CrudService<DnsIpCfgDao, DnsIpCfg>{
|
2018-03-28 10:16:27 +08:00
|
|
|
@Autowired
|
|
|
|
|
private DnsIpCfgDao dnsIpCfgDao;
|
2018-03-22 14:41:10 +08:00
|
|
|
@Override
|
2018-03-27 19:00:57 +08:00
|
|
|
@Transactional(readOnly=false,rollbackFor=RuntimeException.class)
|
2018-03-22 14:41:10 +08:00
|
|
|
public void save(DnsIpCfg dnsIpCfg) {
|
2018-03-26 14:58:47 +08:00
|
|
|
dao.insert(dnsIpCfg);
|
|
|
|
|
|
|
|
|
|
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
|
|
|
|
|
|
|
|
|
|
//组织要发送的json
|
|
|
|
|
ToMaatDnsIpBean toMaatBean = new ToMaatDnsIpBean();
|
|
|
|
|
List<MaatDnsIpCfg> ipCfgList = new ArrayList<MaatDnsIpCfg>();
|
|
|
|
|
MaatDnsIpCfg ipCfg = new MaatDnsIpCfg();
|
|
|
|
|
List<DnsIpCfg> d1 = new ArrayList<DnsIpCfg>();
|
|
|
|
|
d1.add(dnsIpCfg);
|
|
|
|
|
ipCfg.setIpCfg(d1);
|
|
|
|
|
ipCfgList.add(ipCfg);
|
|
|
|
|
toMaatBean.setServiceCfg(ipCfgList);
|
|
|
|
|
String sendJson = gson.toJson(toMaatBean);
|
2018-03-26 18:21:14 +08:00
|
|
|
logger.info("准备下发配置:" + sendJson);
|
2018-03-26 14:58:47 +08:00
|
|
|
//下发配置、接收结果
|
|
|
|
|
String recvJson = "";
|
|
|
|
|
try {
|
|
|
|
|
recvJson = new ConvertTool().saveDNSIPConfig(sendJson);
|
|
|
|
|
logger.info("收到结果:" + recvJson);
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
logger.error("欺骗ip配置新增下发失败,cfgId=" + dnsIpCfg.getCfgId(), e);
|
|
|
|
|
throw new RuntimeException("欺骗ip配置新增下发失败:通讯异常");
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 19:00:57 +08:00
|
|
|
if (StringUtils.isBlank(recvJson)) {
|
2018-03-26 14:58:47 +08:00
|
|
|
logger.error("新增欺骗ip配置失败:收到空的返回结果,cfgId:" + dnsIpCfg.getCfgId());
|
|
|
|
|
throw new RuntimeException("新增欺骗ip配置失败:收到空的返回结果");
|
|
|
|
|
}
|
|
|
|
|
logger.info("结果接收成功");
|
|
|
|
|
|
|
|
|
|
FromMaatBean fromMaatBean = gson.fromJson(recvJson, FromMaatBean.class);
|
|
|
|
|
List<DnsIpRecvData> data = fromMaatBean.getData();
|
|
|
|
|
if (!Collections3.isEmpty(data)) {
|
|
|
|
|
DnsIpRecvData dnsIpRecvData = data.get(0);
|
|
|
|
|
if (dnsIpRecvData.getFakeId() != null && dnsIpRecvData.getCfgId() != null
|
|
|
|
|
&& dnsIpRecvData.getCfgId().longValue() == dnsIpCfg.getCfgId().longValue()) {
|
|
|
|
|
dnsIpCfg.setFakeId(dnsIpRecvData.getFakeId());
|
|
|
|
|
dao.update(dnsIpCfg);
|
|
|
|
|
}
|
2018-03-22 14:41:10 +08:00
|
|
|
} else {
|
2018-03-26 14:58:47 +08:00
|
|
|
logger.error("新增欺骗ip配置失败:收到空的返回结果,cfgId:" + dnsIpCfg.getCfgId());
|
|
|
|
|
throw new RuntimeException("新增欺骗ip配置失败:收到空的返回结果");
|
2018-03-22 14:41:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-28 10:16:27 +08:00
|
|
|
public void delete(String fakeIds) {
|
|
|
|
|
if(fakeIds.endsWith(","))fakeIds=fakeIds.substring(0,fakeIds.lastIndexOf(","));
|
|
|
|
|
String[] fakeIdsArr = fakeIds.split(",");
|
|
|
|
|
for (String fakeId : fakeIdsArr) {
|
|
|
|
|
dnsIpCfgDao.deleteByFakeId(fakeId);
|
2018-03-22 14:41:10 +08:00
|
|
|
}
|
2018-03-26 14:58:47 +08:00
|
|
|
//给配置转换层发送、接收结果
|
2018-03-28 10:16:27 +08:00
|
|
|
String sendJson = "{\"fakeId\":[" + fakeIds + "]}";
|
2018-03-26 18:21:14 +08:00
|
|
|
logger.info("准备下发配置:" + sendJson);
|
2018-03-26 14:58:47 +08:00
|
|
|
Boolean recv = true;
|
|
|
|
|
try {
|
|
|
|
|
recv = new ConvertTool().invalidDNSIPConfig(sendJson);
|
|
|
|
|
} catch (Throwable e) {
|
2018-03-28 10:16:27 +08:00
|
|
|
logger.error("欺骗ip配置删除失败:通讯异常,fakeIds:" + fakeIds, e);
|
2018-03-26 14:58:47 +08:00
|
|
|
throw new RuntimeException("欺骗ip配置删除失败:通讯失败");
|
|
|
|
|
}
|
|
|
|
|
if (!recv) {
|
2018-03-28 10:16:27 +08:00
|
|
|
logger.error("欺骗ip配置删除失败,收到结果 " + recv + " ,fakeIds:" + fakeIds);
|
2018-03-26 14:58:47 +08:00
|
|
|
throw new RuntimeException("欺骗ip配置删除失败");
|
|
|
|
|
}
|
2018-03-22 14:41:10 +08:00
|
|
|
}
|
|
|
|
|
}
|