Updated Branches:
  refs/heads/portablepublicip bd058f58c -> 51509751b

seperate out associate/disassociate with guest network operations from
alloc and release of portable ip


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/51509751
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/51509751
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/51509751

Branch: refs/heads/portablepublicip
Commit: 51509751b290c0e51cbdd104a9aebff189cbe806
Parents: bd058f5
Author: Murali Reddy <[email protected]>
Authored: Mon May 13 12:05:33 2013 +0530
Committer: Murali Reddy <[email protected]>
Committed: Mon May 13 12:05:33 2013 +0530

----------------------------------------------------------------------
 api/src/com/cloud/event/EventTypes.java            |    2 +-
 api/src/com/cloud/network/NetworkService.java      |    4 +-
 .../user/address/DisassociateIPAddrCmd.java        |   12 ++-
 server/src/com/cloud/network/NetworkManager.java   |    5 +
 .../src/com/cloud/network/NetworkManagerImpl.java  |  100 ++++++++++++++-
 .../src/com/cloud/network/NetworkServiceImpl.java  |   12 ++-
 .../com/cloud/network/MockNetworkManagerImpl.java  |   15 ++
 .../test/com/cloud/vpc/MockNetworkManagerImpl.java |   18 ++-
 8 files changed, 155 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/51509751/api/src/com/cloud/event/EventTypes.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/event/EventTypes.java 
