cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alena1...@apache.org
Subject [12/44] git commit: VPC: moved vpc related code from NetworkManager to VpcManager
Date Thu, 02 Aug 2012 02:08:32 GMT
VPC: moved vpc related code from NetworkManager to VpcManager

Conflicts:

	api/src/com/cloud/api/commands/AssociateIPAddrCmd.java
	server/src/com/cloud/network/NetworkManagerImpl.java
	server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
	server/src/com/cloud/network/rules/RulesManagerImpl.java
	server/src/com/cloud/network/vpc/VpcManagerImpl.java
	server/test/com/cloud/network/MockNetworkManagerImpl.java


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

Branch: refs/heads/vpc
Commit: 7cceaae912224ac616f8db20f39cb2cd13345632
Parents: ebe53a9
Author: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Authored: Tue Jul 31 16:40:06 2012 -0700
Committer: Alena Prokharchyk <alena.prokharchyk@citrix.com>
Committed: Wed Aug 1 18:45:06 2012 -0700

----------------------------------------------------------------------
 .../com/cloud/api/commands/AssociateIPAddrCmd.java |    8 +-
 .../com/cloud/api/commands/UpdateNetworkCmd.java   |   20 +-
 api/src/com/cloud/network/NetworkService.java      |    5 +-
 api/src/com/cloud/network/vpc/VpcService.java      |   19 +
 server/src/com/cloud/network/NetworkManager.java   |   36 +-
 .../src/com/cloud/network/NetworkManagerImpl.java  |  296 ++-------------
 .../network/firewall/FirewallManagerImpl.java      |    5 +-
 .../network/lb/LoadBalancingRulesManagerImpl.java  |    8 +-
 .../VpcVirtualNetworkApplianceManagerImpl.java     |    8 +-
 .../com/cloud/network/rules/RulesManagerImpl.java  |   11 +-
 server/src/com/cloud/network/vpc/VpcManager.java   |   64 +++-
 .../src/com/cloud/network/vpc/VpcManagerImpl.java  |  225 +++++++++++-
 .../com/cloud/network/MockNetworkManagerImpl.java  |   75 ++---
 13 files changed, 425 insertions(+), 355 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java b/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java
