From 261c96f99cf19a306c3b0e12c3e7fcb68a9ecca3 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Tue, 17 Aug 2010 19:06:42 -0700 Subject: [PATCH] refactor createPod command to new API framework --- .../com/cloud/api/commands/CreatePodCmd.java | 86 ++++------------- .../com/cloud/api/response/PodResponse.java | 94 +++++++++++++++++++ .../configuration/ConfigurationManager.java | 12 ++- .../ConfigurationManagerImpl.java | 35 ++++++- .../com/cloud/server/ManagementServer.java | 13 --- .../cloud/server/ManagementServerImpl.java | 29 ------ 6 files changed, 153 insertions(+), 116 deletions(-) create mode 100644 server/src/com/cloud/api/response/PodResponse.java diff --git a/server/src/com/cloud/api/commands/CreatePodCmd.java b/server/src/com/cloud/api/commands/CreatePodCmd.java index ab9e2363069..7b40071fa34 100644 --- a/server/src/com/cloud/api/commands/CreatePodCmd.java +++ b/server/src/com/cloud/api/commands/CreatePodCmd.java @@ -18,36 +18,21 @@ package com.cloud.api.commands; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; +import com.cloud.api.BaseCmd.Manager; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; -import com.cloud.dc.DataCenterVO; +import com.cloud.api.response.PodResponse; import com.cloud.dc.HostPodVO; -import com.cloud.user.User; -import com.cloud.utils.Pair; +import com.cloud.serializer.SerializerHelper; +@Implementation(method="createPod", manager=Manager.ConfigManager) public class CreatePodCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(CreatePodCmd.class.getName()); private static final String s_name = "createpodresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.CIDR, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.END_IP, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.GATEWAY, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.START_IP, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.TRUE)); - - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -108,57 +93,22 @@ public class CreatePodCmd extends BaseCmd { public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } @Override - public List> execute(Map params) { - String podName = (String) params.get(BaseCmd.Properties.NAME.getName()); - Long zoneId = (Long) params.get(BaseCmd.Properties.ZONE_ID.getName()); - String gateway = (String) params.get(BaseCmd.Properties.GATEWAY.getName()); - String cidr = (String) params.get(BaseCmd.Properties.CIDR.getName()); - String startIp = (String) params.get(BaseCmd.Properties.START_IP.getName()); - String endIp = (String) params.get(BaseCmd.Properties.END_IP.getName()); - Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); - - if (userId == null) { - userId = Long.valueOf(User.UID_SYSTEM); - } - - //verify input parameters - DataCenterVO zone = getManagementServer().findDataCenterById(zoneId); - if (zone == null) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find zone by id " + zoneId); - } + public String getResponse() { + HostPodVO pod = (HostPodVO)getResponseObject(); - if (endIp != null && startIp == null) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "If an end IP is specified, a start IP must be specified."); - } - - HostPodVO pod = null; - try { - pod = getManagementServer().createPod(userId, podName, zoneId, gateway, cidr, startIp, endIp); - } catch (Exception ex) { - s_logger.error("Exception creating pod", ex); - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage()); - } + PodResponse response = new PodResponse(); + response.setId(pod.getId()); + response.setCidr(pod.getCidrAddress() + "/" + pod.getCidrSize()); + // TODO: implement +// response.setEndIp(pod.getEndIp()); +// response.setStartIp(pod.getStartIp()); +// response.setZoneName(pod.getZoneName()); + response.setGateway(pod.getGateway()); + response.setName(pod.getName()); + response.setZoneId(pod.getDataCenterId()); - List> returnValues = new ArrayList>(); - - if (pod == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create pod; internal error."); - } else { - returnValues.add(new Pair(BaseCmd.Properties.ID.getName(), pod.getId())); - returnValues.add(new Pair(BaseCmd.Properties.NAME.getName(), podName)); - returnValues.add(new Pair(BaseCmd.Properties.ZONE_ID.getName(), zoneId)); - returnValues.add(new Pair(BaseCmd.Properties.ZONE_NAME.getName(), zone.getName())); - returnValues.add(new Pair(BaseCmd.Properties.GATEWAY.getName(), pod.getGateway())); - returnValues.add(new Pair(BaseCmd.Properties.CIDR.getName(), cidr)); - returnValues.add(new Pair(BaseCmd.Properties.START_IP.getName(), startIp)); - returnValues.add(new Pair(BaseCmd.Properties.END_IP.getName(), endIp != null ? endIp : "")); - } - - return returnValues; + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/response/PodResponse.java b/server/src/com/cloud/api/response/PodResponse.java new file mode 100644 index 00000000000..39ac46c6794 --- /dev/null +++ b/server/src/com/cloud/api/response/PodResponse.java @@ -0,0 +1,94 @@ +package com.cloud.api.response; + +import com.cloud.api.ResponseObject; +import com.cloud.serializer.Param; + +public class PodResponse implements ResponseObject { + @Param(name="id") + private Long id; + + @Param(name="name") + private String name; + + @Param(name="zoneid") + private Long zoneId; + + @Param(name="zonename") + private String zoneName; + + @Param(name="gateway") + private String gateway; + + @Param(name="cidr") + private String cidr; + + @Param(name="startip") + private String startIp; + + @Param(name="endIp") + private String endIp; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getZoneId() { + return zoneId; + } + + public void setZoneId(Long zoneId) { + this.zoneId = zoneId; + } + + public String getZoneName() { + return zoneName; + } + + public void setZoneName(String zoneName) { + this.zoneName = zoneName; + } + + public String getGateway() { + return gateway; + } + + public void setGateway(String gateway) { + this.gateway = gateway; + } + + public String getCidr() { + return cidr; + } + + public void setCidr(String cidr) { + this.cidr = cidr; + } + + public String getStartIp() { + return startIp; + } + + public void setStartIp(String startIp) { + this.startIp = startIp; + } + + public String getEndIp() { + return endIp; + } + + public void setEndIp(String endIp) { + this.endIp = endIp; + } +} diff --git a/server/src/com/cloud/configuration/ConfigurationManager.java b/server/src/com/cloud/configuration/ConfigurationManager.java index 925a642248d..892b7a07a2f 100644 --- a/server/src/com/cloud/configuration/ConfigurationManager.java +++ b/server/src/com/cloud/configuration/ConfigurationManager.java @@ -21,6 +21,7 @@ import java.util.List; import com.cloud.api.commands.AddConfigCmd; import com.cloud.api.commands.CreateDiskOfferingCmd; +import com.cloud.api.commands.CreatePodCmd; import com.cloud.api.commands.DeleteDiskOfferingCmd; import com.cloud.api.commands.DeletePodCmd; import com.cloud.api.commands.UpdateCfgCmd; @@ -140,7 +141,16 @@ public interface ConfigurationManager extends Manager { * @return Pod */ HostPodVO createPod(long userId, String podName, long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException; - + + /** + * Creates a new pod based on the parameters specified in the command object + * @param cmd the command object that specifies the name, zone, gateway, cidr, and ip range for the pod + * @return the new pod if successful, null otherwise + * @throws InvalidParameterValueException + * @throws InternalErrorException + */ + HostPodVO createPod(CreatePodCmd cmd) throws InvalidParameterValueException, InternalErrorException; + /** * Edits a pod in the database. Will not allow you to edit pods that are being used anywhere in the system. * @param userId diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index d286a89354f..b81bba854bf 100644 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -35,6 +35,7 @@ import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; import com.cloud.api.commands.AddConfigCmd; import com.cloud.api.commands.CreateDiskOfferingCmd; +import com.cloud.api.commands.CreatePodCmd; import com.cloud.api.commands.DeleteDiskOfferingCmd; import com.cloud.api.commands.DeletePodCmd; import com.cloud.api.commands.UpdateCfgCmd; @@ -508,8 +509,35 @@ public class ConfigurationManagerImpl implements ConfigurationManager { return pod; } - - @DB + + @Override + public HostPodVO createPod(CreatePodCmd cmd) throws InvalidParameterValueException, InternalErrorException { + String cidr = cmd.getCidr(); + String endIp = cmd.getEndIp(); + String gateway = cmd.getGateway(); + String name = cmd.getPodName(); + String startIp = cmd.getStartIp(); + Long zoneId = cmd.getZoneId(); + + //verify input parameters + DataCenterVO zone = _zoneDao.findById(zoneId); + if (zone == null) { + throw new InvalidParameterValueException("Failed to create pod " + name + " -- unable to find zone " + zoneId); + } + + if (endIp != null && startIp == null) { + throw new InvalidParameterValueException("Failed to create pod " + name + " -- if an end IP is specified, a start IP must be specified."); + } + + Long userId = UserContext.current().getUserId(); + if (userId == null) { + userId = Long.valueOf(User.UID_SYSTEM); + } + + return createPod(userId.longValue(), name, zoneId, gateway, cidr, startIp, endIp); + } + + @Override @DB public HostPodVO createPod(long userId, String podName, long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException { checkPodAttributes(-1, podName, zoneId, gateway, cidr, startIp, endIp, true); @@ -786,7 +814,6 @@ public class ConfigurationManagerImpl implements ConfigurationManager { } //5. Reached here, hence editable - DataCenterVO zoneHandle = _zoneDao.findById(zoneId); String oldZoneName = zone.getName(); if (zoneName == null) { @@ -1067,8 +1094,6 @@ public class ConfigurationManagerImpl implements ConfigurationManager { } } - - public String changePrivateIPRange(boolean add, long podId, String startIP, String endIP) throws InvalidParameterValueException { checkPrivateIpRangeErrors(podId, startIP, endIP); diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index a261dc76f1f..98620a6ec6d 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -993,19 +993,6 @@ public interface ManagementServer { */ // ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags); - /** - * Adds a new pod to the database - * @param userId - * @param podName - * @param zoneId - * @param gateway - * @param cidr - * @param startIp - * @param endIp - * @return Pod - */ - HostPodVO createPod(long userId, String podName, Long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException; - /** * Edits a pod in the database * @param userId diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 77fc4b3c6df..ca205497935 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -38,7 +38,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.StringTokenizer; import java.util.TimeZone; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -89,7 +88,6 @@ import com.cloud.api.commands.UpdateTemplateCmd; import com.cloud.api.commands.UpdateTemplateOrIsoPermissionsCmd; import com.cloud.api.commands.UpdateTemplatePermissionsCmd; import com.cloud.api.commands.UpdateUserCmd; -import com.cloud.api.commands.UpgradeVMCmd; import com.cloud.async.AsyncInstanceCreateStatus; import com.cloud.async.AsyncJobExecutor; import com.cloud.async.AsyncJobManager; @@ -282,7 +280,6 @@ import com.cloud.vm.dao.SecondaryStorageVmDao; import com.cloud.vm.dao.UserVmDao; import com.cloud.vm.dao.VMInstanceDao; import com.google.gson.Gson; -import com.sun.org.apache.bcel.internal.generic.INSTANCEOF; public class ManagementServerImpl implements ManagementServer { public static final Logger s_logger = Logger.getLogger(ManagementServerImpl.class.getName()); @@ -4316,37 +4313,11 @@ public class ManagementServerImpl implements ManagementServer { return _configMgr.createServiceOffering(userId, name, cpu, ramSize, speed, displayText, localStorageRequired, offerHA, useVirtualNetwork, tags); } -// @Override -// public ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags) { -// return _configMgr.updateServiceOffering(userId, serviceOfferingId, name, displayText, offerHA, useVirtualNetwork, tags); -// } - - - @Override - public HostPodVO createPod(long userId, String podName, Long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException { - return _configMgr.createPod(userId, podName, zoneId, gateway, cidr, startIp, endIp); - } - -// @Override -// public HostPodVO editPod(long userId, long podId, String newPodName, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException { -// return _configMgr.editPod(userId, podId, newPodName, gateway, cidr, startIp, endIp); -// } - -// @Override -// public void deletePod(long userId, long podId) throws InvalidParameterValueException, InternalErrorException { -// _configMgr.deletePod(userId, podId); -// } - @Override public DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String internalDns1, String internalDns2, String vnetRange,String guestCidr) throws InvalidParameterValueException, InternalErrorException { return _configMgr.createZone(userId, zoneName, dns1, dns2, internalDns1, internalDns2, vnetRange, guestCidr); } -// @Override -// public DataCenterVO editZone(long userId, Long zoneId, String newZoneName, String dns1, String dns2, String dns3, String dns4, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException { -// return _configMgr.editZone(userId, zoneId, newZoneName, dns1, dns2, dns3, dns4, vnetRange, guestCidr); -// } - @Override public void deleteZone(long userId, Long zoneId) throws InvalidParameterValueException, InternalErrorException { _configMgr.deleteZone(userId, zoneId);