You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ah...@apache.org on 2013/01/09 14:26:48 UTC

[11/50] [abbrv] Merge branch 'api_refactoring' into javelin

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
index 892d457,1a6eb09..875303f
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@@ -34,11 -39,10 +39,12 @@@ import java.util.concurrent.ScheduledEx
  import java.util.concurrent.TimeUnit;
  
  import javax.ejb.Local;
 +import javax.inject.Inject;
  import javax.naming.ConfigurationException;
  
+ import org.apache.cloudstack.api.command.admin.router.UpgradeRouterCmd;
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
  import com.cloud.agent.AgentManager;
  import com.cloud.agent.AgentManager.OnError;
@@@ -351,9 -356,12 +358,12 @@@ public class VirtualNetworkApplianceMan
      ScheduledExecutorService _executor;
      ScheduledExecutorService _checkExecutor;
      ScheduledExecutorService _networkStatsUpdateExecutor;
+     ExecutorService _rvrStatusUpdateExecutor;
  
      Account _systemAcct;
 -    
 +
+     BlockingQueue<Long> _vrUpdateQueue = null;
+ 
      @Override
      public boolean sendSshKeysToHost(Long hostId, String pubKey, String prvKey) {
          ModifySshKeysCommand cmd = new ModifySshKeysCommand(pubKey, prvKey);
@@@ -365,11 -373,11 +375,11 @@@
              return false;
          }
      }
 -    
 +
+     
+ 
      @Override