index 7fab4c1..7aaa5b5 100644
--- a/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java
+++ b/api/src/com/cloud/api/commands/AssociateIPAddrCmd.java
@@ -238,9 +238,13 @@ public class AssociateIPAddrCmd extends BaseAsyncCreateCmd {
         UserContext.current().setEventDetails("Ip Id: " + getEntityId());
 
         IpAddress result = null;
-        
-        result = _networkService.associateIP(getEntityId(), getNetworkId(), getVpcId());        
 
+        if (getVpcId() != null) {
+            result = _vpcService.associateIPToVpc(getEntityId(), getVpcId());
+        } else if (getNetworkId() != null) {
+            result = _networkService.associateIPToNetwork(getEntityId(), getNetworkId()); 
+        }
+        
         if (result != null) {
             IPAddressResponse ipResponse = _responseGenerator.createIPAddressResponse(result);
             ipResponse.setResponseName(getCommandName());

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/api/src/com/cloud/api/commands/UpdateNetworkCmd.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/api/commands/UpdateNetworkCmd.java b/api/src/com/cloud/api/commands/UpdateNetworkCmd.java
index 927b37b..c3657fc 100644
--- a/api/src/com/cloud/api/commands/UpdateNetworkCmd.java
+++ b/api/src/com/cloud/api/commands/UpdateNetworkCmd.java
@@ -116,14 +116,26 @@ public class UpdateNetworkCmd extends BaseAsyncCmd {
     @Override
     public void execute() throws InsufficientCapacityException, ConcurrentOperationException{
         User callerUser = _accountService.getActiveUser(UserContext.current().getCallerUserId());
-        Account callerAccount = _accountService.getActiveAccountById(callerUser.getAccountId());      
-        Network result = _networkService.updateGuestNetwork(getId(), getNetworkName(), getDisplayText(), callerAccount,
-                callerUser, getNetworkDomain(), getNetworkOfferingId(), getChangeCidr());
+        Account callerAccount = _accountService.getActiveAccountById(callerUser.getAccountId());
+        Network network = _networkService.getNetwork(id);
+        if (network == null) {
+            throw new InvalidParameterValueException("Couldn't find network by id", null);
+        }
+        
+        Network result = null;
+        if (network.getVpcId() != null) {
+            result = _vpcService.updateVpcGuestNetwork(getId(), getNetworkName(), getDisplayText(), callerAccount,
+                    callerUser, getNetworkDomain(), getNetworkOfferingId(), getChangeCidr());
+        } else {
+            result = _networkService.updateGuestNetwork(getId(), getNetworkName(), getDisplayText(), callerAccount,
+                    callerUser, getNetworkDomain(), getNetworkOfferingId(), getChangeCidr());
+        }
+        
         if (result != null) {
             NetworkResponse response = _responseGenerator.createNetworkResponse(result);
             response.setResponseName(getCommandName());
             this.setResponseObject(response);
-        }else {
+        } else {
             throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update network");
         }
     }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/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 6a60ebf..6bbc36c 100755
--- a/api/src/com/cloud/network/NetworkService.java
+++ b/api/src/com/cloud/network/NetworkService.java
@@ -138,16 +138,15 @@ public interface NetworkService {
     boolean isVmPartOfNetwork(long vmId, long ntwkId);
 
     /**
-     * @param entityId
      * @param networkId
-     * @param vpcId
+     * @param entityId
      * @return
      * @throws ConcurrentOperationException 
      * @throws ResourceUnavailableException 
      * @throws ResourceAllocationException 
      * @throws InsufficientAddressCapacityException 
      */
-    IpAddress associateIP(long ipId, Long networkId, Long vpcId) throws InsufficientAddressCapacityException,
+    IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAddressCapacityException,
         ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException;
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/api/src/com/cloud/network/vpc/VpcService.java
----------------------------------------------------------------------
diff --git a/api/src/com/cloud/network/vpc/VpcService.java b/api/src/com/cloud/network/vpc/VpcService.java
index 2a33822..e305c38 100644
--- a/api/src/com/cloud/network/vpc/VpcService.java
+++ b/api/src/com/cloud/network/vpc/VpcService.java
@@ -23,13 +23,17 @@ import java.util.Set;
 import com.cloud.api.commands.ListPrivateGatewaysCmd;
 import com.cloud.api.commands.ListStaticRoutesCmd;
 import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientAddressCapacityException;
 import com.cloud.exception.InsufficientCapacityException;
 import com.cloud.exception.NetworkRuleConflictException;
 import com.cloud.exception.ResourceAllocationException;
 import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.network.IpAddress;
 import com.cloud.network.Network;
 import com.cloud.network.Network.Provider;
 import com.cloud.network.Network.Service;
+import com.cloud.user.Account;
+import com.cloud.user.User;
 
 public interface VpcService {
     
@@ -221,4 +225,19 @@ public interface VpcService {
      * @return
      */
     VpcGateway getVpcGateway(long id);
+    
+    /**
+     * @param ipId
+     * @param vpcId
+     * @return
+     * @throws ResourceAllocationException
+     * @throws ResourceUnavailableException
+     * @throws InsufficientAddressCapacityException
+     * @throws ConcurrentOperationException
+     */
+    IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationException, ResourceUnavailableException,
+        InsufficientAddressCapacityException, ConcurrentOperationException;
+    
+    public Network updateVpcGuestNetwork(long networkId, String name, String displayText, Account callerAccount, 
+            User callerUser, String domainSuffix, Long ntwkOffId, Boolean changeCidr);
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/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 8c53f96..7f06d91 100755
--- a/server/src/com/cloud/network/NetworkManager.java
+++ b/server/src/com/cloud/network/NetworkManager.java
@@ -47,7 +47,6 @@ import com.cloud.network.element.UserDataServiceProvider;
 import com.cloud.network.guru.NetworkGuru;
 import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.StaticNat;
-import com.cloud.network.vpc.Vpc;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.offerings.NetworkOfferingVO;
 import com.cloud.user.Account;
@@ -274,8 +273,6 @@ public interface NetworkManager extends NetworkService {
 
     public boolean checkIpForService(IPAddressVO ip, Service service, Long networkId);
 
-    void checkVirtualNetworkCidrOverlap(Long zoneId, String cidr);
-
     void checkCapabilityForProvider(Set<Provider> providers, Service service,
             Capability cap, String capValue);
 
@@ -320,16 +317,6 @@ public interface NetworkManager extends NetworkService {
 
 
     /**
-     * @param owner
-     * @param vpc
-     * @return
-     * @throws ConcurrentOperationException 
-     * @throws InsufficientAddressCapacityException 
-     */
-    PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException;
-
-
-    /**
      * @param accountId
      * @param zoneId
      * @return
@@ -410,13 +397,6 @@ public interface NetworkManager extends NetworkService {
 
 
     /**
-     * @param ipId
-     * @param networkId TODO
-     */
-    void unassignIPFromVpcNetwork(long ipId, long networkId);
-
-
-    /**
      * @param vm
      * @param networkId
      * @param broadcastUri TODO
@@ -479,9 +459,21 @@ public interface NetworkManager extends NetworkService {
 
 
     /**
-     * @param ip
+     * @param addr
+     */
+    void markPublicIpAsAllocated(IPAddressVO addr);
+
+
+    /**
+     * @param owner
+     * @param guestNtwkId
+     * @param vpcId
+     * @param dcId
+     * @param isSourceNat
      * @return
+     * @throws ConcurrentOperationException
+     * @throws InsufficientAddressCapacityException
      */
-    boolean ipUsedInVpc(IpAddress ip);
+    PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId, boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException;
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/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 3f088e6..6d243ed 100755
--- a/server/src/com/cloud/network/NetworkManagerImpl.java
+++ b/server/src/com/cloud/network/NetworkManagerImpl.java
@@ -160,7 +160,6 @@ import com.cloud.network.rules.StaticNatRuleImpl;
 import com.cloud.network.rules.dao.PortForwardingRulesDao;
 import com.cloud.network.vpc.NetworkACLManager;
 import com.cloud.network.vpc.PrivateIpVO;
-import com.cloud.network.vpc.Vpc;
 import com.cloud.network.vpc.VpcManager;
 import com.cloud.network.vpc.Dao.PrivateIpDao;
 import com.cloud.network.vpn.RemoteAccessVpnService;
@@ -472,7 +471,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
     }
 
     @DB
-    protected void markPublicIpAsAllocated(IPAddressVO addr) {
+    @Override
+    public void markPublicIpAsAllocated(IPAddressVO addr) {
 
         assert (addr.getState() == IpAddress.State.Allocating || addr.getState() == IpAddress.State.Free) :
             "Unable to transition from state " + addr.getState() + " to " + IpAddress.State.Allocated;
@@ -503,25 +503,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
 
         txn.commit();
     }
-    
-    
-    @Override
-    public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException {
-        long dcId = vpc.getZoneId();
-        
-        IPAddressVO sourceNatIp = getExistingSourceNat(owner.getId(), null, vpc.getId());
-        
-        PublicIp ipToReturn = null;
-        
-        if (sourceNatIp != null) {
-            ipToReturn = new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()), 
-                    NetUtils.createSequenceBasedMacAddress(sourceNatIp.getMacAddress()));
-        } else {
-            ipToReturn = assignDedicateIpAddress(owner, null, vpc.getId(), dcId, true);
-        }
-        
-        return ipToReturn;
-    } 
+
     
     @Override
     public PublicIp assignSourceNatIpAddressToGuestNetwork(Account owner, Network guestNetwork) 
@@ -529,9 +511,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
         assert (guestNetwork.getTrafficType() != null) : "You're asking for a source nat but your network " +
         		"can't participate in source nat.  What do you have to say for yourself?";
         long dcId = guestNetwork.getDataCenterId();
-        
-        IPAddressVO sourceNatIp = getExistingSourceNat(owner.getId(), guestNetwork.getId(), guestNetwork.getVpcId());
-                
+
+        IPAddressVO sourceNatIp = getExistingSourceNatInNetwork(owner.getId(), guestNetwork.getId());
+
         PublicIp ipToReturn = null;
         if (sourceNatIp != null) {
             ipToReturn = new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()), 
@@ -550,6 +532,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
     
 
     @DB
+    @Override
     public PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId, boolean isSourceNat) 
             throws ConcurrentOperationException, InsufficientAddressCapacityException {
 
@@ -1118,15 +1101,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
         return ip;
     }
 
-    protected IPAddressVO getExistingSourceNat(long ownerId, Long networkId, Long vpcId) {
-        
-        List<IPAddressVO> addrs = null;
-        if (vpcId != null) {
-            addrs = listPublicIpsAssignedToVpc(ownerId, true, vpcId);
-        } else if (networkId != null) {
-            addrs = listPublicIpsAssignedToGuestNtwk(ownerId, networkId, true);
-        }
-        
+    
+    protected IPAddressVO getExistingSourceNatInNetwork(long ownerId, Long networkId) {
+
+        List<IPAddressVO> addrs = listPublicIpsAssignedToGuestNtwk(ownerId, networkId, true);
+
         IPAddressVO sourceNatIp = null;
         if (addrs.isEmpty()) {
             return null;
@@ -1140,7 +1119,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
             }
 
             assert (sourceNatIp != null) : "How do we get a bunch of ip addresses but none of them are source nat? " +
-                    "account=" + ownerId + "; networkId=" + networkId + "; vpcId=" + vpcId;
+                    "account=" + ownerId + "; networkId=" + networkId;
         } 
         
         return sourceNatIp;
@@ -1203,7 +1182,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
         boolean sharedSourceNat = offering.getSharedSourceNat();
         boolean isSourceNat = false;
         if (!sharedSourceNat) {
-            if (getExistingSourceNat(owner.getId(), networkId, null) == null) {
+            if (getExistingSourceNatInNetwork(owner.getId(), networkId) == null) {
                 if (network.getGuestType() == GuestType.Isolated && network.getVpcId() == null) {
                     isSourceNat = true;
                 }
@@ -1246,54 +1225,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
         }
     }
     
-    
-    @DB
-    protected IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationException, ResourceUnavailableException, 
-    InsufficientAddressCapacityException, ConcurrentOperationException {
-        Account caller = UserContext.current().getCaller();
-        Account owner = null;
-
-        IpAddress ipToAssoc = getIp(ipId);
-        if (ipToAssoc != null) {
-            _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;
-        }
-        
-        Vpc vpc = _vpcMgr.getVpc(vpcId);
-        if (vpc == null) {
-            throw new InvalidParameterValueException("Invalid VPC id " + vpcId);
-        }
-        
-        // check permissions
-        _accountMgr.checkAccess(caller, null, true, owner, vpc);
-                
-        boolean isSourceNat = false;
-        if (getExistingSourceNat(owner.getId(), null, vpcId) == null) {
-            isSourceNat = true;
-        }
-        
-        s_logger.debug("Associating ip " + ipToAssoc + " to vpc " + vpc);
-        
-        Transaction txn = Transaction.currentTxn();
-        txn.start();
-        IPAddressVO ip = _ipAddressDao.findById(ipId);
-        //update ip address with networkId
-        ip.setVpcId(vpcId);
-        ip.setSourceNat(isSourceNat);
-        _ipAddressDao.update(ipId, ip);
-        
-        //mark ip as allocated
-        markPublicIpAsAllocated(ip);
-        txn.commit();
-                
-        s_logger.debug("Successfully assigned ip " + ipToAssoc + " to vpc " + vpc);
-
-        return _ipAddressDao.findById(ipId);
-    }
-    
 
     @Override
     @DB
@@ -1658,19 +1589,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
 
         return _ipAddressDao.search(sc, null);
     }
-    
-    protected List<IPAddressVO> listPublicIpsAssignedToVpc(long accountId, Boolean sourceNat, long vpcId) {
-        SearchCriteria<IPAddressVO> sc = IpAddressSearch.create();
-        sc.setParameters("accountId", accountId);
-        sc.setParameters("vpcId", vpcId);
-
-        if (sourceNat != null) {
-            sc.addAnd("sourceNat", SearchCriteria.Op.EQ, sourceNat);
-        }
-        sc.setJoinParameters("virtualNetworkVlanSB", "vlanType", VlanType.VirtualNetwork);
 
-        return _ipAddressDao.search(sc, null);
-    }
 
     @Override
     public List<NetworkVO> setupNetwork(Account owner, NetworkOfferingVO offering, DeploymentPlan plan, String name, 
@@ -2082,24 +2001,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
                 && !sharedSourceNat) {
             
             List<IPAddressVO> ips = null;
-            Vpc vpc = null;
             if (network.getVpcId() != null) {
-                vpc = _vpcMgr.getVpc(network.getVpcId());
-                ips = _ipAddressDao.listByAssociatedVpc(vpc.getId(), true);
+                ips = _ipAddressDao.listByAssociatedVpc(network.getVpcId(), true);
+                if (ips.isEmpty()) {
+                    throw new CloudRuntimeException("Vpc is not implemented; there is no source nat ip");
+                }
             } else {
                 ips = _ipAddressDao.listByAssociatedNetwork(network.getId(), true);
             }
-            
 
             if (ips.isEmpty()) {
-                String target = vpc != null ? vpc.toString() : network.toString();
-                s_logger.debug("Creating a source nat ip for " + target);
+                s_logger.debug("Creating a source nat ip for network " + network);
                 Account owner = _accountMgr.getAccount(network.getAccountId());
-                if (vpc != null) {
-                    assignSourceNatIpAddressToVpc(owner, vpc);
-                } else {
-                    assignSourceNatIpAddressToGuestNetwork(owner, network);
-                }
+                assignSourceNatIpAddressToGuestNetwork(owner, network);
             }
         }
 
@@ -2537,50 +2451,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
             _nicDao.expunge(nic.getId());
         }
     }
-    
-    
-    @Override
-    public void checkVirtualNetworkCidrOverlap(Long zoneId, String cidr) {
-        if (zoneId == null) {
-            return;
-        }
-        if (cidr == null) {
-            return;
-        }
-        List<NetworkVO> networks = _networksDao.listByZone((long) zoneId);
-        Map<Long, String> networkToCidr = new HashMap<Long, String>();
-        for (NetworkVO network : networks) {
-            if (network.getGuestType() != GuestType.Isolated) {
-                continue;
-            }
-            if (network.getCidr() != null) {
-                networkToCidr.put(network.getId(), network.getCidr());
-            }
-        }
-        if (networkToCidr == null || networkToCidr.isEmpty()) {
-            return;
-        }
-
-        String currCidrAddress = getCidrAddress(cidr);
-        int currCidrSize = getCidrSize(cidr);
-
-        for (long networkId : networkToCidr.keySet()) {
-            String ntwkCidr = networkToCidr.get(networkId);
-            String ntwkCidrAddress = getCidrAddress(ntwkCidr);
-            int ntwkCidrSize = getCidrSize(ntwkCidr);
-
-            long cidrSizeToUse = currCidrSize < ntwkCidrSize ? currCidrSize : ntwkCidrSize;
-
-            String ntwkCidrSubnet = NetUtils.getCidrSubNet(ntwkCidrAddress, cidrSizeToUse);
-            String cidrSubnet = NetUtils.getCidrSubNet(currCidrAddress, cidrSizeToUse);
-
-            if (cidrSubnet.equals(ntwkCidrSubnet)) {
-            	InvalidParameterValueException ex = new InvalidParameterValueException("Warning: The specified existing network has conflict CIDR subnets with new network!");
-            	ex.addProxyObject("networks", networkId, "networkId");
-            	throw ex;
-            }
-        }
-    }
 
     @Override
     @DB
@@ -2646,17 +2516,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
         	ex.addProxyObject(zone, zoneId, "zoneId");
             throw ex;        	
         }
-        
-        //validate vpc
-        if (vpcId != null) {
-            Vpc vpc = _vpcMgr.getActiveVpc(vpcId);
-            if (vpc == null) {
-                InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC ");
-                ex.addProxyObject("vpc", vpcId, "VPC");
-                throw ex;
-            }
-            _accountMgr.checkAccess(caller, null, false, vpc);
-        }
 
         // Only domain and account ACL types are supported in Acton.
         ACLType aclType = null;
@@ -2835,8 +2694,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
             if (!_configMgr.isOfferingForVpc(ntwkOff)){
                 throw new InvalidParameterValueException("Network offering can't be used for VPC networks");
             }
-            network = createVpcGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId, 
-                    networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId);
+            network = _vpcMgr.createVpcGuestNetwork(networkOfferingId, name, displayText, gateway, cidr, vlanId, 
+                    networkDomain, owner, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId, caller);
         } else {
             if (_configMgr.isOfferingForVpc(ntwkOff)){
                 throw new InvalidParameterValueException("Network offering can be used for VPC networks only");
@@ -2855,30 +2714,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
 
         return network;
     }
-    
-    @DB
-    protected Network createVpcGuestNetwork(long ntwkOffId, String name, String displayText, String gateway, 
-            String cidr, String vlanId, String networkDomain, Account owner, Long domainId,
-            PhysicalNetwork pNtwk, long zoneId, ACLType aclType, Boolean subdomainAccess, long vpcId) 
-                    throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException {
-        
-        Vpc vpc = _vpcMgr.getActiveVpc(vpcId);
-        if (networkDomain == null) {
-            networkDomain = vpc.getNetworkDomain();
-        }
-        
-        if (vpc.getZoneId() != zoneId) {
-            throw new InvalidParameterValueException("New network doesn't belong to vpc zone", null);
-        }
-        
-        //1) Validate if network can be created for VPC
-        _vpcMgr.validateGuestNtkwForVpc(_configMgr.getNetworkOffering(ntwkOffId), cidr, networkDomain, owner, vpc, null, gateway);
-        //2) Create network
-        Network guestNetwork = createGuestNetwork(ntwkOffId, name, displayText, gateway, cidr, vlanId, 
-                networkDomain, owner, domainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId);
-        
-        return guestNetwork;
-    }
 
     @Override
     @DB
@@ -4730,20 +4565,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
                 String errMsg = forVpcNew ? "a vpc offering " : "not a vpc offering";
                 throw new InvalidParameterValueException("Can't update as the new offering is " + errMsg);
             }
-            
-            //can't update from vpc to non-vpc network offering
-            boolean forVpcNew = _configMgr.isOfferingForVpc(networkOffering);
-            boolean vorVpcOriginal = _configMgr.isOfferingForVpc(_configMgr.getNetworkOffering(oldNetworkOfferingId));
-            if (forVpcNew != vorVpcOriginal) {
-                String errMsg = forVpcNew ? "a vpc offering " : "not a vpc offering";
-                throw new InvalidParameterValueException("Can't update as the new offering is " + errMsg);
-            }
-
-            //perform below validation if the network is vpc network
-            if (network.getVpcId() != null) {
-                Vpc vpc = _vpcMgr.getVpc(network.getVpcId());
-                _vpcMgr.validateGuestNtkwForVpc(networkOffering, null, null, null,vpc, networkId, null);
-            }
 
             if (networkOfferingId != oldNetworkOfferingId) {
                 if (networkOfferingIsConfiguredForExternalNetworking(networkOfferingId) != networkOfferingIsConfiguredForExternalNetworking(oldNetworkOfferingId)
@@ -6117,7 +5938,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
                 IPAddressVO ip = markIpAsUnavailable(ipToRelease.getId());
                 assert (ip != null) : "Unable to mark the ip address id=" + ipToRelease.getId() + " as unavailable.";
             } else {
-                unassignIPFromVpcNetwork(ipToRelease.getId(), network.getId());
+                _vpcMgr.unassignIPFromVpcNetwork(ipToRelease.getId(), network.getId());
             }
         }
 
@@ -7113,7 +6934,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
                 // allocate ip
                 ip = allocateIP(owner, true, guestNetwork.getDataCenterId());
                 // apply ip associations
-                ip = associateIP(ip.getId(), networkId, null);
+                ip = associateIPToNetwork(ip.getId(), networkId);
             } catch (ResourceAllocationException ex) {
                 throw new CloudRuntimeException("Failed to allocate system ip due to ", ex);
             } catch (ConcurrentOperationException ex) {
@@ -7288,69 +7109,22 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
     
     @Override
     @ActionEvent(eventType = EventTypes.EVENT_NET_IP_ASSIGN, eventDescription = "associating Ip", async = true)
-    public IpAddress associateIP(long ipId, Long networkId, Long vpcId) throws InsufficientAddressCapacityException,
-        ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
-        if (vpcId != null) {
-            return associateIPToVpc(ipId, vpcId);
-        }
-        
-        if (networkId != null) {
-            Network network = _networksDao.findById(networkId);
-            if (network == null) {
-                throw new InvalidParameterValueException("Invalid network id is given");
-            }
-            
-            if (network.getVpcId() != null) {
-                throw new InvalidParameterValueException("Can't assign ip to the network directly when network belongs" +
-                		" to VPC.Specify vpcId to associate ip address to VPC");
-            }
-            return associateIPToGuestNetwork(ipId, networkId, true);
-        }
-        
-        return null;
-    }
-
-    
-    @Override
-    public void unassignIPFromVpcNetwork(long ipId, long networkId) {
-        IPAddressVO ip = _ipAddressDao.findById(ipId);
-
-        if (ipUsedInVpc(ip)) {
-            return;
-        }
-        
-        if (ip == null || ip.getVpcId() == null) {
-            return;
-        }
-        
-        s_logger.debug("Releasing VPC ip address " + ip + " from vpc network id=" + networkId);
+    public IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAddressCapacityException,
+    ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
 
-        long  vpcId = ip.getVpcId();
-        boolean success = false;
-        try {
-            //unassign ip from the VPC router
-            success = applyIpAssociations(getNetwork(networkId), true);
-        } catch (ResourceUnavailableException ex) {
-            throw new CloudRuntimeException("Failed to apply ip associations for network id=" + networkId + 
-                    " as a part of unassigning ip " + ipId + " from vpc", ex);
+        Network network = _networksDao.findById(networkId);
+        if (network == null) {
+            throw new InvalidParameterValueException("Invalid network id is given");
         }
 
-        if (success) {
-            ip.setAssociatedWithNetworkId(null);
-            _ipAddressDao.update(ipId, ip);
-            s_logger.debug("IP address " + ip + " is no longer associated with the network inside vpc id=" + vpcId);
-        } else {
-            throw new CloudRuntimeException("Failed to apply ip associations for network id=" + networkId + 
-                    " as a part of unassigning ip " + ipId + " from vpc");
+        if (network.getVpcId() != null) {
+            throw new InvalidParameterValueException("Can't assign ip to the network directly when network belongs" +
+                    " to VPC.Specify vpcId to associate ip address to VPC");
         }
-        s_logger.debug("Successfully released VPC ip address " + ip + " back to VPC pool ");
-    }
-
-    @Override
-    public boolean ipUsedInVpc(IpAddress ip) {
-        return (ip != null && ip.getVpcId() != null && 
-                (ip.isOneToOneNat() || !_firewallDao.listByIp(ip.getId()).isEmpty()));
+        return associateIPToGuestNetwork(ipId, networkId, true);
+        
     }
+    
 
     @Override @DB
     public Network createPrivateNetwork(String networkName, String displayText, long physicalNetworkId, 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/firewall/FirewallManagerImpl.java b/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
index fe45806..032d878 100644
--- a/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
+++ b/server/src/com/cloud/network/firewall/FirewallManagerImpl.java
@@ -57,6 +57,7 @@ import com.cloud.network.rules.FirewallRule.State;
 import com.cloud.network.rules.FirewallRuleVO;
 import com.cloud.network.rules.PortForwardingRuleVO;
 import com.cloud.network.rules.dao.PortForwardingRulesDao;
+import com.cloud.network.vpc.VpcManager;
 import com.cloud.projects.Project.ListProjectResourcesCriteria;
 import com.cloud.server.ResourceTag.TaggedResourceType;
 import com.cloud.tags.ResourceTagVO;
@@ -111,6 +112,8 @@ public class FirewallManagerImpl implements FirewallService, FirewallManager, Ma
     UserVmDao _vmDao;
     @Inject
     ResourceTagDao _resourceTagDao;
+    @Inject
+    VpcManager _vpcMgr;
 
     private boolean _elbEnabled = false;
 
@@ -458,7 +461,7 @@ public class FirewallManagerImpl implements FirewallService, FirewallManager, Ma
         if (rule.getSourceIpAddressId() != null) {
             //if the rule is the last one for the ip address assigned to VPC, unassign it from the network
             IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
-            _networkMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
+            _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
index e7ece27..c430c67 100755
--- a/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
+++ b/server/src/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java
@@ -83,6 +83,7 @@ import com.cloud.network.rules.LbStickinessMethod.LbStickinessMethodParam;
 import com.cloud.network.rules.LoadBalancer;
 import com.cloud.network.rules.RulesManager;
 import com.cloud.network.rules.StickinessPolicy;
+import com.cloud.network.vpc.VpcManager;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.projects.Project.ListProjectResourcesCriteria;
 import com.cloud.server.ResourceTag.TaggedResourceType;
@@ -165,6 +166,8 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
     NetworkServiceMapDao _ntwkSrvcDao;
     @Inject
     ResourceTagDao _resourceTagDao;
+    @Inject
+    VpcManager _vpcMgr;
 
     private String getLBStickinessCapability(long networkid) {
         Map<Service, Map<Capability, String>> serviceCapabilitiesMap = _networkMgr.getNetworkCapabilities(networkid);
@@ -780,8 +783,7 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
                 // release ip address if ipassoc was perfored
                 if (performedIpAssoc) {
                     ipVO = _ipAddressDao.findById(ipVO.getId());
-                    _networkMgr.unassignIPFromVpcNetwork(ipVO.getId(), lb.getNetworkId());
-                    
+                    _vpcMgr.unassignIPFromVpcNetwork(ipVO.getId(), lb.getNetworkId());
                 }
             }
         }
@@ -1351,6 +1353,6 @@ public class LoadBalancingRulesManagerImpl<Type> implements LoadBalancingRulesMa
         
         //if the rule is the last one for the ip address assigned to VPC, unassign it from the network
         IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
-        _networkMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
+        _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java
index 24b0f32..72dad34 100644
--- a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java
@@ -206,7 +206,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
                 }
             }
             
-            PublicIp sourceNatIp = _networkMgr.assignSourceNatIpAddressToVpc(owner, vpc);
+            PublicIp sourceNatIp = _vpcMgr.assignSourceNatIpAddressToVpc(owner, vpc);
             
             DomainRouterVO router = deployVpcRouter(owner, dest, plan, params, false, vpcVrProvider, offeringId,
                     vpc.getId(), sourceNatIp);
@@ -1198,7 +1198,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
             PublicIp publicIp = new PublicIp(ip, _vlanDao.findById(ip.getVlanId()), 
                     NetUtils.createSequenceBasedMacAddress(ip.getMacAddress()));
             if ((ip.getState() == IpAddress.State.Allocated || ip.getState() == IpAddress.State.Allocating) 
-                    && _networkMgr.ipUsedInVpc(ip)&& !publicVlans.contains(publicIp.getVlanTag())) {
+                    && _vpcMgr.ipUsedInVpc(ip)&& !publicVlans.contains(publicIp.getVlanTag())) {
                 s_logger.debug("Allocating nic for router in vlan " + publicIp.getVlanTag());
                 NicProfile publicNic = new NicProfile();
                 publicNic.setDefaultNic(false);
@@ -1267,7 +1267,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
             long publicNtwkId = ip.getNetworkId();
             
             //if ip is not associated to any network, and there are no firewall rules, release it on the backend
-            if (!_networkMgr.ipUsedInVpc(ip)) {
+            if (!_vpcMgr.ipUsedInVpc(ip)) {
                 ip.setState(IpAddress.State.Releasing);
             }
                          
@@ -1287,7 +1287,7 @@ public class VpcVirtualNetworkApplianceManagerImpl extends VirtualNetworkApplian
             long publicNtwkId = ip.getNetworkId();
             
             //if ip is not associated to any network, and there are no firewall rules, release it on the backend
-            if (!_networkMgr.ipUsedInVpc(ip)) {
+            if (!_vpcMgr.ipUsedInVpc(ip)) {
                 ip.setState(IpAddress.State.Releasing);
             }
                          

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/server/src/com/cloud/network/rules/RulesManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/rules/RulesManagerImpl.java b/server/src/com/cloud/network/rules/RulesManagerImpl.java
index 3e000a3..479e349 100755
--- a/server/src/com/cloud/network/rules/RulesManagerImpl.java
+++ b/server/src/com/cloud/network/rules/RulesManagerImpl.java
@@ -50,6 +50,7 @@ import com.cloud.network.dao.IPAddressDao;
 import com.cloud.network.rules.FirewallRule.FirewallRuleType;
 import com.cloud.network.rules.FirewallRule.Purpose;
 import com.cloud.network.rules.dao.PortForwardingRulesDao;
+import com.cloud.network.vpc.VpcManager;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.projects.Project.ListProjectResourcesCriteria;
 import com.cloud.server.ResourceTag.TaggedResourceType;
@@ -114,6 +115,8 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
     NicDao _nicDao;
     @Inject
     ResourceTagDao _resourceTagDao;
+    @Inject
+    VpcManager _vpcMgr;
 
     @Override
     public void checkIpAndUserVm(IpAddress ipAddress, UserVm userVm, Account caller) {
@@ -289,7 +292,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
             if (performedIpAssoc) {
                 //if the rule is the last one for the ip address assigned to VPC, unassign it from the network
                 IpAddress ip = _ipAddressDao.findById(ipAddress.getId());
-                _networkMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);  
+                _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);  
             }
         }
     }
@@ -468,7 +471,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
                 if (performedIpAssoc) {
                     //if the rule is the last one for the ip address assigned to VPC, unassign it from the network
                     IpAddress ip = _ipAddressDao.findById(ipAddress.getId());
-                    _networkMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);
+                    _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), networkId);
                 }
             }
         }
