From f82e73b861b4d0f32994b6a588d262c5a4c477f2 Mon Sep 17 00:00:00 2001 From: abhishek Date: Mon, 13 Sep 2010 09:30:09 -0700 Subject: [PATCH] bug 6159: incremental checkin --- .../cloud/server/ManagementServerImpl.java | 68 +++++++++++++++---- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 4ecb921ebc9..2472a85f020 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -3393,27 +3393,69 @@ public class ManagementServerImpl implements ManagementServer { // check for ip address/port conflicts by checking existing forwarding and load balancing rules List existingRulesOnPubIp = _firewallRulesDao.listIPForwarding(ipAddress); - Map> mappedPublicPorts = new HashMap>(); - + Map mappedPublicPorts = new HashMap(); + Map publicPortToProtocolMapping=new HashMap(); if (existingRulesOnPubIp != null) { for (FirewallRuleVO fwRule : existingRulesOnPubIp) { - mappedPublicPorts.put(fwRule.getPublicPort(), new Pair(fwRule.getPrivateIpAddress(), fwRule.getPrivatePort())); + + //mappedPublicPorts.put(fwRule.getPublicPort(), new Pair(fwRule.getPrivateIpAddress(), fwRule.getPrivatePort())); + if(mappedPublicPorts.containsKey(fwRule.getPublicPort())){ + mappedPublicPorts.put(fwRule.getPublicPort(), mappedPublicPorts.get(fwRule.getPublicPort()).append(";").append(fwRule.getPrivateIpAddress().concat(",").concat(fwRule.getPrivatePort()))); + } + else{ + mappedPublicPorts.put(fwRule.getPublicPort(), new StringBuilder(fwRule.getPrivateIpAddress()+","+fwRule.getPrivatePort())); + } + + if(publicPortToProtocolMapping.containsKey(fwRule.getPublicPort())){ + publicPortToProtocolMapping.put(fwRule.getPublicPort(), publicPortToProtocolMapping.get(fwRule.getPublicPort()).append(";").append(fwRule.getProtocol())); + } + else{ + publicPortToProtocolMapping.put(fwRule.getPublicPort(),new StringBuilder(fwRule.getProtocol())); + } } } - if (userVm != null) { - Pair privateIpPort = mappedPublicPorts.get(publicPort); - if (privateIpPort != null) { - if (privateIpPort.first().equals(userVm.getGuestIpAddress()) && privateIpPort.second().equals(privatePort)) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("skipping the creating of firewall rule " + ipAddress + ":" + publicPort + " to " + userVm.getGuestIpAddress() + ":" + privatePort + "; rule already exists."); - } - return null; // already mapped - } else { + if (userVm != null) + { + String privateIpPort = mappedPublicPorts.get(publicPort).toString();//eg: 10.1.1.2,30 ; 10.1.1.2,34 + if (privateIpPort != null && privateIpPort.length()>0) + { + String publicPortProtocol = publicPortToProtocolMapping.get(publicPort).toString(); + String[] privateIpPortPairs = privateIpPort.toString().split(";"); //eg. 10.1.1.2,30 + String[] privateIpAndPortStr; + boolean errFlag = false; + + for(String pair: privateIpPortPairs) + { + privateIpAndPortStr = pair.split(",");//split into 10.1.1.2 & 30 + + if (privateIpAndPortStr[0].equals(userVm.getGuestIpAddress()) && privateIpAndPortStr[1].equals(privatePort)) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("skipping the creating of firewall rule " + ipAddress + ":" + publicPort + " to " + userVm.getGuestIpAddress() + ":" + privatePort + "; rule already exists."); + } + return null; // already mapped + } + //at this point protocol string looks like: eg. tcp;udp || tcp || udp || udp;tcp + else if(!publicPortProtocol.contains(protocol))//check if this public port is mapped to the protocol or not + { + //this is the case eg: + //pub:1 pri:2 pro: tcp + //pub 1 pri:3 pro: udp + break; //we break here out of the loop, for the record to be created + } + else + { + errFlag = true; +// throw new NetworkRuleConflictException("An existing port forwarding service rule for " + ipAddress + ":" + publicPort +// + " already exists, found while trying to create mapping to " + userVm.getGuestIpAddress() + ":" + privatePort + ((securityGroupId == null) ? "." : " from port forwarding service " +// + securityGroupId.toString() + ".")); + } + } + + if(errFlag) throw new NetworkRuleConflictException("An existing port forwarding service rule for " + ipAddress + ":" + publicPort + " already exists, found while trying to create mapping to " + userVm.getGuestIpAddress() + ":" + privatePort + ((securityGroupId == null) ? "." : " from port forwarding service " + securityGroupId.toString() + ".")); - } } FirewallRuleVO newFwRule = new FirewallRuleVO();