From 44eaf247b5dd395aab4716d17cce580e8d4ebf13 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Wed, 1 Sep 2010 14:17:00 -0700 Subject: [PATCH] Refactoring listPods to new API framework. --- .../com/cloud/api/commands/ListPodsByCmd.java | 95 +++++++------------ .../com/cloud/server/ManagementServer.java | 5 +- .../cloud/server/ManagementServerImpl.java | 13 +-- 3 files changed, 42 insertions(+), 71 deletions(-) diff --git a/server/src/com/cloud/api/commands/ListPodsByCmd.java b/server/src/com/cloud/api/commands/ListPodsByCmd.java index ed9d21804e1..b3e510883cc 100644 --- a/server/src/com/cloud/api/commands/ListPodsByCmd.java +++ b/server/src/com/cloud/api/commands/ListPodsByCmd.java @@ -20,30 +20,22 @@ 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.BaseListCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; +import com.cloud.api.response.PodResponse; import com.cloud.dc.HostPodVO; -import com.cloud.server.Criteria; +import com.cloud.serializer.SerializerHelper; import com.cloud.test.PodZoneConfig; -import com.cloud.utils.Pair; -public class ListPodsByCmd extends BaseCmd { +@Implementation(method="searchForPods") +public class ListPodsByCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListPodsByCmd.class.getName()); private static final String s_name = "listpodsresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ZONE_ID, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -79,60 +71,37 @@ public class ListPodsByCmd extends BaseCmd { /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// + @Override public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - Long id = (Long)params.get(BaseCmd.Properties.ID.getName()); - String name = (String)params.get(BaseCmd.Properties.NAME.getName()); - Long zoneId = (Long)params.get(BaseCmd.Properties.ZONE_ID.getName()); - String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName()); - - Criteria c = new Criteria(); - - if (keyword != null) { - c.addCriteria(Criteria.KEYWORD, keyword); - } else { - c.addCriteria(Criteria.ID, id); - c.addCriteria(Criteria.NAME, name); - c.addCriteria(Criteria.DATACENTERID, zoneId); - } - - List pods = getManagementServer().searchForPods(c); - - if (pods == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Unable to find pods for specified search criteria."); - } - - List> podTags = new ArrayList>(); - Object[] podDataTags = new Object[pods.size()]; - - int i = 0; + @Override @SuppressWarnings("unchecked") + public String getResponse() { + List pods = (List)getResponseObject(); + + List response = new ArrayList(); for (HostPodVO pod : pods) { - String[] ipRange = new String[2]; - if (pod.getDescription() != null && pod.getDescription().length() > 0) { - ipRange = pod.getDescription().split("-"); - } else { - ipRange[0] = pod.getDescription(); - } - List> podData = new ArrayList>(); - podData.add(new Pair(BaseCmd.Properties.ID.getName(), pod.getId())); - podData.add(new Pair(BaseCmd.Properties.NAME.getName(), pod.getName())); - podData.add(new Pair(BaseCmd.Properties.ZONE_ID.getName(), pod.getDataCenterId())); - podData.add(new Pair(BaseCmd.Properties.ZONE_NAME.getName(), PodZoneConfig.getZoneName(pod.getDataCenterId()))); - podData.add(new Pair(BaseCmd.Properties.CIDR.getName(), pod.getCidrAddress() +"/" + pod.getCidrSize())); - podData.add(new Pair(BaseCmd.Properties.START_IP.getName(), ipRange[0])); - podData.add(new Pair(BaseCmd.Properties.END_IP.getName(), (ipRange.length > 1 && ipRange[1] != null) ? ipRange[1] : "")); - podData.add(new Pair(BaseCmd.Properties.GATEWAY.getName(), pod.getGateway())); - podDataTags[i++] = podData; + String[] ipRange = new String[2]; + if (pod.getDescription() != null && pod.getDescription().length() > 0) { + ipRange = pod.getDescription().split("-"); + } else { + ipRange[0] = pod.getDescription(); + } + + PodResponse podResponse = new PodResponse(); + podResponse.setId(pod.getId()); + podResponse.setName(pod.getName()); + podResponse.setZoneId(pod.getDataCenterId()); + podResponse.setZoneName(PodZoneConfig.getZoneName(pod.getDataCenterId())); + podResponse.setCidr(pod.getCidrAddress() +"/" + pod.getCidrSize()); + podResponse.setStartIp(ipRange[0]); + podResponse.setEndIp(((ipRange.length > 1) && (ipRange[1] != null)) ? ipRange[1] : ""); + podResponse.setGateway(pod.getGateway()); + + response.add(podResponse); } - Pair podTag = new Pair("pod", podDataTags); - podTags.add(podTag); - return podTags; + + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 8da5b4b7555..25967628f15 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -46,6 +46,7 @@ import com.cloud.api.commands.ListHostsCmd; import com.cloud.api.commands.ListIsosCmd; import com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd; import com.cloud.api.commands.ListLoadBalancerRulesCmd; +import com.cloud.api.commands.ListPodsByCmd; import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; @@ -882,10 +883,10 @@ public interface ManagementServer { /** * Searches for Pods by the specified search criteria * Can search by: pod name and/or zone name - * @param c + * @param cmd * @return List of Pods */ - List searchForPods(Criteria c); + List searchForPods(ListPodsByCmd cmd); /** * Searches for Zones by the specified search criteria diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index dbdaaae4e41..5ae9930e630 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -84,6 +84,7 @@ import com.cloud.api.commands.ListHostsCmd; import com.cloud.api.commands.ListIsosCmd; import com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd; import com.cloud.api.commands.ListLoadBalancerRulesCmd; +import com.cloud.api.commands.ListPodsByCmd; import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; @@ -3948,14 +3949,14 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public List searchForPods(Criteria c) { - Filter searchFilter = new Filter(HostPodVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit()); + public List searchForPods(ListPodsByCmd cmd) { + Filter searchFilter = new Filter(HostPodVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); SearchCriteria sc = _hostPodDao.createSearchCriteria(); - String podName = (String) c.getCriteria(Criteria.NAME); - Long id = (Long) c.getCriteria(Criteria.ID); - Long zoneId = (Long) c.getCriteria(Criteria.DATACENTERID); - Object keyword = c.getCriteria(Criteria.KEYWORD); + String podName = cmd.getPodName(); + Long id = cmd.getId(); + Long zoneId = cmd.getZoneId(); + Object keyword = cmd.getKeyword(); if (keyword != null) { SearchCriteria ssc = _hostPodDao.createSearchCriteria();