-     public VirtualRouter destroyRouter(final long routerId) throws ResourceUnavailableException, ConcurrentOperationException {
-         UserContext context = UserContext.current();
-         User user = _accountMgr.getActiveUser(context.getCallerUserId());
+     public VirtualRouter destroyRouter(final long routerId, Account caller, Long callerUserId) throws ResourceUnavailableException, ConcurrentOperationException {
  
          if (s_logger.isDebugEnabled()) {
              s_logger.debug("Attempting to destroy router " + routerId);
@@@ -573,7 -615,18 +617,18 @@@
  
          value = configs.get("router.check.interval");
          _routerCheckInterval = NumbersUtil.parseInt(value, 30);
 -
 +        
+         value = configs.get("router.check.poolsize");
+         _rvrStatusUpdatePoolSize = NumbersUtil.parseInt(value, 10);
+ 
+         /* 
+          * We assume that one thread can handle 20 requests in 1 minute in normal situation, so here we give the queue size up to 50 minutes.
+          * It's mostly for buffer, since each time CheckRouterTask running, it would add all the redundant networks in the queue immediately
+          */
+         _vrUpdateQueue = new LinkedBlockingQueue<Long>(_rvrStatusUpdatePoolSize * 1000); 
+ 
+         _rvrStatusUpdateExecutor = Executors.newFixedThreadPool(_rvrStatusUpdatePoolSize, new NamedThreadFactory("RedundantRouterStatusMonitor"));
+ 
          _instance = configs.get("instance.name");
          if (_instance == null) {
              _instance = "DEFAULT";
@@@ -1095,11 -1148,11 +1150,11 @@@
          return priority;
      }
  
-     protected class CheckRouterTask implements Runnable {
+     protected class RvRStatusUpdateTask implements Runnable {
  
-         public CheckRouterTask() {
+         public RvRStatusUpdateTask() {
          }
 -        
 +
          /*
           * In order to make fail-over works well at any time, we have to ensure:
           * 1. Backup router's priority = Master's priority - DELTA + 1
@@@ -1175,18 -1228,60 +1230,60 @@@
  
          @Override
          public void run() {
+             while (true) {
 -                try {
 +            try {
-                 final List<DomainRouterVO> routers = _routerDao.listIsolatedByHostId(null);
-                 s_logger.debug("Found " + routers.size() + " routers. ");
+                     Long networkId = _vrUpdateQueue.take();
+                     List <DomainRouterVO> routers = _routerDao.listByNetworkAndRole(networkId, Role.VIRTUAL_ROUTER);
  
+                     if (routers.size() != 2) {
+                         continue;
+                     }
+                     /*
+                      * We update the router pair which the lower id router owned by this mgmt server, in order
+                      * to prevent duplicate update of router status from cluster mgmt servers
+                      */
+                     DomainRouterVO router = routers.get(0);
+                     if (routers.get(1).getId() < router.getId()) {
+                         router = routers.get(1);
+                     }
+                     HostVO host = _hostDao.findById(router.getHostId());
+                     if (host == null || host.getManagementServerId() == null ||
+                             host.getManagementServerId() != ManagementServerNode.getManagementServerId()) {
+                         continue;
+                     }
 -                    updateRoutersRedundantState(routers);
 -                    checkDuplicateMaster(routers);
 -                    checkSanity(routers);
 -                } catch (Exception ex) {
 +                updateRoutersRedundantState(routers);
-                 updateSite2SiteVpnConnectionState(routers);
- 
-                 /* FIXME assumed the a pair of redundant routers managed by same mgmt server,
-                  * then the update above can get the latest status */
 +                checkDuplicateMaster(routers);
 +                checkSanity(routers);
 +            } catch (Exception ex) {
+                     s_logger.error("Fail to complete the RvRStatusUpdateTask! ", ex);
+                 }
+             }
+         }
+         
+     }
+     
+     protected class CheckRouterTask implements Runnable {
+ 
+         public CheckRouterTask() {
+         }
+ 
+         @Override
+         public void run() {
+             try {
+                 final List<DomainRouterVO> routers = _routerDao.listIsolatedByHostId(null);
+                 s_logger.debug("Found " + routers.size() + " routers to update status. ");
+ 
+                 updateSite2SiteVpnConnectionState(routers);
+ 
+                 final List<NetworkVO> networks = _networkDao.listRedundantNetworks();
+                 s_logger.debug("Found " + networks.size() + " networks to update RvR status. ");
+                 for (NetworkVO network : networks) {
+                     if (!_vrUpdateQueue.offer(network.getId(), 500, TimeUnit.MILLISECONDS)) {
+                         s_logger.warn("Cannot insert into virtual router update queue! Adjustment of router.check.interval and router.check.poolsize maybe needed.");
+                         break;
+                     }
+                 }
+             } catch (Exception ex) {
                  s_logger.error("Fail to complete the CheckRouterTask! ", ex);
              }
          }
@@@ -1287,74 -1381,128 +1383,128 @@@
              boolean isPodBased = (dest.getDataCenter().getNetworkType() == NetworkType.Basic ||
                      _networkMgr.areServicesSupportedInNetwork(guestNetwork.getId(), Service.SecurityGroup))
                      && guestNetwork.getTrafficType() == TrafficType.Guest;
 -
 +        
-             Pair<DeploymentPlan, List<DomainRouterVO>> planAndRouters = getDeploymentPlanAndRouters(isPodBased, dest, guestNetwork.getId());
+             // dest has pod=null, for Basic Zone findOrDeployVRs for all Pods
+             List<DeployDestination> destinations = new ArrayList<DeployDestination>();
+ 
+             if (dest.getDataCenter().getNetworkType() == NetworkType.Basic) {
+                 // Find all pods in the data center with running or starting user vms
+                 long dcId = dest.getDataCenter().getId();
+                 List<HostPodVO> pods = _podDao.listByDataCenterIdVMTypeAndStates(dcId, VirtualMachine.Type.User, VirtualMachine.State.Starting, VirtualMachine.State.Running);
+ 
+                 // Loop through all the pods skip those with running or starting VRs
+                 for (HostPodVO pod: pods) {
+                     // Get list of VRs in starting or running state
+                     long podId = pod.getId();
+                     List<DomainRouterVO> virtualRouters = _routerDao.listByPodIdAndStates(podId, VirtualMachine.State.Starting, VirtualMachine.State.Running);
+ 
+                     assert (virtualRouters.size() <= 1) : "Pod can have utmost one VR in Basic Zone, please check!";
+ 
+                     // Add virtualRouters to the routers, this avoids the situation when
+                     // all routers are skipped and VirtualRouterElement throws exception
+                     routers.addAll(virtualRouters);
+ 
+                     // If List size is one, we already have a starting or running VR, skip deployment
+                     if (virtualRouters.size() == 1) {
+                         s_logger.debug("Skipping VR deployment: Found a running or starting VR in Pod "
+                                 + pod.getName() + " id=" + podId);
+                         continue;
+                     }
+                     // Add new DeployDestination for this pod
+                     destinations.add(new DeployDestination(dest.getDataCenter(), pod, null, null));
+                 }
+             }
+             else {
+                 // Else, just add the supplied dest
+                 destinations.add(dest);
+             }
+ 
+             // Except for Basic Zone, the for loop will iterate only once
+             for (DeployDestination destination: destinations) {
+                 Pair<DeploymentPlan, List<DomainRouterVO>> planAndRouters = getDeploymentPlanAndRouters(isPodBased, destination, guestNetwork.getId());
 -                routers = planAndRouters.second();
 -
 -                // 2) Figure out required routers count
 -                int routerCount = 1;
 -                if (isRedundant) {
 -                    routerCount = 2;
 -                }
 -
 +            routers = planAndRouters.second();
 +        
 +            // 2) Figure out required routers count
 +            int routerCount = 1;
 +            if (isRedundant) {
 +                routerCount = 2;
 +            }
 +        
-             /* If old network is redundant but new is single router, then routers.size() = 2 but routerCount = 1 */
+                 // If old network is redundant but new is single router, then routers.size() = 2 but routerCount = 1
 -                if (routers.size() >= routerCount) {
 -                    return routers;
 -                }
 -
 -                if (routers.size() >= 5) {
 -                    s_logger.error("Too much redundant routers!");
 -                }
 +            if (routers.size() >= routerCount) {
 +                return routers;
 +            }
 +        
 +            if (routers.size() >= 5) {
 +                s_logger.error("Too much redundant routers!");
 +            }
  
 -                // Check if providers are supported in the physical networks
 -                VirtualRouterProviderType type = VirtualRouterProviderType.VirtualRouter;
 -                Long physicalNetworkId = _networkMgr.getPhysicalNetworkId(guestNetwork);
 -                PhysicalNetworkServiceProvider provider = _physicalProviderDao.findByServiceProvider(physicalNetworkId, type.toString());
 -                if (provider == null) {
 -                    throw new CloudRuntimeException("Cannot find service provider " + type.toString() + " in physical network " + physicalNetworkId);
 -                }
 -                VirtualRouterProvider vrProvider = _vrProviderDao.findByNspIdAndType(provider.getId(), type);
 -                if (vrProvider == null) {
 +            // Check if providers are supported in the physical networks
 +            VirtualRouterProviderType type = VirtualRouterProviderType.VirtualRouter;
 +            Long physicalNetworkId = _networkMgr.getPhysicalNetworkId(guestNetwork);
 +            PhysicalNetworkServiceProvider provider = _physicalProviderDao.findByServiceProvider(physicalNetworkId, type.toString());
 +            if (provider == null) {
 +                throw new CloudRuntimeException("Cannot find service provider " + type.toString() + " in physical network " + physicalNetworkId);
 +            }
 +            VirtualRouterProvider vrProvider = _vrProviderDao.findByNspIdAndType(provider.getId(), type);
 +            if (vrProvider == null) {
-                 throw new CloudRuntimeException("Cannot find virtual router provider " + type.toString()+ " as service provider " + provider.getId());
+                     throw new CloudRuntimeException("Cannot find virtual router provider " + type.toString() + " as service provider " + provider.getId());
 -                }
 +            }
  
 -                if (_networkMgr.isNetworkSystem(guestNetwork) || guestNetwork.getGuestType() == Network.GuestType.Shared) {
 -                    owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM);
 -                }
 +            if (_networkMgr.isNetworkSystem(guestNetwork) || guestNetwork.getGuestType() == Network.GuestType.Shared) {
 +                owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM);
 +            }
  
-             //Check if public network has to be set on VR
+                 // Check if public network has to be set on VR
 -                boolean publicNetwork = false;
 -                if (_networkMgr.isProviderSupportServiceInNetwork(guestNetwork.getId(), Service.SourceNat, Provider.VirtualRouter)) {
 -                    publicNetwork = true;
 -                }
 -                if (isRedundant && !publicNetwork) {
 -                    s_logger.error("Didn't support redundant virtual router without public network!");
 -                    return null;
 -                }
 +            boolean publicNetwork = false;
 +            if (_networkMgr.isProviderSupportServiceInNetwork(guestNetwork.getId(), Service.SourceNat, Provider.VirtualRouter)) {
 +                publicNetwork = true;
 +            }
 +            if (isRedundant && !publicNetwork) {
 +                s_logger.error("Didn't support redundant virtual router without public network!");
 +                return null;
 +            }
  
 -                Long offeringId = _networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId();
 -                if (offeringId == null) {
 -                    offeringId = _offering.getId();
 -                }
 +            Long offeringId = _networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId();
 +            if (offeringId == null) {
 +                offeringId = _offering.getId();
 +            }
  
+                 // 3) deploy virtual router(s)
+                 int count = routerCount - routers.size();
+                 DeploymentPlan plan = planAndRouters.first();
+                 for (int i = 0; i < count; i++) {
 -                    PublicIp sourceNatIp = null;
 -                    if (publicNetwork) {
 +            PublicIp sourceNatIp = null;
 +            if (publicNetwork) {
+                         int failCount = 0;
+                         // Generate different MAC for VR
+                         while (sourceNatIp == null) {
 -                            sourceNatIp = _networkMgr.assignSourceNatIpAddressToGuestNetwork(owner, guestNetwork);
 +                sourceNatIp = _networkMgr.assignSourceNatIpAddressToGuestNetwork(owner, guestNetwork);
+                             NicVO nic = _nicDao.findByMacAddress(sourceNatIp.getMacAddress());
+                             // We got duplicate MAC here, so regenerate the mac
+                             if (nic != null) {
+                                 s_logger.debug("Failed to find a different mac for redundant router. Try again. The current mac is " + sourceNatIp.getMacAddress());
+                                 sourceNatIp = null;
+                                 failCount ++;
+                             }
+                             //Prevent infinite loop
+                             if (failCount > 3) {
+                                 s_logger.error("Failed to find a different mac for redundant router! Abort operation!");
+                                 throw new InsufficientAddressCapacityException("Failed to find a different mac for redundant router", null, offeringId);
+                             }
+                         }
 -                    }
 -                    
 -                    List<Pair<NetworkVO, NicProfile>> networks = createRouterNetworks(owner, isRedundant, plan, guestNetwork,
 -                            new Pair<Boolean, PublicIp>(publicNetwork, sourceNatIp));
 -                    //don't start the router as we are holding the network lock that needs to be released at the end of router allocation
 +            }
 +
-             //3) deploy virtual router(s)
-             int count = routerCount - routers.size();
-             DeploymentPlan plan = planAndRouters.first();
-             for (int i = 0; i < count; i++) {
 +                List<Pair<NetworkVO, NicProfile>> networks = createRouterNetworks(owner, isRedundant, plan, guestNetwork,
 +                        new Pair<Boolean, PublicIp>(publicNetwork, sourceNatIp));
 +                //don't start the router as we are holding the network lock that needs to be released at the end of router allocation
-                 DomainRouterVO router = deployRouter(owner, dest, plan, params, isRedundant, vrProvider, offeringId,
+                     DomainRouterVO router = deployRouter(owner, destination, plan, params, isRedundant, vrProvider, offeringId,
 -                            null, networks, false, null);
 +                        null, networks, false, null);
  
 -                    _routerDao.addRouterToGuestNetwork(router, guestNetwork);
 -                    routers.add(router);
 -                }
 +                _routerDao.addRouterToGuestNetwork(router, guestNetwork);
 +                routers.add(router);
 +            }
+             }
          } finally {
              if (lock != null) {
                  _networkDao.releaseFromLockTable(lock.getId());
@@@ -1448,11 -1599,7 +1601,7 @@@
      protected List<HypervisorType> getHypervisors(DeployDestination dest, DeploymentPlan plan, 
              List<HypervisorType> supportedHypervisors) throws InsufficientServerCapacityException {
          List<HypervisorType> hypervisors = new ArrayList<HypervisorType>();
-         HypervisorType defaults = _resourceMgr.getDefaultHypervisor(dest.getDataCenter().getId());
-         if (defaults != HypervisorType.None) {
-             hypervisors.add(defaults);
-         }
 -        
 +
          if (dest.getCluster() != null) {
              if (dest.getCluster().getHypervisorType() == HypervisorType.Ovm) {
                  hypervisors.add(getClusterToStartDomainRouterForOvm(dest.getCluster().getPodId()));
@@@ -1460,9 -1607,15 +1609,15 @@@
                  hypervisors.add(dest.getCluster().getHypervisorType());
              }
          } else {
+             HypervisorType defaults = _resourceMgr.getDefaultHypervisor(dest.getDataCenter().getId());
+             if (defaults != HypervisorType.None) {
+                 hypervisors.add(defaults);
+             } else {
+                 //if there is no default hypervisor, get it from the cluster
 -                hypervisors = _resourceMgr.getSupportedHypervisorTypes(dest.getDataCenter().getId(), true,
 -                        plan.getPodId());
 -            }
 +            hypervisors = _resourceMgr.getSupportedHypervisorTypes(dest.getDataCenter().getId(), true,
 +                    plan.getPodId());
 +        }
+         }
  
          //keep only elements defined in supported hypervisors
          StringBuilder hTypesStr = new StringBuilder();
@@@ -2856,14 -3023,22 +3025,22 @@@
          DhcpEntryCommand dhcpCommand = new DhcpEntryCommand(nic.getMacAddress(), nic.getIp4Address(), vm.getHostName());
          DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn());
          String gatewayIp = findGatewayIp(vm.getId());
+         boolean needGateway = true;
          if (!gatewayIp.equals(nic.getGateway())) {
+             needGateway = false;
              GuestOSVO guestOS = _guestOSDao.findById(vm.getGuestOSId());
-             // Don't set dhcp:router option for non-default nic on CentOS/RHEL, because they would set routing on wrong interface
-             // This is tricky, we may need to update this when we have more information on various OS's behavior
-             if (guestOS.getDisplayName().startsWith("CentOS") || guestOS.getDisplayName().startsWith("Red Hat Enterprise")) {
-                 gatewayIp = "0.0.0.0";
+             // Do set dhcp:router option for non-default nic on certain OS(including Windows), and leave other OS unset.
+             // Because some OS(e.g. CentOS) would set routing on wrong interface
+             for (String name : _guestOSNeedGatewayOnNonDefaultNetwork) {
+                 if (guestOS.getDisplayName().startsWith(name)) {
+                     needGateway = true;
+                     break;
 -                }
              }
          }
++        }
+         if (!needGateway) {
+             gatewayIp = "0.0.0.0";
+         }
          dhcpCommand.setDefaultRouter(gatewayIp);
          dhcpCommand.setDefaultDns(findDefaultDnsIp(vm.getId()));
  

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java
index 00e767e,1fd710d..2f1f76d
--- a/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java
@@@ -303,10 -301,8 +303,8 @@@ public class VpcVirtualNetworkAppliance
          result = result && _itMgr.removeVmFromNetwork(router, network, null);
          
          if (result) {
-             if (result) {
 -            _routerDao.removeRouterFromGuestNetwork(router.getId(), network.getId());
 -        }
 +                _routerDao.removeRouterFromGuestNetwork(router.getId(), network.getId());
 +            }
-         }
          return result;
      }
      

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/rules/FirewallRuleVO.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/rules/FirewallRuleVO.java
index b64e8bf,63ace5b..99140c3
--- a/server/src/com/cloud/network/rules/FirewallRuleVO.java
+++ b/server/src/com/cloud/network/rules/FirewallRuleVO.java
@@@ -35,10 -34,12 +35,11 @@@ import javax.persistence.InheritanceTyp
  import javax.persistence.Table;
  import javax.persistence.Transient;
  
- import com.cloud.api.Identity;
+ import org.apache.cloudstack.api.Identity;
  import com.cloud.network.dao.FirewallRulesCidrsDaoImpl;
 -import com.cloud.utils.component.ComponentLocator;
  import com.cloud.utils.db.GenericDao;
  import com.cloud.utils.net.NetUtils;
