Java tutorial
/* * Copyright 2014 by SCSK Corporation. * * This file is part of PrimeCloud Controller(TM). * * PrimeCloud Controller(TM) is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * PrimeCloud Controller(TM) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PrimeCloud Controller(TM). If not, see <http://www.gnu.org/licenses/>. */ package jp.primecloud.auto.service.impl; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; import jp.primecloud.auto.common.constant.PCCConstant; import jp.primecloud.auto.common.status.LoadBalancerInstanceStatus; import jp.primecloud.auto.common.status.LoadBalancerListenerStatus; import jp.primecloud.auto.common.status.LoadBalancerStatus; import jp.primecloud.auto.config.Config; import jp.primecloud.auto.entity.crud.AutoScalingConf; import jp.primecloud.auto.entity.crud.AwsCertificate; import jp.primecloud.auto.entity.crud.AwsInstance; import jp.primecloud.auto.entity.crud.AwsLoadBalancer; import jp.primecloud.auto.entity.crud.AwsSslKey; import jp.primecloud.auto.entity.crud.CloudstackLoadBalancer; import jp.primecloud.auto.entity.crud.Component; import jp.primecloud.auto.entity.crud.ComponentInstance; import jp.primecloud.auto.entity.crud.ComponentLoadBalancer; import jp.primecloud.auto.entity.crud.ComponentType; import jp.primecloud.auto.entity.crud.Farm; import jp.primecloud.auto.entity.crud.Image; import jp.primecloud.auto.entity.crud.ImageAws; import jp.primecloud.auto.entity.crud.ImageAzure; import jp.primecloud.auto.entity.crud.ImageCloudstack; import jp.primecloud.auto.entity.crud.ImageNifty; import jp.primecloud.auto.entity.crud.ImageOpenstack; import jp.primecloud.auto.entity.crud.ImageVcloud; import jp.primecloud.auto.entity.crud.ImageVmware; import jp.primecloud.auto.entity.crud.Instance; import jp.primecloud.auto.entity.crud.LoadBalancer; import jp.primecloud.auto.entity.crud.LoadBalancerHealthCheck; import jp.primecloud.auto.entity.crud.LoadBalancerInstance; import jp.primecloud.auto.entity.crud.LoadBalancerListener; import jp.primecloud.auto.entity.crud.Platform; import jp.primecloud.auto.entity.crud.PlatformAws; import jp.primecloud.auto.entity.crud.PlatformAzure; import jp.primecloud.auto.entity.crud.PlatformCloudstack; import jp.primecloud.auto.entity.crud.PlatformNifty; import jp.primecloud.auto.entity.crud.PlatformOpenstack; import jp.primecloud.auto.entity.crud.PlatformVcloud; import jp.primecloud.auto.entity.crud.PlatformVmware; import jp.primecloud.auto.exception.AutoApplicationException; import jp.primecloud.auto.log.EventLogLevel; import jp.primecloud.auto.log.EventLogger; import jp.primecloud.auto.process.zabbix.ElbZabbixHostProcess; import jp.primecloud.auto.process.zabbix.ZabbixProcessClient; import jp.primecloud.auto.process.zabbix.ZabbixProcessClientFactory; import jp.primecloud.auto.service.ComponentService; import jp.primecloud.auto.service.IaasDescribeService; import jp.primecloud.auto.service.InstanceService; import jp.primecloud.auto.service.LoadBalancerService; import jp.primecloud.auto.service.ServiceSupport; import jp.primecloud.auto.service.dto.AutoScalingConfDto; import jp.primecloud.auto.service.dto.ComponentLoadBalancerDto; import jp.primecloud.auto.service.dto.ImageDto; import jp.primecloud.auto.service.dto.LoadBalancerDto; import jp.primecloud.auto.service.dto.LoadBalancerPlatformDto; import jp.primecloud.auto.service.dto.PlatformDto; import jp.primecloud.auto.service.dto.SecurityGroupDto; import jp.primecloud.auto.service.dto.SslKeyDto; import jp.primecloud.auto.service.dto.SubnetDto; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.StringUtils; /** * <p> * TODO: * </p> * */ public class LoadBalancerServiceImpl extends ServiceSupport implements LoadBalancerService { protected ComponentService componentService; protected InstanceService instanceService; protected IaasDescribeService iaasDescribeService; protected EventLogger eventLogger; protected ZabbixProcessClientFactory zabbixProcessClientFactory; protected ElbZabbixHostProcess elbZabbixHostProcess; /** * {@inheritDoc} */ @Override public List<LoadBalancerDto> getLoadBalancers(Long farmNo) { // ? if (farmNo == null) { throw new AutoApplicationException("ECOMMON-000003", "farmNo"); } List<LoadBalancer> loadBalancers = loadBalancerDao.readByFarmNo(farmNo); // ??? List<Long> loadBalancerNos = new ArrayList<Long>(); for (LoadBalancer loadBalancer : loadBalancers) { loadBalancerNos.add(loadBalancer.getLoadBalancerNo()); } // (AWS)? List<PlatformAws> platformAwss = platformAwsDao.readAll(); Map<Long, PlatformAws> platformAwsMap = new LinkedHashMap<Long, PlatformAws>(); for (PlatformAws platformAws : platformAwss) { platformAwsMap.put(platformAws.getPlatformNo(), platformAws); } // (VMWare)? List<PlatformVmware> platformVmwares = platformVmwareDao.readAll(); Map<Long, PlatformVmware> platformVmwareMap = new LinkedHashMap<Long, PlatformVmware>(); for (PlatformVmware platformVmware : platformVmwares) { platformVmwareMap.put(platformVmware.getPlatformNo(), platformVmware); } // (Nifty)? List<PlatformNifty> platformNifties = platformNiftyDao.readAll(); Map<Long, PlatformNifty> platformNiftyMap = new LinkedHashMap<Long, PlatformNifty>(); for (PlatformNifty platformNifty : platformNifties) { platformNiftyMap.put(platformNifty.getPlatformNo(), platformNifty); } // (CloudStack)? List<PlatformCloudstack> platformCloudstacks = platformCloudstackDao.readAll(); Map<Long, PlatformCloudstack> platformCloudstackMap = new LinkedHashMap<Long, PlatformCloudstack>(); for (PlatformCloudstack platformCloudstack : platformCloudstacks) { platformCloudstackMap.put(platformCloudstack.getPlatformNo(), platformCloudstack); } // (Vcloud)? List<PlatformVcloud> platformVclouds = platformVcloudDao.readAll(); Map<Long, PlatformVcloud> platformVcloudMap = new LinkedHashMap<Long, PlatformVcloud>(); for (PlatformVcloud platformVcloud : platformVclouds) { platformVcloudMap.put(platformVcloud.getPlatformNo(), platformVcloud); } // (Azure)? List<PlatformAzure> platformAzures = platformAzureDao.readAll(); Map<Long, PlatformAzure> platformAzureMap = new LinkedHashMap<Long, PlatformAzure>(); for (PlatformAzure platformAzure : platformAzures) { platformAzureMap.put(platformAzure.getPlatformNo(), platformAzure); } // ? List<Platform> platforms = platformDao.readAll(); // PlatformDto? Map<Long, PlatformDto> platformDtoMap = new LinkedHashMap<Long, PlatformDto>(); for (Platform platform : platforms) { PlatformDto platformDto = new PlatformDto(); platformDto.setPlatform(platform); platformDto.setPlatformAws(platformAwsMap.get(platform.getPlatformNo())); platformDto.setPlatformCloudstack(platformCloudstackMap.get(platform.getPlatformNo())); platformDto.setPlatformVmware(platformVmwareMap.get(platform.getPlatformNo())); platformDto.setPlatformNifty(platformNiftyMap.get(platform.getPlatformNo())); platformDto.setPlatformVcloud(platformVcloudMap.get(platform.getPlatformNo())); platformDto.setPlatformAzure(platformAzureMap.get(platform.getPlatformNo())); platformDtoMap.put(platform.getPlatformNo(), platformDto); } // (AWS)? List<ImageAws> imageAwses = imageAwsDao.readAll(); Map<Long, ImageAws> imageAwsMap = new LinkedHashMap<Long, ImageAws>(); for (ImageAws imageAws : imageAwses) { imageAwsMap.put(imageAws.getImageNo(), imageAws); } // (Cloudstack)? List<ImageCloudstack> imageCloudstacks = imageCloudstackDao.readAll(); Map<Long, ImageCloudstack> imageCloudstackMap = new LinkedHashMap<Long, ImageCloudstack>(); for (ImageCloudstack imageCloudstack : imageCloudstacks) { imageCloudstackMap.put(imageCloudstack.getImageNo(), imageCloudstack); } // (VMWare)? List<ImageVmware> imageVmwares = imageVmwareDao.readAll(); Map<Long, ImageVmware> imageVmwareMap = new LinkedHashMap<Long, ImageVmware>(); for (ImageVmware imageVmware : imageVmwares) { imageVmwareMap.put(imageVmware.getImageNo(), imageVmware); } // (Nifty)? List<ImageNifty> imageNifties = imageNiftyDao.readAll(); Map<Long, ImageNifty> imageNiftyMap = new LinkedHashMap<Long, ImageNifty>(); for (ImageNifty imageNifty : imageNifties) { imageNiftyMap.put(imageNifty.getImageNo(), imageNifty); } // (VCloud)? List<ImageVcloud> imageVclouds = imageVcloudDao.readAll(); Map<Long, ImageVcloud> imageVcloudMap = new LinkedHashMap<Long, ImageVcloud>(); for (ImageVcloud imageVcloud : imageVclouds) { imageVcloudMap.put(imageVcloud.getImageNo(), imageVcloud); } // (Azure)? List<ImageAzure> imageAzures = imageAzureDao.readAll(); Map<Long, ImageAzure> imageAzureMap = new LinkedHashMap<Long, ImageAzure>(); for (ImageAzure imageAzure : imageAzures) { imageAzureMap.put(imageAzure.getImageNo(), imageAzure); } // ? List<Image> images = imageDao.readAll(); // ImageDto? Map<Long, ImageDto> imageDtoMap = new LinkedHashMap<Long, ImageDto>(); for (Image image : images) { ImageDto imageDto = new ImageDto(); imageDto.setImage(image); imageDto.setImageAws(imageAwsMap.get(image.getImageNo())); imageDto.setImageVmware(imageVmwareMap.get(image.getImageNo())); imageDto.setImageCloudstack(imageCloudstackMap.get(image.getImageNo())); imageDto.setImageNifty(imageNiftyMap.get(image.getImageNo())); imageDto.setImageVcloud(imageVcloudMap.get(image.getImageNo())); imageDto.setImageAzure(imageAzureMap.get(image.getImageNo())); imageDtoMap.put(image.getImageNo(), imageDto); } // AWS?? List<AwsLoadBalancer> awsLoadBalancers = awsLoadBalancerDao.readInLoadBalancerNos(loadBalancerNos); Map<Long, AwsLoadBalancer> awsLoadBalancerMap = new LinkedHashMap<Long, AwsLoadBalancer>(); for (AwsLoadBalancer awsLoadBalancer : awsLoadBalancers) { awsLoadBalancerMap.put(awsLoadBalancer.getLoadBalancerNo(), awsLoadBalancer); } // CloudStack?? List<CloudstackLoadBalancer> cloudstackLoadBalancers = cloudstackLoadBalancerDao .readInLoadBalancerNos(loadBalancerNos); Map<Long, CloudstackLoadBalancer> cloudstackLoadBalancerMap = new LinkedHashMap<Long, CloudstackLoadBalancer>(); for (CloudstackLoadBalancer cloudstackLoadBalancer : cloudstackLoadBalancers) { cloudstackLoadBalancerMap.put(cloudstackLoadBalancer.getLoadBalancerNo(), cloudstackLoadBalancer); } // ???? List<ComponentLoadBalancer> componentLoadBalancers = componentLoadBalancerDao .readInLoadBalancerNos(loadBalancerNos); Map<Long, ComponentLoadBalancer> componentLoadBalancerMap = new LinkedHashMap<Long, ComponentLoadBalancer>(); for (ComponentLoadBalancer componentLoadBalancer : componentLoadBalancers) { componentLoadBalancerMap.put(componentLoadBalancer.getLoadBalancerNo(), componentLoadBalancer); } // ? List<LoadBalancerListener> allListeners = loadBalancerListenerDao.readInLoadBalancerNos(loadBalancerNos); Map<Long, List<LoadBalancerListener>> listenersMap = new LinkedHashMap<Long, List<LoadBalancerListener>>(); for (LoadBalancerListener listener : allListeners) { List<LoadBalancerListener> listeners = listenersMap.get(listener.getLoadBalancerNo()); if (listeners == null) { listeners = new ArrayList<LoadBalancerListener>(); listenersMap.put(listener.getLoadBalancerNo(), listeners); } listeners.add(listener); } // ?? List<LoadBalancerHealthCheck> allHealthChecks = loadBalancerHealthCheckDao .readInLoadBalancerNos(loadBalancerNos); Map<Long, LoadBalancerHealthCheck> healthCheckMap = new LinkedHashMap<Long, LoadBalancerHealthCheck>(); for (LoadBalancerHealthCheck healthCheck : allHealthChecks) { healthCheckMap.put(healthCheck.getLoadBalancerNo(), healthCheck); } // ? List<AutoScalingConf> autoScalingConfs = autoScalingConfDao.readInLoadBalancerNos(loadBalancerNos); Map<Long, AutoScalingConf> autoScalingConfMap = new LinkedHashMap<Long, AutoScalingConf>(); for (AutoScalingConf autoScalingConf : autoScalingConfs) { autoScalingConfMap.put(autoScalingConf.getLoadBalancerNo(), autoScalingConf); } // ?? List<LoadBalancerInstance> allLbInstances = loadBalancerInstanceDao.readInLoadBalancerNos(loadBalancerNos); Map<Long, List<LoadBalancerInstance>> lbInstancesMap = new LinkedHashMap<Long, List<LoadBalancerInstance>>(); for (LoadBalancerInstance lbInstance : allLbInstances) { List<LoadBalancerInstance> lbInstances = lbInstancesMap.get(lbInstance.getLoadBalancerNo()); if (lbInstances == null) { lbInstances = new ArrayList<LoadBalancerInstance>(); lbInstancesMap.put(lbInstance.getLoadBalancerNo(), lbInstances); } lbInstances.add(lbInstance); } // ? Set<Long> targetInstanceNos = new HashSet<Long>(); for (LoadBalancerInstance lbInstance : allLbInstances) { targetInstanceNos.add(lbInstance.getInstanceNo()); } List<Instance> targetInstances = instanceDao.readInInstanceNos(targetInstanceNos); Map<Long, Instance> targetInstanceMap = new HashMap<Long, Instance>(); for (Instance targetInstance : targetInstances) { targetInstanceMap.put(targetInstance.getInstanceNo(), targetInstance); } List<LoadBalancerDto> dtos = new ArrayList<LoadBalancerDto>(); for (LoadBalancer loadBalancer : loadBalancers) { Long loadBalancerNo = loadBalancer.getLoadBalancerNo(); AwsLoadBalancer awsLoadBalancer = awsLoadBalancerMap.get(loadBalancerNo); CloudstackLoadBalancer cloudstackLoadBalancer = cloudstackLoadBalancerMap.get(loadBalancerNo); ComponentLoadBalancer componentLoadBalancer = componentLoadBalancerMap .get(loadBalancer.getLoadBalancerNo()); List<LoadBalancerListener> listeners = listenersMap.get(loadBalancerNo); if (listeners == null) { listeners = new ArrayList<LoadBalancerListener>(); } LoadBalancerHealthCheck healthCheck = healthCheckMap.get(loadBalancerNo); AutoScalingConfDto autoScalingConfDto = null; if (BooleanUtils.toBoolean(Config.getProperty("autoScaling.useAutoScaling"))) { autoScalingConfDto = new AutoScalingConfDto(); AutoScalingConf autoScalingConf = autoScalingConfMap.get(loadBalancerNo); autoScalingConfDto.setAutoScalingConf(autoScalingConf); autoScalingConfDto.setPlatform(platformDtoMap.get(autoScalingConf.getPlatformNo())); autoScalingConfDto.setImage(imageDtoMap.get(autoScalingConf.getImageNo())); } List<LoadBalancerInstance> lbInstances = lbInstancesMap.get(loadBalancerNo); if (lbInstances == null) { lbInstances = new ArrayList<LoadBalancerInstance>(); } // ?????? ComponentLoadBalancerDto componentLoadBalancerDto = null; if (componentLoadBalancer != null) { Component component = componentDao.read(componentLoadBalancer.getComponentNo()); List<Long> instanceNos = new ArrayList<Long>(); List<ComponentInstance> componentInstances = componentInstanceDao .readByComponentNo(componentLoadBalancer.getComponentNo()); for (ComponentInstance componentInstance : componentInstances) { instanceNos.add(componentInstance.getInstanceNo()); } List<Instance> instances = instanceDao.readInInstanceNos(instanceNos); // IP String ipAddress = null; if (!instances.isEmpty()) { Boolean showPublicIp = BooleanUtils.toBooleanObject(Config.getProperty("ui.showPublicIp")); if (BooleanUtils.isTrue(showPublicIp)) { //ui.showPublicIp = true ???URL?PublicIp ipAddress = instances.get(0).getPublicIp(); } else { //ui.showPublicIp = false ???URL?PrivateIp ipAddress = instances.get(0).getPrivateIp(); } } componentLoadBalancerDto = new ComponentLoadBalancerDto(); componentLoadBalancerDto.setComponentLoadBalancer(componentLoadBalancer); componentLoadBalancerDto.setComponent(component); componentLoadBalancerDto.setInstances(instances); componentLoadBalancerDto.setIpAddress(ipAddress); } // Collections.sort(listeners, Comparators.COMPARATOR_LOAD_BALANCER_LISTENER); Collections.sort(lbInstances, Comparators.COMPARATOR_LOAD_BALANCER_INSTANCE); // TODO: ???????????????? // ?? LoadBalancerStatus status = LoadBalancerStatus.fromStatus(loadBalancer.getStatus()); if (BooleanUtils.isTrue(loadBalancer.getEnabled())) { if (status == LoadBalancerStatus.STOPPED) { status = LoadBalancerStatus.STARTING; } else if (status == LoadBalancerStatus.RUNNING && BooleanUtils.isTrue(loadBalancer.getConfigure())) { status = LoadBalancerStatus.CONFIGURING; } } else { if (status == LoadBalancerStatus.RUNNING || status == LoadBalancerStatus.WARNING) { status = LoadBalancerStatus.STOPPING; } } loadBalancer.setStatus(status.toString()); // ? for (LoadBalancerListener listener : listeners) { LoadBalancerListenerStatus status2 = LoadBalancerListenerStatus.fromStatus(listener.getStatus()); if (BooleanUtils.isTrue(loadBalancer.getEnabled()) && BooleanUtils.isTrue(listener.getEnabled())) { if (status2 == LoadBalancerListenerStatus.STOPPED) { status2 = LoadBalancerListenerStatus.STARTING; } else if (status2 == LoadBalancerListenerStatus.RUNNING && BooleanUtils.isTrue(listener.getConfigure())) { status2 = LoadBalancerListenerStatus.CONFIGURING; } } else { if (status2 == LoadBalancerListenerStatus.RUNNING || status2 == LoadBalancerListenerStatus.WARNING) { status2 = LoadBalancerListenerStatus.STOPPING; } } listener.setStatus(status2.toString()); } LoadBalancerDto dto = new LoadBalancerDto(); dto.setLoadBalancer(loadBalancer); dto.setPlatform(platformDtoMap.get(loadBalancer.getPlatformNo())); dto.setAwsLoadBalancer(awsLoadBalancer); dto.setCloudstackLoadBalancer(cloudstackLoadBalancer); dto.setComponentLoadBalancerDto(componentLoadBalancerDto); dto.setLoadBalancerListeners(listeners); dto.setLoadBalancerHealthCheck(healthCheck); dto.setLoadBalancerInstances(lbInstances); dto.setAutoScalingConf(autoScalingConfDto); dtos.add(dto); } // Collections.sort(dtos, Comparators.COMPARATOR_LOAD_BALANCER_DTO); return dtos; } /** * {@inheritDoc} */ @Override public Long getLoadBalancerInstance(Long loadBalancerNo) { //????ID?? ComponentLoadBalancer componentLoadBalancer = componentLoadBalancerDao.read(loadBalancerNo); List<ComponentInstance> componentInstances = componentInstanceDao .readByComponentNo(componentLoadBalancer.getComponentNo()); if (!componentInstances.isEmpty()) { return componentInstances.get(0).getInstanceNo(); } return null; } /** * {@inheritDoc} */ @Override public Long createAwsLoadBalancer(Long farmNo, String loadBalancerName, String comment, Long platformNo, Long componentNo, boolean internal) { // ? if (farmNo == null) { throw new AutoApplicationException("ECOMMON-000003", "farmNo"); } if (loadBalancerName == null || loadBalancerName.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerName"); } if (platformNo == null) { throw new AutoApplicationException("ECOMMON-000003", "platformNo"); } if (componentNo == null) { throw new AutoApplicationException("ECOMMON-000003", "componentNo"); } // ?? if (!Pattern.matches("^[0-9a-z]|[0-9a-z][0-9a-z-]*[0-9a-z]$", loadBalancerName)) { throw new AutoApplicationException("ECOMMON-000012", "loadBalancerName"); } // TODO: ?? // ?? Platform platform = platformDao.read(platformNo); if (PCCConstant.PLATFORM_TYPE_AWS.equals(platform.getPlatformType()) == false) { throw new AutoApplicationException("ESERVICE-000606", platformNo); } // ?? Image image = null; List<Image> images = imageDao.readAll(); for (Image image2 : images) { if (image2.getPlatformNo().equals(platformNo.longValue()) && PCCConstant.IMAGE_NAME_ELB.equals(image2.getImageName())) { image = image2; break; } } if (image == null) { // aws????? throw new AutoApplicationException("ESERVICE-000631", platformNo); } // ?????? LoadBalancer checkLoadBalancer = loadBalancerDao.readByFarmNoAndLoadBalancerName(farmNo, loadBalancerName); if (checkLoadBalancer != null) { // ???????? throw new AutoApplicationException("ESERVICE-000601", loadBalancerName); } // ???? Instance checkInstance = instanceDao.readByFarmNoAndInstanceName(farmNo, loadBalancerName); if (checkInstance != null) { // ??????? throw new AutoApplicationException("ESERVICE-000626", loadBalancerName); } // ?? Farm farm = farmDao.read(farmNo); if (farm == null) { throw new AutoApplicationException("ESERVICE-000602", farmNo); } // ???? long countComponent = componentDao.countByComponentNo(componentNo); if (countComponent == 0) { // ??????? throw new AutoApplicationException("ESERVICE-000607", componentNo); } //AWS??? AwsCertificate awsCertificate = awsCertificateDao.read(farm.getUserNo(), platformNo); //?()?? PlatformAws platformAws = platformAwsDao.read(platformNo); String subnetId = null; String availabilityZone = null; if (platformAws.getVpc() && StringUtils.isNotEmpty(awsCertificate.getDefLbSubnet())) { //VPC ?? ?(?)??? List<String> defLbSubnets = new ArrayList<String>(); for (String lbSubnet : awsCertificate.getDefLbSubnet().split(",")) { defLbSubnets.add(lbSubnet.trim()); } List<String> zones = new ArrayList<String>(); StringBuffer subnetBuffer = new StringBuffer(); StringBuffer zoneBuffer = new StringBuffer(); List<SubnetDto> subnetDtos = iaasDescribeService.getSubnets(farm.getUserNo(), platformNo, platformAws.getVpcId()); for (SubnetDto subnetDto : subnetDtos) { if (defLbSubnets.contains(subnetDto.getSubnetId()) && zones.contains(subnetDto.getZoneid()) == false) { subnetBuffer.append( subnetBuffer.length() > 0 ? "," + subnetDto.getSubnetId() : subnetDto.getSubnetId()); zoneBuffer .append(zoneBuffer.length() > 0 ? "," + subnetDto.getZoneid() : subnetDto.getZoneid()); zones.add(subnetDto.getZoneid()); } } //?? subnetId = subnetBuffer.toString(); availabilityZone = zoneBuffer.toString(); } //?? String groupName = null; if (platformAws.getVpc()) { //VPC?? List<SecurityGroupDto> securityGroupDtos = iaasDescribeService.getSecurityGroups(farm.getUserNo(), platformNo, platformAws.getVpcId()); for (SecurityGroupDto securityGroupDto : securityGroupDtos) { if ("default".equals(securityGroupDto.getGroupName())) { //default?????default? groupName = securityGroupDto.getGroupName(); break; } } if (groupName == null && securityGroupDtos.size() > 0) { //default????1 groupName = securityGroupDtos.get(0).getGroupName(); } } // ??? LoadBalancer loadBalancer = new LoadBalancer(); loadBalancer.setFarmNo(farmNo); loadBalancer.setLoadBalancerName(loadBalancerName); loadBalancer.setComment(comment); loadBalancer.setFqdn(loadBalancerName + "." + farm.getDomainName()); loadBalancer.setPlatformNo(platformNo); loadBalancer.setType(PCCConstant.LOAD_BALANCER_ELB); loadBalancer.setEnabled(false); loadBalancer.setStatus(LoadBalancerStatus.STOPPED.toString()); loadBalancer.setComponentNo(componentNo); loadBalancerDao.create(loadBalancer); Long loadBalancerNo = loadBalancer.getLoadBalancerNo(); // AWS??? AwsLoadBalancer awsLoadBalancer = new AwsLoadBalancer(); awsLoadBalancer.setLoadBalancerNo(loadBalancerNo); awsLoadBalancer.setName(loadBalancerName + "-" + loadBalancerNo); awsLoadBalancer.setDnsName(null); awsLoadBalancer.setSubnetId(subnetId); awsLoadBalancer.setSecurityGroups(groupName); awsLoadBalancer.setAvailabilityZone(availabilityZone); awsLoadBalancer.setInternal(internal); awsLoadBalancerDao.create(awsLoadBalancer); // ??? createDefaultHealthCheck(loadBalancer); // ?? createDefaultAutoScalingConf(loadBalancer); // ?? registerInstances(loadBalancer); // ?? Boolean useZabbix = BooleanUtils.toBooleanObject(Config.getProperty("zabbix.useZabbix")); if (BooleanUtils.isTrue(useZabbix)) { elbZabbixHostProcess.createElbHostgroup(loadBalancerNo); } // eventLogger.log(EventLogLevel.INFO, farmNo, farm.getFarmName(), null, null, null, null, "LoadBalancerCreate", null, null, new Object[] { loadBalancerName, platform.getPlatformName(), PCCConstant.LOAD_BALANCER_ELB }); return loadBalancerNo; } /** * {@inheritDoc} */ @Override public Long createCloudstackLoadBalancer(Long farmNo, String loadBalancerName, String comment, Long platformNo, Long componentNo) { // ? if (farmNo == null) { throw new AutoApplicationException("ECOMMON-000003", "farmNo"); } if (loadBalancerName == null || loadBalancerName.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerName"); } if (platformNo == null) { throw new AutoApplicationException("ECOMMON-000003", "platformNo"); } if (componentNo == null) { throw new AutoApplicationException("ECOMMON-000003", "componentNo"); } // ?? if (!Pattern.matches("^[0-9a-z]|[0-9a-z][0-9a-z-]*[0-9a-z]$", loadBalancerName)) { throw new AutoApplicationException("ECOMMON-000012", "loadBalancerName"); } // TODO: ?? // ?? Platform platform = platformDao.read(platformNo); if (PCCConstant.PLATFORM_TYPE_CLOUDSTACK.equals(platform.getPlatformType()) == false) { throw new AutoApplicationException("ESERVICE-000606", platformNo); } // ?????? LoadBalancer checkLoadBalancer = loadBalancerDao.readByFarmNoAndLoadBalancerName(farmNo, loadBalancerName); if (checkLoadBalancer != null) { // ???????? throw new AutoApplicationException("ESERVICE-000601", loadBalancerName); } // ???? Instance checkInstance = instanceDao.readByFarmNoAndInstanceName(farmNo, loadBalancerName); if (checkInstance != null) { // ??????? throw new AutoApplicationException("ESERVICE-000626", loadBalancerName); } // ?? Farm farm = farmDao.read(farmNo); if (farm == null) { throw new AutoApplicationException("ESERVICE-000602", farmNo); } // ???? long countComponent = componentDao.countByComponentNo(componentNo); if (countComponent == 0) { // ??????? throw new AutoApplicationException("ESERVICE-000607", componentNo); } // ??? LoadBalancer loadBalancer = new LoadBalancer(); loadBalancer.setFarmNo(farmNo); loadBalancer.setLoadBalancerName(loadBalancerName); loadBalancer.setComment(comment); loadBalancer.setFqdn(loadBalancerName + "." + farm.getDomainName()); loadBalancer.setPlatformNo(platformNo); loadBalancer.setType(PCCConstant.LOAD_BALANCER_CLOUDSTACK); loadBalancer.setEnabled(false); loadBalancer.setStatus(LoadBalancerStatus.STOPPED.toString()); loadBalancer.setComponentNo(componentNo); loadBalancerDao.create(loadBalancer); Long loadBalancerNo = loadBalancer.getLoadBalancerNo(); PlatformCloudstack platformCloudstack = platformCloudstackDao.read(platformNo); // Cloudstack??? //"NAME", "ALGORITHM", "ZONEID", "PUBLICPORT", "PRIVATEPORT"??? //?? CloudstackLoadBalancer cloudstackLoadBalancer = new CloudstackLoadBalancer(); cloudstackLoadBalancer.setLoadBalancerNo(loadBalancerNo); cloudstackLoadBalancer.setName(loadBalancerName + "-" + loadBalancerNo); cloudstackLoadBalancer.setAlgorithm("roundrobin"); cloudstackLoadBalancer.setZoneid(platformCloudstack.getZoneId()); cloudstackLoadBalancer.setPublicport("80"); cloudstackLoadBalancer.setPrivateport("80"); cloudstackLoadBalancerDao.create(cloudstackLoadBalancer); // ?? createDefaultAutoScalingConf(loadBalancer); // ?? registerInstances(loadBalancer); // eventLogger.log(EventLogLevel.INFO, farmNo, farm.getFarmName(), null, null, null, null, "LoadBalancerCreate", null, null, new Object[] { loadBalancerName, platform.getPlatformName(), PCCConstant.LOAD_BALANCER_ELB }); return loadBalancerNo; } /** * {@inheritDoc} */ @Override public Long createUltraMonkeyLoadBalancer(Long farmNo, String loadBalancerName, String comment, Long platformNo, Long componentNo) { // ? if (farmNo == null) { throw new AutoApplicationException("ECOMMON-000003", "farmNo"); } if (loadBalancerName == null || loadBalancerName.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerName"); } if (platformNo == null) { throw new AutoApplicationException("ECOMMON-000003", "platformNo"); } if (componentNo == null) { throw new AutoApplicationException("ECOMMON-000003", "componentNo"); } // ?? if (!Pattern.matches("^[0-9a-z]|[0-9a-z][0-9a-z-]*[0-9a-z]$", loadBalancerName)) { throw new AutoApplicationException("ECOMMON-000012", "loadBalancerName"); } // TODO: ?? // ?? Image image = null; List<Image> images = imageDao.readAll(); for (Image image2 : images) { if (image2.getPlatformNo().equals(platformNo.longValue()) && PCCConstant.IMAGE_NAME_ULTRAMONKEY.equals(image2.getImageName())) { image = image2; break; } } if (image == null) { // UltraMonkey????? throw new AutoApplicationException("ESERVICE-000625", platformNo); } // ?????? LoadBalancer checkLoadBalancer = loadBalancerDao.readByFarmNoAndLoadBalancerName(farmNo, loadBalancerName); if (checkLoadBalancer != null) { // ???????? throw new AutoApplicationException("ESERVICE-000601", loadBalancerName); } // ???? Instance checkInstance = instanceDao.readByFarmNoAndInstanceName(farmNo, loadBalancerName); if (checkInstance != null) { // ??????? throw new AutoApplicationException("ESERVICE-000626", loadBalancerName); } // ?? Farm farm = farmDao.read(farmNo); if (farm == null) { throw new AutoApplicationException("ESERVICE-000602", farmNo); } // ???? long countComponent = componentDao.countByComponentNo(componentNo); if (countComponent == 0) { // ??????? throw new AutoApplicationException("ESERVICE-000607", componentNo); } // UltraMonkey?? //String lbInstanceName = "lb-" + loadBalancerNo; String lbInstanceName = loadBalancerName; Long lbInstanceNo = null; Platform platform = platformDao.read(platformNo); // TODO CLOUD BRANCHING if (PCCConstant.PLATFORM_TYPE_AWS.equals(platform.getPlatformType())) { ImageAws imageAws = imageAwsDao.read(image.getImageNo()); String[] instanceTypes = StringUtils.split(imageAws.getInstanceTypes(), ","); lbInstanceNo = instanceService.createIaasInstance(farmNo, lbInstanceName, platformNo, null, image.getImageNo(), instanceTypes[0].trim()); } else if (PCCConstant.PLATFORM_TYPE_VMWARE.equals(platform.getPlatformType())) { ImageVmware imageVmware = imageVmwareDao.read(image.getImageNo()); String[] instanceTypes = StringUtils.split(imageVmware.getInstanceTypes(), ","); lbInstanceNo = instanceService.createVmwareInstance(farmNo, lbInstanceName, platformNo, null, image.getImageNo(), instanceTypes[0].trim()); } else if (PCCConstant.PLATFORM_TYPE_NIFTY.equals(platform.getPlatformType())) { ImageNifty imageNifty = imageNiftyDao.read(image.getImageNo()); String[] instanceTypes = StringUtils.split(imageNifty.getInstanceTypes(), ","); lbInstanceNo = instanceService.createNiftyInstance(farmNo, lbInstanceName, platformNo, null, image.getImageNo(), instanceTypes[0].trim()); } else if (PCCConstant.PLATFORM_TYPE_CLOUDSTACK.equals(platform.getPlatformType())) { ImageCloudstack imageCloudstack = imageCloudstackDao.read(image.getImageNo()); String[] instanceTypes = StringUtils.split(imageCloudstack.getInstanceTypes(), ","); lbInstanceNo = instanceService.createIaasInstance(farmNo, lbInstanceName, platformNo, null, image.getImageNo(), instanceTypes[0].trim()); } else if (PCCConstant.PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) { ImageVcloud imageVcloud = imageVcloudDao.read(image.getImageNo()); String[] instanceTypes = StringUtils.split(imageVcloud.getInstanceTypes(), ","); lbInstanceNo = instanceService.createIaasInstance(farmNo, lbInstanceName, platformNo, null, image.getImageNo(), instanceTypes[0].trim()); } else if (PCCConstant.PLATFORM_TYPE_AZURE.equals(platform.getPlatformType())) { ImageAzure imageAzure = imageAzureDao.read(image.getImageNo()); String[] instanceTypes = StringUtils.split(imageAzure.getInstanceTypes(), ","); lbInstanceNo = instanceService.createIaasInstance(farmNo, lbInstanceName, platformNo, null, image.getImageNo(), instanceTypes[0].trim()); } else if (PCCConstant.PLATFORM_TYPE_OPENSTACK.equals(platform.getPlatformType())) { ImageOpenstack imageOpenstack = imageOpenstackDao.read(image.getImageNo()); String[] instanceTypes = StringUtils.split(imageOpenstack.getInstanceTypes(), ","); lbInstanceNo = instanceService.createIaasInstance(farmNo, lbInstanceName, platformNo, null, image.getImageNo(), instanceTypes[0].trim()); } // UltraMonkey??? Instance lbInstance = instanceDao.read(lbInstanceNo); lbInstance.setLoadBalancer(true); instanceDao.update(lbInstance); // ??? LoadBalancer loadBalancer = new LoadBalancer(); loadBalancer.setFarmNo(farmNo); loadBalancer.setLoadBalancerName(loadBalancerName); loadBalancer.setComment(comment); loadBalancer.setFqdn(loadBalancerName + "." + farm.getDomainName()); loadBalancer.setPlatformNo(platformNo); loadBalancer.setType(PCCConstant.LOAD_BALANCER_ULTRAMONKEY); loadBalancer.setEnabled(false); loadBalancer.setStatus(LoadBalancerStatus.STOPPED.toString()); loadBalancer.setComponentNo(componentNo); loadBalancerDao.create(loadBalancer); Long loadBalancerNo = loadBalancer.getLoadBalancerNo(); // UltraMonkey???? String lbComponentName = "lb-" + loadBalancerNo; Long lbComponentTypeNo = Long.valueOf(image.getComponentTypeNos()); Long lbComponentNo = componentService.createComponent(farmNo, lbComponentName, lbComponentTypeNo, null, null); // UltraMonkey????? Component lbComponent = componentDao.read(lbComponentNo); lbComponent.setLoadBalancer(true); componentDao.update(lbComponent); // ????? componentService.associateInstances(lbComponentNo, Arrays.asList(lbInstanceNo)); // ????? ComponentLoadBalancer componentLoadBalancer = new ComponentLoadBalancer(); componentLoadBalancer.setLoadBalancerNo(loadBalancerNo); componentLoadBalancer.setComponentNo(lbComponentNo); componentLoadBalancerDao.create(componentLoadBalancer); // ??? createDefaultHealthCheck(loadBalancer); // ?? createDefaultAutoScalingConf(loadBalancer); // ?? registerInstances(loadBalancer); // eventLogger.log(EventLogLevel.INFO, farmNo, farm.getFarmName(), null, null, null, null, "LoadBalancerCreate", null, null, new Object[] { loadBalancerName, platform.getPlatformName(), PCCConstant.LOAD_BALANCER_ULTRAMONKEY }); return loadBalancerNo; } protected void createDefaultHealthCheck(LoadBalancer loadBalancer) { Component component = componentDao.read(loadBalancer.getComponentNo()); ComponentType componentType = componentTypeDao.read(component.getComponentTypeNo()); String checkProtocol; Integer checkPort; String checkPath = null; Integer checkTimeout = 5; Integer checkInterval = 10; Integer healthyThreshold = 3; Integer unhealthyThreshold = 2; if ("apache".equals(componentType.getComponentTypeName())) { checkProtocol = "HTTP"; checkPort = 80; checkPath = "/index.html"; } else if ("tomcat".equals(componentType.getComponentTypeName()) || "geronimo".equals(componentType.getComponentTypeName())) { checkProtocol = "HTTP"; checkPort = 8080; checkPath = "/index.html"; } else if ("mysql".equals(componentType.getComponentTypeName())) { checkProtocol = "TCP"; checkPort = 3306; } else { return; } configureHealthCheck(loadBalancer.getLoadBalancerNo(), checkProtocol, checkPort, checkPath, checkTimeout, checkInterval, healthyThreshold, unhealthyThreshold); } protected void createDefaultAutoScalingConf(LoadBalancer loadBalancer) { // ?/ AutoScalingConf autoScalingConf = autoScalingConfDao.read(loadBalancer.getLoadBalancerNo()); boolean exist = (autoScalingConf != null); if (!exist) { autoScalingConf = new AutoScalingConf(); autoScalingConf.setLoadBalancerNo(loadBalancer.getLoadBalancerNo()); autoScalingConf.setFarmNo(loadBalancer.getFarmNo()); } autoScalingConf.setPlatformNo(0L); autoScalingConf.setImageNo(0L); //autoScalingConf.setInstanceType(null); //autoScalingConf.setNamingRule(null;) autoScalingConf.setIdleTimeMax(0L); autoScalingConf.setIdleTimeMin(0L); autoScalingConf.setContinueLimit(0L); autoScalingConf.setAddCount(0L); autoScalingConf.setDelCount(0L); autoScalingConf.setEnabled(false); autoScalingConfDao.create(autoScalingConf); } protected void registerInstances(LoadBalancer loadBalancer) { // ?????? List<ComponentInstance> componentInstances = componentInstanceDao .readByComponentNo(loadBalancer.getComponentNo()); List<Long> instanceNos = new ArrayList<Long>(); for (ComponentInstance componentInstance : componentInstances) { if (BooleanUtils.isNotTrue(componentInstance.getAssociate())) { continue; } instanceNos.add(componentInstance.getInstanceNo()); } List<Instance> instances = instanceDao.readInInstanceNos(instanceNos); if (instanceNos.isEmpty()) { return; } // ????? List<LoadBalancer> allLoadBalancers = loadBalancerDao.readByFarmNo(loadBalancer.getFarmNo()); List<Long> loadBalancerNos = new ArrayList<Long>(); for (LoadBalancer tmpLoadBalancer : allLoadBalancers) { loadBalancerNos.add(tmpLoadBalancer.getLoadBalancerNo()); } List<LoadBalancerInstance> allLbInstances = loadBalancerInstanceDao.readInLoadBalancerNos(loadBalancerNos); List<Instance> tmpInstances = new ArrayList<Instance>(); for (Instance instance : instances) { boolean check = checkAssociate(loadBalancer, instance, allLoadBalancers, allLbInstances); if (check) { tmpInstances.add(instance); } } instances = tmpInstances; // ???? for (Instance instance : instances) { LoadBalancerInstance lbInstance = new LoadBalancerInstance(); lbInstance.setLoadBalancerNo(loadBalancer.getLoadBalancerNo()); lbInstance.setInstanceNo(instance.getInstanceNo()); lbInstance.setEnabled(true); lbInstance.setStatus(LoadBalancerInstanceStatus.STOPPED.toString()); loadBalancerInstanceDao.create(lbInstance); } } protected boolean checkAssociate(LoadBalancer loadBalancer, Instance instance, List<LoadBalancer> allLoadBalancers, List<LoadBalancerInstance> allLbInstances) { Map<Long, LoadBalancer> allLoadBalancerMap = new HashMap<Long, LoadBalancer>(); for (LoadBalancer loadBalancer2 : allLoadBalancers) { allLoadBalancerMap.put(loadBalancer2.getLoadBalancerNo(), loadBalancer2); } // AWS??? String type = loadBalancer.getType(); if (PCCConstant.LOAD_BALANCER_ELB.equals(type)) { // ?????????? if (!loadBalancer.getPlatformNo().equals(instance.getPlatformNo())) { return false; } // ?AWS?????????????? for (LoadBalancerInstance lbInstance : allLbInstances) { if (!instance.getInstanceNo().equals(lbInstance.getInstanceNo())) { continue; } // ??AWS??????????? if (loadBalancer.getLoadBalancerNo().equals(lbInstance.getLoadBalancerNo())) { continue; } // ????????????? if (BooleanUtils.isNotTrue(lbInstance.getEnabled()) && LoadBalancerInstanceStatus .fromStatus(lbInstance.getStatus()) == LoadBalancerInstanceStatus.STOPPED) { continue; } LoadBalancer loadBalancer2 = allLoadBalancerMap.get(lbInstance.getLoadBalancerNo()); String type2 = loadBalancer2.getType(); if (PCCConstant.LOAD_BALANCER_ELB.equals(type2) || PCCConstant.LOAD_BALANCER_CLOUDSTACK.equals(type2)) { // ?AWS?????????????? return false; } } //VPC????? PlatformAws platformAws = platformAwsDao.read(loadBalancer.getPlatformNo()); if (platformAws.getVpc()) { AwsLoadBalancer awsLoadBalancer = awsLoadBalancerDao.read(loadBalancer.getLoadBalancerNo()); AwsInstance awsInstance = awsInstanceDao.read(instance.getInstanceNo()); if (StringUtils.isEmpty(awsLoadBalancer.getAvailabilityZone())) { //???????(??????)????(???) return false; } else { //?????(????) List<String> zones = new ArrayList<String>(); for (String zone : awsLoadBalancer.getAvailabilityZone().split(",")) { zones.add(zone.trim()); } if (zones.contains(awsInstance.getAvailabilityZone()) == false) { //??????(??????????)???? return false; } } } } return true; } /** * {@inheritDoc} */ @Override public void updateAwsLoadBalancer(Long loadBalancerNo, String loadBalancerName, String comment, Long componentNo, String subnetId, String securityGroupName, String availabilityZone, boolean internal) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (loadBalancerName == null || loadBalancerName.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerName"); } if (componentNo == null) { throw new AutoApplicationException("ECOMMON-000003", "componentNo"); } // ?? if (!Pattern.matches("^[0-9a-z]|[0-9a-z][0-9a-z-]*[0-9a-z]$", loadBalancerName)) { throw new AutoApplicationException("ECOMMON-000012", "loadBalancerName"); } // TODO: ?? // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); AwsLoadBalancer awsLoadBalancer = awsLoadBalancerDao.read(loadBalancerNo); if (loadBalancer == null || awsLoadBalancer == null) { // ?????? throw new AutoApplicationException("ESERVICE-000603", loadBalancerNo); } // ??????? if (LoadBalancerStatus.fromStatus(loadBalancer.getStatus()) != LoadBalancerStatus.STOPPED) { // ?????????????????? if (!StringUtils.equals(loadBalancer.getLoadBalancerName(), loadBalancerName) || !componentNo.equals(loadBalancer.getComponentNo())) { throw new AutoApplicationException("ESERVICE-000604", loadBalancer.getLoadBalancerName()); } if (StringUtils.isEmpty(awsLoadBalancer.getSubnetId()) ? StringUtils.isNotEmpty(subnetId) : !StringUtils.equals(awsLoadBalancer.getSubnetId(), subnetId)) { throw new AutoApplicationException("ESERVICE-000604", loadBalancer.getLoadBalancerName()); } if (StringUtils.isEmpty(awsLoadBalancer.getSecurityGroups()) ? StringUtils.isNotEmpty(securityGroupName) : !StringUtils.equals(awsLoadBalancer.getSecurityGroups(), securityGroupName)) { throw new AutoApplicationException("ESERVICE-000604", loadBalancer.getLoadBalancerName()); } } // ????? if (!StringUtils.equals(loadBalancer.getLoadBalancerName(), loadBalancerName)) { // ?????? LoadBalancer checkLoadBalancer = loadBalancerDao .readByFarmNoAndLoadBalancerName(loadBalancer.getFarmNo(), loadBalancerName); if (checkLoadBalancer != null && !loadBalancerNo.equals(checkLoadBalancer.getLoadBalancerNo())) { // ???????? throw new AutoApplicationException("ESERVICE-000601", loadBalancerName); } // ???? Instance checkInstance = instanceDao.readByFarmNoAndInstanceName(loadBalancer.getFarmNo(), loadBalancerName); if (checkInstance != null) { // ??????? throw new AutoApplicationException("ESERVICE-000626", loadBalancerName); } } // ???? long countComponent = componentDao.countByComponentNo(componentNo); if (countComponent == 0) { // ??????? throw new AutoApplicationException("ESERVICE-000607", componentNo); } // ??????????? long countListener = loadBalancerListenerDao.countByLoadBalancerNo(loadBalancerNo); if (countListener > 0) { if (!loadBalancer.getComponentNo().equals(componentNo)) { // ???????? throw new AutoApplicationException("ESERVICE-000608"); } } Farm farm = farmDao.read(loadBalancer.getFarmNo()); //? if (loadBalancer.getComponentNo().equals(componentNo) == false || (StringUtils.isNotEmpty(awsLoadBalancer.getAvailabilityZone()) && StringUtils.equals(awsLoadBalancer.getAvailabilityZone(), availabilityZone) == false)) { //?????????EnableFalse?? List<LoadBalancerInstance> loadBalancerInstances = loadBalancerInstanceDao .readByLoadBalancerNo(loadBalancerNo); for (LoadBalancerInstance loadBalancerInstance : loadBalancerInstances) { loadBalancerInstance.setEnabled(false); loadBalancerInstanceDao.update(loadBalancerInstance); } } //AWS? awsLoadBalancer.setSubnetId(subnetId); awsLoadBalancer.setSecurityGroups(securityGroupName); awsLoadBalancer.setAvailabilityZone(availabilityZone); awsLoadBalancer.setInternal(internal); awsLoadBalancerDao.update(awsLoadBalancer); // ?? loadBalancer.setLoadBalancerName(loadBalancerName); loadBalancer.setComment(comment); loadBalancer.setFqdn(loadBalancerName + "." + farm.getDomainName()); loadBalancer.setComponentNo(componentNo); loadBalancerDao.update(loadBalancer); // eventLogger.log(EventLogLevel.INFO, farm.getFarmNo(), farm.getFarmName(), null, null, null, null, "LoadBalancerUpdate", null, null, new Object[] { loadBalancerName }); } /** * {@inheritDoc} */ @Override public void updateCloudstackLoadBalancer(Long loadBalancerNo, String loadBalancerName, String comment, Long componentNo, String algorithm, String pubricPort, String privatePort) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (loadBalancerName == null || loadBalancerName.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerName"); } if (componentNo == null) { throw new AutoApplicationException("ECOMMON-000003", "componentNo"); } if (algorithm == null) { throw new AutoApplicationException("ECOMMON-000003", "algorithm"); } if (pubricPort == null) { throw new AutoApplicationException("ECOMMON-000003", "pubricPort"); } if (privatePort == null) { throw new AutoApplicationException("ECOMMON-000003", "privatePort"); } // ?? if (!Pattern.matches("^[0-9a-z]|[0-9a-z][0-9a-z-]*[0-9a-z]$", loadBalancerName)) { throw new AutoApplicationException("ECOMMON-000012", "loadBalancerName"); } // TODO: ?? // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); CloudstackLoadBalancer csloadBalancer = cloudstackLoadBalancerDao.read(loadBalancerNo); if (loadBalancer == null) { // ?????? throw new AutoApplicationException("ESERVICE-000603", loadBalancerNo); } // ??????? if (LoadBalancerStatus.fromStatus(loadBalancer.getStatus()) != LoadBalancerStatus.STOPPED) { // ?????????????????? if (!StringUtils.equals(loadBalancer.getLoadBalancerName(), loadBalancerName) || !componentNo.equals(loadBalancer.getComponentNo())) { throw new AutoApplicationException("ESERVICE-000604", loadBalancer.getLoadBalancerName()); } if (!StringUtils.equals(csloadBalancer.getAlgorithm(), algorithm) || !StringUtils.equals(csloadBalancer.getPublicport(), pubricPort) || !StringUtils.equals(csloadBalancer.getPrivateport(), privatePort)) { throw new AutoApplicationException("ESERVICE-000604", loadBalancer.getLoadBalancerName()); } } // ????? if (!StringUtils.equals(loadBalancer.getLoadBalancerName(), loadBalancerName)) { // ?????? LoadBalancer checkLoadBalancer = loadBalancerDao .readByFarmNoAndLoadBalancerName(loadBalancer.getFarmNo(), loadBalancerName); if (checkLoadBalancer != null && !loadBalancerNo.equals(checkLoadBalancer.getLoadBalancerNo())) { // ???????? throw new AutoApplicationException("ESERVICE-000601", loadBalancerName); } // ???? Instance checkInstance = instanceDao.readByFarmNoAndInstanceName(loadBalancer.getFarmNo(), loadBalancerName); if (checkInstance != null) { // ??????? throw new AutoApplicationException("ESERVICE-000626", loadBalancerName); } } // ???? long countComponent = componentDao.countByComponentNo(componentNo); if (countComponent == 0) { // ??????? throw new AutoApplicationException("ESERVICE-000607", componentNo); } Farm farm = farmDao.read(loadBalancer.getFarmNo()); // ?? loadBalancer.setLoadBalancerName(loadBalancerName); loadBalancer.setComment(comment); loadBalancer.setFqdn(loadBalancerName + "." + farm.getDomainName()); loadBalancer.setComponentNo(componentNo); loadBalancerDao.update(loadBalancer); // ?? csloadBalancer.setAlgorithm(algorithm); csloadBalancer.setPublicport(pubricPort); csloadBalancer.setPrivateport(privatePort); cloudstackLoadBalancerDao.update(csloadBalancer); // eventLogger.log(EventLogLevel.INFO, farm.getFarmNo(), farm.getFarmName(), null, null, null, null, "LoadBalancerUpdate", null, null, new Object[] { loadBalancerName }); } /** * {@inheritDoc} */ @Override public void updateUltraMonkeyLoadBalancer(Long loadBalancerNo, String loadBalancerName, String comment, Long componentNo) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (loadBalancerName == null || loadBalancerName.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerName"); } if (componentNo == null) { throw new AutoApplicationException("ECOMMON-000003", "componentNo"); } // ?? if (!Pattern.matches("^[0-9a-z]|[0-9a-z][0-9a-z-]*[0-9a-z]$", loadBalancerName)) { throw new AutoApplicationException("ECOMMON-000012", "loadBalancerName"); } // TODO: ?? // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); if (loadBalancer == null) { // ?????? throw new AutoApplicationException("ESERVICE-000603", loadBalancerNo); } // ??????? if (LoadBalancerStatus.fromStatus(loadBalancer.getStatus()) != LoadBalancerStatus.STOPPED) { // ?????????????????? if (!StringUtils.equals(loadBalancer.getLoadBalancerName(), loadBalancerName) || !componentNo.equals(loadBalancer.getComponentNo())) { throw new AutoApplicationException("ESERVICE-000604", loadBalancer.getLoadBalancerName()); } } // ?????? LoadBalancer checkLoadBalancer = loadBalancerDao.readByFarmNoAndLoadBalancerName(loadBalancer.getFarmNo(), loadBalancerName); if (checkLoadBalancer != null && !loadBalancerNo.equals(checkLoadBalancer.getLoadBalancerNo())) { // ???????? throw new AutoApplicationException("ESERVICE-000601", loadBalancerName); } // ???? long countComponent = componentDao.countByComponentNo(componentNo); if (countComponent == 0) { // ??????? throw new AutoApplicationException("ESERVICE-000607", componentNo); } // ??????????? long countListener = loadBalancerListenerDao.countByLoadBalancerNo(loadBalancerNo); if (countListener > 0) { if (!loadBalancer.getComponentNo().equals(componentNo)) { // ???????? throw new AutoApplicationException("ESERVICE-000608"); } } Farm farm = farmDao.read(loadBalancer.getFarmNo()); // ?? loadBalancer.setLoadBalancerName(loadBalancerName); loadBalancer.setComment(comment); loadBalancer.setFqdn(loadBalancerName + "." + farm.getDomainName()); loadBalancer.setComponentNo(componentNo); loadBalancerDao.update(loadBalancer); // eventLogger.log(EventLogLevel.INFO, farm.getFarmNo(), farm.getFarmName(), null, null, null, null, "LoadBalancerUpdate", null, null, new Object[] { loadBalancerName }); } /** * {@inheritDoc} */ @Override public void deleteLoadBalancer(Long loadBalancerNo) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); if (loadBalancer == null) { // ?????? return; } // ???????????? if (LoadBalancerStatus.fromStatus(loadBalancer.getStatus()) != LoadBalancerStatus.STOPPED) { // ??????? throw new AutoApplicationException("ESERVICE-000605", loadBalancer.getLoadBalancerName()); } // ??? loadBalancerInstanceDao.deleteByLoadBalancerNo(loadBalancerNo); // ??? loadBalancerHealthCheckDao.deleteByLoadBalancerNo(loadBalancerNo); // ?? autoScalingConfDao.deleteByLoadBalancerNo(loadBalancerNo); // ?? List<LoadBalancerListener> listeners = loadBalancerListenerDao.readByLoadBalancerNo(loadBalancerNo); for (LoadBalancerListener listener : listeners) { deleteListener(loadBalancerNo, listener.getLoadBalancerPort()); } // ????? if (PCCConstant.LOAD_BALANCER_ELB.equals(loadBalancer.getType())) { deleteAwsLoadBalancer(loadBalancerNo); } else if (PCCConstant.LOAD_BALANCER_CLOUDSTACK.equals(loadBalancer.getType())) { deleteCloudstackLoadBalancer(loadBalancerNo); } else if (PCCConstant.LOAD_BALANCER_ULTRAMONKEY.equals(loadBalancer.getType())) { deleteUltraMonkeyLoadBalancer(loadBalancerNo); } // ??? loadBalancerDao.delete(loadBalancer); // Farm farm = farmDao.read(loadBalancer.getFarmNo()); eventLogger.log(EventLogLevel.INFO, farm.getFarmNo(), farm.getFarmName(), null, null, null, null, "LoadBalancerDelete", null, null, new Object[] { loadBalancer.getLoadBalancerName() }); } protected void deleteAwsLoadBalancer(Long loadBalancerNo) { LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); AwsLoadBalancer awsLoadbalancer = awsLoadBalancerDao.read(loadBalancerNo); Farm farm = farmDao.read(loadBalancer.getFarmNo()); // Zabbix?? try { // Zabbix?????? ZabbixProcessClient client = zabbixProcessClientFactory.createZabbixProcessClient(); client.deleteHost(awsLoadbalancer.getHostid()); // eventLogger.log(EventLogLevel.DEBUG, farm.getFarmNo(), farm.getFarmName(), null, null, null, null, "ZabbixUnregist", null, loadBalancer.getPlatformNo(), new Object[] { loadBalancer.getFqdn(), awsLoadbalancer.getHostid() }); } catch (RuntimeException ignore) { // ??????????? log.warn(ignore.getMessage()); } // AWS??? awsLoadBalancerDao.deleteByLoadBalancerNo(loadBalancerNo); } protected void deleteCloudstackLoadBalancer(Long loadBalancerNo) { // Cloudstack??? cloudstackLoadBalancerDao.deleteByLoadBalancerNo(loadBalancerNo); } protected void deleteUltraMonkeyLoadBalancer(Long loadBalancerNo) { ComponentLoadBalancer componentLoadBalancer = componentLoadBalancerDao.read(loadBalancerNo); List<ComponentInstance> componentInstances = componentInstanceDao .readByComponentNo(componentLoadBalancer.getComponentNo()); // ????? componentLoadBalancerDao.deleteByLoadBalancerNo(loadBalancerNo); // ??? for (ComponentInstance componentInstance : componentInstances) { instanceService.deleteInstance(componentInstance.getInstanceNo()); } // ????? componentService.deleteComponent(componentLoadBalancer.getComponentNo()); } /** * {@inheritDoc} */ @Override public void createListener(Long loadBalancerNo, Integer loadBalancerPort, Integer servicePort, String protocol, Long sslKeyNo) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (loadBalancerPort == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerPort"); } if (servicePort == null) { throw new AutoApplicationException("ECOMMON-000003", "servicePort"); } if (protocol == null || protocol.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "protocol"); } // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); if (loadBalancer == null) { // ?????? throw new AutoApplicationException("ESERVICE-000603", loadBalancerNo); } // ????? long countListener = loadBalancerListenerDao.countByLoadBalancerNoAndLoadBalancerPort(loadBalancerNo, loadBalancerPort); if (countListener != 0) { // ???????? throw new AutoApplicationException("ESERVICE-000609", loadBalancerPort); } // AWS????? if (PCCConstant.LOAD_BALANCER_ELB.equals(loadBalancer.getType())) { // ???? if (loadBalancerPort != 80 && loadBalancerPort != 443 && (loadBalancerPort < 1024 || 65535 < loadBalancerPort)) { // ????? throw new AutoApplicationException("ESERVICE-000610"); } //SSL?(HTTPS????) if ("HTTPS".equals(protocol)) { if (sslKeyNo == null) { throw new AutoApplicationException("ECOMMON-000003", "sslKey"); } } } // ??? if (servicePort < 1 || 65535 < servicePort) { // ???? throw new AutoApplicationException("ESERVICE-000611"); } // ?? List<String> protocols = Arrays.asList("TCP", "HTTP", "HTTPS"); if (!protocols.contains(protocol)) { // ??????? throw new AutoApplicationException("ESERVICE-000612"); } // ?? LoadBalancerListener listener = new LoadBalancerListener(); listener.setLoadBalancerNo(loadBalancerNo); listener.setLoadBalancerPort(loadBalancerPort); listener.setServicePort(servicePort); listener.setProtocol(protocol); listener.setSslKeyNo(sslKeyNo); loadBalancerListenerDao.create(listener); // Farm farm = farmDao.read(loadBalancer.getFarmNo()); eventLogger.log(EventLogLevel.INFO, farm.getFarmNo(), farm.getFarmName(), null, null, null, null, "LoadBalancerListenerCreate", null, null, new Object[] { loadBalancer.getLoadBalancerName(), loadBalancerPort }); } /** * {@inheritDoc} */ @Override public void updateListener(Long loadBalancerNo, Integer originalLoadBalancerPort, Integer loadBalancerPort, Integer servicePort, String protocol, Long sslKeyNo) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (originalLoadBalancerPort == null) { throw new AutoApplicationException("ECOMMON-000003", "originalLoadBalancerPort"); } if (loadBalancerPort == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerPort"); } if (servicePort == null) { throw new AutoApplicationException("ECOMMON-000003", "servicePort"); } if (protocol == null || protocol.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "protocol"); } // ??? LoadBalancerListener listener = loadBalancerListenerDao.read(loadBalancerNo, originalLoadBalancerPort); if (listener == null) { // ????? throw new AutoApplicationException("ESERVICE-000613", originalLoadBalancerPort); } // ????????? if (LoadBalancerListenerStatus.fromStatus(listener.getStatus()) != LoadBalancerListenerStatus.STOPPED) { // ????? throw new AutoApplicationException("ESERVICE-000627"); } if (!originalLoadBalancerPort.equals(loadBalancerPort)) { // ????? long countListener = loadBalancerListenerDao.countByLoadBalancerNoAndLoadBalancerPort(loadBalancerNo, loadBalancerPort); if (countListener != 0) { // ???????? throw new AutoApplicationException("ESERVICE-000609", loadBalancerPort); } } // AWS????? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); if (PCCConstant.LOAD_BALANCER_ELB.equals(loadBalancer.getType())) { // ???? if (loadBalancerPort != 80 && loadBalancerPort != 443 && (loadBalancerPort < 1024 || 65535 < loadBalancerPort)) { // ????? throw new AutoApplicationException("ESERVICE-000610"); } //SSL?(HTTPS????) if ("HTTPS".equals(protocol)) { if (sslKeyNo == null) { throw new AutoApplicationException("ECOMMON-000003", "sslKey"); } } } // ??? if (servicePort < 1 || 65535 < servicePort) { // ???? throw new AutoApplicationException("ESERVICE-000611"); } // ?? List<String> protocols = Arrays.asList("TCP", "HTTP", "HTTPS"); if (!protocols.contains(protocol)) { // ??????? throw new AutoApplicationException("ESERVICE-000612"); } // ? if (originalLoadBalancerPort.equals(loadBalancerPort)) { listener.setServicePort(servicePort); listener.setProtocol(protocol); listener.setSslKeyNo(sslKeyNo); loadBalancerListenerDao.update(listener); } else { // ???????? LoadBalancerListener listener2 = new LoadBalancerListener(); listener2.setLoadBalancerNo(loadBalancerNo); listener2.setLoadBalancerPort(loadBalancerPort); listener2.setServicePort(servicePort); listener2.setProtocol(protocol); listener2.setSslKeyNo(sslKeyNo); listener2.setEnabled(listener.getEnabled()); listener2.setStatus(listener.getStatus()); listener2.setConfigure(listener.getConfigure()); loadBalancerListenerDao.create(listener2); loadBalancerListenerDao.delete(listener); } // Farm farm = farmDao.read(loadBalancer.getFarmNo()); eventLogger.log(EventLogLevel.INFO, farm.getFarmNo(), farm.getFarmName(), null, null, null, null, "LoadBalancerListenerUpdate", null, null, new Object[] { loadBalancer.getLoadBalancerName(), loadBalancerPort }); } /** * {@inheritDoc} */ @Override public void deleteListener(Long loadBalancerNo, Integer loadBalancerPort) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (loadBalancerPort == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerPort"); } // ??? LoadBalancerListener listener = loadBalancerListenerDao.read(loadBalancerNo, loadBalancerPort); if (listener == null) { // ????? throw new AutoApplicationException("ESERVICE-000613", loadBalancerPort); } // ????????? if (LoadBalancerListenerStatus.fromStatus(listener.getStatus()) != LoadBalancerListenerStatus.STOPPED) { // ????? throw new AutoApplicationException("ESERVICE-000628"); } // ?? loadBalancerListenerDao.deleteByLoadBalancerNoAndLoadBalancerPort(loadBalancerNo, loadBalancerPort); // LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); Farm farm = farmDao.read(loadBalancer.getFarmNo()); eventLogger.log(EventLogLevel.INFO, farm.getFarmNo(), farm.getFarmName(), null, null, null, null, "LoadBalancerListenerDelete", null, null, new Object[] { loadBalancer.getLoadBalancerName(), loadBalancerPort }); } /** * {@inheritDoc} */ @Override public void configureHealthCheck(Long loadBalancerNo, String checkProtocol, Integer checkPort, String checkPath, Integer checkTimeout, Integer checkInterval, Integer healthyThreshold, Integer unhealthyThreshold) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (checkProtocol == null || checkProtocol.length() == 0) { throw new AutoApplicationException("ECOMMON-000003", "checkProtocol"); } if (checkPort == null) { throw new AutoApplicationException("ECOMMON-000003", "checkPort"); } if (checkTimeout == null) { throw new AutoApplicationException("ECOMMON-000003", "checkTimeout"); } if (checkInterval == null) { throw new AutoApplicationException("ECOMMON-000003", "checkInterval"); } if (unhealthyThreshold == null) { throw new AutoApplicationException("ECOMMON-000003", "unhealthyThreshold"); } // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); if (loadBalancer == null) { // ?????? throw new AutoApplicationException("ESERVICE-000603", loadBalancerNo); } if (PCCConstant.LOAD_BALANCER_ELB.equals(loadBalancer.getType())) { if (healthyThreshold == null) { throw new AutoApplicationException("ECOMMON-000003", "healthyThreshold"); } } // ?? List<String> protocols = Arrays.asList("TCP", "HTTP"); if (!protocols.contains(checkProtocol)) { // ???????? throw new AutoApplicationException("ESERVICE-000614"); } // ??? if (checkPort < 1 || 65535 < checkPort) { // ???? throw new AutoApplicationException("ESERVICE-000615"); } // ?? if ("HTTP".equals(checkProtocol)) { if (checkPath == null || checkPath.length() == 0) { throw new AutoApplicationException("ESERVICE-000616"); } } // ?? if (checkTimeout < 2 || 60 < checkTimeout) { // ??? throw new AutoApplicationException("ESERVICE-000617", 2, 60); } // ??? if (checkInterval < 5 || 600 < checkInterval) { // ???? throw new AutoApplicationException("ESERVICE-000618", 5, 600); } // ?? if (PCCConstant.LOAD_BALANCER_ELB.equals(loadBalancer.getType())) { if (healthyThreshold < 2 || 10 < healthyThreshold) { // ??? throw new AutoApplicationException("ESERVICE-000619", 2, 10); } } else if (PCCConstant.LOAD_BALANCER_ULTRAMONKEY.equals(loadBalancer.getType())) { // UltraMonkey????1?? healthyThreshold = 1; } // ?? if (unhealthyThreshold < 2 || 10 < unhealthyThreshold) { // ??? throw new AutoApplicationException("ESERVICE-000620", 2, 10); } // ??/ LoadBalancerHealthCheck healthCheck = loadBalancerHealthCheckDao.read(loadBalancerNo); boolean exist = (healthCheck != null); if (!exist) { healthCheck = new LoadBalancerHealthCheck(); healthCheck.setLoadBalancerNo(loadBalancerNo); } healthCheck.setCheckProtocol(checkProtocol); healthCheck.setCheckPort(checkPort); healthCheck.setCheckPath(checkPath); healthCheck.setCheckTimeout(checkTimeout); healthCheck.setCheckInterval(checkInterval); healthCheck.setHealthyThreshold(healthyThreshold); healthCheck.setUnhealthyThreshold(unhealthyThreshold); if (!exist) { loadBalancerHealthCheckDao.create(healthCheck); } else { loadBalancerHealthCheckDao.update(healthCheck); } } /** * {@inheritDoc} */ @Override public void updateAutoScalingConf(Long farmNo, Long loadBalancerNo, Long platformNo, Long imageNo, String instanceType, Integer enabled, String namingRule, Long idleTimeMax, Long idleTimeMin, Long continueLimit, Long addCount, Long delCount) { // ??????? // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); if (loadBalancer == null) { // ?????? throw new AutoApplicationException("ESERVICE-000603", loadBalancerNo); } // ? AutoScalingConf autoScalingConf = autoScalingConfDao.read(loadBalancerNo); boolean exist = (autoScalingConf != null); if (!exist) { autoScalingConf = new AutoScalingConf(); autoScalingConf.setLoadBalancerNo(loadBalancerNo); autoScalingConf.setFarmNo(farmNo); } autoScalingConf.setPlatformNo(platformNo); autoScalingConf.setImageNo(imageNo); autoScalingConf.setInstanceType(instanceType); autoScalingConf.setNamingRule(namingRule); autoScalingConf.setIdleTimeMax(idleTimeMax); autoScalingConf.setIdleTimeMin(idleTimeMin); autoScalingConf.setContinueLimit(continueLimit); autoScalingConf.setAddCount(addCount); autoScalingConf.setDelCount(delCount); if (enabled == 1) { autoScalingConf.setEnabled(true); } else { autoScalingConf.setEnabled(false); } if (!exist) { autoScalingConfDao.create(autoScalingConf); } else { autoScalingConfDao.update(autoScalingConf); } } /** * {@inheritDoc} */ @Override public void enableInstances(Long loadBalancerNo, List<Long> instanceNos) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (instanceNos == null) { throw new AutoApplicationException("ECOMMON-000003", "instanceNos"); } // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); if (loadBalancer == null) { // ?????? throw new AutoApplicationException("ESERVICE-000603", loadBalancerNo); } // ??? List<Long> tmpInstanceNos = new ArrayList<Long>(); for (Long instanceNo : instanceNos) { if (!tmpInstanceNos.contains(instanceNo)) { tmpInstanceNos.add(instanceNo); } } instanceNos = tmpInstanceNos; // ?? List<Instance> instances = instanceDao.readInInstanceNos(instanceNos); if (instanceNos.size() != instances.size()) { tmpInstanceNos = new ArrayList<Long>(instanceNos); for (Instance instance : instances) { tmpInstanceNos.remove(instance.getInstanceNo()); } if (tmpInstanceNos.size() > 0) { throw new AutoApplicationException("ESERVICE-000621", tmpInstanceNos.iterator().next()); } } // ???? List<ComponentInstance> componentInstances = componentInstanceDao .readByComponentNo(loadBalancer.getComponentNo()); for (Instance instance : instances) { boolean contain = false; for (ComponentInstance componentInstance : componentInstances) { if (BooleanUtils.isNotTrue(componentInstance.getAssociate())) { continue; } if (componentInstance.getInstanceNo().equals(instance.getInstanceNo())) { contain = true; break; } } if (!contain) { // ??????????? Component component = componentDao.read(loadBalancer.getComponentNo()); throw new AutoApplicationException("ESERVICE-000622", instance.getInstanceName(), component.getComponentName()); } } // AWS????? // if ("aws".equals(loadBalancer.getType()) || "cloudstack".equals(loadBalancer.getType())) { if (PCCConstant.LOAD_BALANCER_ELB.equals(loadBalancer.getType())) { // ? Long platformNo = loadBalancer.getPlatformNo(); for (Instance instance : instances) { if (!platformNo.equals(instance.getPlatformNo())) { throw new AutoApplicationException("ESERVICE-000623", instance.getInstanceName()); } } // ?AWS?????????????? List<LoadBalancerInstance> lbInstances = loadBalancerInstanceDao.readInInstanceNos(instanceNos); Set<Long> otherLoadBalancerNos = new HashSet<Long>(); for (LoadBalancerInstance lbInstance : lbInstances) { if (!loadBalancerNo.equals(lbInstance.getLoadBalancerNo())) { otherLoadBalancerNos.add(lbInstance.getLoadBalancerNo()); } } List<LoadBalancer> otherLoadBalancers = loadBalancerDao.readInLoadBalancerNos(otherLoadBalancerNos); for (LoadBalancer otherLoadBalancer : otherLoadBalancers) { if (PCCConstant.LOAD_BALANCER_ELB.equals(otherLoadBalancer.getType())) { // ?AWS????????? for (LoadBalancerInstance lbInstance : lbInstances) { if (otherLoadBalancer.getLoadBalancerNo().equals(otherLoadBalancer.getLoadBalancerNo())) { for (Instance instance : instances) { if (instance.getInstanceNo().equals(lbInstance.getInstanceNo())) { throw new AutoApplicationException("ESERVICE-000624", instance.getInstanceName()); } } } } } } // //VPC?????????????????? // PlatformAws platformAws = platformAwsDao.read(platformNo); // if (platformAws.getVpc()) { // AwsLoadBalancer awsLoadBalancer = awsLoadBalancerDao.read(loadBalancerNo); // List<String> zones = new ArrayList<String>(); // if (StringUtils.isEmpty(awsLoadBalancer.getAvailabilityZone())) { // //ELB?(?)??????????????? // throw new AutoApplicationException("ESERVICE-000630", loadBalancer.getLoadBalancerName()); // } // for (String zone: awsLoadBalancer.getAvailabilityZone().split(",")) { // zones.add(zone.trim()); // } // for (Instance instance : instances) { // AwsInstance awsInstance = awsInstanceDao.read(instance.getInstanceNo()); // if (zones.contains(awsInstance.getAvailabilityZone()) == false) { // //???????????? // throw new AutoApplicationException("ESERVICE-000629", instance.getInstanceName()); // } // } // } } // ??? List<LoadBalancerInstance> lbInstances = loadBalancerInstanceDao.readByLoadBalancerNo(loadBalancerNo); Map<Long, LoadBalancerInstance> lbInstanceMap = new HashMap<Long, LoadBalancerInstance>(); for (LoadBalancerInstance lbInstance : lbInstances) { lbInstanceMap.put(lbInstance.getInstanceNo(), lbInstance); } // ?? for (Instance instance : instances) { // ??????? LoadBalancerInstance lbInstance = lbInstanceMap.remove(instance.getInstanceNo()); // ???????? if (lbInstance == null) { lbInstance = new LoadBalancerInstance(); lbInstance.setLoadBalancerNo(loadBalancerNo); lbInstance.setInstanceNo(instance.getInstanceNo()); lbInstance.setEnabled(true); lbInstance.setStatus(LoadBalancerInstanceStatus.STOPPED.toString()); loadBalancerInstanceDao.create(lbInstance); } // ?????? else { if (BooleanUtils.isNotTrue(lbInstance.getEnabled())) { lbInstance.setEnabled(true); loadBalancerInstanceDao.update(lbInstance); } } } // ??? if (BooleanUtils.isNotTrue(loadBalancer.getConfigure())) { loadBalancer.setConfigure(true); loadBalancerDao.update(loadBalancer); } // ?? Farm farm = farmDao.read(loadBalancer.getFarmNo()); if (BooleanUtils.isNotTrue(farm.getScheduled())) { farm.setScheduled(true); farmDao.update(farm); } } /** * {@inheritDoc} */ @Override public void disableInstances(Long loadBalancerNo, List<Long> instanceNos) { // ? if (loadBalancerNo == null) { throw new AutoApplicationException("ECOMMON-000003", "loadBalancerNo"); } if (instanceNos == null) { throw new AutoApplicationException("ECOMMON-000003", "instanceNos"); } // ??? LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); if (loadBalancer == null) { // ?????? throw new AutoApplicationException("ESERVICE-000603", loadBalancerNo); } // ??? List<Long> tmpInstanceNos = new ArrayList<Long>(); for (Long instanceNo : instanceNos) { if (!tmpInstanceNos.contains(instanceNo)) { tmpInstanceNos.add(instanceNo); } } instanceNos = tmpInstanceNos; // ?? List<Instance> instances = instanceDao.readInInstanceNos(instanceNos); if (instanceNos.size() != instances.size()) { tmpInstanceNos = new ArrayList<Long>(instanceNos); for (Instance instance : instances) { tmpInstanceNos.remove(instance.getInstanceNo()); } if (tmpInstanceNos.size() > 0) { throw new AutoApplicationException("ESERVICE-000621", tmpInstanceNos.iterator().next()); } } // ???? List<ComponentInstance> componentInstances = componentInstanceDao .readByComponentNo(loadBalancer.getComponentNo()); for (Instance instance : instances) { boolean contain = false; for (ComponentInstance componentInstance : componentInstances) { if (BooleanUtils.isNotTrue(componentInstance.getAssociate())) { continue; } if (componentInstance.getInstanceNo().equals(instance.getInstanceNo())) { contain = true; break; } } if (!contain) { // ??????????? Component component = componentDao.read(loadBalancer.getComponentNo()); throw new AutoApplicationException("ESERVICE-000622", instance.getInstanceName(), component.getComponentName()); } } // ??? List<LoadBalancerInstance> lbInstances = loadBalancerInstanceDao.readByLoadBalancerNo(loadBalancerNo); Map<Long, LoadBalancerInstance> lbInstanceMap = new HashMap<Long, LoadBalancerInstance>(); for (LoadBalancerInstance lbInstance : lbInstances) { lbInstanceMap.put(lbInstance.getInstanceNo(), lbInstance); } // ?? for (Instance instance : instances) { // ??????? LoadBalancerInstance lbInstance = lbInstanceMap.remove(instance.getInstanceNo()); if (lbInstance == null) { // ?????? continue; } // ???? if (BooleanUtils.isTrue(lbInstance.getEnabled())) { lbInstance.setEnabled(false); loadBalancerInstanceDao.update(lbInstance); } } // ??? if (BooleanUtils.isNotTrue(loadBalancer.getConfigure())) { loadBalancer.setConfigure(true); loadBalancerDao.update(loadBalancer); } // ?? Farm farm = farmDao.read(loadBalancer.getFarmNo()); if (BooleanUtils.isNotTrue(farm.getScheduled())) { farm.setScheduled(true); farmDao.update(farm); } } /** * {@inheritDoc} */ @Override public List<LoadBalancerPlatformDto> getPlatforms(Long userNo) { // ? if (userNo == null) { throw new AutoApplicationException("ECOMMON-000003", "userNo"); } List<LoadBalancerPlatformDto> dtos = new ArrayList<LoadBalancerPlatformDto>(); List<Platform> platforms = platformDao.readAll(); List<Image> images = imageDao.readAll(); for (Platform platform : platforms) { PlatformAws platformAws = null; PlatformCloudstack platformCloudstack = null; PlatformVmware platformVmware = null; PlatformNifty platformNifty = null; PlatformVcloud platformVcloud = null; PlatformAzure platformAzure = null; PlatformOpenstack platformOpenstack = null; // TODO CLOUD BRANCHING if (PCCConstant.PLATFORM_TYPE_AWS.equals(platform.getPlatformType())) { // AWS??????? if (awsCertificateDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } platformAws = platformAwsDao.read(platform.getPlatformNo()); } else if (PCCConstant.PLATFORM_TYPE_CLOUDSTACK.equals(platform.getPlatformType())) { // CloudStack??????? if (cloudstackCertificateDao.countByAccountAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } platformCloudstack = platformCloudstackDao.read(platform.getPlatformNo()); } else if (PCCConstant.PLATFORM_TYPE_VMWARE.equals(platform.getPlatformType())) { // ????? // TODO: ??? if (vmwareKeyPairDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } platformVmware = platformVmwareDao.read(platform.getPlatformNo()); } else if (PCCConstant.PLATFORM_TYPE_NIFTY.equals(platform.getPlatformType())) { // ??????? // TODO: ??? if (niftyCertificateDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } if (niftyKeyPairDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } platformNifty = platformNiftyDao.read(platform.getPlatformNo()); } else if (PCCConstant.PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) { // ??????? if (vcloudCertificateDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } if (vcloudKeyPairDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } platformVcloud = platformVcloudDao.read(platform.getPlatformNo()); } else if (PCCConstant.PLATFORM_TYPE_AZURE.equals(platform.getPlatformType())) { // ??????? if (azureCertificateDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } platformAzure = platformAzureDao.read(platform.getPlatformNo()); } else if (PCCConstant.PLATFORM_TYPE_OPENSTACK.equals(platform.getPlatformType())) { // ??????? if (openstackCertificateDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) == 0) { continue; } platformOpenstack = platformOpenstackDao.read(platform.getPlatformNo()); } List<String> types = new ArrayList<String>(); // ???????? for (Image image : images) { if (image.getPlatformNo().equals(platform.getPlatformNo())) { if (PCCConstant.IMAGE_NAME_ULTRAMONKEY.equals(image.getImageName())) { types.add(PCCConstant.LOAD_BALANCER_ULTRAMONKEY); } if (PCCConstant.IMAGE_NAME_ELB.equals(image.getImageName())) { types.add(PCCConstant.LOAD_BALANCER_ELB); } } } // ???????? if (types.isEmpty()) { continue; } List<ImageDto> imageDtos = getImages(platform, images); LoadBalancerPlatformDto dto = new LoadBalancerPlatformDto(); dto.setPlatform(platform); dto.setPlatformAws(platformAws); dto.setPlatformCloudstack(platformCloudstack); dto.setPlatformVmware(platformVmware); dto.setPlatformNifty(platformNifty); dto.setPlatformVcloud(platformVcloud); dto.setPlatformAzure(platformAzure); dto.setPlatformOpenstack(platformOpenstack); dto.setImages(imageDtos); dto.setTypes(types); dtos.add(dto); } return dtos; } private List<ImageDto> getImages(Platform platform, List<Image> images) { // ? List<ImageDto> imageDtos = new ArrayList<ImageDto>(); for (Image image : images) { // ???? if (platform.getPlatformNo().equals(image.getPlatformNo()) == false) { continue; } ImageAws imageAws = null; ImageCloudstack imageCloudstack = null; ImageVmware imageVmware = null; ImageNifty imageNifty = null; ImageVcloud imageVcloud = null; ImageAzure imageAzure = null; ImageOpenstack imageOpenstack = null; // TODO CLOUD BRANCHING if (PCCConstant.PLATFORM_TYPE_AWS.equals(platform.getPlatformType())) { imageAws = imageAwsDao.read(image.getImageNo()); } else if (PCCConstant.PLATFORM_TYPE_CLOUDSTACK.equals(platform.getPlatformType())) { imageCloudstack = imageCloudstackDao.read(image.getImageNo()); } else if (PCCConstant.PLATFORM_TYPE_VMWARE.equals(platform.getPlatformType())) { imageVmware = imageVmwareDao.read(image.getImageNo()); } else if (PCCConstant.PLATFORM_TYPE_NIFTY.equals(platform.getPlatformType())) { imageNifty = imageNiftyDao.read(image.getImageNo()); } else if (PCCConstant.PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) { imageVcloud = imageVcloudDao.read(image.getImageNo()); } else if (PCCConstant.PLATFORM_TYPE_AZURE.equals(platform.getPlatformType())) { imageAzure = imageAzureDao.read(image.getImageNo()); } else if (PCCConstant.PLATFORM_TYPE_OPENSTACK.equals(platform.getPlatformType())) { imageOpenstack = imageOpenstackDao.read(image.getImageNo()); } ImageDto imageDto = new ImageDto(); imageDto.setImage(image); imageDto.setImageAws(imageAws); imageDto.setImageCloudstack(imageCloudstack); imageDto.setImageVmware(imageVmware); imageDto.setImageNifty(imageNifty); imageDto.setImageVcloud(imageVcloud); imageDto.setImageAzure(imageAzure); imageDto.setImageOpenstack(imageOpenstack); imageDtos.add(imageDto); } return imageDtos; } /** * {@inheritDoc} */ @Override public List<SslKeyDto> getSSLKey(Long loadBalancerNo) { // ? List<SslKeyDto> keyDtos = new ArrayList<SslKeyDto>(); LoadBalancer loadBalancer = loadBalancerDao.read(loadBalancerNo); //??AWS?? if (PCCConstant.LOAD_BALANCER_ELB.equals(loadBalancer.getType())) { List<AwsSslKey> keys = awsSslKeyDao.readAll(); for (AwsSslKey key : keys) { if (loadBalancer.getFarmNo().equals(key.getFarmNo()) && loadBalancer.getPlatformNo().equals(key.getPlatformNo())) { SslKeyDto sslkey = new SslKeyDto(); sslkey.setKeyNo(key.getKeyNo()); sslkey.setKeyName(key.getKeyName()); sslkey.setKeyId(key.getSslcertificateid()); keyDtos.add(sslkey); } } } return keyDtos; } /** * componentService??? * * @param componentService componentService */ public void setComponentService(ComponentService componentService) { this.componentService = componentService; } /** * instanceService??? * * @param instanceService instanceService */ public void setInstanceService(InstanceService instanceService) { this.instanceService = instanceService; } /** * iaasDescribeService??? * * @param iaasDescribeService iaasDescribeService */ public void setIaasDescribeService(IaasDescribeService iaasDescribeService) { this.iaasDescribeService = iaasDescribeService; } /** * eventLogger??? * * @param eventLogger eventLogger */ public void setEventLogger(EventLogger eventLogger) { this.eventLogger = eventLogger; } /** * zabbixProcessClientFactory??? * * @param zabbixProcessClientFactory zabbixProcessClientFactory */ public void setZabbixProcessClientFactory(ZabbixProcessClientFactory zabbixProcessClientFactory) { this.zabbixProcessClientFactory = zabbixProcessClientFactory; } /** * elbZabbixHostProcess??? * * @param elbZabbixHostProcess elbZabbixHostProcess */ public void setElbZabbixHostProcess(ElbZabbixHostProcess elbZabbixHostProcess) { this.elbZabbixHostProcess = elbZabbixHostProcess; } }