jp.primecloud.auto.service.impl.FarmServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for jp.primecloud.auto.service.impl.FarmServiceImpl.java

Source

/*
 * 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.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import jp.primecloud.auto.common.constant.PCCConstant;
import jp.primecloud.auto.common.status.ComponentInstanceStatus;
import jp.primecloud.auto.common.status.InstanceStatus;
import jp.primecloud.auto.common.status.LoadBalancerStatus;
import jp.primecloud.auto.config.Config;
import jp.primecloud.auto.entity.crud.AwsVolume;
import jp.primecloud.auto.entity.crud.Component;
import jp.primecloud.auto.entity.crud.ComponentInstance;
import jp.primecloud.auto.entity.crud.Farm;
import jp.primecloud.auto.entity.crud.Instance;
import jp.primecloud.auto.entity.crud.LoadBalancer;
import jp.primecloud.auto.entity.crud.Platform;
import jp.primecloud.auto.entity.crud.User;
import jp.primecloud.auto.entity.crud.UserAuth;
import jp.primecloud.auto.entity.crud.VmwareNetwork;
import jp.primecloud.auto.exception.AutoApplicationException;
import jp.primecloud.auto.exception.AutoException;
import jp.primecloud.auto.iaasgw.IaasGatewayFactory;
import jp.primecloud.auto.iaasgw.IaasGatewayWrapper;
import jp.primecloud.auto.log.EventLogLevel;
import jp.primecloud.auto.log.EventLogger;
import jp.primecloud.auto.process.vmware.VmwareNetworkProcess;
import jp.primecloud.auto.process.vmware.VmwareProcessClient;
import jp.primecloud.auto.process.vmware.VmwareProcessClientFactory;
import jp.primecloud.auto.process.zabbix.ZabbixHostProcess;
import jp.primecloud.auto.service.ComponentService;
import jp.primecloud.auto.service.FarmService;
import jp.primecloud.auto.service.InstanceService;
import jp.primecloud.auto.service.LoadBalancerService;
import jp.primecloud.auto.service.ServiceSupport;
import jp.primecloud.auto.service.dto.FarmDto;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;

/**
 * <p>
 * TODO: 
 * </p>
 *
 */
public class FarmServiceImpl extends ServiceSupport implements FarmService {

    protected IaasGatewayFactory iaasGatewayFactory;

    protected VmwareProcessClientFactory vmwareProcessClientFactory;

    protected ComponentService componentService;

    protected InstanceService instanceService;

    protected VmwareNetworkProcess vmwareNetworkProcess;

    protected ZabbixHostProcess zabbixHostProcess;

    protected EventLogger eventLogger;

    protected LoadBalancerService loadBalancerService;

