Refactor listPortForwardingRules to new API framework. Clean up imports.

This commit is contained in:
Kris McQueen 2010-09-01 15:00:22 -07:00
parent 44eaf247b5
commit 931706ad87
6 changed files with 87 additions and 132 deletions

View File

@ -25,26 +25,21 @@ import java.util.Map;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.BaseCmd.Manager;
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.FirewallRuleResponse;
import com.cloud.network.FirewallRuleVO;
import com.cloud.network.IPAddressVO;
import com.cloud.serializer.SerializerHelper;
import com.cloud.server.Criteria;
import com.cloud.user.Account;
import com.cloud.utils.Pair;
import com.cloud.vm.UserVmVO;
public class ListPortForwardingRulesCmd extends BaseCmd {
@Implementation(method="listPortForwardingRules", manager=Manager.NetworkManager)
public class ListPortForwardingRulesCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(ListPortForwardingRulesCmd.class.getName());
private static final String s_name = "listportforwardingrulesresponse";
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_OBJ, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.IP_ADDRESS, Boolean.TRUE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -69,109 +64,44 @@ public class ListPortForwardingRulesCmd 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 ipAddress = (String)params.get(BaseCmd.Properties.IP_ADDRESS.getName());
IPAddressVO ipAddressVO = getManagementServer().findIPAddressById(ipAddress);
if (ipAddressVO == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find IP address " + ipAddress);
}
Account addrOwner = getManagementServer().findAccountById(ipAddressVO.getAccountId());
// if an admin account was passed in, or no account was passed in, make sure we honor the accountName/domainId parameters
if ((account != null) && isAdmin(account.getType())) {
if (ipAddressVO.getAccountId() != null) {
if ((addrOwner != null) && !getManagementServer().isChildDomain(account.getDomainId(), addrOwner.getDomainId())) {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to list port forwarding rules for address " + ipAddress + ", permission denied for account " + account.getId());
}
} else {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to list port forwarding rules for address " + ipAddress + ", address not in use.");
}
} else {
if (account != null) {
if ((ipAddressVO.getAccountId() == null) || (account.getId().longValue() != ipAddressVO.getAccountId().longValue())) {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to list port forwarding rules for address " + ipAddress + ", permission denied for account " + account.getId());
}
addrOwner = account;
}
}
List<FirewallRuleVO> firewallRules = getManagementServer().listIPForwarding(ipAddress, true);
if (firewallRules == null) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error searching port forwarding rules for address " + ipAddress);
}
/*
Criteria lbCriteria = new Criteria();
lbCriteria.addCriteria(Criteria.IPADDRESS, ipAddress);
List<LoadBalancerVO> loadBalancers = getManagementServer().searchForLoadBalancers(lbCriteria);
if (loadBalancers == null) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error searching load balancer rules for address " + ipAddress);
}
*/
Map<String, UserVmVO> userVmCache = new HashMap<String, UserVmVO>();
List<Pair<String, Object>> groupsTags = new ArrayList<Pair<String, Object>>();
Object[] forwardingTag = new Object[firewallRules.size()];
int i = 0;
for (FirewallRuleVO fwRule : firewallRules) {
List<Pair<String, Object>> ruleData = new ArrayList<Pair<String, Object>>();
ruleData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), fwRule.getId().toString()));
ruleData.add(new Pair<String, Object>(BaseCmd.Properties.PUBLIC_PORT.getName(), fwRule.getPublicPort()));
ruleData.add(new Pair<String, Object>(BaseCmd.Properties.PRIVATE_PORT.getName(), fwRule.getPrivatePort()));
ruleData.add(new Pair<String, Object>(BaseCmd.Properties.PROTOCOL.getName(), fwRule.getProtocol()));
UserVmVO userVM = userVmCache.get(fwRule.getPrivateIpAddress());
if (userVM == null) {
Criteria c = new Criteria();
c.addCriteria(Criteria.ACCOUNTID, new Object[] {addrOwner.getId()});
c.addCriteria(Criteria.DATACENTERID, ipAddressVO.getDataCenterId());
c.addCriteria(Criteria.IPADDRESS, fwRule.getPrivateIpAddress());
List<UserVmVO> userVMs = getManagementServer().searchForUserVMs(c);
if ((userVMs != null) && (userVMs.size() > 0)) {
userVM = userVMs.get(0);
userVmCache.put(fwRule.getPrivateIpAddress(), userVM);
}
}
if (userVM != null) {
ruleData.add(new Pair<String, Object>(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName(), Long.toString(userVM.getId())));
ruleData.add(new Pair<String, Object>(BaseCmd.Properties.VIRTUAL_MACHINE_NAME.getName(), userVM.getName()));
}
forwardingTag[i++] = ruleData;
}
Pair<String, Object> forwardingTags = new Pair<String, Object>("portforwardingrule", forwardingTag);
groupsTags.add(forwardingTags);
/*
Object[] lbTag = new Object[loadBalancers.size()];
i = 0;
for (LoadBalancerVO loadBalancer : loadBalancers) {
List<Pair<String, Object>> lbData = new ArrayList<Pair<String, Object>>();
lbData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), loadBalancer.getId().toString()));
lbData.add(new Pair<String, Object>(BaseCmd.Properties.PUBLIC_PORT.getName(), loadBalancer.getPublicPort()));
lbData.add(new Pair<String, Object>(BaseCmd.Properties.PRIVATE_PORT.getName(), loadBalancer.getPrivatePort()));
lbData.add(new Pair<String, Object>(BaseCmd.Properties.ALGORITHM.getName(), loadBalancer.getAlgorithm()));
lbTag[i++] = lbData;
}
Pair<String, Object> lbTags = new Pair<String, Object>("loadbalancer", lbTag);
groupsTags.add(lbTags);
*/
return groupsTags;
@Override @SuppressWarnings("unchecked")
public String getResponse() {
List<FirewallRuleVO> firewallRules = (List<FirewallRuleVO>)getResponseObject();
Map<String, UserVmVO> userVmCache = new HashMap<String, UserVmVO>();
List<FirewallRuleResponse> response = new ArrayList<FirewallRuleResponse>();
for (FirewallRuleVO fwRule : firewallRules) {
FirewallRuleResponse ruleData = new FirewallRuleResponse();
ruleData.setId(fwRule.getId());
ruleData.setPublicPort(fwRule.getPublicPort());
ruleData.setPrivatePort(fwRule.getPrivatePort());
ruleData.setProtocol(fwRule.getProtocol());
UserVmVO userVM = userVmCache.get(fwRule.getPrivateIpAddress());
if (userVM == null) {
Criteria c = new Criteria();
c.addCriteria(Criteria.ACCOUNTID, new Object[] {addrOwner.getId()});
c.addCriteria(Criteria.DATACENTERID, ipAddressVO.getDataCenterId());
c.addCriteria(Criteria.IPADDRESS, fwRule.getPrivateIpAddress());
List<UserVmVO> userVMs = getManagementServer().searchForUserVMs(c);
if ((userVMs != null) && (userVMs.size() > 0)) {
userVM = userVMs.get(0);
userVmCache.put(fwRule.getPrivateIpAddress(), userVM);
}
}
if (userVM != null) {
ruleData.setVirtualMachineId(userVM.getId());
ruleData.setVirtualMachineName(userVM.getName());
}
response.add(ruleData);
}
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -26,6 +26,7 @@ import com.cloud.api.commands.CreateIPForwardingRuleCmd;
import com.cloud.api.commands.CreateLoadBalancerRuleCmd;
import com.cloud.api.commands.DeletePortForwardingServiceRuleCmd;
import com.cloud.api.commands.DisassociateIPAddrCmd;
import com.cloud.api.commands.ListPortForwardingRulesCmd;
import com.cloud.api.commands.RemoveFromLoadBalancerRuleCmd;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO;
@ -177,6 +178,13 @@ public interface NetworkManager extends Manager {
*/
public FirewallRuleVO createPortForwardingRule(CreateIPForwardingRuleCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, NetworkRuleConflictException;
/**
* List port forwarding rules assigned to an ip address
* @param cmd the command object holding the criteria for listing port forwarding rules (the ipAddress)
* @return list of port forwarding rules on the given address, empty list if no rules exist
*/
public List<FirewallRuleVO> listPortForwardingRules(ListPortForwardingRulesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException;
/**
* Create a load balancer rule from the given ipAddress/port to the given private port
* @param cmd the command specifying the ip address, public port, protocol, private port, and algorithm

View File

@ -64,6 +64,7 @@ import com.cloud.api.commands.CreateIPForwardingRuleCmd;
import com.cloud.api.commands.CreateLoadBalancerRuleCmd;
import com.cloud.api.commands.DeletePortForwardingServiceRuleCmd;
import com.cloud.api.commands.DisassociateIPAddrCmd;
import com.cloud.api.commands.ListPortForwardingRulesCmd;
import com.cloud.api.commands.RemoveFromLoadBalancerRuleCmd;
import com.cloud.async.AsyncJobExecutor;
import com.cloud.async.AsyncJobManager;
@ -1813,6 +1814,38 @@ public class NetworkManagerImpl implements NetworkManager, VirtualMachineManager
return newFwRule;
}
@Override
public List<FirewallRuleVO> listPortForwardingRules(ListPortForwardingRulesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
String ipAddress = cmd.getIpAddress();
Account account = (Account)UserContext.current().getAccountObject();
IPAddressVO ipAddressVO = _ipAddressDao.findById(ipAddress);
if (ipAddressVO == null) {
throw new InvalidParameterValueException("Unable to find IP address " + ipAddress);
}
Account addrOwner = _accountDao.findById(ipAddressVO.getAccountId());
// if an admin account was passed in, or no account was passed in, make sure we honor the accountName/domainId parameters
if ((account != null) && isAdmin(account.getType())) {
if (ipAddressVO.getAccountId() != null) {
if ((addrOwner != null) && !_domainDao.isChildDomain(account.getDomainId(), addrOwner.getDomainId())) {
throw new PermissionDeniedException("Unable to list port forwarding rules for address " + ipAddress + ", permission denied for account " + account.getId());
}
} else {
throw new InvalidParameterValueException("Unable to list port forwarding rules for address " + ipAddress + ", address not in use.");
}
} else {
if (account != null) {
if ((ipAddressVO.getAccountId() == null) || (account.getId().longValue() != ipAddressVO.getAccountId().longValue())) {
throw new PermissionDeniedException("Unable to list port forwarding rules for address " + ipAddress + ", permission denied for account " + account.getId());
}
}
}
return _rulesDao.listIPForwarding(cmd.getIpAddress(), true);
}
@Override
public void assignToLoadBalancer(AssignToLoadBalancerRuleCmd cmd) throws NetworkRuleConflictException, InternalErrorException,
PermissionDeniedException, InvalidParameterValueException {

View File

@ -1121,14 +1121,6 @@ public interface ManagementServer {
* @return List of UserVMs.
*/
List<UserVmVO> searchForUserVMs(Criteria c);
/**
* Obtains a list of firewall rules by the specified IP address and forwarding flag.
* @param publicIPAddress
* @param forwarding
* @return
*/
List<FirewallRuleVO> listIPForwarding(String publicIPAddress, boolean forwarding);
/**
* Update an existing port forwarding rule on the given public IP / public port for the given protocol

View File

@ -4996,11 +4996,6 @@ public class ManagementServerImpl implements ManagementServer {
return _userVmDao.search(sc, searchFilter);
}
@Override
public List<FirewallRuleVO> listIPForwarding(String publicIPAddress, boolean forwarding) {
return _firewallRulesDao.listIPForwarding(publicIPAddress, forwarding);
}
@Override
public FirewallRuleVO updatePortForwardingRule(long userId, String publicIp, String privateIp, String publicPort, String privatePort, String protocol) {
List<FirewallRuleVO> fwRules = _firewallRulesDao.listIPForwardingForUpdate(publicIp, publicPort, protocol);

View File

@ -79,7 +79,6 @@ import com.cloud.async.AsyncJobManager;
import com.cloud.async.AsyncJobResult;
import com.cloud.async.AsyncJobVO;
import com.cloud.async.BaseAsyncJobExecutor;
import com.cloud.async.executor.DestroyVMExecutor;
import com.cloud.async.executor.OperationResponse;
import com.cloud.async.executor.RebootVMExecutor;
import com.cloud.async.executor.StartVMExecutor;
@ -87,8 +86,6 @@ import com.cloud.async.executor.StopVMExecutor;
import com.cloud.async.executor.VMExecutorHelper;
import com.cloud.async.executor.VMOperationListener;
import com.cloud.async.executor.VMOperationParam;
import com.cloud.async.executor.VolumeOperationParam;
import com.cloud.async.executor.VolumeOperationParam.VolumeOp;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.configuration.ConfigurationManager;
import com.cloud.configuration.ResourceCount.ResourceType;