+ import org.apache.cloudstack.api.InternalIdentity;
  
  @Entity
  @Table(name="firewall_rules")

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/rules/RulesManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/rules/RulesManagerImpl.java
index c90466f,dd09174..9f67e05
--- a/server/src/com/cloud/network/rules/RulesManagerImpl.java
+++ b/server/src/com/cloud/network/rules/RulesManagerImpl.java
@@@ -23,13 -23,11 +23,13 @@@ import java.util.Map
  import java.util.Set;
  
  import javax.ejb.Local;
 +import javax.inject.Inject;
  import javax.naming.ConfigurationException;
  
+ import org.apache.cloudstack.api.command.user.firewall.ListPortForwardingRulesCmd;
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
- import com.cloud.api.commands.ListPortForwardingRulesCmd;
  import com.cloud.configuration.ConfigurationManager;
  import com.cloud.domain.dao.DomainDao;
  import com.cloud.event.ActionEvent;
@@@ -63,7 -61,9 +63,8 @@@ import com.cloud.user.AccountManager
  import com.cloud.user.DomainManager;
  import com.cloud.user.UserContext;
  import com.cloud.uservm.UserVm;
+ import com.cloud.utils.Pair;
  import com.cloud.utils.Ternary;
 -import com.cloud.utils.component.Inject;
  import com.cloud.utils.component.Manager;
  import com.cloud.utils.db.DB;
  import com.cloud.utils.db.Filter;
@@@ -80,8 -80,12 +81,13 @@@ import com.cloud.vm.VirtualMachine
  import com.cloud.vm.VirtualMachine.Type;
  import com.cloud.vm.dao.NicDao;
  import com.cloud.vm.dao.UserVmDao;
+ import org.apache.log4j.Logger;
+ 
+ import javax.ejb.Local;
+ import javax.naming.ConfigurationException;
+ import java.util.*;
  
 +@Component
  @Local(value = { RulesManager.class, RulesService.class })
  public class RulesManagerImpl implements RulesManager, RulesService, Manager {
      private static final Logger s_logger = Logger.getLogger(RulesManagerImpl.class);
@@@ -1183,12 -1189,13 +1191,13 @@@
          Network guestNetwork = _networkMgr.getNetwork(ipAddress.getAssociatedWithNetworkId());
          NetworkOffering offering = _configMgr.getNetworkOffering(guestNetwork.getNetworkOfferingId());
          if (offering.getElasticIp()) {
+             if (offering.getAssociatePublicIP()) {
 -                getSystemIpAndEnableStaticNatForVm(_vmDao.findById(vmId), true);
 -                return true;
 -            }
 +            getSystemIpAndEnableStaticNatForVm(_vmDao.findById(vmId), true);
 +            return true;
-         } else {
-             return disableStaticNat(ipId, caller, ctx.getCallerUserId(), false);
          }
 +    }
+         return disableStaticNat(ipId, caller, ctx.getCallerUserId(), false);
+     }
  
      @Override
      public boolean disableStaticNat(long ipId, Account caller, long callerUserId, boolean releaseIpIfElastic) throws ResourceUnavailableException {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/security/SecurityGroupManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/security/SecurityGroupManagerImpl.java
index f0a268b,8e45899..0b0a347
--- a/server/src/com/cloud/network/security/SecurityGroupManagerImpl.java
+++ b/server/src/com/cloud/network/security/SecurityGroupManagerImpl.java
@@@ -34,12 -34,11 +34,13 @@@ import java.util.concurrent.ScheduledEx
  import java.util.concurrent.TimeUnit;
  
  import javax.ejb.Local;
 +import javax.inject.Inject;
  import javax.naming.ConfigurationException;
  
+ import org.apache.cloudstack.api.command.user.securitygroup.*;
  import org.apache.commons.codec.digest.DigestUtils;
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
  import com.cloud.agent.AgentManager;
  import com.cloud.agent.api.NetworkRulesSystemVmCommand;
@@@ -1093,101 -1089,24 +1091,24 @@@ public class SecurityGroupManagerImpl i
          return true;
      }
  
-     @Override
-     public List<SecurityGroupRulesVO> searchForSecurityGroupRules(ListSecurityGroupsCmd cmd) throws PermissionDeniedException, InvalidParameterValueException {
-         Account caller = UserContext.current().getCaller();
-         Long instanceId = cmd.getVirtualMachineId();
-         String securityGroup = cmd.getSecurityGroupName();
-         Long id = cmd.getId();
-         Object keyword = cmd.getKeyword();
-         List<Long> permittedAccounts = new ArrayList<Long>();
-         Map<String, String> tags = cmd.getTags();
- 
-         if (instanceId != null) {
-             UserVmVO userVM = _userVMDao.findById(instanceId);
-             if (userVM == null) {
-                 throw new InvalidParameterValueException("Unable to list network groups for virtual machine instance " + instanceId + "; instance not found.");
-             }
-             _accountMgr.checkAccess(caller, null, true, userVM);
-             return listSecurityGroupRulesByVM(instanceId.longValue());
-         }
- 
-         List<SecurityGroupRulesVO> securityRulesList = new ArrayList<SecurityGroupRulesVO>();
-         
-         Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
-         _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
-         Long domainId = domainIdRecursiveListProject.first();
-         Boolean isRecursive = domainIdRecursiveListProject.second();
-         ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();        
-         
-         Filter searchFilter = new Filter(SecurityGroupVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
-         SearchBuilder<SecurityGroupVO> sb = _securityGroupDao.createSearchBuilder();
-         _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
- 
-         sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
-         sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
-         
-         if (tags != null && !tags.isEmpty()) {
-             SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
-             for (int count=0; count < tags.size(); count++) {
-                 tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
-                 tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
-                 tagSearch.cp();
-             }
-             tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
-             sb.groupBy(sb.entity().getId());
-             sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
-         }
- 
-         SearchCriteria<SecurityGroupVO> sc = sb.create();
-         _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
- 
-         if (id != null) {
-             sc.setParameters("id", id);
-         }
-         
-         if (tags != null && !tags.isEmpty()) {
-             int count = 0;
-             sc.setJoinParameters("tagSearch", "resourceType", TaggedResourceType.SecurityGroup.toString());
-             for (String key : tags.keySet()) {
-                 sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
-                 sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
-                 count++;
-             }
-         }
- 
-         if (securityGroup != null) {
-             sc.setParameters("name", securityGroup);
-         }
- 
-         if (keyword != null) {
-             SearchCriteria<SecurityGroupRulesVO> ssc = _securityGroupRulesDao.createSearchCriteria();
-             ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
-             ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%");
-             sc.addAnd("name", SearchCriteria.Op.SC, ssc);
-         }
  
-         List<SecurityGroupVO> securityGroups = _securityGroupDao.search(sc, searchFilter);
-         for (SecurityGroupVO group : securityGroups) {
-             securityRulesList.addAll(_securityGroupRulesDao.listSecurityRulesByGroupId(group.getId()));
-         }
  
-         return securityRulesList;
-     }
- 
-     private List<SecurityGroupRulesVO> listSecurityGroupRulesByVM(long vmId) {
-         List<SecurityGroupRulesVO> results = new ArrayList<SecurityGroupRulesVO>();
-         List<SecurityGroupVMMapVO> networkGroupMappings = _securityGroupVMMapDao.listByInstanceId(vmId);
-         if (networkGroupMappings != null) {
-             for (SecurityGroupVMMapVO networkGroupMapping : networkGroupMappings) {
-                 SecurityGroupVO group = _securityGroupDao.findById(networkGroupMapping.getSecurityGroupId());
-                 List<SecurityGroupRulesVO> rules = _securityGroupRulesDao.listSecurityGroupRules(group.getAccountId(), networkGroupMapping.getGroupName());
-                 if (rules != null) {
-                     results.addAll(rules);
-                 }
+     private Pair<List<SecurityGroupJoinVO>, Integer> listSecurityGroupRulesByVM(long vmId, long pageInd, long pageSize) {
+         Filter sf = new Filter(SecurityGroupVMMapVO.class, null, true, pageInd, pageSize);
+         Pair<List<SecurityGroupVMMapVO>, Integer> sgVmMappingPair = _securityGroupVMMapDao.listByInstanceId(vmId, sf);
+         Integer count = sgVmMappingPair.second();
+         if (count.intValue() == 0) {
+             // handle empty result cases
+             return new Pair<List<SecurityGroupJoinVO>, Integer>(new ArrayList<SecurityGroupJoinVO>(), count);
 -        }
 +            }
+         List<SecurityGroupVMMapVO> sgVmMappings = sgVmMappingPair.first();
+         Long[] sgIds = new Long[sgVmMappings.size()];
+         int i = 0;
+         for (SecurityGroupVMMapVO sgVm : sgVmMappings) {
+             sgIds[i++] = sgVm.getSecurityGroupId();
          }
-         return results;
+         List<SecurityGroupJoinVO> sgs = _securityGroupJoinDao.searchByIds(sgIds);
+         return new Pair<List<SecurityGroupJoinVO>, Integer>(sgs, count);
      }
  
      @Override

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/security/dao/SecurityGroupVMMapDaoImpl.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java
index 0e3d314,4697acb..cfeb15a
--- a/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java
+++ b/server/src/com/cloud/network/vpc/NetworkACLManagerImpl.java
@@@ -21,14 -21,12 +21,14 @@@ import java.util.List
  import java.util.Map;
  
  import javax.ejb.Local;
 +import javax.inject.Inject;
  import javax.naming.ConfigurationException;
  
+ import org.apache.cloudstack.api.command.user.network.ListNetworkACLsCmd;
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
- import com.cloud.acl.SecurityChecker.AccessType;
- import com.cloud.api.commands.ListNetworkACLsCmd;
+ import org.apache.cloudstack.acl.SecurityChecker.AccessType;
  import com.cloud.event.ActionEvent;
  import com.cloud.event.EventTypes;
  import com.cloud.exception.InvalidParameterValueException;
@@@ -53,7 -51,9 +53,8 @@@ import com.cloud.tags.dao.ResourceTagDa
  import com.cloud.user.Account;
  import com.cloud.user.AccountManager;
  import com.cloud.user.UserContext;
+ import com.cloud.utils.Pair;
  import com.cloud.utils.Ternary;
 -import com.cloud.utils.component.Inject;
  import com.cloud.utils.component.Manager;
  import com.cloud.utils.db.DB;
  import com.cloud.utils.db.Filter;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/vpc/VpcManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/vpc/VpcManagerImpl.java
index 876e520,7448cc3..1412d08
--- a/server/src/com/cloud/network/vpc/VpcManagerImpl.java
+++ b/server/src/com/cloud/network/vpc/VpcManagerImpl.java
@@@ -28,15 -28,13 +28,15 @@@ import java.util.concurrent.ScheduledEx
  import java.util.concurrent.TimeUnit;
  
  import javax.ejb.Local;
 +import javax.inject.Inject;
  import javax.naming.ConfigurationException;
  
+ import org.apache.cloudstack.api.command.user.vpc.ListStaticRoutesCmd;
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
- import com.cloud.acl.ControlledEntity.ACLType;
- import com.cloud.api.commands.ListPrivateGatewaysCmd;
- import com.cloud.api.commands.ListStaticRoutesCmd;
+ import org.apache.cloudstack.acl.ControlledEntity.ACLType;
+ import org.apache.cloudstack.api.command.user.vpc.ListPrivateGatewaysCmd;
  import com.cloud.configuration.Config;
  import com.cloud.configuration.ConfigurationManager;
  import com.cloud.configuration.Resource.ResourceType;
@@@ -97,8 -95,10 +97,9 @@@ import com.cloud.user.ResourceLimitServ
  import com.cloud.user.User;
  import com.cloud.user.UserContext;
  import com.cloud.utils.NumbersUtil;
+ import com.cloud.utils.Pair;
  import com.cloud.utils.Ternary;
  import com.cloud.utils.component.ComponentLocator;
 -import com.cloud.utils.component.Inject;
  import com.cloud.utils.component.Manager;
  import com.cloud.utils.concurrency.NamedThreadFactory;
  import com.cloud.utils.db.DB;
@@@ -625,9 -624,9 +626,9 @@@ public class VpcManagerImpl implements 
          }
          
          //verify permissions
-         _accountMgr.checkAccess(caller, null, false, vpc);
+         _accountMgr.checkAccess(ctx.getCaller(), null, false, vpc);
 -
 +        
-         return destroyVpc(vpc);
+         return destroyVpc(vpc, ctx.getCaller(), ctx.getCallerUserId());
      }
  
      @Override
@@@ -1443,14 -1442,14 +1444,14 @@@
          if (vlan != null) {
              sc.setJoinParameters("networkSearch", "vlan", BroadcastDomainType.Vlan.toUri(vlan));
          }
 -
 +       
-         List<VpcGatewayVO> vos = _vpcGatewayDao.search(sc, searchFilter);
-         List<PrivateGateway> privateGtws = new ArrayList<PrivateGateway>(vos.size());
-         for (VpcGateway vo : vos) {
+         Pair<List<VpcGatewayVO>, Integer> vos = _vpcGatewayDao.searchAndCount(sc, searchFilter);
+         List<PrivateGateway> privateGtws = new ArrayList<PrivateGateway>(vos.first().size());
+         for (VpcGateway vo : vos.first()) {
              privateGtws.add(getPrivateGatewayProfile(vo));
          }
 -
 +        
-         return privateGtws;
+         return new Pair<List<PrivateGateway>, Integer>(privateGtws, vos.second());
      }
      
      @Override
@@@ -1675,10 -1674,11 +1676,11 @@@
                  count++;
              }   
          }
 -
 +        