    /**
     * {@inheritDoc}
     */
    @Override
    public List<FarmDto> getFarms(Long userNo, Long loginUserNo) {

        //?
        User user = userDao.read(loginUserNo);
        //?
        List<Farm> farms = new ArrayList<Farm>();
        //
        List<FarmDto> dtos = new ArrayList<FarmDto>();

        //?
        if (user.getPowerUser()) {
            farms = farmDao.readAll();
            for (Farm farm : farms) {
                FarmDto dto = new FarmDto();
                dto.setFarm(farm);
                dtos.add(dto);
            }
        }
        //????
        else {
            //??
            farms = farmDao.readByUserNo(userNo);

            //??
            List<UserAuth> userAuth = userAuthDao.readByUserNo(loginUserNo);
            HashMap<Long, Boolean> authMap = new HashMap<Long, Boolean>();
            for (UserAuth auth : userAuth) {
                if (auth.getFarmUse()) {
                    authMap.put(auth.getFarmNo(), auth.getFarmUse());
                }
            }

            for (Farm farm : farms) {
                //????? ??
                if ((loginUserNo.equals(userNo)) || authMap.containsKey(farm.getFarmNo())) {
                    FarmDto dto = new FarmDto();
                    dto.setFarm(farm);
                    dtos.add(dto);
                }
            }
        }

        // 
        Collections.sort(dtos, Comparators.COMPARATOR_FARM_DTO);

        return dtos;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public FarmDto getFarm(Long farmNo) {
        Farm farm = farmDao.read(farmNo);

        if (farm == null) {
            // ?????
            return null;
        }

        FarmDto dto = new FarmDto();
        dto.setFarm(farm);

        return dto;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long createFarm(Long userNo, String farmName, String comment) {
        // ?
        if (userNo == null) {
            throw new AutoApplicationException("ECOMMON-000003", "userNo");
        }
        if (farmName == null || farmName.length() == 0) {
            throw new AutoApplicationException("ECOMMON-000003", "farmName");
        }

        // ??
        if (!Pattern.matches("^[0-9a-z]|[0-9a-z][0-9a-z-]*[0-9a-z]$", farmName)) {
            throw new AutoApplicationException("ECOMMON-000012", "farmName");
        }

        // TODO: ??

        // ?????
        Farm checkFarm = farmDao.readByFarmName(farmName);
        if (checkFarm != null) {
            // ???????
            throw new AutoApplicationException("ESERVICE-000201", farmName);
        }

        // ???
        // TODO: ???????
        String domainName = farmName + "." + Config.getProperty("dns.domain");

        // ??
        Farm farm = new Farm();
        farm.setFarmName(farmName);
        farm.setUserNo(userNo);
        farm.setComment(comment);
        farm.setDomainName(domainName);
        farm.setScheduled(false);
        farm.setComponentProcessing(false);
        farmDao.create(farm);

        List<Platform> platforms = platformDao.readAll();
        for (Platform platform : platforms) {
            // TODO CLOUD BRANCHING
            // VMware??
            if (PCCConstant.PLATFORM_TYPE_VMWARE.equals(platform.getPlatformType())) {
                if (vmwareKeyPairDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) > 0) {
                    // ???VLAN?
                    VmwareNetwork publicNetwork = null;
                    VmwareNetwork privateNetwork = null;
                    List<VmwareNetwork> vmwareNetworks = vmwareNetworkDao
                            .readByPlatformNo(platform.getPlatformNo());
                    for (VmwareNetwork vmwareNetwork : vmwareNetworks) {
                        if (vmwareNetwork.getFarmNo() != null) {
                            continue;
                        }
                        if (BooleanUtils.isTrue(vmwareNetwork.getPublicNetwork())) {
                            if (publicNetwork == null) {
                                publicNetwork = vmwareNetwork;
                            }
                        } else {
                            if (privateNetwork == null) {
                                privateNetwork = vmwareNetwork;
                            }
                        }
                    }

                    // VLAN?
                    if (publicNetwork != null) {
                        publicNetwork.setFarmNo(farm.getFarmNo());
                        vmwareNetworkDao.update(publicNetwork);
                    }
                    if (privateNetwork != null) {
                        privateNetwork.setFarmNo(farm.getFarmNo());
                        vmwareNetworkDao.update(privateNetwork);
                    }
                }
                // VCloud??
            } else if (PCCConstant.PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) {
                //????????????vApp??
                if (BooleanUtils.isTrue(platform.getSelectable())
                        && vcloudCertificateDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) > 0
                        && vcloudKeyPairDao.countByUserNoAndPlatformNo(userNo, platform.getPlatformNo()) > 0) {
                    //vApp?
                    IaasGatewayWrapper gateway = iaasGatewayFactory.createIaasGateway(userNo,
                            platform.getPlatformNo());
                    gateway.createMyCloud(farmName);
                }
                // Azure??
            } else if (PCCConstant.PLATFORM_TYPE_AZURE.equals(platform.getPlatformType())) {
                // ????
                // OpenStack??
            } else if (PCCConstant.PLATFORM_TYPE_OPENSTACK.equals(platform.getPlatformType())) {
                // ????
            }
        }

        // VCloud?(iaasGateWay??????)??Zabbix????????
        // ????
        Boolean useZabbix = BooleanUtils.toBooleanObject(Config.getProperty("zabbix.useZabbix"));
        if (BooleanUtils.isTrue(useZabbix)) {
            zabbixHostProcess.createFarmHostgroup(farm.getFarmNo());
        }

        // 
        eventLogger.log(EventLogLevel.INFO, farm.getFarmNo(), farmName, null, null, null, null, "FarmCreate", null,
                null, null);

        return farm.getFarmNo();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void updateFarm(Long farmNo, String comment, String domainName) {
        // ?
        if (farmNo == null) {
            throw new AutoApplicationException("ECOMMON-000003", "farmNo");
        }

        // TODO: ??(comment,domainName)

        // TODO: ??(domainName)

        // ??
        Farm farm = farmDao.read(farmNo);
        if (farm == null) {
            // ?????
            throw new AutoApplicationException("ESERVICE-000204", farmNo);
        }

        if (!farm.getDomainName().equals(domainName)) {
            // ????????

            // ??????????
            List<Instance> instances = instanceDao.readByFarmNo(farmNo);
            for (Instance instance : instances) {
                if (InstanceStatus.fromStatus(instance.getStatus()) != InstanceStatus.STOPPED) {
                    // ??????
                    throw new AutoApplicationException("ESERVICE-000205", instance.getInstanceName());
                }
            }

            // ?????
            // TODO: ???????
            List<Farm> checkFarms = farmDao.readAll();
            for (Farm checkFarm : checkFarms) {
                if (checkFarm.getDomainName().equals(domainName)) {
                    // ?????????
                    throw new AutoApplicationException("ESERVICE-000206", domainName);
                }
            }
        }

        // ?
        farm.setComment(comment);
        farm.setDomainName(domainName);
        farmDao.update(farm);

        // 
        eventLogger.log(EventLogLevel.INFO, farmNo, farm.getFarmName(), null, null, null, null, "FarmUpdate", null,
                null, null);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void deleteFarm(Long farmNo) {
        // ?
        if (farmNo == null) {
            throw new AutoApplicationException("ECOMMON-000003", "farmNo");
        }

        // ??
        Farm farm = farmDao.read(farmNo);
        if (farm == null) {
            // ?????
            return;
        }

        // ?????????????
        List<Component> components = componentDao.readByFarmNo(farmNo);
        for (Component component : components) {
            // ?????????
            if (BooleanUtils.isTrue(component.getLoadBalancer())) {
                continue;
            }

            List<ComponentInstance> componentInstances = componentInstanceDao
                    .readByComponentNo(component.getComponentNo());
            for (ComponentInstance componentInstance : componentInstances) {
                ComponentInstanceStatus status = ComponentInstanceStatus.fromStatus(componentInstance.getStatus());
                if (status != ComponentInstanceStatus.STOPPED) {
                    // ????????
                    throw new AutoApplicationException("ESERVICE-000202", component.getComponentName());
                }
            }
        }

        // ???????????
        List<Instance> instances = instanceDao.readByFarmNo(farmNo);
        for (Instance instance : instances) {
            // ???????
            if (BooleanUtils.isTrue(instance.getLoadBalancer())) {
                continue;
            }

            if (InstanceStatus.fromStatus(instance.getStatus()) != InstanceStatus.STOPPED) {
                // ??????
                throw new AutoApplicationException("ESERVICE-000203", instance.getInstanceName());
            }
        }

        // ????????????
        List<LoadBalancer> loadBalancers = loadBalancerDao.readByFarmNo(farmNo);
        for (LoadBalancer loadBalancer : loadBalancers) {
            if (LoadBalancerStatus.fromStatus(loadBalancer.getStatus()) != LoadBalancerStatus.STOPPED) {
                // ???????
                throw new AutoApplicationException("ESERVICE-000207", loadBalancer.getLoadBalancerName());
            }
        }

        // ??
        Boolean useZabbix = BooleanUtils.toBooleanObject(Config.getProperty("zabbix.useZabbix"));
        if (BooleanUtils.isTrue(useZabbix)) {
            zabbixHostProcess.deleteFarmHostgroup(farmNo);
        }

        // ???
        for (LoadBalancer loadBalancer : loadBalancers) {
            loadBalancerService.deleteLoadBalancer(loadBalancer.getLoadBalancerNo());
        }

        // ????
        for (Component component : components) {
            componentService.deleteComponent(component.getComponentNo());
        }

        // ??
        for (Instance instance : instances) {
            instanceService.deleteInstance(instance.getInstanceNo());
        }

        // TODO CLOUD BRANCHING
        // AWS??
        // AWS??
        // TODO: ???????
        List<AwsVolume> awsVolumes = awsVolumeDao.readByFarmNo(farmNo);
        for (AwsVolume awsVolume : awsVolumes) {
            if (StringUtils.isEmpty(awsVolume.getVolumeId())) {
                continue;
            }

            IaasGatewayWrapper gateway = iaasGatewayFactory.createIaasGateway(farm.getUserNo(),
                    awsVolume.getPlatformNo());
            try {
                // ?
                gateway.deleteVolume(awsVolume.getVolumeId());

                // EC2??DeleteVolume???????Wait???
                //awsProcessClient.waitDeleteVolume(volumeId);
            } catch (AutoException ignore) {
                // ??????????????
            }
        }
        awsVolumeDao.deleteByFarmNo(farmNo);

        // VMware??
        // VLAN??
        List<VmwareNetwork> vmwareNetworks = vmwareNetworkDao.readByFarmNo(farmNo);
        for (VmwareNetwork vmwareNetwork : vmwareNetworks) {
            // PortGroup
            VmwareProcessClient vmwareProcessClient = vmwareProcessClientFactory
                    .createVmwareProcessClient(vmwareNetwork.getPlatformNo());
            try {
                vmwareNetworkProcess.removeNetwork(vmwareProcessClient, vmwareNetwork.getNetworkNo());
            } finally {
                vmwareProcessClient.getVmwareClient().logout();
            }

            // VLAN?
            vmwareNetwork.setFarmNo(null);
            vmwareNetworkDao.update(vmwareNetwork);
        }

        // VCloud??
        // VCloud vApp??
        List<Platform> platforms = platformDao.readAll();
        for (Platform platform : platforms) {
            if (!PCCConstant.PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) {
                //VCloud????
                continue;
            }

            IaasGatewayWrapper gateway = iaasGatewayFactory.createIaasGateway(farm.getUserNo(),
                    platform.getPlatformNo());
            try {
                if (BooleanUtils.isTrue(platform.getSelectable())
                        && vcloudCertificateDao.countByUserNoAndPlatformNo(farm.getUserNo(),
                                platform.getPlatformNo()) > 0
                        && vcloudKeyPairDao.countByUserNoAndPlatformNo(farm.getUserNo(),
                                platform.getPlatformNo()) > 0) {
                    // myCloud(vApp)?
                    gateway.deleteMyCloud(farmNo);
                }
            } catch (AutoException ignore) {
                // ??VCloud????
                // ??????????????
            }
        }

        // Azure??
        // ????

        // OpenStack??
        // ????

        //??
        userAuthDao.deleteByFarmNo(farmNo);

        // ??
        farmDao.deleteByFarmNo(farmNo);

        // 
        eventLogger.log(EventLogLevel.INFO, farmNo, farm.getFarmName(), null, null, null, null, "FarmDelete", null,
                null, null);
    }

    /**
     * iaasGatewayFactory???
     *
     * @param iaasGatewayFactory iaasGatewayFactory
     */
    public void setIaasGatewayFactory(IaasGatewayFactory iaasGatewayFactory) {
        this.iaasGatewayFactory = iaasGatewayFactory;
    }

    /**
     * vmwareProcessClientFactory???
     *
     * @param vmwareProcessClientFactory vmwareProcessClientFactory
     */
    public void setVmwareProcessClientFactory(VmwareProcessClientFactory vmwareProcessClientFactory) {
        this.vmwareProcessClientFactory = vmwareProcessClientFactory;
    }

    /**
     * componentService???
     *
     * @param componentService componentService
     */
    public void setComponentService(ComponentService componentService) {
        this.componentService = componentService;
    }

    /**
     * instanceService???
     *
     * @param instanceService instanceService
     */
    public void setInstanceService(InstanceService instanceService) {
        this.instanceService = instanceService;
    }

    /**
     * vmwareNetworkProcess???
     *
     * @param vmwareNetworkProcess vmwareNetworkProcess
     */
    public void setVmwareNetworkProcess(VmwareNetworkProcess vmwareNetworkProcess) {
        this.vmwareNetworkProcess = vmwareNetworkProcess;
    }

    /**
     * zabbixHostProcess???
     *
     * @param zabbixHostProcess zabbixHostProcess
     */
    public void setZabbixHostProcess(ZabbixHostProcess zabbixHostProcess) {
        this.zabbixHostProcess = zabbixHostProcess;
    }

    /**
     * eventLogger???
     *
     * @param eventLogger eventLogger
     */
    public void setEventLogger(EventLogger eventLogger) {
        this.eventLogger = eventLogger;
    }

    /**
     * loadBalancerService???
     *
     * @param loadBalancerService loadBalancerService
     */
    public void setLoadBalancerService(LoadBalancerService loadBalancerService) {
        this.loadBalancerService = loadBalancerService;
    }

}