Refactor listRouters to new API framework.

This commit is contained in:
Kris McQueen 2010-09-07 16:25:26 -07:00
parent 4e5aa9cd98
commit a905442b2e
4 changed files with 415 additions and 170 deletions

View File

@ -25,35 +25,24 @@ 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.DomainRouterResponse;
import com.cloud.async.AsyncJobVO;
import com.cloud.domain.DomainVO;
import com.cloud.serializer.SerializerHelper;
import com.cloud.server.Criteria;
import com.cloud.user.Account;
import com.cloud.utils.Pair;
import com.cloud.vm.DomainRouterVO;
public class ListRoutersCmd extends BaseCmd {
@Implementation(method="searchForRouters")
public class ListRoutersCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(ListRoutersCmd.class.getName());
private static final String s_name = "listroutersresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.HOST_ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.NAME, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.POD_ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.STATE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ZONE_ID, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.KEYWORD, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGESIZE, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -119,134 +108,60 @@ public class ListRoutersCmd extends BaseCmd {
@Override
public String getName() {
return s_name;
}
@Override
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName());
Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName());
Long zoneId = (Long)params.get(BaseCmd.Properties.ZONE_ID.getName());
Long podId = (Long)params.get(BaseCmd.Properties.POD_ID.getName());
Long hostId = (Long)params.get(BaseCmd.Properties.HOST_ID.getName());
String name = (String)params.get(BaseCmd.Properties.NAME.getName());
String state = (String)params.get(BaseCmd.Properties.STATE.getName());
String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName());
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
Long accountId = null;
Long[] accountIds = null;
// validate domainId before proceeding
if (domainId != null) {
if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list routers");
}
if (accountName != null) {
Account userAccount = getManagementServer().findAccountByName(accountName, domainId);
if (userAccount != null) {
accountId = userAccount.getId();
} else {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId);
}
}
} else {
domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId());
}
Long startIndex = Long.valueOf(0);
int pageSizeNum = 50;
if (pageSize != null) {
pageSizeNum = pageSize.intValue();
}
if (page != null) {
int pageNum = page.intValue();
if (pageNum > 0) {
startIndex = Long.valueOf(pageSizeNum * (pageNum-1));
}
}
if (accountId != null) {
accountIds = new Long[1];
accountIds[0] = accountId;
}
Criteria c = new Criteria("id", Boolean.TRUE, startIndex, Long.valueOf(pageSizeNum));
c.addCriteria(Criteria.ACCOUNTID, accountIds);
if (keyword != null) {
c.addCriteria(Criteria.KEYWORD, keyword);
} else {
c.addCriteria(Criteria.DOMAINID, domainId);
c.addCriteria(Criteria.DATACENTERID, zoneId);
c.addCriteria(Criteria.PODID, podId);
c.addCriteria(Criteria.HOSTID, hostId);
c.addCriteria(Criteria.NAME, name);
c.addCriteria(Criteria.STATE, state);
}
List<DomainRouterVO> routers = getManagementServer().searchForRouters(c);
List<Pair<String, Object>> routerTags = new ArrayList<Pair<String, Object>>();
Object[] rTag = new Object[routers.size()];
int i = 0;
for (DomainRouterVO router : routers) {
List<Pair<String, Object>> routerData = new ArrayList<Pair<String, Object>>();
routerData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), Long.toString(router.getId())));
AsyncJobVO asyncJob = getManagementServer().findInstancePendingAsyncJob("domain_router", router.getId());
if(asyncJob != null) {
routerData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), asyncJob.getId().toString()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_STATUS.getName(), String.valueOf(asyncJob.getStatus())));
}
routerData.add(new Pair<String, Object>(BaseCmd.Properties.ZONE_ID.getName(), Long.valueOf(router.getDataCenterId()).toString()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.ZONE_NAME.getName(), getManagementServer().findDataCenterById(router.getDataCenterId()).getName()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.DNS1.getName(), router.getDns1()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.DNS2.getName(), router.getDns2()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.NETWORK_DOMAIN.getName(), router.getDomain()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.GATEWAY.getName(), router.getGateway()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), router.getName()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.POD_ID.getName(), Long.valueOf(router.getPodId()).toString()));
if (router.getHostId() != null) {
routerData.add(new Pair<String, Object>(BaseCmd.Properties.HOST_ID.getName(), router.getHostId().toString()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.HOST_NAME.getName(), getManagementServer().getHostBy(router.getHostId()).getName()));
}
routerData.add(new Pair<String, Object>(BaseCmd.Properties.PRIVATE_IP.getName(), router.getPrivateIpAddress()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.PRIVATE_MAC_ADDRESS.getName(), router.getPrivateMacAddress()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.PRIVATE_NETMASK.getName(), router.getPrivateNetmask()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.PUBLIC_IP.getName(), router.getPublicIpAddress()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.PUBLIC_MAC_ADDRESS.getName(), router.getPublicMacAddress()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.PUBLIC_NETMASK.getName(), router.getPublicNetmask()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.GUEST_IP_ADDRESS.getName(), router.getGuestIpAddress()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.GUEST_MAC_ADDRESS.getName(), router.getGuestMacAddress()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.GUEST_NETMASK.getName(), router.getGuestNetmask()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.TEMPLATE_ID.getName(), Long.valueOf(router.getTemplateId()).toString()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(router.getCreated())));
if (router.getHostId() != null) {
routerData.add(new Pair<String, Object>(BaseCmd.Properties.HOST_ID.getName(), router.getHostId().toString()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.HOST_NAME.getName(),getManagementServer().getHostBy(router.getHostId()).getName()));
}
if (router.getState() != null) {
routerData.add(new Pair<String, Object>(BaseCmd.Properties.STATE.getName(), router.getState().toString()));
}
Account accountTemp = getManagementServer().findAccountById(router.getAccountId());
if (accountTemp != null) {
routerData.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId()));
routerData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()));
}
rTag[i++] = routerData;
}
Pair<String, Object> routerTag = new Pair<String, Object>("router", rTag);
routerTags.add(routerTag);
return routerTags;
@Override @SuppressWarnings("unchecked")
public String getResponse() {
List<DomainRouterVO> routers = (List<DomainRouterVO>)getResponseObject();
List<DomainRouterResponse> response = new ArrayList<DomainRouterResponse>();
for (DomainRouterVO router : routers) {
DomainRouterResponse routerResponse = new DomainRouterResponse();
routerResponse.setId(router.getId());
AsyncJobVO asyncJob = getManagementServer().findInstancePendingAsyncJob("domain_router", router.getId());
if (asyncJob != null) {
routerResponse.setJobId(asyncJob.getId());
routerResponse.setJobStatus(asyncJob.getStatus());
}
routerResponse.setZoneId(router.getDataCenterId());
routerResponse.setZoneName(getManagementServer().findDataCenterById(router.getDataCenterId()).getName());
routerResponse.setDns1(router.getDns1());
routerResponse.setDns2(router.getDns2());
routerResponse.setNetworkDomain(router.getDomain());
routerResponse.setGateway(router.getGateway());
routerResponse.setName(router.getName());
routerResponse.setPodId(router.getPodId());
if (router.getHostId() != null) {
routerResponse.setHostId(router.getHostId());
routerResponse.setHostName(getManagementServer().getHostBy(router.getHostId()).getName());
}
routerResponse.setPrivateIp(router.getPrivateIpAddress()());
routerResponse.setPrivateMacAddress(router.getPrivateMacAddress());
routerResponse.setPrivateNetmask(router.getPrivateNetmask());
routerResponse.setPublicIp(router.getPublicIpAddress());
routerResponse.setPublicMacAddress(router.getPublicMacAddress());
routerResponse.setPublicNetmask(router.getPublicNetmask());
routerResponse.setGuestIpAddress(router.getGuestIpAddress());
routerResponse.setGuestMacAddress(router.getGuestMacAddress());
routerResponse.setGuestNetmask(router.getGuestNetmask());
routerResponse.setTemplateId(router.getTemplateId());
routerResponse.setCreated(router.getCreated());
routerResponse.setState(router.getState());
Account accountTemp = getManagementServer().findAccountById(router.getAccountId());
if (accountTemp != null) {
routerResponse.setAccountName(accountTemp.getAccountName());
routerResponse.setDomainId(accountTemp.getDomainId());
routerResponse.setDomain(getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName());
}
response.add(routerResponse);
}
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -0,0 +1,317 @@
package com.cloud.api.response;
import java.util.Date;
import com.cloud.api.ResponseObject;
import com.cloud.serializer.Param;
import com.cloud.vm.State;
public class DomainRouterResponse implements ResponseObject {
@Param(name="id")
private Long id;
@Param(name="jobid")
private Long jobId;
@Param(name="jobstatus")
private Integer jobStatus;
@Param(name="zoneid")
private Long zoneId;
@Param(name="zonename")
private String zoneName;
@Param(name="dns1")
private String dns1;
@Param(name="dns2")
private String dns2;
@Param(name="networkdomain")
private String networkDomain;
@Param(name="gateway")
private String gateway;
@Param(name="name")
private String name;
@Param(name="podid")
private Long podId;
@Param(name="hostid")
private Long hostId;
@Param(name="hostname")
private String hostName;
@Param(name="privateip")
private String privateIp;
@Param(name="privatemacaddress")
private String privateMacAddress;
@Param(name="privatenetmask")
private String privateNetmask;
@Param(name="publicip")
private String publicIp;
@Param(name="publicmacaddress")
private String publicMacAddress;
@Param(name="publicnetmask")
private String publicNetmask;
@Param(name="guestipaddress")
private String guestIpAddress;
@Param(name="guestMacAddress")
private String guestMacAddress;
@Param(name="guestNetmask")
private String guestNetmask;
@Param(name="templateid")
private Long templateId;
@Param(name="created")
private Date created;
@Param(name="state")
private State state;
@Param(name="account")
private String accountName;
@Param(name="domainid")
private Long domainId;
@Param(name="domain")
private String domainName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getJobId() {
return jobId;
}
public void setJobId(Long jobId) {
this.jobId = jobId;
}
public Integer getJobStatus() {
return jobStatus;
}
public void setJobStatus(Integer jobStatus) {
this.jobStatus = jobStatus;
}
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 getDns1() {
return dns1;
}
public void setDns1(String dns1) {
this.dns1 = dns1;
}
public String getDns2() {
return dns2;
}
public void setDns2(String dns2) {
this.dns2 = dns2;
}
public String getNetworkDomain() {
return networkDomain;
}
public void setNetworkDomain(String networkDomain) {
this.networkDomain = networkDomain;
}
public String getGateway() {
return gateway;
}
public void setGateway(String gateway) {
this.gateway = gateway;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPodId() {
return podId;
}
public void setPodId(Long podId) {
this.podId = podId;
}
public Long getHostId() {
return hostId;
}
public void setHostId(Long hostId) {
this.hostId = hostId;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public String getPrivateIp() {
return privateIp;
}
public void setPrivateIp(String privateIp) {
this.privateIp = privateIp;
}
public String getPrivateMacAddress() {
return privateMacAddress;
}
public void setPrivateMacAddress(String privateMacAddress) {
this.privateMacAddress = privateMacAddress;
}
public String getPrivateNetmask() {
return privateNetmask;
}
public void setPrivateNetmask(String privateNetmask) {
this.privateNetmask = privateNetmask;
}
public String getPublicIp() {
return publicIp;
}
public void setPublicIp(String publicIp) {
this.publicIp = publicIp;
}
public String getPublicMacAddress() {
return publicMacAddress;
}
public void setPublicMacAddress(String publicMacAddress) {
this.publicMacAddress = publicMacAddress;
}
public String getPublicNetmask() {
return publicNetmask;
}
public void setPublicNetmask(String publicNetmask) {
this.publicNetmask = publicNetmask;
}
public String getGuestIpAddress() {
return guestIpAddress;
}
public void setGuestIpAddress(String guestIpAddress) {
this.guestIpAddress = guestIpAddress;
}
public String getGuestMacAddress() {
return guestMacAddress;
}
public void setGuestMacAddress(String guestMacAddress) {
this.guestMacAddress = guestMacAddress;
}
public String getGuestNetmask() {
return guestNetmask;
}
public void setGuestNetmask(String guestNetmask) {
this.guestNetmask = guestNetmask;
}
public Long getTemplateId() {
return templateId;
}
public void setTemplateId(Long templateId) {
this.templateId = templateId;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Long getDomainId() {
return domainId;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
}

View File

@ -52,6 +52,7 @@ import com.cloud.api.commands.ListPortForwardingServicesByVmCmd;
import com.cloud.api.commands.ListPortForwardingServicesCmd;
import com.cloud.api.commands.ListPreallocatedLunsCmd;
import com.cloud.api.commands.ListPublicIpAddressesCmd;
import com.cloud.api.commands.ListRoutersCmd;
import com.cloud.api.commands.ListTemplatesCmd;
import com.cloud.api.commands.LockAccountCmd;
import com.cloud.api.commands.LockUserCmd;
@ -1196,10 +1197,10 @@ public interface ManagementServer {
/**
* Obtains a list of routers by the specified search criteria.
* Can search by: "userId", "name", "state", "dataCenterId", "podId", "hostId"
* @param c
* @param cmd
* @return List of DomainRouters.
*/
List<DomainRouterVO> searchForRouters(Criteria c);
List<DomainRouterVO> searchForRouters(ListRoutersCmd cmd) throws InvalidParameterValueException, PermissionDeniedException;
List<ConsoleProxyVO> searchForConsoleProxy(Criteria c);
@ -1493,15 +1494,6 @@ public interface ManagementServer {
*/
ResourceLimitVO findLimitById(long limitId);
/**
* Searches for Limits.
* @param domainId
* @param accountId
* @param type
* @return a list of Limits
*/
// List<ResourceLimitVO> searchForLimits(Criteria c);
/**
* Finds the correct limit for an account. I.e. if an account's limit is not present, it will check the account's domain, and as a last resort use the global limit.
* @param type

View File

@ -89,6 +89,7 @@ import com.cloud.api.commands.ListPortForwardingServicesByVmCmd;
import com.cloud.api.commands.ListPortForwardingServicesCmd;
import com.cloud.api.commands.ListPreallocatedLunsCmd;
import com.cloud.api.commands.ListPublicIpAddressesCmd;
import com.cloud.api.commands.ListRoutersCmd;
import com.cloud.api.commands.ListTemplatesCmd;
import com.cloud.api.commands.LockAccountCmd;
import com.cloud.api.commands.LockUserCmd;
@ -5318,17 +5319,37 @@ public class ManagementServerImpl implements ManagementServer {
}
@Override
public List<DomainRouterVO> searchForRouters(Criteria c) {
Filter searchFilter = new Filter(DomainRouterVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
public List<DomainRouterVO> searchForRouters(ListRoutersCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
Long domainId = cmd.getDomainId();
String accountName = cmd.getAccountName();
Long accountId = null;
Account account = (Account)UserContext.current().getAccountObject();
Object[] accountIds = (Object[]) c.getCriteria(Criteria.ACCOUNTID);
Object name = c.getCriteria(Criteria.NAME);
Object state = c.getCriteria(Criteria.STATE);
Object zone = c.getCriteria(Criteria.DATACENTERID);
Object pod = c.getCriteria(Criteria.PODID);
Object hostId = c.getCriteria(Criteria.HOSTID);
Object domainId = c.getCriteria(Criteria.DOMAINID);
Object keyword = c.getCriteria(Criteria.KEYWORD);
// validate domainId before proceeding
if (domainId != null) {
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
throw new PermissionDeniedException("Invalid domain id (" + domainId + ") given, unable to list routers");
}
if (accountName != null) {
Account userAccount = _accountDao.findActiveAccount(accountName, domainId);
if (userAccount != null) {
accountId = userAccount.getId();
} else {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId);
}
}
} else {
domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId());
}
Filter searchFilter = new Filter(DomainRouterVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
Object name = cmd.getRouterName();
Object state = cmd.getState();
Object zone = cmd.getZoneId();
Object pod = cmd.getPodId();
Object hostId = cmd.getHostId();
Object keyword = cmd.getKeyword();
SearchBuilder<DomainRouterVO> sb = _routerDao.createSearchBuilder();
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
@ -5338,7 +5359,7 @@ public class ManagementServerImpl implements ManagementServer {
sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
sb.and("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
if ((accountIds == null) && (domainId != null)) {
if ((accountId == null) && (domainId != null)) {
// if accountId isn't specified, we can do a domain match for the admin case
SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
@ -5360,8 +5381,8 @@ public class ManagementServerImpl implements ManagementServer {
sc.setParameters("name", "%" + name + "%");
}
if (accountIds != null) {
sc.setParameters("accountId", accountIds);
if (accountId != null) {
sc.setParameters("accountId", accountId);
} else if (domainId != null) {
DomainVO domain = _domainDao.findById((Long)domainId);
sc.setJoinParameters("domainSearch", "path", domain.getPath() + "%");