-         return _staticRouteDao.search(sc, searchFilter);
+         Pair<List<StaticRouteVO>, Integer> result = _staticRouteDao.searchAndCount(sc, searchFilter);
+         return new Pair<List<? extends StaticRoute>, Integer>(result.first(), result.second());
      }
 -
 +    
      protected void detectRoutesConflict(StaticRoute newRoute) throws NetworkRuleConflictException {
          List<? extends StaticRoute> routes = _staticRouteDao.listByGatewayIdAndNotRevoked(newRoute.getVpcGatewayId());
          assert (routes.size() >= 1) : "For static routes, we now always first persist the route and then check for " +

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java
index 38ccd2e,67d6b8c..76bf057
--- a/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java
+++ b/server/src/com/cloud/network/vpn/RemoteAccessVpnManagerImpl.java
@@@ -21,14 -21,12 +21,14 @@@ import java.util.List
  import java.util.Map;
  
  import javax.ejb.Local;
 +import javax.inject.Inject;
  import javax.naming.ConfigurationException;
  
+ import org.apache.cloudstack.api.command.user.vpn.ListVpnUsersCmd;
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
- import com.cloud.api.commands.ListRemoteAccessVpnsCmd;
- import com.cloud.api.commands.ListVpnUsersCmd;
+ import org.apache.cloudstack.api.command.user.vpn.ListRemoteAccessVpnsCmd;
  import com.cloud.configuration.Config;
  import com.cloud.configuration.dao.ConfigurationDao;
  import com.cloud.domain.DomainVO;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/network/vpn/Site2SiteVpnManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/vpn/Site2SiteVpnManagerImpl.java
index ddc79a0,5416db7..9b2c049
--- a/server/src/com/cloud/network/vpn/Site2SiteVpnManagerImpl.java
+++ b/server/src/com/cloud/network/vpn/Site2SiteVpnManagerImpl.java
@@@ -25,19 -24,18 +25,19 @@@ import javax.inject.Inject
  import javax.naming.ConfigurationException;
  
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
- import com.cloud.api.commands.CreateVpnConnectionCmd;
- import com.cloud.api.commands.CreateVpnCustomerGatewayCmd;
- import com.cloud.api.commands.CreateVpnGatewayCmd;
- import com.cloud.api.commands.DeleteVpnConnectionCmd;
- import com.cloud.api.commands.DeleteVpnCustomerGatewayCmd;
- import com.cloud.api.commands.DeleteVpnGatewayCmd;
- import com.cloud.api.commands.ListVpnConnectionsCmd;
- import com.cloud.api.commands.ListVpnCustomerGatewaysCmd;
- import com.cloud.api.commands.ListVpnGatewaysCmd;
- import com.cloud.api.commands.ResetVpnConnectionCmd;
- import com.cloud.api.commands.UpdateVpnCustomerGatewayCmd;
+ import org.apache.cloudstack.api.command.user.vpn.CreateVpnConnectionCmd;
+ import org.apache.cloudstack.api.command.user.vpn.CreateVpnCustomerGatewayCmd;
+ import org.apache.cloudstack.api.command.user.vpn.CreateVpnGatewayCmd;
+ import org.apache.cloudstack.api.command.user.vpn.DeleteVpnConnectionCmd;
+ import org.apache.cloudstack.api.command.user.vpn.DeleteVpnCustomerGatewayCmd;
+ import org.apache.cloudstack.api.command.user.vpn.DeleteVpnGatewayCmd;
+ import org.apache.cloudstack.api.command.user.vpn.ListVpnConnectionsCmd;
+ import org.apache.cloudstack.api.command.user.vpn.ListVpnCustomerGatewaysCmd;
+ import org.apache.cloudstack.api.command.user.vpn.ListVpnGatewaysCmd;
+ import org.apache.cloudstack.api.command.user.vpn.ResetVpnConnectionCmd;
+ import org.apache.cloudstack.api.command.user.vpn.UpdateVpnCustomerGatewayCmd;
  import com.cloud.configuration.Config;
  import com.cloud.configuration.dao.ConfigurationDao;
  import com.cloud.event.ActionEvent;
@@@ -67,10 -65,11 +67,10 @@@ import com.cloud.user.Account
  import com.cloud.user.AccountManager;
  import com.cloud.user.UserContext;
  import com.cloud.user.dao.AccountDao;
- import com.cloud.user.dao.UserStatisticsDao;
  import com.cloud.utils.NumbersUtil;
+ import com.cloud.utils.Pair;
  import com.cloud.utils.Ternary;
  import com.cloud.utils.component.ComponentLocator;
 -import com.cloud.utils.component.Inject;
  import com.cloud.utils.component.Manager;
  import com.cloud.utils.db.DB;
  import com.cloud.utils.db.Filter;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/projects/ProjectManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/projects/ProjectManagerImpl.java
index 52b9fca,15d3675..ab7b7b1
--- a/server/src/com/cloud/projects/ProjectManagerImpl.java
+++ b/server/src/com/cloud/projects/ProjectManagerImpl.java
@@@ -40,9 -38,11 +39,12 @@@ import javax.mail.internet.InternetAddr
  import javax.naming.ConfigurationException;
  
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
- import com.cloud.acl.SecurityChecker.AccessType;
+ import org.apache.cloudstack.acl.SecurityChecker.AccessType;
+ import com.cloud.api.query.dao.ProjectAccountJoinDao;
+ import com.cloud.api.query.dao.ProjectInvitationJoinDao;
+ import com.cloud.api.query.dao.ProjectJoinDao;
  import com.cloud.configuration.Config;
  import com.cloud.configuration.ConfigurationManager;
  import com.cloud.configuration.Resource.ResourceType;
@@@ -117,8 -112,10 +115,10 @@@ public class ProjectManagerImpl impleme
      @Inject
      private ProjectInvitationDao _projectInvitationDao;
      @Inject
+     private ProjectInvitationJoinDao _projectInvitationJoinDao;
+     @Inject
      protected ResourceTagDao _resourceTagDao;
 -
 +    
      protected boolean _invitationRequired = false;
      protected long _invitationTimeOut = 86400000;
      protected boolean _allowUserToCreateProject = true;
@@@ -280,12 -277,13 +280,13 @@@
          Account projectOwner = getProjectOwner(project.getId());
          if (projectOwner != null) {
              _resourceLimitMgr.decrementResourceCount(projectOwner.getId(), ResourceType.project);
 -        }
 -
 +        } 
 +        
          txn.commit();
 -
 +        
          if (updateResult) {
-             if (!cleanupProject(project, _accountDao.findById(caller.getId()), callerUserId)) {
+             //pass system caller when clenaup projects account
+             if (!cleanupProject(project, _accountDao.findById(Account.ACCOUNT_ID_SYSTEM), User.UID_SYSTEM)) {
                  s_logger.warn("Failed to cleanup project's id=" + project.getId() + " resources, not removing the project yet");
                  return false;
              } else {
@@@ -352,128 -350,14 +353,14 @@@
      public ProjectVO getProject (long projectId) {
          return _projectDao.findById(projectId);
      }
 -
 -
 +    
-     @Override
-     public List<? extends Project> listProjects(Long id, String name, String displayText, String state, 
-             String accountName, Long domainId, String keyword, Long startIndex, Long pageSize, boolean listAll, 
-             boolean isRecursive, Map<String, String> tags) {
-         Account caller = UserContext.current().getCaller();
-         Long accountId = null;
-         String path = null;
-         
-         Filter searchFilter = new Filter(ProjectVO.class, "id", false, startIndex, pageSize);
-         SearchBuilder<ProjectVO> sb = _projectDao.createSearchBuilder();
-         
-         if (_accountMgr.isAdmin(caller.getType())) {
-             if (domainId != null) {
-                 DomainVO domain = _domainDao.findById(domainId);
-                 if (domain == null) {
-                     throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist in the system");
-                 }
- 
-                 _accountMgr.checkAccess(caller, domain);
- 
-                 if (accountName != null) {
-                     Account owner = _accountMgr.getActiveAccountByName(accountName, domainId);
-                     if (owner == null) {
-                         throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
-                     }
-                     accountId = owner.getId();
-                 }
-             }
-         } else {
-             if (accountName != null && !accountName.equals(caller.getAccountName())) {
-                 throw new PermissionDeniedException("Can't list account " + accountName + " projects; unauthorized");
-             }
-             
-             if (domainId != null && domainId.equals(caller.getDomainId())) {
-                 throw new PermissionDeniedException("Can't list domain id= " + domainId + " projects; unauthorized");
-             }
-             
-             accountId = caller.getId();
-         }
-         
-     	if (domainId == null && accountId == null && (caller.getType() == Account.ACCOUNT_TYPE_NORMAL || !listAll)) {
-     		accountId = caller.getId();
-     	} else if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || (isRecursive && !listAll)) {
-             DomainVO domain = _domainDao.findById(caller.getDomainId());
-             path = domain.getPath();
-         }
-         
-         if (path != null) {
-             SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
-             domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
-             sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER);
-         }
-         
-         if (accountId != null) {
-             SearchBuilder<ProjectAccountVO> projectAccountSearch = _projectAccountDao.createSearchBuilder();
-             projectAccountSearch.and("accountId", projectAccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
-             sb.join("projectAccountSearch", projectAccountSearch, sb.entity().getId(), projectAccountSearch.entity().getProjectId(), JoinBuilder.JoinType.INNER);
-         }
-         
-         if (tags != null && !tags.isEmpty()) {
-             SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
-                 for (int count=0; count < tags.size(); count++) {
-                     tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), SearchCriteria.Op.EQ);
-                     tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), SearchCriteria.Op.EQ);
-                     tagSearch.cp();
-                 }
-                 tagSearch.and("resourceType", tagSearch.entity().getResourceType(), SearchCriteria.Op.EQ);
-                 sb.groupBy(sb.entity().getId());
-                 sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
-         }
-         
-         SearchCriteria<ProjectVO> sc = sb.create();
-         
-         if (id != null) {
-             sc.addAnd("id", Op.EQ, id);
-         }
-         
-         if (domainId != null && !isRecursive) {
-             sc.addAnd("domainId", Op.EQ, domainId);
-         }
-         
-         if (name != null) {
-             sc.addAnd("name", Op.EQ, name);
-         }
-         
-         if (displayText != null) {
-             sc.addAnd("displayText", Op.EQ, displayText);
-         }
-         
-         if (accountId != null) {
-             sc.setJoinParameters("projectAccountSearch", "accountId", accountId);
-         }
-         
-         if (state != null) {
-             sc.addAnd("state", Op.EQ, state);
-         }
-         
-         if (keyword != null) {
-             SearchCriteria<ProjectVO> ssc = _projectDao.createSearchCriteria();
-             ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
-             ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%");
-             sc.addAnd("name", SearchCriteria.Op.SC, ssc);
-         }
-         
-         if (path != null) {
-             sc.setJoinParameters("domainSearch", "path", path);
-         }
 +        