@@ -1208,7 +1211,7 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
                 ipAddress.setSystem(false);
             }
             _ipAddressDao.update(ipAddress.getId(), ipAddress);
-            _networkMgr.unassignIPFromVpcNetwork(ipAddress.getId(), networkId);
+            _vpcMgr.unassignIPFromVpcNetwork(ipAddress.getId(), networkId);
 
             if (isIpSystem && releaseIpIfElastic && !_networkMgr.handleSystemIpRelease(ipAddress)) {
                 s_logger.warn("Failed to release system ip address " + ipAddress);
@@ -1398,6 +1401,6 @@ public class RulesManagerImpl implements RulesManager, RulesService, Manager {
         
         //if the rule is the last one for the ip address assigned to VPC, unassign it from the network
         IpAddress ip = _ipAddressDao.findById(rule.getSourceIpAddressId());
-        _networkMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());   
+        _vpcMgr.unassignIPFromVpcNetwork(ip.getId(), rule.getNetworkId());   
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/server/src/com/cloud/network/vpc/VpcManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/vpc/VpcManager.java b/server/src/com/cloud/network/vpc/VpcManager.java
index 25dc5f0..c3bae6f 100644
--- a/server/src/com/cloud/network/vpc/VpcManager.java
+++ b/server/src/com/cloud/network/vpc/VpcManager.java
@@ -18,9 +18,16 @@ package com.cloud.network.vpc;
 
 import java.util.List;
 
+import com.cloud.acl.ControlledEntity.ACLType;
 import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientAddressCapacityException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.ResourceAllocationException;
 import com.cloud.exception.ResourceUnavailableException;
-import com.cloud.offering.NetworkOffering;
+import com.cloud.network.IpAddress;
+import com.cloud.network.Network;
+import com.cloud.network.PhysicalNetwork;
+import com.cloud.network.addr.PublicIp;
 import com.cloud.user.Account;
 import com.cloud.vm.DomainRouterVO;
 
@@ -28,7 +35,7 @@ import com.cloud.vm.DomainRouterVO;
 public interface VpcManager extends VpcService{
 
     /**
-     * @param guestNtwkOff
+     * @param ntwkOffId
      * @param cidr
      * @param networkDomain
      * @param networkOwner
@@ -37,7 +44,7 @@ public interface VpcManager extends VpcService{
      * @param gateway TODO
      * @return
      */
-    void validateGuestNtkwForVpc(NetworkOffering guestNtwkOff, String cidr, String networkDomain, Account networkOwner, 
+    void validateNtkwOffForVpc(long ntwkOffId, String cidr, String networkDomain, Account networkOwner, 
             Vpc vpc, Long networkId, String gateway);
 
     
@@ -69,4 +76,55 @@ public interface VpcManager extends VpcService{
      */
     VpcGateway getPrivateGatewayForVpc(long vpcId);
 
+
+    /**
+     * @param ip
+     * @return
+     */
+    boolean ipUsedInVpc(IpAddress ip);
+
+
+    /**
+     * @param ipId
+     * @param networkId
+     */
+    void unassignIPFromVpcNetwork(long ipId, long networkId);
+
+
+    /**
+     * @param ntwkOffId
+     * @param name
+     * @param displayText
+     * @param gateway
+     * @param cidr
+     * @param vlanId
+     * @param networkDomain
+     * @param owner
+     * @param domainId
+     * @param pNtwk
+     * @param zoneId
+     * @param aclType
+     * @param subdomainAccess
+     * @param vpcId
+     * @param caller
+     * @return
+     * @throws ConcurrentOperationException
+     * @throws InsufficientCapacityException
+     * @throws ResourceAllocationException
+     */
+    Network createVpcGuestNetwork(long ntwkOffId, String name, String displayText, String gateway, String cidr, 
+            String vlanId, String networkDomain, Account owner, Long domainId, PhysicalNetwork pNtwk, long zoneId,
+            ACLType aclType, Boolean subdomainAccess, long vpcId, Account caller) 
+                    throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException;
+
+
+    /**
+     * @param owner
+     * @param vpc
+     * @return
+     * @throws InsufficientAddressCapacityException
+     * @throws ConcurrentOperationException
+     */
+    PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException;
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/server/src/com/cloud/network/vpc/VpcManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/network/vpc/VpcManagerImpl.java b/server/src/com/cloud/network/vpc/VpcManagerImpl.java
index cd279d5..6236a05 100644
--- a/server/src/com/cloud/network/vpc/VpcManagerImpl.java
+++ b/server/src/com/cloud/network/vpc/VpcManagerImpl.java
@@ -31,16 +31,21 @@ import javax.naming.ConfigurationException;
 
 import org.apache.log4j.Logger;
 
+import com.cloud.acl.ControlledEntity.ACLType;
 import com.cloud.api.commands.ListPrivateGatewaysCmd;
 import com.cloud.api.commands.ListStaticRoutesCmd;
 import com.cloud.configuration.Config;
 import com.cloud.configuration.ConfigurationManager;
 import com.cloud.configuration.dao.ConfigurationDao;
 import com.cloud.dc.DataCenter;
+import com.cloud.dc.Vlan.VlanType;
+import com.cloud.dc.VlanVO;
+import com.cloud.dc.dao.VlanDao;
 import com.cloud.deploy.DeployDestination;
 import com.cloud.event.ActionEvent;
 import com.cloud.event.EventTypes;
 import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientAddressCapacityException;
 import com.cloud.exception.InsufficientCapacityException;
 import com.cloud.exception.InvalidParameterValueException;
 import com.cloud.exception.NetworkRuleConflictException;
@@ -49,6 +54,7 @@ import com.cloud.exception.ResourceAllocationException;
 import com.cloud.exception.ResourceUnavailableException;
 import com.cloud.exception.UnsupportedServiceException;
 import com.cloud.network.IPAddressVO;
+import com.cloud.network.IpAddress;
 import com.cloud.network.Network;
 import com.cloud.network.Network.GuestType;
 import com.cloud.network.Network.Provider;
@@ -58,6 +64,7 @@ import com.cloud.network.NetworkVO;
 import com.cloud.network.Networks.BroadcastDomainType;
 import com.cloud.network.Networks.TrafficType;
 import com.cloud.network.PhysicalNetwork;
+import com.cloud.network.addr.PublicIp;
 import com.cloud.network.dao.FirewallRulesDao;
 import com.cloud.network.dao.IPAddressDao;
 import com.cloud.network.dao.NetworkDao;
@@ -147,6 +154,8 @@ public class VpcManagerImpl implements VpcManager, Manager{
     Site2SiteVpnGatewayDao _vpnGatewayDao;
     @Inject
     Site2SiteVpnManager _s2sVpnMgr;
+    @Inject
+    VlanDao _vlanDao = null;
 
     private final ScheduledExecutorService _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("VpcChecker"));
     private VpcProvider vpcElement = null;
@@ -154,6 +163,7 @@ public class VpcManagerImpl implements VpcManager, Manager{
     String _name;
     int _cleanupInterval;
     int _maxNetworks;
+    SearchBuilder<IPAddressVO> IpAddressSearch;
 
     @Override
     @DB
@@ -194,6 +204,18 @@ public class VpcManagerImpl implements VpcManager, Manager{
 
         String maxNtwks = configs.get(Config.VpcMaxNetworks.key());
         _maxNetworks = NumbersUtil.parseInt(maxNtwks, 3); // max=3 is default
+        
+        
+        IpAddressSearch = _ipAddressDao.createSearchBuilder();
+        IpAddressSearch.and("accountId", IpAddressSearch.entity().getAllocatedToAccountId(), Op.EQ);
+        IpAddressSearch.and("dataCenterId", IpAddressSearch.entity().getDataCenterId(), Op.EQ);
+        IpAddressSearch.and("vpcId", IpAddressSearch.entity().getVpcId(), Op.EQ);
+        IpAddressSearch.and("associatedWithNetworkId", IpAddressSearch.entity().getAssociatedWithNetworkId(), Op.EQ);
+        SearchBuilder<VlanVO> virtualNetworkVlanSB = _vlanDao.createSearchBuilder();
+        virtualNetworkVlanSB.and("vlanType", virtualNetworkVlanSB.entity().getVlanType(), Op.EQ);
+        IpAddressSearch.join("virtualNetworkVlanSB", virtualNetworkVlanSB, IpAddressSearch.entity().getVlanId(), virtualNetworkVlanSB.entity().getId(), JoinBuilder.JoinType.INNER);
+        IpAddressSearch.done();
+        
         return true;
     }
 
@@ -878,8 +900,10 @@ public class VpcManagerImpl implements VpcManager, Manager{
     
     @Override
     @DB
-    public void validateGuestNtkwForVpc(NetworkOffering guestNtwkOff, String cidr, String networkDomain, 
+    public void validateNtkwOffForVpc(long ntwkOffId, String cidr, String networkDomain, 
             Account networkOwner, Vpc vpc, Long networkId, String gateway) {
+        
+        NetworkOffering guestNtwkOff = _configMgr.getNetworkOffering(ntwkOffId);
 
         if (networkId == null) {
             //1) Validate attributes that has to be passed in when create new guest network
@@ -1614,4 +1638,203 @@ public class VpcManagerImpl implements VpcManager, Manager{
     public VpcGateway getPrivateGatewayForVpc(long vpcId) {
         return _vpcGatewayDao.getPrivateGatewayForVpc(vpcId);
     }
+
+    
+    @DB
+    @Override
+    @ActionEvent(eventType = EventTypes.EVENT_NET_IP_ASSIGN, eventDescription = "associating Ip", async = true)
+    public IpAddress associateIPToVpc(long ipId, long vpcId) throws ResourceAllocationException, ResourceUnavailableException, 
+    InsufficientAddressCapacityException, ConcurrentOperationException {
+        Account caller = UserContext.current().getCaller();
+        Account owner = null;
+
+        IpAddress ipToAssoc = _ntwkMgr.getIp(ipId);
+        if (ipToAssoc != null) {
+            _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;
+        }
+
+        Vpc vpc = getVpc(vpcId);
+        if (vpc == null) {
+            throw new InvalidParameterValueException("Invalid VPC id provided");
+        }
+
+        // check permissions
+        _accountMgr.checkAccess(caller, null, true, owner, vpc);
+
+        boolean isSourceNat = false;
+        if (getExistingSourceNatInVpc(owner.getId(), vpcId) == null) {
+            isSourceNat = true;
+        }
+
+        s_logger.debug("Associating ip " + ipToAssoc + " to vpc " + vpc);
+
+        Transaction txn = Transaction.currentTxn();
+        txn.start();
+        IPAddressVO ip = _ipAddressDao.findById(ipId);
+        //update ip address with networkId
+        ip.setVpcId(vpcId);
+        ip.setSourceNat(isSourceNat);
+        _ipAddressDao.update(ipId, ip);
+
+        //mark ip as allocated
+        _ntwkMgr.markPublicIpAsAllocated(ip);
+        txn.commit();
+
+        s_logger.debug("Successfully assigned ip " + ipToAssoc + " to vpc " + vpc);
+
+        return _ipAddressDao.findById(ipId);
+    }
+    
+    
+    @Override
+    public void unassignIPFromVpcNetwork(long ipId, long networkId) {
+        IPAddressVO ip = _ipAddressDao.findById(ipId);
+        if (ipUsedInVpc(ip)) {
+            return;
+        }
+
+        if (ip == null || ip.getVpcId() == null) {
+            return;
+        }
+
+        s_logger.debug("Releasing VPC ip address " + ip + " from vpc network id=" + networkId);
+
+        long  vpcId = ip.getVpcId();
+        boolean success = false;
+        try {
+            //unassign ip from the VPC router
+            success = _ntwkMgr.applyIpAssociations(_ntwkMgr.getNetwork(networkId), true);
+        } catch (ResourceUnavailableException ex) {
+            throw new CloudRuntimeException("Failed to apply ip associations for network id=" + networkId + 
+                    " as a part of unassigning ip " + ipId + " from vpc", ex);
+        }
+
+        if (success) {
+            ip.setAssociatedWithNetworkId(null);
+            _ipAddressDao.update(ipId, ip);
+            s_logger.debug("IP address " + ip + " is no longer associated with the network inside vpc id=" + vpcId);
+        } else {
+            throw new CloudRuntimeException("Failed to apply ip associations for network id=" + networkId + 
+                    " as a part of unassigning ip " + ipId + " from vpc");
+        }
+        s_logger.debug("Successfully released VPC ip address " + ip + " back to VPC pool ");
+    }
+    
+    @Override
+    public boolean ipUsedInVpc(IpAddress ip) {
+        return (ip != null && ip.getVpcId() != null && 
+                (ip.isOneToOneNat() || !_firewallDao.listByIp(ip.getId()).isEmpty()));
+    }
+    
+    @DB
+    @Override
+    public Network createVpcGuestNetwork(long ntwkOffId, String name, String displayText, String gateway, 
+            String cidr, String vlanId, String networkDomain, Account owner, Long domainId,
+            PhysicalNetwork pNtwk, long zoneId, ACLType aclType, Boolean subdomainAccess, long vpcId, Account caller) 
+                    throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException {
+
+        Vpc vpc = getActiveVpc(vpcId);
+
+        if (vpc == null) {
+            InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find Enabled VPC ");
+            ex.addProxyObject("vpc", vpcId, "VPC");
+            throw ex;
+        }
+        _accountMgr.checkAccess(caller, null, false, vpc);
+        
+        if (networkDomain == null) {
+            networkDomain = vpc.getNetworkDomain();
+        }
+        
+        if (vpc.getZoneId() != zoneId) {
+            throw new InvalidParameterValueException("New network doesn't belong to vpc zone");
+        }
+        
+        //1) Validate if network can be created for VPC
+        validateNtkwOffForVpc(ntwkOffId, cidr, networkDomain, owner, vpc, null, gateway);
+
+        //2) Create network
+        Network guestNetwork = _ntwkMgr.createGuestNetwork(ntwkOffId, name, displayText, gateway, cidr, vlanId, 
+                networkDomain, owner, domainId, pNtwk, zoneId, aclType, subdomainAccess, vpcId);
+
+        return guestNetwork;
+    }
+    
+    
+    protected IPAddressVO getExistingSourceNatInVpc(long ownerId, long vpcId) {
+
+        List<IPAddressVO> addrs = listPublicIpsAssignedToVpc(ownerId, true, vpcId);
+        
+        IPAddressVO sourceNatIp = null;
+        if (addrs.isEmpty()) {
+            return null;
+        } else {
+            // Account already has ip addresses
+            for (IPAddressVO addr : addrs) {
+                if (addr.isSourceNat()) {
+                    sourceNatIp = addr;
+                    return sourceNatIp;
+                }
+            }
+
+            assert (sourceNatIp != null) : "How do we get a bunch of ip addresses but none of them are source nat? " +
+            "account=" + ownerId + "; vpcId=" + vpcId;
+        } 
+
+        return sourceNatIp;
+    }
+    
+    protected List<IPAddressVO> listPublicIpsAssignedToVpc(long accountId, Boolean sourceNat, long vpcId) {
+        SearchCriteria<IPAddressVO> sc = IpAddressSearch.create();
+        sc.setParameters("accountId", accountId);
+        sc.setParameters("vpcId", vpcId);
+
+        if (sourceNat != null) {
+            sc.addAnd("sourceNat", SearchCriteria.Op.EQ, sourceNat);
+        }
+        sc.setJoinParameters("virtualNetworkVlanSB", "vlanType", VlanType.VirtualNetwork);
+
+        return _ipAddressDao.search(sc, null);
+    }
+    
+    
+    @Override
+    public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException {
+        long dcId = vpc.getZoneId();
+
+        IPAddressVO sourceNatIp = getExistingSourceNatInVpc(owner.getId(), vpc.getId());
+
+        PublicIp ipToReturn = null;
+
+        if (sourceNatIp != null) {
+            ipToReturn = new PublicIp(sourceNatIp, _vlanDao.findById(sourceNatIp.getVlanId()), 
+                    NetUtils.createSequenceBasedMacAddress(sourceNatIp.getMacAddress()));
+        } else {
+            ipToReturn = _ntwkMgr.assignDedicateIpAddress(owner, null, vpc.getId(), dcId, true);
+        }
+
+        return ipToReturn;
+    }
+
+
+    @Override
+    public Network updateVpcGuestNetwork(long networkId, String name, String displayText, Account callerAccount, 
+            User callerUser, String domainSuffix, Long ntwkOffId, Boolean changeCidr) {
+        NetworkVO network = _ntwkDao.findById(networkId);
+        if (network == null) {
+            throw new InvalidParameterValueException("Couldn't find network by id");
+        }
+        //perform below validation if the network is vpc network
+        if (network.getVpcId() != null && ntwkOffId != null) {
+            Vpc vpc = getVpc(network.getVpcId());
+            validateNtkwOffForVpc(ntwkOffId, null, null, null, vpc, networkId, null);
+        }
+        
+        return _ntwkMgr.updateGuestNetwork(networkId, name, displayText, callerAccount, callerUser, domainSuffix,
+                ntwkOffId, changeCidr);
+    } 
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/7cceaae9/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 171d9c7..2483c89 100755
--- a/server/test/com/cloud/network/MockNetworkManagerImpl.java
+++ b/server/test/com/cloud/network/MockNetworkManagerImpl.java
@@ -55,7 +55,6 @@ import com.cloud.network.element.UserDataServiceProvider;
 import com.cloud.network.guru.NetworkGuru;
 import com.cloud.network.rules.FirewallRule;
 import com.cloud.network.rules.StaticNat;
-import com.cloud.network.vpc.Vpc;
 import com.cloud.offering.NetworkOffering;
 import com.cloud.offerings.NetworkOfferingVO;
 import com.cloud.user.Account;
@@ -775,15 +774,6 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
     }
 
     /* (non-Javadoc)
-     * @see com.cloud.network.NetworkService#associateIP(long, java.lang.Long, java.lang.Long)
-     */
-    @Override
-    public IpAddress associateIP(long ipId, Long networkId, Long vpcId) throws InsufficientAddressCapacityException, ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    /* (non-Javadoc)
      * @see com.cloud.network.NetworkManager#allocateNic(com.cloud.vm.NicProfile, com.cloud.network.Network, java.lang.Boolean, int, com.cloud.vm.VirtualMachineProfile)
      */
     @Override
@@ -835,7 +825,7 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
      * @see com.cloud.network.NetworkService#getIsolatedNetworksWithSourceNATOwnedByAccountInZone(long, com.cloud.user.Account)
      */
     @Override
-    public List<? extends Network> getIsolatedNetworksWithSourceNATOwnedByAccountInZone(long zoneId, Account owner) {
+    public IpAddress associateIPToNetwork(long ipId, long networkId) throws InsufficientAddressCapacityException, ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException {
         // TODO Auto-generated method stub
         return null;
     }
@@ -946,16 +936,6 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
 		return false;
 	}
 
-
-    /* (non-Javadoc)
-     * @see com.cloud.network.NetworkManager#checkVirtualNetworkCidrOverlap(java.lang.Long, java.lang.String)
-     */
-    @Override
-    public void checkVirtualNetworkCidrOverlap(Long zoneId, String cidr) {
-        // TODO Auto-generated method stub
-        
-    }
-
     /* (non-Javadoc)
      * @see com.cloud.network.NetworkManager#getDefaultStorageTrafficLabel(long, com.cloud.hypervisor.Hypervisor.HypervisorType)
      */
@@ -983,15 +963,6 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
         return null;
     }
 
-    /* (non-Javadoc)
-     * @see com.cloud.network.NetworkManager#assignSourceNatIpAddressToVpc(com.cloud.user.Account, com.cloud.network.vpc.Vpc)
-     */
-    @Override
-    public PublicIp assignSourceNatIpAddressToVpc(Account owner, Vpc vpc) throws InsufficientAddressCapacityException, ConcurrentOperationException {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
 
     /* (non-Javadoc)
      * @see com.cloud.network.NetworkManager#setupDns(com.cloud.network.Network, com.cloud.network.Network.Provider)
@@ -1086,23 +1057,6 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
         return null;
     }
 
-    /* (non-Javadoc)
-     * @see com.cloud.network.NetworkManager#unassignIPFromVpcNetwork(long, long)
-     */
-    @Override
-    public void unassignIPFromVpcNetwork(long ipId, long networkId) {
-        // TODO Auto-generated method stub
-        
-    }
-
-    /* (non-Javadoc)
-     * @see com.cloud.network.NetworkManager#ipUsedInVpc(com.cloud.network.IpAddress)
-     */
-    @Override
-    public boolean ipUsedInVpc(IpAddress ip) {
-        // TODO Auto-generated method stub
-        return false;
-    }
 
     /* (non-Javadoc)
      * @see com.cloud.network.NetworkManager#handleSystemIpRelease(com.cloud.network.IpAddress)
@@ -1148,4 +1102,31 @@ public class MockNetworkManagerImpl implements NetworkManager, Manager, NetworkS
         // TODO Auto-generated method stub
         return null;
     }
+
+    /* (non-Javadoc)
+     * @see com.cloud.network.NetworkService#getIsolatedNetworksWithSourceNATOwnedByAccountInZone(long, com.cloud.user.Account)
+     */
+    @Override
+    public List<? extends Network> getIsolatedNetworksWithSourceNATOwnedByAccountInZone(long zoneId, Account owner) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see com.cloud.network.NetworkManager#markPublicIpAsAllocated(com.cloud.network.IPAddressVO)
+     */
+    @Override
+    public void markPublicIpAsAllocated(IPAddressVO addr) {
+        // TODO Auto-generated method stub
+        
+    }
+
+    /* (non-Javadoc)
+     * @see com.cloud.network.NetworkManager#assignDedicateIpAddress(com.cloud.user.Account, java.lang.Long, java.lang.Long, long, boolean)
+     */
+    @Override
+    public PublicIp assignDedicateIpAddress(Account owner, Long guestNtwkId, Long vpcId, long dcId, boolean isSourceNat) throws ConcurrentOperationException, InsufficientAddressCapacityException {
+        // TODO Auto-generated method stub
+        return null;
+    }
 }


Mime
View raw message