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.api.instance; import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import jp.primecloud.auto.api.ApiSupport; import jp.primecloud.auto.api.ApiValidate; import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.StringUtils; import jp.primecloud.auto.api.response.instance.AwsInstanceResponse; import jp.primecloud.auto.api.response.instance.AzureInstanceResponse; import jp.primecloud.auto.api.response.instance.CloudstackInstanceResponse; import jp.primecloud.auto.api.response.instance.DescribeInstanceResponse; import jp.primecloud.auto.api.response.instance.NiftyInstanceResponse; import jp.primecloud.auto.api.response.instance.OpenstackInstanceResponse; import jp.primecloud.auto.api.response.instance.VcloudInstanceNetworkResponse; import jp.primecloud.auto.api.response.instance.VcloudInstanceResponse; import jp.primecloud.auto.api.response.instance.VmwareInstanceResponse; import jp.primecloud.auto.entity.crud.AwsInstance; import jp.primecloud.auto.entity.crud.AzureInstance; import jp.primecloud.auto.entity.crud.CloudstackInstance; import jp.primecloud.auto.entity.crud.Instance; import jp.primecloud.auto.entity.crud.NiftyInstance; import jp.primecloud.auto.entity.crud.NiftyKeyPair; import jp.primecloud.auto.entity.crud.OpenstackInstance; import jp.primecloud.auto.entity.crud.Platform; import jp.primecloud.auto.entity.crud.PlatformAws; import jp.primecloud.auto.entity.crud.PlatformVcloudStorageType; import jp.primecloud.auto.entity.crud.User; import jp.primecloud.auto.entity.crud.VcloudInstance; import jp.primecloud.auto.entity.crud.VcloudInstanceNetwork; import jp.primecloud.auto.entity.crud.VmwareAddress; import jp.primecloud.auto.entity.crud.VmwareInstance; import jp.primecloud.auto.entity.crud.VmwareKeyPair; import jp.primecloud.auto.exception.AutoApplicationException; import jp.primecloud.auto.exception.AutoException; import jp.primecloud.auto.service.dto.KeyPairDto; import jp.primecloud.auto.service.dto.SubnetDto; import jp.primecloud.auto.util.MessageUtils; @Path("/DescribeInstance") public class DescribeInstance extends ApiSupport { /** * * ?? * @param userName ?? * @param farmNo ? * @param instanceNo ? * * @return DescribeInstanceResponse */ @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public DescribeInstanceResponse describeInstance(@QueryParam(PARAM_NAME_USER) String userName, @QueryParam(PARAM_NAME_FARM_NO) String farmNo, @QueryParam(PARAM_NAME_INSTANCE_NO) String instanceNo) { DescribeInstanceResponse response = new DescribeInstanceResponse(); try { // ? //Key ApiValidate.validateUser(userName); //FarmNo ApiValidate.validateFarmNo(farmNo); // InstanceNo ApiValidate.validateInstanceNo(instanceNo); //? User user = userDao.readByUsername(userName); if (user == null) { // ???? throw new AutoApplicationException("EAPI-100000", "User", PARAM_NAME_USER, userName); } // ?? Instance instance = instanceDao.read(Long.parseLong(instanceNo)); if (instance == null || BooleanUtils.isTrue(instance.getLoadBalancer())) { // ???? or ?? throw new AutoApplicationException("EAPI-100000", "Instance", PARAM_NAME_INSTANCE_NO, instanceNo); } if (BooleanUtils.isFalse(instance.getFarmNo().equals(Long.parseLong(farmNo)))) { //????? throw new AutoApplicationException("EAPI-100022", "Instance", farmNo, PARAM_NAME_INSTANCE_NO, instanceNo); } //? Platform platform = platformDao.read(instance.getPlatformNo()); if (platform == null) { // ???? throw new AutoApplicationException("EAPI-100000", "Platform", PARAM_NAME_PLATFORM_NO, instance.getPlatformNo()); } // response = new DescribeInstanceResponse(instance); // TODO CLOUD BRANCHING if (PLATFORM_TYPE_AWS.equals(platform.getPlatformType())) { //AWS PlatformAws platformAws = platformAwsDao.read(instance.getPlatformNo()); AwsInstance awsInstance = awsInstanceDao.read(Long.parseLong(instanceNo)); if (awsInstance == null) { // AWS???? throw new AutoApplicationException("EAPI-100000", "AwsInstance", PARAM_NAME_INSTANCE_NO, instanceNo); } AwsInstanceResponse awsResponse = new AwsInstanceResponse(awsInstance); //SUBNET? if (StringUtils.isNotEmpty(awsInstance.getSubnetId())) { List<SubnetDto> subnets = iaasDescribeService.getSubnets(user.getUserNo(), platform.getPlatformNo(), platformAws.getVpcId()); for (SubnetDto subnet : subnets) { if (subnet.getSubnetId().equals(awsInstance.getSubnetId())) { awsResponse.setSubnet(subnet.getCidrBlock()); break; } } } //AWS response.setAws(awsResponse); } else if (PLATFORM_TYPE_CLOUDSTACK.equals(platform.getPlatformType())) { //CloudStack CloudstackInstance cloudstackInstance = cloudstackInstanceDao.read(Long.parseLong(instanceNo)); if (cloudstackInstance == null) { // CloudStack???? throw new AutoApplicationException("EAPI-100000", "CloudstackInstance", PARAM_NAME_INSTANCE_NO, instanceNo); } //CloudStack response.setCloudstack(new CloudstackInstanceResponse(cloudstackInstance)); } else if (PLATFORM_TYPE_VMWARE.equals(platform.getPlatformType())) { //VMWare VmwareInstance vmwareInstance = vmwareInstanceDao.read(Long.parseLong(instanceNo)); if (vmwareInstance == null) { // VMWare???? throw new AutoApplicationException("EAPI-100000", "VmwareInstance", PARAM_NAME_INSTANCE_NO, instanceNo); } VmwareInstanceResponse vmResponse = new VmwareInstanceResponse(vmwareInstance); //VMWARE_ADDRESS? VmwareAddress vmwareAddress = vmwareAddressDao.readByInstanceNo(Long.parseLong(instanceNo)); if (vmwareAddress != null && BooleanUtils.isTrue(vmwareAddress.getEnabled())) { vmResponse.setIsStaticIp(true); vmResponse.setSubnetMask(vmwareAddress.getSubnetMask()); vmResponse.setDefaultGateway(vmwareAddress.getDefaultGateway()); } else { vmResponse.setIsStaticIp(false); } //VMWARE_KEYPAIR? List<VmwareKeyPair> keyPairs = vmwareDescribeService.getKeyPairs(user.getUserNo(), instance.getPlatformNo()); for (VmwareKeyPair keyPair : keyPairs) { if (keyPair.getKeyNo().equals(vmwareInstance.getKeyPairNo())) { vmResponse.setKeyName(keyPair.getKeyName()); break; } } //VMWare response.setVmware(vmResponse); } else if (PLATFORM_TYPE_NIFTY.equals(platform.getPlatformType())) { //Nifty NiftyInstance niftyInstance = niftyInstanceDao.read(Long.parseLong(instanceNo)); if (niftyInstance == null) { // /Nifty???? throw new AutoApplicationException("EAPI-100000", "NiftyInstance", PARAM_NAME_INSTANCE_NO, instanceNo); } NiftyInstanceResponse niftyResponse = new NiftyInstanceResponse(niftyInstance); //NIFTY_KEYPAIR? List<NiftyKeyPair> keyPairs = niftyDescribeService.getKeyPairs(user.getUserNo(), instance.getPlatformNo()); for (NiftyKeyPair keyPair : keyPairs) { if (keyPair.getKeyNo().equals(niftyInstance.getKeyPairNo())) { niftyResponse.setKeyName(keyPair.getKeyName()); break; } } //Nifty response.setNifty(niftyResponse); } else if (PLATFORM_TYPE_VCLOUD.equals(platform.getPlatformType())) { //VCLOUD_INSTANCE VcloudInstance vcloudInstance = vcloudInstanceDao.read(Long.parseLong(instanceNo)); if (vcloudInstance == null) { // VMWare???? throw new AutoApplicationException("EAPI-100000", "VcloudInstance", PARAM_NAME_INSTANCE_NO, instanceNo); } VcloudInstanceResponse vcloudInstanceResponse = new VcloudInstanceResponse(vcloudInstance); //PLATFORM_VCLOUD_STORAGE_TYPE? List<PlatformVcloudStorageType> storageTypes = platformVcloudStorageTypeDao .readByPlatformNo(platform.getPlatformNo()); for (PlatformVcloudStorageType storageType : storageTypes) { if (storageType.getStorageTypeNo().equals(vcloudInstance.getStorageTypeNo())) { vcloudInstanceResponse.setStorageTypeName(storageType.getStorageTypeName()); break; } } //VCLOUD_KEYPAIR? List<KeyPairDto> keyPairs = iaasDescribeService.getKeyPairs(user.getUserNo(), instance.getPlatformNo()); for (KeyPairDto keyPair : keyPairs) { if (keyPair.getKeyNo().equals(vcloudInstance.getKeyPairNo())) { vcloudInstanceResponse.setKeyName(keyPair.getKeyName()); break; } } // VCloudNetwork? List<VcloudInstanceNetwork> vcloudInstanceNetworks = vcloudInstanceNetworkDao .readByInstanceNo(Long.parseLong(instanceNo)); for (VcloudInstanceNetwork vcloudInstanceNetwork : vcloudInstanceNetworks) { // VCloudNetwork VcloudInstanceNetworkResponse vcloudInstanceNetworkResponse = new VcloudInstanceNetworkResponse( vcloudInstanceNetwork); // if (BooleanUtils.isTrue(vcloudInstanceNetwork.getIsPrimary())) { vcloudInstanceNetworkResponse.setIsPrimary(true); } else { vcloudInstanceNetworkResponse.setIsPrimary(false); } vcloudInstanceResponse.addVcloudNetwok(vcloudInstanceNetworkResponse); } //VCloud response.setVcloud(vcloudInstanceResponse); } else if (PLATFORM_TYPE_OPENSTACK.equals(platform.getPlatformType())) { //OpenStack OpenstackInstance openstackInstance = openstackInstanceDao.read(Long.parseLong(instanceNo)); if (openstackInstance == null) { // OpenStack???? throw new AutoApplicationException("EAPI-100000", "OpenstackInstance", PARAM_NAME_INSTANCE_NO, instanceNo); } //OpenStack response.setOpenstack(new OpenstackInstanceResponse(openstackInstance)); } else if (PLATFORM_TYPE_AZURE.equals(platform.getPlatformType())) { //Azure AzureInstance azureInstance = azureInstanceDao.read(Long.parseLong(instanceNo)); if (azureInstance == null) { // Azure???? throw new AutoApplicationException("EAPI-100000", "AzureInstance", PARAM_NAME_INSTANCE_NO, instanceNo); } //Azure response.setAzure(new AzureInstanceResponse(azureInstance)); } response.setSuccess(true); } catch (Throwable e) { String message = ""; if (e instanceof AutoException || e instanceof AutoApplicationException) { message = e.getMessage(); } else { message = MessageUtils.getMessage("EAPI-000000"); } log.error(message, e); response.setMessage(message); response.setSuccess(false); } return response; } }