-         if (tags != null && !tags.isEmpty()) {
-             int count = 0;
-             sc.setJoinParameters("tagSearch", "resourceType", TaggedResourceType.Project.toString());
-             for (String key : tags.keySet()) {
-                 sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
-                 sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
-                 count++;
-             }
+     @Override
+     public long getInvitationTimeout() {
+         return _invitationTimeOut;
 -    }
 -
 -
 +        }
 +        
-         return _projectDao.search(sc, searchFilter);
-     }
 +    
      @Override
      public ProjectAccount assignAccountToProject(Project project, long accountId, ProjectAccount.Role accountRole) {
          return _projectAccountDao.persist(new ProjectAccountVO(project, accountId, accountRole));
@@@ -726,65 -610,24 +613,24 @@@
          if (projectAccount == null) {
          	InvalidParameterValueException ex = new InvalidParameterValueException("Account " + accountName + " is not assigned to the project with specified id");
          	// Use the projectVO object and not the projectAccount object to inject the projectId.
 -		ex.addProxyObject(project, projectId, "projectId");
 +        	ex.addProxyObject(project, projectId, "projectId");
              throw ex;
          }
 -
 +        
          //can't remove the owner of the project
          if (projectAccount.getAccountRole() == Role.Admin) {
 -		InvalidParameterValueException ex = new InvalidParameterValueException("Unable to delete account " + accountName + " from the project with specified id as the account is the owner of the project");
 -		ex.addProxyObject(project, projectId, "projectId");
 +        	InvalidParameterValueException ex = new InvalidParameterValueException("Unable to delete account " + accountName + " from the project with specified id as the account is the owner of the project");
 +        	ex.addProxyObject(project, projectId, "projectId");
              throw ex;
          }
 -
 +        
          return deleteAccountFromProject(projectId, account.getId());
      }
 -
 -
 -
 -
 -    public ProjectInvitation createAccountInvitation(Project project, Long accountId) {
 +    
 +    
-     @Override
-     public List<? extends ProjectAccount> listProjectAccounts(long projectId, String accountName, String role, Long startIndex, Long pageSizeVal) {
-         Account caller = UserContext.current().getCaller();
-         
-         //check that the project exists
-         Project project = getProject(projectId);
-         
-         if (project == null) {
-             throw new InvalidParameterValueException("Unable to find the project id=" + projectId);
-         }
-         
-         //verify permissions - only accounts belonging to the project can list project's account
-         if (!_accountMgr.isAdmin(caller.getType()) && _projectAccountDao.findByProjectIdAccountId(projectId, caller.getAccountId()) == null) {
-             throw new PermissionDeniedException("Account " + caller + " is not authorized to list users of the project id=" + projectId);
-         }
-         
-         Filter searchFilter = new Filter(ProjectAccountVO.class, "id", false, startIndex, pageSizeVal);
-         SearchBuilder<ProjectAccountVO> sb = _projectAccountDao.createSearchBuilder();
-         sb.and("accountRole", sb.entity().getAccountRole(), Op.EQ);
-         sb.and("projectId", sb.entity().getProjectId(), Op.EQ);
-         
-         SearchBuilder<AccountVO> accountSearch;
-         if (accountName != null) {
-             accountSearch = _accountDao.createSearchBuilder();
-             accountSearch.and("accountName", accountSearch.entity().getAccountName(), SearchCriteria.Op.EQ);
-             sb.join("accountSearch", accountSearch, sb.entity().getAccountId(), accountSearch.entity().getId(), JoinBuilder.JoinType.INNER);
-         }
 +        
-         SearchCriteria<ProjectAccountVO> sc = sb.create();
-         
-         sc.setParameters("projectId", projectId);
-         
-         if (role != null) {
-             sc.setParameters("accountRole", role);
-         }
-         
-         if (accountName != null) {
-             sc.setJoinParameters("accountSearch", "accountName", accountName);
-         }
-         
-         return _projectAccountDao.search(sc, searchFilter);
-     }
 +    
 +    public ProjectInvitation createAccountInvitation(Project project, Long accountId) { 
          if (activeInviteExists(project, accountId, null)) {
              throw new InvalidParameterValueException("There is already a pending invitation for account id=" + accountId + " to the project id=" + project);
          }
@@@ -851,51 -694,9 +697,9 @@@
          invite.setState(ProjectInvitation.State.Expired);
          return _projectInvitationDao.update(invite.getId(), invite);
      }
 +    
-     @Override
-     public List<? extends ProjectInvitation> listProjectInvitations(Long id, Long projectId, String accountName, Long domainId, String state, boolean activeOnly, Long startIndex, Long pageSizeVal, boolean isRecursive, boolean listAll) {
-     	Account caller = UserContext.current().getCaller();
-         List<Long> permittedAccounts = new ArrayList<Long>();
-         
-         Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, isRecursive, null);
-         _accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, true);
-         domainId = domainIdRecursiveListProject.first();
-         isRecursive = domainIdRecursiveListProject.second();
-         ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();        
-         
-         Filter searchFilter = new Filter(ProjectInvitationVO.class, "id", true, startIndex, pageSizeVal);
-         SearchBuilder<ProjectInvitationVO> sb = _projectInvitationDao.createSearchBuilder();
-         _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
- 
-         sb.and("projectId", sb.entity().getProjectId(), SearchCriteria.Op.EQ);
-         sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
-         sb.and("created", sb.entity().getCreated(), SearchCriteria.Op.GT);
-         sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
- 
-         SearchCriteria<ProjectInvitationVO> sc = sb.create();
-         _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
  
