From 8fb948650f53052c3622c04b540ea05bfb55f405 Mon Sep 17 00:00:00 2001 From: alena Date: Wed, 8 Dec 2010 17:06:13 -0800 Subject: [PATCH] Fixed createPortForwardingRule command. Modified createVlanIpRange command: added optional networkId parameter; only startIp parameter is required now. --- .../commands/CreatePortForwardingRuleCmd.java | 30 ++++----- .../api/commands/CreateVlanIpRangeCmd.java | 15 +++-- .../src/com/cloud/api/ApiResponseHelper.java | 28 +++++---- .../ConfigurationManagerImpl.java | 62 ++++++++++++++++++- .../com/cloud/network/NetworkManagerImpl.java | 1 + .../cloud/network/guru/GuestNetworkGuru.java | 4 +- 6 files changed, 102 insertions(+), 38 deletions(-) diff --git a/api/src/com/cloud/api/commands/CreatePortForwardingRuleCmd.java b/api/src/com/cloud/api/commands/CreatePortForwardingRuleCmd.java index 6cc1fd8e519..11f465ac4e3 100644 --- a/api/src/com/cloud/api/commands/CreatePortForwardingRuleCmd.java +++ b/api/src/com/cloud/api/commands/CreatePortForwardingRuleCmd.java @@ -97,28 +97,20 @@ public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements @Override public void execute() throws ResourceUnavailableException { + UserContext callerContext = UserContext.current(); + + boolean success = false; + PortForwardingRule rule = _entityMgr.findById(PortForwardingRule.class, getEntityId()); try { - UserContext callerContext = UserContext.current(); - - PortForwardingRule result = _rulesService.createPortForwardingRule(this, virtualMachineId); - if (result == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "An existing rule for ipAddress / port / protocol of " + ipAddress + " / " + publicPort + " / " + protocol + " exits."); + success = _rulesService.applyPortForwardingRules(rule.getSourceIpAddress(), callerContext.getAccount()); + } finally { + if (!success) { + _rulesService.revokePortForwardingRule(getEntityId(), true); } - boolean success = false; - try { - success = _rulesService.applyPortForwardingRules(result.getSourceIpAddress(), callerContext.getAccount()); - } finally { - if (!success) { - _rulesService.revokePortForwardingRule(result.getId(), true); - } - } - FirewallRuleResponse fwResponse = _responseGenerator.createFirewallRuleResponse(result); - fwResponse.setResponseName(getName()); - setResponseObject(fwResponse); - } catch (NetworkRuleConflictException ex) { - s_logger.warn("Exception: ", ex); - throw new ServerApiException(BaseCmd.NETWORK_RULE_CONFLICT_ERROR, ex.getMessage()); } + FirewallRuleResponse fwResponse = _responseGenerator.createFirewallRuleResponse(rule); + fwResponse.setResponseName(getName()); + setResponseObject(fwResponse); } @Override diff --git a/api/src/com/cloud/api/commands/CreateVlanIpRangeCmd.java b/api/src/com/cloud/api/commands/CreateVlanIpRangeCmd.java index ce4dffbc955..368f8a89282 100644 --- a/api/src/com/cloud/api/commands/CreateVlanIpRangeCmd.java +++ b/api/src/com/cloud/api/commands/CreateVlanIpRangeCmd.java @@ -52,10 +52,10 @@ public class CreateVlanIpRangeCmd extends BaseCmd { @Parameter(name=ApiConstants.FOR_VIRTUAL_NETWORK, type=CommandType.BOOLEAN, description="true if VLAN is of Virtual type, false if Direct") private Boolean forVirtualNetwork; - @Parameter(name=ApiConstants.GATEWAY, type=CommandType.STRING, required=true, description="the gateway of the VLAN IP range") + @Parameter(name=ApiConstants.GATEWAY, type=CommandType.STRING, description="the gateway of the VLAN IP range") private String gateway; - @Parameter(name=ApiConstants.NETMASK, type=CommandType.STRING, required=true, description="the netmask of the VLAN IP range") + @Parameter(name=ApiConstants.NETMASK, type=CommandType.STRING, description="the netmask of the VLAN IP range") private String netmask; @Parameter(name=ApiConstants.POD_ID, type=CommandType.LONG, description="optional parameter. Have to be specified for Direct Untagged vlan only.") @@ -67,8 +67,11 @@ public class CreateVlanIpRangeCmd extends BaseCmd { @Parameter(name=ApiConstants.VLAN, type=CommandType.STRING, description="the ID or VID of the VLAN. Default is an \"untagged\" VLAN.") private String vlan; - @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, required=true, description=" the Zone ID of the VLAN IP range") + @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, description="the Zone ID of the VLAN IP range") private Long zoneId; + + @Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.LONG, description="the network id") + private Long networkID; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -114,11 +117,15 @@ public class CreateVlanIpRangeCmd extends BaseCmd { return zoneId; } - + ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// + public Long getNetworkID() { + return networkID; + } + @Override public String getName() { return s_name; diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 9c65cbbe15b..61f0a10122d 100644 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -737,7 +737,6 @@ public class ApiResponseHelper implements ResponseGenerator { @Override public VlanIpRangeResponse createVlanIpRangeResponse(Vlan vlan) { - Long accountId = ApiDBUtils.getAccountIdForVlan(vlan.getId()); Long podId = ApiDBUtils.getPodIdForVlan(vlan.getId()); VlanIpRangeResponse vlanResponse = new VlanIpRangeResponse(); @@ -746,13 +745,6 @@ public class ApiResponseHelper implements ResponseGenerator { vlanResponse.setVlan(vlan.getVlanId()); vlanResponse.setZoneId(vlan.getDataCenterId()); - if (accountId != null) { - Account account = ApiDBUtils.findAccountById(accountId); - vlanResponse.setAccountName(account.getAccountName()); - vlanResponse.setDomainId(account.getDomainId()); - vlanResponse.setDomainName(ApiDBUtils.findDomainById(account.getDomainId()).getName()); - } - if (podId != null) { HostPodVO pod = ApiDBUtils.findPodById(podId); vlanResponse.setPodId(podId); @@ -767,10 +759,24 @@ public class ApiResponseHelper implements ResponseGenerator { String[] range = ipRange.split("-"); vlanResponse.setStartIp(range[0]); vlanResponse.setEndIp(range[1]); - - vlanResponse.setNetworkId(vlan.getNetworkId()); + + Long networkId = vlan.getNetworkId(); + if (networkId != null) { + vlanResponse.setNetworkId(vlan.getNetworkId()); + Network network = ApiDBUtils.findNetworkById(networkId); + if (network != null) { + Long accountId = network.getAccountId(); + //Set account information + if (accountId != null) { + Account account = ApiDBUtils.findAccountById(accountId); + vlanResponse.setAccountName(account.getAccountName()); + vlanResponse.setDomainId(account.getDomainId()); + vlanResponse.setDomainName(ApiDBUtils.findDomainById(account.getDomainId()).getName()); + } + } + } + vlanResponse.setObjectName("vlan"); - return vlanResponse; } diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index 5b7778536ba..b45b6600f5d 100755 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -16,6 +16,7 @@ */ package com.cloud.configuration; +import java.net.URI; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -89,6 +90,7 @@ import com.cloud.network.NetworkVO; import com.cloud.network.Networks.BroadcastDomainType; import com.cloud.network.Networks.TrafficType; import com.cloud.network.dao.IPAddressDao; +import com.cloud.network.dao.NetworkDao; import com.cloud.offering.DiskOffering; import com.cloud.offering.NetworkOffering; import com.cloud.offering.NetworkOffering.GuestIpType; @@ -151,6 +153,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura @Inject AccountDao _accountDao; @Inject EventDao _eventDao; @Inject UserDao _userDao; + @Inject NetworkDao _networkDao; @Inject ConsoleProxyDao _consoleDao; @Inject SecondaryStorageVmDao _secStorageDao; @Inject AccountManager _accountMgr; @@ -1483,6 +1486,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura Long userId = UserContext.current().getUserId(); String vlanId = cmd.getVlan(); Boolean forVirtualNetwork = cmd.isForVirtualNetwork(); + Long networkId = cmd.getNetworkID(); // If an account name and domain ID are specified, look up the account String accountName = cmd.getAccountName(); Long domainId = cmd.getDomainId(); @@ -1494,13 +1498,67 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura } } - return createVlanAndPublicIpRange(userId, zoneId, podId, startIP, endIP, vlanGateway, vlanNetmask, forVirtualNetwork, vlanId, account, null); + //if Vlan is direct, don't allow to specify networkId + if (forVirtualNetwork && networkId != null) { + throw new InvalidParameterValueException("Can't specify networkId for Virtual network"); + } + + if (forVirtualNetwork && (vlanGateway == null || vlanNetmask == null || zoneId == null)) { + throw new InvalidParameterValueException("Gateway, netmask and zoneId have to be passed in for virtual network"); + } + + //Verify that network is valid, and ip range matches network's cidr + if (networkId != null) { + NetworkVO network = _networkDao.findById(networkId); + if (network == null) { + throw new InvalidParameterValueException("Unable to find network by id " + networkId); + } else { + //Check that network is of type Direct + if (network.getGuestType() == GuestIpType.Virtual) { + throw new InvalidParameterValueException("Can't create direct vlan for network with GuestType " + network.getGuestType().toString()); + } + + //check if startIp and endIp belong to network Cidr + String networkCidr = network.getCidr(); + String networkGateway = network.getGateway(); + + Long networkZoneId = network.getDataCenterId(); + String[] splitResult = networkCidr.split("\\/"); + long size = Long.valueOf(splitResult[1]); + String networkNetmask = NetUtils.getCidrNetmask(size); + + //Check if ip addresses are in network range + if (!NetUtils.sameSubnet(startIP, networkGateway, networkNetmask)) { + throw new InvalidParameterValueException("Start ip is not in network cidr: " + networkCidr); + } + + if (endIP != null) { + if (!NetUtils.sameSubnet(endIP, networkGateway, networkNetmask)) { + throw new InvalidParameterValueException("End ip is not in network cidr: " + networkCidr); + } + } + + //set gateway, netmask, zone from network object + vlanGateway = networkGateway; + vlanNetmask = networkNetmask; + zoneId = networkZoneId; + + //set vlanId if it's not null for the network + URI uri = network.getBroadcastUri(); + if (uri != null) { + String[] vlan = uri.toString().split("vlan:\\/\\/"); + vlanId = vlan[1]; + } + } + } + + return createVlanAndPublicIpRange(userId, zoneId, podId, startIP, endIP, vlanGateway, vlanNetmask, forVirtualNetwork, vlanId, account, networkId); } @Override public Vlan createVlanAndPublicIpRange(Long userId, Long zoneId, Long podId, String startIP, String endIP, String vlanGateway, String vlanNetmask, boolean forVirtualNetwork, String vlanId, Account account, Long networkId) throws InsufficientCapacityException, ConcurrentOperationException, InvalidParameterValueException{ - + // Check that the pod ID is valid if (podId != null && ((_podDao.findById(podId)) == null)) { throw new InvalidParameterValueException("Please specify a valid pod."); diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 2b32f30b99d..967078c9991 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -1662,6 +1662,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag userNetwork.setGateway(gateway); if (vlanId != null) { userNetwork.setBroadcastUri(URI.create("vlan://" + vlanId)); + userNetwork.setBroadcastDomainType(BroadcastDomainType.Vlan); if (!vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) { userNetwork.setBroadcastDomainType(BroadcastDomainType.Vlan); } else { diff --git a/server/src/com/cloud/network/guru/GuestNetworkGuru.java b/server/src/com/cloud/network/guru/GuestNetworkGuru.java index ae8f0556e39..5a914922ef5 100644 --- a/server/src/com/cloud/network/guru/GuestNetworkGuru.java +++ b/server/src/com/cloud/network/guru/GuestNetworkGuru.java @@ -74,10 +74,10 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru { if (offering.getTrafficType() != TrafficType.Guest) { return null; } - - GuestIpType ipType = offering.getGuestIpType(); + BroadcastDomainType broadcastType = null; Mode mode = null; + GuestIpType ipType = offering.getGuestIpType(); if (ipType == GuestIpType.Virtual) { mode = Mode.Dhcp; broadcastType = BroadcastDomainType.Vlan;