This commit is contained in:
crusader 2018-06-21 17:42:32 +09:00
parent 18cba274cf
commit 8d0e5a0839
2 changed files with 49 additions and 3 deletions

View File

@ -50,7 +50,7 @@
<dependency>
<groupId>com.loafle.overflow</groupId>
<artifactId>commons-java</artifactId>
<version>1.0.89-SNAPSHOT</version>
<version>1.0.90-SNAPSHOT</version>
</dependency>
<dependency>

View File

@ -1,11 +1,15 @@
package com.loafle.overflow.central.module.infra.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loafle.overflow.central.commons.utils.PageUtil;
import com.loafle.overflow.central.module.infra.dao.InfraDAO;
import com.loafle.overflow.central.module.infra.dao.InfraHostDAO;
import com.loafle.overflow.central.module.infra.dao.InfraServiceDAO;
import com.loafle.overflow.central.module.infra.dao.InfraZoneDAO;
import com.loafle.overflow.central.module.meta.service.CentralMetaCryptoTypeService;
import com.loafle.overflow.central.module.meta.service.CentralMetaTargetServiceTypeService;
import com.loafle.overflow.core.model.PageParams;
import com.loafle.overflow.model.probe.Probe;
import com.loafle.overflow.core.exception.OverflowException;
@ -21,6 +25,7 @@ import com.loafle.overflow.model.infra.InfraZone;
import com.loafle.overflow.model.meta.MetaCryptoType;
import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.model.meta.MetaTargetHostType;
import com.loafle.overflow.model.meta.MetaTargetServiceType;
import com.loafle.overflow.model.meta.MetaTargetZoneType;
import com.loafle.overflow.service.central.infra.InfraService;
import com.loafle.overflow.service.central.probe.ProbeService;
@ -33,11 +38,18 @@ import org.springframework.transaction.annotation.Transactional;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.zip.GZIPInputStream;
@Service("InfraService")
public class CentralInfraService implements InfraService {
@Autowired
private ObjectMapper objectMapper;
@Autowired
ProbeService probeService;
@ -74,6 +86,9 @@ public class CentralInfraService implements InfraService {
@Autowired
CentralMetaCryptoTypeService metaCryptoTypeService;
@Autowired
CentralMetaTargetServiceTypeService metaTargetServiceTypeService;
@Override
public Infra regist(Infra infra) throws OverflowException {
return this.infraDAO.save(infra);
@ -189,6 +204,15 @@ public class CentralInfraService implements InfraService {
String.format("MetaCryptoTypeKey[%s] is not valid", service.getMetaCryptoType().getKey()));
}
if (null == service.getName()) {
throw new OverflowException("Name of service is not valid");
}
MetaTargetServiceType metaTargetServiceType = this.metaTargetServiceTypeService.readByKey(service.getName());
if (null == metaTargetServiceType) {
throw new OverflowException(String.format("MetaTargetServiceType[%s] is not valid", service.getName()));
}
Probe probe = this.probeService.read(probeID);
if (null == probe) {
throw new OverflowException(String.format("ID[%d] of Probe is not valid", probeID));
@ -247,6 +271,7 @@ public class CentralInfraService implements InfraService {
com.loafle.overflow.model.infra.InfraService infraService = new com.loafle.overflow.model.infra.InfraService();
infraService.setMetaInfraType(MetaInfraType.Enum.SERVICE.to());
infraService.setProbe(probe);
infraService.setMetaTargetServiceType(metaTargetServiceType);
infraService.setInfraHostPort(infraHostPort);
infraService.setMetaCryptoType(metaCryptoType);
@ -255,8 +280,29 @@ public class CentralInfraService implements InfraService {
@Override
@Transactional
public List<Infra> registDiscoverd(Long probeID, List<Host> hosts,
List<com.loafle.overflow.model.discovery.Service> services) throws OverflowException {
public List<Infra> registDiscoverd(Long probeID, String compressedHostsAndServices) throws OverflowException {
GZIPInputStream gis = null;
try {
byte[] compressed = Base64.getMimeDecoder().decode(compressedHostsAndServices);
ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
gis = new GZIPInputStream(bis);
} catch (Exception e) {
throw new OverflowException("Cannot deflate", e);
}
List<Host> hosts = null;
List<com.loafle.overflow.model.discovery.Service> services = null;
try {
JsonNode node = this.objectMapper.readTree(gis);
hosts = this.objectMapper.readValue(node.get("hosts").traverse(), new TypeReference<List<Host>>() {
});
services = this.objectMapper.readValue(node.get("services").traverse(),
new TypeReference<List<com.loafle.overflow.model.discovery.Service>>() {
});
} catch (IOException e) {
throw new OverflowException("Cannot convert to json", e);
}
List<Infra> infras = new ArrayList<>();