-         
-         if (projectId != null){
-             sc.setParameters("projectId", projectId);
-         }
-         
-         if (state != null) {
-             sc.setParameters("state", state);
-         }
-         
-         if (id != null) {
-             sc.setParameters("id", id);
-         }
-         
-         if (activeOnly) {
-             sc.setParameters("state", ProjectInvitation.State.Pending);
-             sc.setParameters("created", new Date((DateUtil.currentGMTTime().getTime()) - _invitationTimeOut));
-         }
-         
-         return _projectInvitationDao.search(sc, searchFilter);
-     }
 -
 -
 +    
      @Override @DB
      @ActionEvent(eventType = EventTypes.EVENT_PROJECT_INVITATION_UPDATE, eventDescription = "updating project invitation", async=true)
      public boolean updateInvitation(long projectId, String accountName, String token, boolean accept) {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/resource/ResourceManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/resource/ResourceManagerImpl.java
index b4f6d14,ffc09f0..cbcdccb
--- a/server/src/com/cloud/resource/ResourceManagerImpl.java
+++ b/server/src/com/cloud/resource/ResourceManagerImpl.java
@@@ -27,13 -27,20 +27,23 @@@ import java.util.List
  import java.util.Map;
  import java.util.Set;
  
 +import javax.annotation.PostConstruct;
  import javax.ejb.Local;
 +import javax.inject.Inject;
  import javax.naming.ConfigurationException;
  
+ import org.apache.cloudstack.api.command.admin.cluster.AddClusterCmd;
+ import org.apache.cloudstack.api.command.admin.storage.ListS3sCmd;
+ import org.apache.cloudstack.api.command.admin.swift.AddSwiftCmd;
+ import org.apache.cloudstack.api.command.admin.cluster.DeleteClusterCmd;
+ import org.apache.cloudstack.api.command.admin.host.*;
+ import org.apache.cloudstack.api.command.admin.swift.ListSwiftsCmd;
+ import org.apache.cloudstack.api.command.admin.storage.AddS3Cmd;
+ import com.cloud.storage.S3;
+ import com.cloud.storage.S3VO;
+ import com.cloud.storage.s3.S3Manager;
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
  import com.cloud.agent.AgentManager;
  import com.cloud.agent.AgentManager.TapAgentsAction;
@@@ -133,8 -132,8 +135,7 @@@ import com.cloud.user.UserContext
  import com.cloud.utils.Pair;
  import com.cloud.utils.StringUtils;
  import com.cloud.utils.UriUtils;
- import com.cloud.utils.cisco.n1kv.vsm.NetconfHelper;
  import com.cloud.utils.component.Adapters;
 -import com.cloud.utils.component.Inject;
  import com.cloud.utils.component.Manager;
  import com.cloud.utils.db.DB;
  import com.cloud.utils.db.SearchCriteria;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
index 53e260b,c17b0ea..3566834
--- a/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
+++ b/server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java
@@@ -30,9 -29,8 +30,9 @@@ import javax.inject.Inject
  import javax.naming.ConfigurationException;
  
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
- import com.cloud.acl.SecurityChecker.AccessType;
+ import org.apache.cloudstack.acl.SecurityChecker.AccessType;
  import com.cloud.alert.AlertManager;
  import com.cloud.configuration.Config;
  import com.cloud.configuration.Resource;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/server/ConfigurationServerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/server/ConfigurationServerImpl.java
index 781305b,43a1d99..28f28cb
--- a/server/src/com/cloud/server/ConfigurationServerImpl.java
+++ b/server/src/com/cloud/server/ConfigurationServerImpl.java
@@@ -106,8 -71,21 +71,22 @@@ import com.cloud.utils.exception.CloudR
  import com.cloud.utils.net.NetUtils;
  import com.cloud.utils.script.Script;
  import com.cloud.uuididentity.dao.IdentityDao;
+ import org.apache.commons.codec.binary.Base64;
+ import org.apache.log4j.Logger;
+ 
+ import javax.crypto.KeyGenerator;
+ import javax.crypto.SecretKey;
+ import java.io.*;
+ import java.net.InetAddress;
+ import java.net.UnknownHostException;
+ import java.security.NoSuchAlgorithmException;
+ import java.sql.PreparedStatement;
+ import java.sql.ResultSet;
+ import java.sql.SQLException;
+ import java.util.*;
+ import java.util.regex.Pattern;
  
 +@Component
  public class ConfigurationServerImpl implements ConfigurationServer {
      public static final Logger s_logger = Logger.getLogger(ConfigurationServerImpl.class.getName());
  

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/server/ManagementServer.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/server/ManagementServer.java
index 0b77acd,29c76e0..8655d15
--- a/server/src/com/cloud/server/ManagementServer.java
+++ b/server/src/com/cloud/server/ManagementServer.java
@@@ -45,9 -44,7 +46,7 @@@ public interface ManagementServer exten
      */
      @Override
      String getVersion();
 -
 +    
-     String[] getApiConfig();
- 
      /**
       * Retrieves a host by id
       * 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/server/ManagementServerExtImpl.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/server/ManagementServerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/server/ManagementServerImpl.java
index d98825b,22ae179..871d0d3
--- a/server/src/com/cloud/server/ManagementServerImpl.java
+++ b/server/src/com/cloud/server/ManagementServerImpl.java
@@@ -39,16 -39,35 +39,38 @@@ import java.util.concurrent.Executors
  import java.util.concurrent.ScheduledExecutorService;
  import java.util.concurrent.TimeUnit;
  
 +import javax.annotation.PostConstruct;
  import javax.crypto.Mac;
  import javax.crypto.spec.SecretKeySpec;
 +import javax.inject.Inject;
  
+ import org.apache.cloudstack.api.BaseUpdateTemplateOrIsoCmd;
+ import org.apache.cloudstack.api.command.admin.cluster.ListClustersCmd;
+ import org.apache.cloudstack.api.command.admin.config.ListCfgsByCmd;
+ import org.apache.cloudstack.api.command.admin.resource.ListAlertsCmd;
+ import org.apache.cloudstack.api.command.admin.storage.ListStoragePoolsCmd;
+ import org.apache.cloudstack.api.command.admin.systemvm.DestroySystemVmCmd;
+ import org.apache.cloudstack.api.command.admin.systemvm.UpgradeSystemVMCmd;
+ import org.apache.cloudstack.api.command.user.address.ListPublicIpAddressesCmd;
+ import org.apache.cloudstack.api.command.user.config.ListCapabilitiesCmd;
+ import org.apache.cloudstack.api.command.user.guest.ListGuestOsCategoriesCmd;
+ import org.apache.cloudstack.api.command.user.guest.ListGuestOsCmd;
+ import org.apache.cloudstack.api.command.user.iso.ListIsosCmd;
+ import org.apache.cloudstack.api.command.user.iso.UpdateIsoCmd;
+ import org.apache.cloudstack.api.command.user.offering.ListDiskOfferingsCmd;
+ import org.apache.cloudstack.api.command.user.offering.ListServiceOfferingsCmd;
+ import org.apache.cloudstack.api.command.user.ssh.ListSSHKeyPairsCmd;
+ import org.apache.cloudstack.api.command.user.ssh.DeleteSSHKeyPairCmd;
+ import org.apache.cloudstack.api.command.user.ssh.RegisterSSHKeyPairCmd;
+ import org.apache.cloudstack.api.command.user.template.ListTemplatesCmd;
+ import org.apache.cloudstack.api.command.user.template.UpdateTemplateCmd;
+ import org.apache.cloudstack.api.command.user.vm.GetVMPasswordCmd;
+ import org.apache.cloudstack.api.command.user.zone.ListZonesByCmd;
  import org.apache.commons.codec.binary.Base64;
  import org.apache.log4j.Logger;
 +import org.springframework.stereotype.Component;
  
- import com.cloud.acl.SecurityChecker.AccessType;
+ import org.apache.cloudstack.acl.SecurityChecker.AccessType;
  import com.cloud.agent.AgentManager;
  import com.cloud.agent.api.GetVncPortAnswer;
  import com.cloud.agent.api.GetVncPortCommand;
@@@ -336,13 -345,71 +351,75 @@@ public class ManagementServerImpl imple
  
      private String _hashKey = null;
  
-     public ManagementServerImpl() {
+     protected ManagementServerImpl() {
+         ComponentLocator locator = ComponentLocator.getLocator(Name);
+         _configDao = locator.getDao(ConfigurationDao.class);
+         _routerDao = locator.getDao(DomainRouterDao.class);
+         _routerJoinDao = locator.getDao(DomainRouterJoinDao.class);
+         _eventDao = locator.getDao(EventDao.class);
+         _eventJoinDao = locator.getDao(EventJoinDao.class);
+         _dcDao = locator.getDao(DataCenterDao.class);
+         _vlanDao = locator.getDao(VlanDao.class);
+         _accountVlanMapDao = locator.getDao(AccountVlanMapDao.class);
+         _podVlanMapDao = locator.getDao(PodVlanMapDao.class);
+         _hostDao = locator.getDao(HostDao.class);
+         _detailsDao = locator.getDao(HostDetailsDao.class);
+         _hostPodDao = locator.getDao(HostPodDao.class);
+         _jobDao = locator.getDao(AsyncJobDao.class);
+         _clusterDao = locator.getDao(ClusterDao.class);
+         _nicDao = locator.getDao(NicDao.class);
+         _networkDao = locator.getDao(NetworkDao.class);
+         _loadbalancerDao = locator.getDao(LoadBalancerDao.class);
+ 
+         _accountMgr = locator.getManager(AccountManager.class);
+         _agentMgr = locator.getManager(AgentManager.class);
+         _alertMgr = locator.getManager(AlertManager.class);
+         _consoleProxyMgr = locator.getManager(ConsoleProxyManager.class);
+         _secStorageVmMgr = locator.getManager(SecondaryStorageVmManager.class);
+         _swiftMgr = locator.getManager(SwiftManager.class);
+         _s3Mgr = locator.getManager(S3Manager.class);
+         _storageMgr = locator.getManager(StorageManager.class);
+         _publicIpAddressDao = locator.getDao(IPAddressDao.class);
+         _consoleProxyDao = locator.getDao(ConsoleProxyDao.class);
+         _secStorageVmDao = locator.getDao(SecondaryStorageVmDao.class);
+         _userDao = locator.getDao(UserDao.class);
+         _userVmDao = locator.getDao(UserVmDao.class);
+         _offeringsDao = locator.getDao(ServiceOfferingDao.class);
+         _diskOfferingDao = locator.getDao(DiskOfferingDao.class);
+         _templateDao = locator.getDao(VMTemplateDao.class);
+         _domainDao = locator.getDao(DomainDao.class);
+         _accountDao = locator.getDao(AccountDao.class);
+         _alertDao = locator.getDao(AlertDao.class);
+         _capacityDao = locator.getDao(CapacityDao.class);
+         _guestOSDao = locator.getDao(GuestOSDao.class);
+         _guestOSCategoryDao = locator.getDao(GuestOSCategoryDao.class);
+         _poolDao = locator.getDao(StoragePoolDao.class);
+         _vmGroupDao = locator.getDao(InstanceGroupDao.class);
+         _vmGroupJoinDao = locator.getDao(InstanceGroupJoinDao.class);
+         _uploadDao = locator.getDao(UploadDao.class);
+         _configs = _configDao.getConfiguration();
+         _vmInstanceDao = locator.getDao(VMInstanceDao.class);
+         _volumeDao = locator.getDao(VolumeDao.class);
+         _asyncMgr = locator.getManager(AsyncJobManager.class);
+         _uploadMonitor = locator.getManager(UploadMonitor.class);
+         _sshKeyPairDao = locator.getDao(SSHKeyPairDao.class);
+         _itMgr = locator.getManager(VirtualMachineManager.class);
+         _ksMgr = locator.getManager(KeystoreManager.class);
+         _resourceMgr = locator.getManager(ResourceManager.class);
+         _configMgr = locator.getManager(ConfigurationManager.class);
+         _resourceTagDao = locator.getDao(ResourceTagDao.class);
+ 
+         _hypervisorCapabilitiesDao = locator.getDao(HypervisorCapabilitiesDao.class);
+ 
+         _hostAllocators = locator.getAdapters(HostAllocator.class);
+         if (_hostAllocators == null || !_hostAllocators.isSet()) {
+             s_logger.error("Unable to find HostAllocators");
 -        }
 -
 +    }
 +    
 +    @PostConstruct
 +    void init() {
 +         _configs = _configDao.getConfiguration();
 + 
          String value = _configs.get("event.purge.interval");
          int cleanup = NumbersUtil.parseInt(value, 60 * 60 * 24); // 1 day.
  
@@@ -358,55 -425,13 +435,60 @@@
          for (String id : availableIds) {
              _availableIdsMap.put(id, true);
          }
+ 
+         _userAuthenticators = locator.getAdapters(UserAuthenticator.class);
+         if (_userAuthenticators == null || !_userAuthenticators.isSet()) {
+             s_logger.error("Unable to find an user authenticator.");
+         }
      }
 -
 +    
 +    public void startup() {
 +    	s_logger.info("Startup CloudStack management server...");
 +    	initCloudStackComponents();
 +    }
 +    
 +	private void initCloudStackComponents() {
 +        runCheckers();
 +        startDaos();    // daos should not be using managers and adapters.
 +     
 +/*        
 +        configureManagers();
 +        configureAdapters();
 +        startManagers();
 +        startAdapters();
 +*/	
 +	}
 +	
 +    private void runCheckers() {
 +		Map<String, SystemIntegrityChecker> checkers = ComponentContext.getApplicationContext().getBeansOfType(
 +			SystemIntegrityChecker.class);
 +		
 +		for(SystemIntegrityChecker checker : checkers.values()) {
 +			try {
 +				checker.check();
 +			} catch (Exception e) {
 +                s_logger.error("Problems with running checker:" + checker.getClass().getName(), e);
 +                System.exit(1);
 +			}
 +		}
 +    }
 +	
 +    private void startDaos() {
 +		@SuppressWarnings("rawtypes")
 +		Map<String, GenericDaoBase> daos = ComponentContext.getApplicationContext().getBeansOfType(
 +				GenericDaoBase.class);
 +			
 +		for(GenericDaoBase dao : daos.values()) {
 +			try {
 +				
 +				// dao.configure(dao.getClass().getSimpleName(), params);
 +			} catch (Exception e) {
 +                s_logger.error("Problems with running checker:" + dao.getClass().getName(), e);
 +                System.exit(1);
 +			}
 +		}
 +    }
 +  
      protected Map<String, String> getConfigs() {
          return _configs;
      }
@@@ -802,22 -846,22 +903,22 @@@
  
          return sol;
      }
 -
 +    
      @Override
      public List<? extends Cluster> searchForClusters(long zoneId, Long startIndex, Long pageSizeVal, String hypervisorType) {
 -        Filter searchFilter = new Filter(ClusterVO.class, "id", true, startIndex, pageSizeVal);
 -        SearchCriteria<ClusterVO> sc = _clusterDao.createSearchCriteria();
 -
 -        zoneId = _accountMgr.checkAccessAndSpecifyAuthority(UserContext.current().getCaller(), zoneId);
 +    	Filter searchFilter = new Filter(ClusterVO.class, "id", true, startIndex, pageSizeVal);
 +    	SearchCriteria<ClusterVO> sc = _clusterDao.createSearchCriteria();
 +    	
 +    	zoneId = _accountMgr.checkAccessAndSpecifyAuthority(UserContext.current().getCaller(), zoneId);
  
 -        sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
 -        sc.addAnd("hypervisorType", SearchCriteria.Op.EQ, hypervisorType);
 -
 -        return _clusterDao.search(sc, searchFilter);
 +    	sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
 +    	sc.addAnd("hypervisorType", SearchCriteria.Op.EQ, hypervisorType);
 +    	
 +    	return _clusterDao.search(sc, searchFilter);
      }
 -
 +    
      @Override
