diff --git a/src/main/java/com/loafle/overflow/central/module/infra/service/CentralInfraService.java b/src/main/java/com/loafle/overflow/central/module/infra/service/CentralInfraService.java index 96974ee..2f0d258 100644 --- a/src/main/java/com/loafle/overflow/central/module/infra/service/CentralInfraService.java +++ b/src/main/java/com/loafle/overflow/central/module/infra/service/CentralInfraService.java @@ -10,14 +10,21 @@ import com.loafle.overflow.model.discovery.Host; import com.loafle.overflow.model.discovery.Zone; import com.loafle.overflow.model.infra.Infra; import com.loafle.overflow.model.infra.InfraHost; +import com.loafle.overflow.model.infra.InfraHostIP; import com.loafle.overflow.model.infra.InfraZone; +import com.loafle.overflow.model.meta.MetaIPType; +import com.loafle.overflow.model.meta.MetaInfraType; +import com.loafle.overflow.model.meta.MetaTargetHostType; import com.loafle.overflow.model.noauthprobe.NoAuthProbeDescription; +import com.loafle.overflow.model.noauthprobe.NoAuthProbeDescriptionHost; +import com.loafle.overflow.model.noauthprobe.NoAuthProbeDescriptionNetwork; import com.loafle.overflow.service.central.infra.InfraService; import com.loafle.overflow.service.central.probe.ProbeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; @Service("InfraService") @@ -56,6 +63,10 @@ public class CentralInfraService implements InfraService { } public InfraZone regist(Long probeID, List hosts, List services) throws OverflowException { + // List targets = new ArrayList(); + // targets.addAll(registDiscoveredHostTargets(probeId, hosts)); + // targets.addAll(this.registDiscoveredServiceTargets(probeId, services)); + return null; } @@ -73,7 +84,22 @@ public class CentralInfraService implements InfraService { } public InfraHost regist(Long probeID, NoAuthProbeDescription noAuthProbeDescription) throws OverflowException { - return null; + NoAuthProbeDescriptionNetwork networkInfo = noAuthProbeDescription.getNetwork(); + InfraHostIP infraHostIP = new InfraHostIP(); + // FIXME getting IPType from networkInfo + infraHostIP.setMetaIPType(new MetaIPType((short)1)); + infraHostIP.setAddress(networkInfo.getAddress()); + infraHostIP.setMac(networkInfo.getMacAddress()); + List infraHostIPs = new ArrayList<>(); + infraHostIPs.add(infraHostIP); + + InfraHost infraHost = new InfraHost(); + infraHost.setProbe(new Probe(probeID)); + infraHost.setMetaInfraType(new MetaInfraType(1)); + infraHost.setMetaTargetHostType(new MetaTargetHostType(1)); + infraHost.setInfraHostIPs(infraHostIPs); + + return this.infraDAO.save(infraHost); } } diff --git a/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java b/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java index 435e122..746c984 100644 --- a/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java +++ b/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java @@ -5,6 +5,7 @@ import com.loafle.overflow.core.annotation.ProbeAPI; import com.loafle.overflow.core.annotation.WebappAPI; import com.loafle.overflow.central.commons.utils.GenerateKey; import com.loafle.overflow.central.commons.utils.SessionMetadata; +import com.loafle.overflow.central.module.infra.service.CentralInfraService; import com.loafle.overflow.central.module.noauthprobe.dao.NoAuthProbeDAO; import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.model.apikey.ApiKey; @@ -12,18 +13,14 @@ import com.loafle.overflow.model.domain.Domain; import com.loafle.overflow.model.domain.DomainMember; import com.loafle.overflow.model.infra.InfraHost; import com.loafle.overflow.model.member.Member; -import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaNoAuthProbeStatus; import com.loafle.overflow.model.meta.MetaProbeStatus; import com.loafle.overflow.model.noauthprobe.NoAuthProbe; import com.loafle.overflow.model.noauthprobe.NoAuthProbeDescription; -import com.loafle.overflow.model.noauthprobe.NoAuthProbeDescriptionHost; -import com.loafle.overflow.model.noauthprobe.NoAuthProbeDescriptionNetwork; import com.loafle.overflow.model.probe.Probe; import com.loafle.overflow.model.probe.ProbeHost; import com.loafle.overflow.service.central.apikey.ApiKeyService; import com.loafle.overflow.service.central.domain.DomainMemberService; -import com.loafle.overflow.service.central.infra.InfraHostService; import com.loafle.overflow.service.central.noauthprobe.NoAuthProbeService; import com.loafle.overflow.service.central.probe.ProbeHostService; import com.loafle.overflow.service.central.probe.ProbeService; @@ -63,13 +60,10 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { @Autowired private ObjectMapper objectMapper; + + @Autowired + private CentralInfraService infraService; - @Autowired - private InfraMachineService infraMachineService; - @Autowired - private InfraOSService infraOSService; - @Autowired - private InfraHostService infraHostService; @Autowired private ProbeHostService probeHostService; @@ -109,9 +103,7 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { } Probe probe = this.newProbe(noAuthProbe, noAuthProbeDescription); - InfraMachine machine = this.newInfraMachine(noAuthProbe); - InfraOS os = this.newInfraOS(machine, noAuthProbeDescription.getHost()); - InfraHost host = this.newInfraHost(os, noAuthProbeDescription.getNetwork()); + InfraHost host = this.infraService.regist(probe.getId(), noAuthProbeDescription); this.newProbeHost(host, probe); noAuthProbe.setMetaNoAuthProbeStatus(new MetaNoAuthProbeStatus((short) 1)); @@ -152,43 +144,6 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { return this.probeService.regist(probe); } - private InfraMachine newInfraMachine(NoAuthProbe noauthprobe) throws OverflowException { - MetaInfraType metaInfraType = new MetaInfraType(1); - - InfraMachine infraMachine = new InfraMachine(); - infraMachine.setMeta(noauthprobe.getDescription()); - infraMachine.setMetaInfraType(metaInfraType); - - return this.infraMachineService.regist(infraMachine); - } - - private InfraOS newInfraOS(InfraMachine infraMachine, NoAuthProbeDescriptionHost noAuthProbeDescriptionHost) - throws OverflowException { - MetaInfraType metaInfraType = new MetaInfraType(3); - - InfraOS infraOS = new InfraOS(); - infraOS.setInfraMachine(infraMachine); - infraOS.setMetaInfraType(metaInfraType); - infraOS.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByOS(noAuthProbeDescriptionHost.getOS())); - - return this.infraOSService.regist(infraOS); - } - - private InfraHost newInfraHost(InfraOS infraOS, NoAuthProbeDescriptionNetwork noAuthProbeDescriptionNetwork) - throws OverflowException { - MetaInfraType metaInfraType = new MetaInfraType(2); - - InfraHost infraHost = new InfraHost(); - String addrStr = noAuthProbeDescriptionNetwork.getAddress(); - String[] addrArr = addrStr.split("\\|"); - - infraHost.setIpv4(addrArr[0]); - infraHost.setMac(noAuthProbeDescriptionNetwork.getMacAddress()); - infraHost.setInfraOS(infraOS); - infraHost.setMetaInfraType(metaInfraType); - - return this.infraHostService.regist(infraHost); - } private void newProbeHost(InfraHost infraHost, Probe probe) throws OverflowException { ProbeHost probeHost = new ProbeHost(); diff --git a/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetService.java b/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetService.java index 71e5fc2..79afa8c 100644 --- a/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetService.java +++ b/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetService.java @@ -4,8 +4,6 @@ import java.util.ArrayList; import java.util.List; import com.loafle.overflow.central.commons.utils.PageUtil; -import com.loafle.overflow.central.module.infra.service.CentralInfraHostService; -import com.loafle.overflow.central.module.infra.service.CentralInfraServiceService; import com.loafle.overflow.central.module.probe.service.CentralProbeService; import com.loafle.overflow.central.module.target.dao.TargetDAO; import com.loafle.overflow.core.exception.OverflowException; @@ -13,11 +11,11 @@ import com.loafle.overflow.core.model.PageParams; import com.loafle.overflow.core.type.CryptoType; import com.loafle.overflow.model.discovery.Host; import com.loafle.overflow.model.discovery.Service; -import com.loafle.overflow.model.infra.InfraHost; -import com.loafle.overflow.model.infra.InfraService; +import com.loafle.overflow.model.infra.Infra; import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.probe.Probe; import com.loafle.overflow.model.target.Target; +import com.loafle.overflow.service.central.infra.InfraService; import com.loafle.overflow.service.central.target.TargetService; import org.springframework.beans.factory.annotation.Autowired; @@ -34,10 +32,12 @@ public class CentralTargetService implements TargetService { private TargetDAO targetDAO; @Autowired private CentralProbeService probeService; + // @Autowired + // private CentralInfraHostService infraHostService; + // @Autowired + // private CentralInfraServiceService infraServiceService; @Autowired - private CentralInfraHostService infraHostService; - @Autowired - private CentralInfraServiceService infraServiceService; + private InfraService infraService; @Transactional public Target regist(Target target, Long probeID) throws OverflowException { @@ -76,18 +76,20 @@ public class CentralTargetService implements TargetService { } public Target readExistHostTarget(Long probeId, String ip) throws OverflowException { - InfraHost infraHost = this.infraHostService.readByProbeIdAndIpv4(probeId, ip); - if (null == infraHost) - return null; - return this.targetDAO.findByInfraId(infraHost.getId()); + // InfraHost infraHost = this.infraHostService.readByProbeIdAndIpv4(probeId, ip); + // if (null == infraHost) + // return null; + // return this.targetDAO.findByInfraId(infraHost.getId()); + return null; } public Target readExistServiceTarget(Long hostId, int portNumber, String portType) throws OverflowException { - InfraService infraService = this.infraServiceService.readByInfraHostIDAndPortAndPortType(hostId, portNumber, - portType); - if (null == infraService) - return null; - return this.targetDAO.findByInfraId(infraService.getId()); + // InfraService infraService = this.infraServiceService.readByInfraHostIDAndPortAndPortType(hostId, portNumber, + // portType); + // if (null == infraService) + // return null; + // return this.targetDAO.findByInfraId(infraService.getId()); + return null; } public Page readAllByProbeID(Long probeID, PageParams pageParams) throws OverflowException { @@ -95,75 +97,81 @@ public class CentralTargetService implements TargetService { } @Transactional - public List registDiscoveredTargets(Long probeId, List hosts, List services) + public List registDiscoveredTargets(Long probeID, List hosts, List services) throws OverflowException { - List targets = new ArrayList(); - targets.addAll(registDiscoveredHostTargets(probeId, hosts)); - targets.addAll(this.registDiscoveredServiceTargets(probeId, services)); - return targets; - } - - private List registDiscoveredHostTargets(Long probeId, List hosts) throws OverflowException { - List targets = new ArrayList<>(); - if (null == hosts) - return targets; - for (Host host : hosts) { - InfraHost infraHost = this.registInfraHostByDiscoveredHost(host, probeId); + List infras = this.infraService.regist(probeID, hosts, null); + for (Infra infra : infras) { Target target = new Target(); - target.setInfra(infraHost); - String displayName = (host.getIpv6() == null || host.getIpv6().trim().isEmpty()) ? host.getIpv6() - : host.getIpv4(); - target.setDisplayName(displayName); - targets.add(this.targetDAO.save(target)); - } - return targets; - } - - private List registDiscoveredServiceTargets(Long probeId, List services) throws OverflowException { - List targets = new ArrayList<>(); - if (null == services) - return targets; - for (Service service : services) { - Host host = service.getPort().getHost(); - InfraHost infraHost = null; - InfraHost existInfraHost = this.infraHostService.readByProbeIdAndIpv4(probeId, host.getIpv4()); - InfraService infraService = null; - if (null != existInfraHost) { - infraHost = existInfraHost; - } else { - infraHost = this.registInfraHostByDiscoveredHost(host, probeId); - infraService = this.registInfraServiceByDiscoveredService(infraHost, service, probeId); + if (infra.getMetaInfraType().getKey().equals("")) { + } - - Target target = new Target(); - target.setInfra(infraService); - String displayName = service.getServiceName(); - target.setDisplayName(displayName); - targets.add(this.targetDAO.save(target)); } - return targets; + + return null; } - private InfraHost registInfraHostByDiscoveredHost(Host host, Long probeId) throws OverflowException { - InfraHost infraHost = new InfraHost(); - infraHost.setMetaInfraType(new MetaInfraType(2)); - infraHost.setProbe(new Probe(probeId)); - // infraHost.setIpv4(host.getIpv4()); - // infraHost.setIpv6(host.getIpv6()); - // infraHost.setMac(host.getMac()); - return this.infraHostService.regist(infraHost); - } + // private List registDiscoveredHostTargets(Long probeId, List hosts) throws OverflowException { + // List targets = new ArrayList<>(); + // if (null == hosts) + // return targets; + // for (Host host : hosts) { + // InfraHost infraHost = this.registInfraHostByDiscoveredHost(host, probeId); + // Target target = new Target(); + // target.setInfra(infraHost); + // String displayName = (host.getIpv6() == null || host.getIpv6().trim().isEmpty()) ? host.getIpv6() + // : host.getIpv4(); + // target.setDisplayName(displayName); + // targets.add(this.targetDAO.save(target)); + // } + // return targets; + // return null; + // } - private InfraService registInfraServiceByDiscoveredService(InfraHost infraHost, Service service, Long probeId) - throws OverflowException { - InfraService infraService = new InfraService(); - infraHost.setMetaInfraType(new MetaInfraType(7)); - // infraService.setInfraHost(infraHost); - // infraService.setPort(service.getPort().getPortNumber()); - // infraService.setPortType(service.getPort().getPortType().toString()); - // infraService.setTlsType(service.getCryptoType() == CryptoType.TLS); - // infraService.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); - return this.infraServiceService.regist(infraService); - } + // private List registDiscoveredServiceTargets(Long probeId, List services) throws OverflowException { + // List targets = new ArrayList<>(); + // if (null == services) + // return targets; + // for (Service service : services) { + // Host host = service.getPort().getHost(); + // InfraHost infraHost = null; + // InfraHost existInfraHost = this.infraHostService.readByProbeIdAndIpv4(probeId, host.getIpv4()); + // InfraService infraService = null; + // if (null != existInfraHost) { + // infraHost = existInfraHost; + // } else { + // infraHost = this.registInfraHostByDiscoveredHost(host, probeId); + // infraService = this.registInfraServiceByDiscoveredService(infraHost, service, probeId); + // } + + // Target target = new Target(); + // target.setInfra(infraService); + // String displayName = service.getServiceName(); + // target.setDisplayName(displayName); + // targets.add(this.targetDAO.save(target)); + // } + // return targets; + // } + + // private InfraHost registInfraHostByDiscoveredHost(Host host, Long probeId) throws OverflowException { + // InfraHost infraHost = new InfraHost(); + // infraHost.setMetaInfraType(new MetaInfraType(2)); + // infraHost.setProbe(new Probe(probeId)); + // // infraHost.setIpv4(host.getIpv4()); + // // infraHost.setIpv6(host.getIpv6()); + // // infraHost.setMac(host.getMac()); + // return this.infraHostService.regist(infraHost); + // } + + // private InfraService registInfraServiceByDiscoveredService(InfraHost infraHost, Service service, Long probeId) + // throws OverflowException { + // InfraService infraService = new InfraService(); + // infraHost.setMetaInfraType(new MetaInfraType(7)); + // // infraService.setInfraHost(infraHost); + // // infraService.setPort(service.getPort().getPortNumber()); + // // infraService.setPortType(service.getPort().getPortType().toString()); + // // infraService.setTlsType(service.getCryptoType() == CryptoType.TLS); + // // infraService.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); + // return this.infraServiceService.regist(infraService); + // } }