b/api/src/com/cloud/event/EventTypes.java
index 9034541..8561ecc 100755
--- a/api/src/com/cloud/event/EventTypes.java
+++ b/api/src/com/cloud/event/EventTypes.java
@@ -101,7 +101,7 @@ public class EventTypes {
     public static final String EVENT_NET_IP_ASSIGN = "NET.IPASSIGN";
     public static final String EVENT_NET_IP_RELEASE = "NET.IPRELEASE";
     public static final String EVENT_PORTABLE_IP_ASSIGN = "PORTABLE.IPASSIGN";
-    public static final String EVENT_PORTABLE_RELEASE = "PORTABLEIPRELEASE";
+    public static final String EVENT_PORTABLE_IP_RELEASE = "PORTABLEIPRELEASE";
     public static final String EVENT_NET_RULE_ADD = "NET.RULEADD";
     public static final String EVENT_NET_RULE_DELETE = "NET.RULEDELETE";
     public static final String EVENT_NET_RULE_MODIFY = "NET.RULEMODIFY";

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/51509751/api/src/com/cloud/network/NetworkService.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/network/NetworkService.java 
b/api/src/com/cloud/network/NetworkService.java
index 8f23561..078aa04 100755
--- a/api/src/com/cloud/network/NetworkService.java
+++ b/api/src/com/cloud/network/NetworkService.java
@@ -52,10 +52,12 @@ public interface NetworkService {
     IpAddress allocateIP(Account ipOwner, long zoneId, Long networkId) throws 
ResourceAllocationException,
         InsufficientAddressCapacityException, ConcurrentOperationException;
 
+    boolean releaseIpAddress(long ipAddressId) throws 
InsufficientAddressCapacityException;
+
     IpAddress allocatePortableIP(Account ipOwner, int regionId, Long zoneId, 
Long networkId, Long vpcId) throws ResourceAllocationException,
             InsufficientAddressCapacityException, ConcurrentOperationException;
 
-    boolean releaseIpAddress(long ipAddressId) throws 
InsufficientAddressCapacityException;
+    boolean releasePortableIpAddress(long ipAddressId) throws 
InsufficientAddressCapacityException;
 
     Network createGuestNetwork(CreateNetworkCmd cmd) throws 
InsufficientCapacityException, ConcurrentOperationException,
     ResourceAllocationException;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/51509751/api/src/org/apache/cloudstack/api/command/user/address/DisassociateIPAddrCmd.java
----------------------------------------------------------------------
diff --git 
a/api/src/org/apache/cloudstack/api/command/user/address/DisassociateIPAddrCmd.java
 
b/api/src/org/apache/cloudstack/api/command/user/address/DisassociateIPAddrCmd.java
index 8271119..8f78fe3 100644
--- 
a/api/src/org/apache/cloudstack/api/command/user/address/DisassociateIPAddrCmd.java
+++ 
b/api/src/org/apache/cloudstack/api/command/user/address/DisassociateIPAddrCmd.java
@@ -74,7 +74,12 @@ public class DisassociateIPAddrCmd extends BaseAsyncCmd {
     @Override
     public void execute() throws InsufficientAddressCapacityException{
         UserContext.current().setEventDetails("Ip Id: " + getIpAddressId());
-        boolean result = _networkService.releaseIpAddress(getIpAddressId());
+        boolean result = false;
+        if (!isPortable(id)) {
+            _networkService.releaseIpAddress(getIpAddressId());
+        } else {
+            _networkService.releaseIpAddress(getIpAddressId());
+        }
         if (result) {
             SuccessResponse response = new SuccessResponse(getCommandName());
             this.setResponseObject(response);
@@ -139,4 +144,9 @@ public class DisassociateIPAddrCmd extends BaseAsyncCmd {
     public Long getInstanceId() {
         return getIpAddressId();
     }
+
+    private boolean isPortable(long id) {
+        IpAddress ip = getIpAddress(id);
+        return  ip.isPortable();
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/51509751/server/src/com/cloud/network/NetworkManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkManager.java 
b/server/src/com/cloud/network/NetworkManager.java
index 15bb450..2bb40c8 100755
--- a/server/src/com/cloud/network/NetworkManager.java
+++ b/server/src/com/cloud/network/NetworkManager.java
@@ -239,6 +239,11 @@ public interface NetworkManager  {
     IPAddressVO associateIPToGuestNetwork(long ipAddrId, long networkId, 
boolean releaseOnFailure) throws ResourceAllocationException, 
ResourceUnavailableException, 
         InsufficientAddressCapacityException, ConcurrentOperationException;
 
+    IPAddressVO associatePortableIPToGuestNetwork(long ipAddrId, long 
networkId, boolean releaseOnFailure) throws ResourceAllocationException, 
ResourceUnavailableException,
+            InsufficientAddressCapacityException, ConcurrentOperationException;
+
+    IPAddressVO disassociatePortableIPToGuestNetwork(long ipAddrId, long 
networkId) throws ResourceAllocationException, ResourceUnavailableException,
+            InsufficientAddressCapacityException, ConcurrentOperationException;
 
     /**
      * @param network

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/51509751/server/src/com/cloud/network/NetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java 
b/server/src/com/cloud/network/NetworkManagerImpl.java
index 03c606b..cac6bf2 100755
--- a/server/src/com/cloud/network/NetworkManagerImpl.java
+++ b/server/src/com/cloud/network/NetworkManagerImpl.java
@@ -797,12 +797,14 @@ public class NetworkManagerImpl extends ManagerBase 
implements NetworkManager, L
             }
 
             DataCenter zone = _configMgr.getZone(network.getDataCenterId());
-            if (network.getGuestType() == Network.GuestType.Shared && 
zone.getNetworkType() == NetworkType.Advanced) {
-                if 
(isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
-                    _accountMgr.checkAccess(UserContext.current().getCaller(), 
AccessType.UseNetwork, false, network);
-                } else {
-                    throw new InvalidParameterValueException("IP can be 
associated with guest network of 'shared' type only if " +
-                        "network services Source Nat, Static Nat, Port 
Forwarding, Load balancing, firewall are enabled in the network");
+            if (zone.getNetworkType() == NetworkType.Advanced) {
+                if (network.getGuestType() == Network.GuestType.Shared) {
+                    if 
(isSharedNetworkOfferingWithServices(network.getNetworkOfferingId())) {
+                        
_accountMgr.checkAccess(UserContext.current().getCaller(), 
AccessType.UseNetwork, false, network);
+                    } else {
+                        throw new InvalidParameterValueException("IP can be 
associated with guest network of 'shared' type only if " +
+                                "network services Source Nat, Static Nat, Port 
Forwarding, Load balancing, firewall are enabled in the network");
+                    }
                 }
             } else {
                 _accountMgr.checkAccess(caller, null, true, ipToAssoc);
@@ -900,6 +902,92 @@ public class NetworkManagerImpl extends ManagerBase 
implements NetworkManager, L
         }
     }
 
+    @Override
+    public IPAddressVO associatePortableIPToGuestNetwork(long ipAddrId, long 
networkId, boolean releaseOnFailure) throws ResourceAllocationException, 
ResourceUnavailableException,
+            InsufficientAddressCapacityException, ConcurrentOperationException 
{
+        return associateIPToGuestNetwork(ipAddrId, networkId, 
releaseOnFailure);
+    }
+
+    @DB
+    @Override
+    public IPAddressVO disassociatePortableIPToGuestNetwork(long ipId, long 
networkId)
+            throws ResourceAllocationException, ResourceUnavailableException,
+            InsufficientAddressCapacityException, ConcurrentOperationException 
{
+
+        Account caller = UserContext.current().getCaller();
+        Account owner = null;
+
+        Network network = _networksDao.findById(networkId);
+        if (network == null) {
+            throw new InvalidParameterValueException("Invalid network id is 
given");
+        }
+
+        IPAddressVO ipToAssoc = _ipAddressDao.findById(ipId);
+        if (ipToAssoc != null) {
+
+            if (ipToAssoc.getAssociatedWithNetworkId() == null) {
+                throw new InvalidParameterValueException("IP " + ipToAssoc + " 
is not assocaited with any network");
+            }
+
+            if (ipToAssoc.getAssociatedWithNetworkId() != network.getId()) {
+                throw new InvalidParameterValueException("IP " + ipToAssoc + " 
is not assocaited with network id" + networkId);
+            }
+
+            DataCenter zone = _configMgr.getZone(network.getDataCenterId());
+            if (zone.getNetworkType() == NetworkType.Advanced) {
+                if (network.getGuestType() == Network.GuestType.Shared) {
+                    assert 
(isSharedNetworkOfferingWithServices(network.getNetworkOfferingId()));
+                    _accountMgr.checkAccess(UserContext.current().getCaller(), 
AccessType.UseNetwork, false, network);
+                }
+            } else {
+                _accountMgr.checkAccess(caller, null, true, ipToAssoc);
+            }
+            owner = 
_accountMgr.getAccount(ipToAssoc.getAllocatedToAccountId());
+        } else {
+            s_logger.debug("Unable to find ip address by id: " + ipId);
+            return null;
+        }
+
+        DataCenter zone = _configMgr.getZone(network.getDataCenterId());
+
+        // Check that network belongs to IP owner - skip this check
+        //     - if zone is basic zone as there is just one guest network,
+        //     - if shared network in Advanced zone
+        //     - and it belongs to the system
+        if (network.getAccountId() != owner.getId()) {
+            if (zone.getNetworkType() != NetworkType.Basic && 
!(zone.getNetworkType() == NetworkType.Advanced && network.getGuestType() == 
Network.GuestType.Shared)) {
+                throw new InvalidParameterValueException("The owner of the 
network is not the same as owner of the IP");
+            }
+        }
+
+        // Check if IP has any services (rules) associated in the network
+        List<PublicIpAddress> ipList = new ArrayList<PublicIpAddress>();
+        PublicIp publicIp = PublicIp.createFromAddrAndVlan(ipToAssoc, 
_vlanDao.findById(ipToAssoc.getVlanId()));
+        ipList.add(publicIp);
+        Map<PublicIpAddress, Set<Service>> ipToServices = 
_networkModel.getIpToServices(ipList, false, true);
+        if (ipToServices != null & !ipToServices.isEmpty()) {
+            Set<Service> services = ipToServices.get(publicIp);
+            if (services != null && !services.isEmpty()) {
+                throw new InvalidParameterValueException("IP " + ipToAssoc + " 
has services and rules associated in the network " +  networkId);
+            }
+        }
+
+        IPAddressVO ip = _ipAddressDao.findById(ipId);
+        ip.setAssociatedWithNetworkId(null);
+        _ipAddressDao.update(ipId, ip);
+
+        try {
+            boolean success = applyIpAssociations(network, false);
+            if (success) {
+                s_logger.debug("Successfully associated ip address " + 
ip.getAddress().addr() + " to network " + network);
+            } else {
+                s_logger.warn("Failed to associate ip address " + 
ip.getAddress().addr() + " to network " + network);
+            }
+            return ip;
+        } finally {
+
+        }
+    }
 
     @Override
     @DB

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/51509751/server/src/com/cloud/network/NetworkServiceImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/NetworkServiceImpl.java 
b/server/src/com/cloud/network/NetworkServiceImpl.java
index e67026c..876ea8f 100755
--- a/server/src/com/cloud/network/NetworkServiceImpl.java
+++ b/server/src/com/cloud/network/NetworkServiceImpl.java
@@ -571,6 +571,12 @@ public class NetworkServiceImpl extends ManagerBase 
implements  NetworkService {
     }
 
     @Override
+    @ActionEvent(eventType = EventTypes.EVENT_PORTABLE_IP_RELEASE, 
eventDescription = "disassociating portable Ip", async = true)
+    public boolean releasePortableIpAddress(long ipAddressId) throws 
InsufficientAddressCapacityException {
+        return releaseIpAddressInternal(ipAddressId);
+    }
+
+    @Override
     @DB
     public boolean configure(final String name, final Map<String, Object> 
params) throws ConfigurationException {
         _configs = _configDao.getConfiguration("Network", params);
@@ -805,9 +811,13 @@ public class NetworkServiceImpl extends ManagerBase 
implements  NetworkService {
     }
 
     @Override
-    @DB
     @ActionEvent(eventType = EventTypes.EVENT_NET_IP_RELEASE, eventDescription 
= "disassociating Ip", async = true)
     public boolean releaseIpAddress(long ipAddressId) throws 
InsufficientAddressCapacityException {
+        return releaseIpAddressInternal(ipAddressId);
+    }
+
+    @DB
+    private boolean releaseIpAddressInternal(long ipAddressId) throws 
InsufficientAddressCapacityException {
         Long userId = UserContext.current().getCallerUserId();
         Account caller = UserContext.current().getCaller();
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/51509751/server/test/com/cloud/network/MockNetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/network/MockNetworkManagerImpl.java 
b/server/test/com/cloud/network/MockNetworkManagerImpl.java
index f2d1fd4..c521058 100755
--- a/server/test/com/cloud/network/MockNetworkManagerImpl.java
+++ b/server/test/com/cloud/network/MockNetworkManagerImpl.java
@@ -88,6 +88,16 @@ public class MockNetworkManagerImpl extends ManagerBase 
implements NetworkManage
     }
 
     @Override
+    public IPAddressVO associatePortableIPToGuestNetwork(long ipAddrId, long 
networkId, boolean releaseOnFailure) throws ResourceAllocationException, 
ResourceUnavailableException {
+        return null;// TODO Auto-generated method stub
+    }
+
+    @Override
+    public IPAddressVO disassociatePortableIPToGuestNetwork(long ipAddrId, 
long networkId) throws ResourceAllocationException, 
ResourceUnavailableException, InsufficientAddressCapacityException, 
ConcurrentOperationException {
+        return null;  // TODO Auto-generated method stub
+    }
+
+    @Override
     public boolean releaseIpAddress(long ipAddressId) {
         // TODO Auto-generated method stub
         return false;
@@ -857,6 +867,11 @@ public class MockNetworkManagerImpl extends ManagerBase 
implements NetworkManage
     }
 
     @Override
+    public boolean releasePortableIpAddress(long ipAddressId) throws 
InsufficientAddressCapacityException {
+        return false;// TODO Auto-generated method stub
+    }
+
+    @Override
     public boolean isSecondaryIpSetForNic(long nicId) {
         // TODO Auto-generated method stub
         return false;

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/51509751/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java 
b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
index e6e9257..c770000 100644
--- a/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
+++ b/server/test/com/cloud/vpc/MockNetworkManagerImpl.java
@@ -208,6 +208,11 @@ public class MockNetworkManagerImpl extends ManagerBase 
implements NetworkManage
         return null;
     }
 
+    @Override
+    public boolean releasePortableIpAddress(long ipAddressId) throws 
InsufficientAddressCapacityException {
+        return false;// TODO Auto-generated method stub
+    }
+
     /* (non-Javadoc)
     * @see com.cloud.network.NetworkService#releaseIpAddress(long)
     */
@@ -1117,13 +1122,20 @@ public class MockNetworkManagerImpl extends ManagerBase 
implements NetworkManage
         return null;
     }
 
+    @Override
+    public IPAddressVO associatePortableIPToGuestNetwork(long ipAddrId, long 
networkId, boolean releaseOnFailure) throws ResourceAllocationException, 
ResourceUnavailableException {
+        return null;// TODO Auto-generated method stub
+    }
 
-
+    @Override
+    public IPAddressVO disassociatePortableIPToGuestNetwork(long ipAddrId, 
long networkId) throws ResourceAllocationException, 
ResourceUnavailableException, InsufficientAddressCapacityException, 
ConcurrentOperationException {
+        return null;// TODO Auto-generated method stub
+    }
 
 
     /* (non-Javadoc)
-     * @see 
com.cloud.network.NetworkManager#setupDns(com.cloud.network.Network, 
com.cloud.network.Network.Provider)
-     */
+    * @see 
com.cloud.network.NetworkManager#setupDns(com.cloud.network.Network, 
com.cloud.network.Network.Provider)
+    */
     @Override
     public boolean setupDns(Network network, Provider provider) {
         // TODO Auto-generated method stub

Reply via email to