-     public List<ClusterVO> searchForClusters(ListClustersCmd cmd) {
+     public Pair<List<? extends Cluster>, Integer> searchForClusters(ListClustersCmd cmd) {
          Filter searchFilter = new Filter(ClusterVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
          SearchCriteria<ClusterVO> sc = _clusterDao.createSearchCriteria();
  
@@@ -935,8 -964,9 +1021,9 @@@
              if (s_logger.isDebugEnabled()) {
                  s_logger.debug("Unable to find the host with id: " + srcHostId + " of this VM:" + vm);
              }
-             InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find the host (with specified id) of VM with specified id");
+             InvalidParameterValueException ex = new InvalidParameterValueException(
+                     "Unable to find the host (with specified id) of VM with specified id");
 -            ex.addProxyObject(srcHost, srcHostId, "hostId");
 +            ex.addProxyObject(srcHost, srcHostId, "hostId");            
              ex.addProxyObject(vm, vmId, "vmId");
              throw ex;
          }
@@@ -984,10 -1016,10 +1072,10 @@@
          return new Pair<List<? extends Host>, List<? extends Host>>(allHostsInCluster, suitableHosts);
      }
  
-     private List<HostVO> searchForServers(Long startIndex, Long pageSize, Object name, Object type, Object state, Object zone, Object pod, Object cluster, Object id, Object keyword,
-             Object resourceState, Object haHosts) {
+     private List<HostVO> searchForServers(Long startIndex, Long pageSize, Object name, Object type, Object state, Object zone, Object pod,
+             Object cluster, Object id, Object keyword, Object resourceState, Object haHosts) {
          Filter searchFilter = new Filter(HostVO.class, "id", Boolean.TRUE, startIndex, pageSize);
 -
 +        
          SearchBuilder<HostVO> sb = _hostDao.createSearchBuilder();
          sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
          sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
@@@ -1337,7 -1392,29 +1448,29 @@@
          Set<Pair<Long, Long>> templateZonePairSet = new HashSet<Pair<Long, Long>>();
          if (_swiftMgr.isSwiftEnabled()) {
              if (template == null) {
+                 templateZonePairSet = _templateDao.searchSwiftTemplates(name, keyword, templateFilter, isIso, hypers, bootable, domain, pageSize,
+                         startIndex, zoneId, hyperType, onlyReady, showDomr, permittedAccounts, caller, tags);
+                 Set<Pair<Long, Long>> templateZonePairSet2 = new HashSet<Pair<Long, Long>>();
+                 templateZonePairSet2 = _templateDao.searchTemplates(name, keyword, templateFilter, isIso, hypers, bootable, domain, pageSize,
+                         startIndex, zoneId, hyperType, onlyReady, showDomr, permittedAccounts, caller, listProjectResourcesCriteria, tags);
+ 
+                 for (Pair<Long, Long> tmpltPair : templateZonePairSet2) {
+                     if (!templateZonePairSet.contains(new Pair<Long, Long>(tmpltPair.first(), -1L))) {
+                         templateZonePairSet.add(tmpltPair);
+                     }
+                 }
+ 
+             } else {
+                 // if template is not public, perform permission check here
+                 if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
+                     Account owner = _accountMgr.getAccount(template.getAccountId());
+                     _accountMgr.checkAccess(caller, null, true, owner);
+                 }
+                 templateZonePairSet.add(new Pair<Long, Long>(template.getId(), zoneId));
+             }
+         } else if (_s3Mgr.isS3Enabled()) {
+             if (template == null) {
 -                templateZonePairSet = _templateDao.searchSwiftTemplates(name, keyword, templateFilter, isIso,
 +                templateZonePairSet = _templateDao.searchSwiftTemplates(name, keyword, templateFilter, isIso, 
                          hypers, bootable, domain, pageSize, startIndex, zoneId, hyperType, onlyReady, showDomr,
                          permittedAccounts, caller, tags);
                  Set<Pair<Long, Long>> templateZonePairSet2 = new HashSet<Pair<Long, Long>>();
@@@ -1977,11 -1834,12 +1890,12 @@@
          // check if domain exists in the system
          DomainVO domain = _domainDao.findById(domainId);
          if (domain == null) {
 -            InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id");
 -            ex.addProxyObject(domain, domainId, "domainId");
 +        	InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find domain with specified domain id");
 +        	ex.addProxyObject(domain, domainId, "domainId");            
              throw ex;
          } else if (domain.getParent() == null && domainName != null) {
-             // check if domain is ROOT domain - and deny to edit it with the new name
+             // check if domain is ROOT domain - and deny to edit it with the new
+             // name
              throw new InvalidParameterValueException("ROOT domain can not be edited with a new name");
          }
  
@@@ -1998,9 -1856,10 +1912,10 @@@
              boolean sameDomain = (domains.size() == 1 && domains.get(0).getId() == domainId);
  
              if (!domains.isEmpty() && !sameDomain) {
-                 InvalidParameterValueException ex = new InvalidParameterValueException("Failed to update specified domain id with name '" + domainName + "' since it already exists in the system");
+                 InvalidParameterValueException ex = new InvalidParameterValueException("Failed to update specified domain id with name '"
+                         + domainName + "' since it already exists in the system");
 -                ex.addProxyObject(domain, domainId, "domainId");
 -                throw ex;
 +                ex.addProxyObject(domain, domainId, "domainId");                
 +            	throw ex;
              }
          }
  
@@@ -2232,11 -2097,11 +2153,11 @@@
          } else if (zoneId != null) {
              dcList.add(ApiDBUtils.findZoneById(zoneId));
          } else {
-         	if (clusterId != null){
+             if (clusterId != null) {
 -                zoneId = ApiDBUtils.findClusterById(clusterId).getDataCenterId();
 +        		zoneId = ApiDBUtils.findClusterById(clusterId).getDataCenterId();
-         	}else{
+             } else {
 -                zoneId = ApiDBUtils.findPodById(podId).getDataCenterId();
 -            }
 +        		zoneId = ApiDBUtils.findPodById(podId).getDataCenterId();
 +        	}
              if (capacityType == null || capacityType == Capacity.CAPACITY_TYPE_STORAGE) {
                  capacities.add(_storageMgr.getStoragePoolUsedStats(null, clusterId, podId, zoneId));
              }
@@@ -2519,82 -2402,9 +2458,9 @@@
              sc.addAnd("clusterId", SearchCriteria.Op.EQ, cluster);
          }
  
-         return _poolDao.search(sc, searchFilter);
-     }
- 
-     @Override
-     public List<AsyncJobVO> searchForAsyncJobs(ListAsyncJobsCmd cmd) {
- 
-         Account caller = UserContext.current().getCaller();
- 
-         List<Long> permittedAccounts = new ArrayList<Long>();
- 
-         Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
-         _accountMgr.buildACLSearchParameters(caller, null, cmd.getAccountName(), null, permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
-         Long domainId = domainIdRecursiveListProject.first();
-         Boolean isRecursive = domainIdRecursiveListProject.second();
-         ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
- 
-         Filter searchFilter = new Filter(AsyncJobVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
-         SearchBuilder<AsyncJobVO> sb = _jobDao.createSearchBuilder();
-         sb.and("accountIdIN", sb.entity().getAccountId(), SearchCriteria.Op.IN);
-         SearchBuilder<AccountVO> accountSearch = null;
-         boolean accountJoinIsDone = false;
-         if (permittedAccounts.isEmpty() && domainId != null) {
-             accountSearch = _accountDao.createSearchBuilder();
-             // if accountId isn't specified, we can do a domain match for the admin case if isRecursive is true
-             SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
-             domainSearch.and("domainId", domainSearch.entity().getId(), SearchCriteria.Op.EQ);
-             domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
-             sb.join("accountSearch", accountSearch, sb.entity().getAccountId(), accountSearch.entity().getId(), JoinBuilder.JoinType.INNER);
-             accountJoinIsDone = true;
-             accountSearch.join("domainSearch", domainSearch, accountSearch.entity().getDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER);
-         }
- 
-         if (listProjectResourcesCriteria != null) {
-             if (accountSearch == null) {
-                 accountSearch = _accountDao.createSearchBuilder();
-             }
-             if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.ListProjectResourcesOnly) {
-                 accountSearch.and("type", accountSearch.entity().getType(), SearchCriteria.Op.EQ);
-             } else if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.SkipProjectResources) {
-                 accountSearch.and("type", accountSearch.entity().getType(), SearchCriteria.Op.NEQ);
-             }
- 
-             if (!accountJoinIsDone) {
-                 sb.join("accountSearch", accountSearch, sb.entity().getAccountId(), accountSearch.entity().getId(), JoinBuilder.JoinType.INNER);
+         return _poolDao.searchAndCount(sc, searchFilter);
 -    }
 +            }
-         }
- 
-         Object keyword = cmd.getKeyword();
-         Object startDate = cmd.getStartDate();
  
-         SearchCriteria<AsyncJobVO> sc = sb.create();
-         if (listProjectResourcesCriteria != null) {
-             sc.setJoinParameters("accountSearch", "type", Account.ACCOUNT_TYPE_PROJECT);
-         }
- 
-         if (!permittedAccounts.isEmpty()) {
-             sc.setParameters("accountIdIN", permittedAccounts.toArray());
-         } else if (domainId != null) {
-             DomainVO domain = _domainDao.findById(domainId);
-             if (isRecursive) {
-                 sc.setJoinParameters("domainSearch", "path", domain.getPath() + "%");
-             } else {
-                 sc.setJoinParameters("domainSearch", "domainId", domainId);
-             }
-         }
- 
-         if (keyword != null) {
-             sc.addAnd("cmd", SearchCriteria.Op.LIKE, "%" + keyword + "%");
-         }
- 
-         if (startDate != null) {
-             sc.addAnd("created", SearchCriteria.Op.GTEQ, startDate);
-         }
- 
-         return _jobDao.search(sc, searchFilter);
-     }
  
      @ActionEvent(eventType = EventTypes.EVENT_SSVM_START, eventDescription = "starting secondary storage Vm", async = true)
      public SecondaryStorageVmVO startSecondaryStorageVm(long instanceId) {
@@@ -2924,15 -2740,20 +2796,20 @@@
              throw ex;
          }
  
-         if (volume.getVolumeType() != Volume.Type.DATADISK) { // Datadisk dont have any template dependence.
+         if (volume.getVolumeType() != Volume.Type.DATADISK) { // Datadisk dont
+                                                               // have any
+                                                               // template
+                                                               // dependence.
  
              VMTemplateVO template = ApiDBUtils.findTemplateById(volume.getTemplateId());
-             if (template != null) { // For ISO based volumes template = null and we allow extraction of all ISO based volumes
+             if (template != null) { // For ISO based volumes template = null and
+                                     // we allow extraction of all ISO based
+                                     // volumes
                  boolean isExtractable = template.isExtractable() && template.getTemplateType() != Storage.TemplateType.SYSTEM;
                  if (!isExtractable && account != null && account.getType() != Account.ACCOUNT_TYPE_ADMIN) { // Global
- // admins are always allowed to extract
+                 // admins are always allowed to extract
 -                    PermissionDeniedException ex = new PermissionDeniedException("The volume with specified volumeId is not allowed to be extracted");
 -                    ex.addProxyObject(volume, volumeId, "volumeId");
 +                	PermissionDeniedException ex = new PermissionDeniedException("The volume with specified volumeId is not allowed to be extracted");
 +                	ex.addProxyObject(volume, volumeId, "volumeId");                    
                      throw ex;
                  }
              }
@@@ -3290,8 -3053,9 +3109,9 @@@
  
          SSHKeyPairVO s = _sshKeyPairDao.findByName(owner.getAccountId(), owner.getDomainId(), cmd.getName());
          if (s == null) {
-         	InvalidParameterValueException ex = new InvalidParameterValueException("A key pair with name '" + cmd.getName() + "' does not exist for account " + owner.getAccountName() + " in specified domain id");
+             InvalidParameterValueException ex = new InvalidParameterValueException("A key pair with name '" + cmd.getName()
+                     + "' does not exist for account " + owner.getAccountName() + " in specified domain id");
 -            ex.addProxyObject(owner, owner.getDomainId(), "domainId");
 +        	ex.addProxyObject(owner, owner.getDomainId(), "domainId");
              throw ex;
          }
  
@@@ -3518,8 -3289,7 +3345,7 @@@
              return null;
          }
      }
 -
 +    
-     
      @Override
      public VirtualMachine upgradeSystemVM(UpgradeSystemVMCmd cmd) {
          Long systemVmId = cmd.getId();

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/30f2565d/server/src/com/cloud/servlet/CloudStartupServlet.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/servlet/CloudStartupServlet.java
index dfd4c60,389bd26..4466ca8
--- a/server/src/com/cloud/servlet/CloudStartupServlet.java
+++ b/server/src/com/cloud/servlet/CloudStartupServlet.java
@@@ -40,17 -35,20 +40,18 @@@ public class CloudStartupServlet extend
  	
      static final long serialVersionUID = SerialVersionUID.CloudStartupServlet;
     
 -    protected static ComponentLocator s_locator;
 -    
  	@Override
      public void init() throws ServletException {
 -	    // Save Configuration Values
 -        //ComponentLocator loc = ComponentLocator.getLocator(ConfigurationServer.Name);
 -	    ConfigurationServer c = (ConfigurationServer)ComponentLocator.getComponent(ConfigurationServer.Name);
 -	    //ConfigurationServer c = new ConfigurationServerImpl();
 +    	initLog4j();
 +
 +    	// Save Configuration Values
 + 	    ConfigurationServer c = (ConfigurationServer)ComponentContext.getCompanent(ConfigurationServer.class);
  	    try {
  	    	c.persistDefaultValues();
- 		    ManagementServer ms = (ManagementServer)ComponentContext.getCompanent(ManagementServer.class);
- 		    ms.startup();
- 		    ApiServer.initApiServer(ms.getApiConfig());
+ 	    	s_locator = ComponentLocator.getLocator(ManagementServer.Name);
+ 		    ManagementServer ms = (ManagementServer)ComponentLocator.getComponent(ManagementServer.Name);
+ 		    ms.enableAdminUser("password");
+ 		    ApiServer.initApiServer(ms.getPropertiesFiles());
  	    } catch (InvalidParameterValueException ipve) {
  	    	s_logger.error("Exception starting management server ", ipve);
  	    	throw new ServletException (ipve.getMessage());