incubator-cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bhais...@apache.org
Subject [3/3] api: Optimize and improve api, db call perfomance
Date Thu, 06 Dec 2012 04:08:20 GMT
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/vm/UserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java
index eacf7f4..5729035 100755
--- a/server/src/com/cloud/vm/UserVmManagerImpl.java
+++ b/server/src/com/cloud/vm/UserVmManagerImpl.java
@@ -74,6 +74,7 @@ import org.apache.cloudstack.api.user.vm.command.RestoreVMCmd;
 import org.apache.cloudstack.api.user.vm.command.StartVMCmd;
 import org.apache.cloudstack.api.user.vm.command.UpdateVMCmd;
 import org.apache.cloudstack.api.user.vm.command.UpgradeVMCmd;
+import com.cloud.api.view.vo.UserVmJoinVO;
 import com.cloud.async.AsyncJobExecutor;
 import com.cloud.async.AsyncJobManager;
 import com.cloud.async.AsyncJobVO;
@@ -151,6 +152,8 @@ import com.cloud.offerings.dao.NetworkOfferingDao;
 import com.cloud.org.Cluster;
 import com.cloud.org.Grouping;
 import com.cloud.projects.Project.ListProjectResourcesCriteria;
+import com.cloud.projects.Project;
+import com.cloud.projects.ProjectInvitationVO;
 import com.cloud.projects.ProjectManager;
 import com.cloud.resource.ResourceManager;
 import com.cloud.resource.ResourceState;
@@ -222,6 +225,7 @@ import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
 import com.cloud.utils.db.Transaction;
+import com.cloud.utils.db.SearchCriteria.Func;
 import com.cloud.utils.exception.CloudRuntimeException;
 import com.cloud.utils.exception.ExecutionException;
 import com.cloud.utils.fsm.NoTransitionException;
@@ -232,6 +236,7 @@ import com.cloud.vm.dao.InstanceGroupVMMapDao;
 import com.cloud.vm.dao.NicDao;
 import com.cloud.vm.dao.UserVmDao;
 import com.cloud.vm.dao.UserVmDetailsDao;
+import com.cloud.vm.dao.UserVmJoinDao;
 import com.cloud.vm.dao.VMInstanceDao;
 
 @Local(value = { UserVmManager.class, UserVmService.class })
@@ -259,6 +264,8 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
     @Inject
     protected UserVmDao _vmDao = null;
     @Inject
+    protected UserVmJoinDao _vmJoinDao = null;
+    @Inject
     protected VolumeDao _volsDao = null;
     @Inject
     protected DataCenterDao _dcDao = null;
@@ -3040,7 +3047,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
     }
 
     @Override
-    public Pair<List<? extends UserVm>, Integer> searchForUserVMs(ListVMsCmd cmd) {
+    public Pair<List<UserVmJoinVO>, Integer> searchForUserVMs(ListVMsCmd cmd) {
         Account caller = UserContext.current().getCaller();
         List<Long> permittedAccounts = new ArrayList<Long>();
         String hypervisor = cmd.getHypervisor();
@@ -3054,7 +3061,9 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
         Boolean isRecursive = domainIdRecursiveListProject.second();
         ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
 
-        Criteria c = new Criteria("id", Boolean.TRUE, cmd.getStartIndex(), cmd.getPageSizeVal());
+        // removing order by, orderBy shouold be specified in ListVMsCmd parameters
+        //Criteria c = new Criteria("id", Boolean.TRUE, cmd.getStartIndex(), cmd.getPageSizeVal());
+        Criteria c = new Criteria(null, Boolean.FALSE, cmd.getStartIndex(), cmd.getPageSizeVal());
         c.addCriteria(Criteria.KEYWORD, cmd.getKeyword());
         c.addCriteria(Criteria.ID, cmd.getId());
         c.addCriteria(Criteria.NAME, cmd.getInstanceName());
@@ -3089,18 +3098,19 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
         }
         c.addCriteria(Criteria.ISADMIN, _accountMgr.isAdmin(caller.getType()));
 
-        Pair<List<UserVmVO>, Integer> result = searchForUserVMs(c, caller, domainId, isRecursive,
+        return searchForUserVMs(c, caller, domainId, isRecursive,
                 permittedAccounts, listAll, listProjectResourcesCriteria, tags);
-        return new Pair<List<? extends UserVm>, Integer>(result.first(), result.second());
     }
 
     @Override
-    public Pair<List<UserVmVO>, Integer> searchForUserVMs(Criteria c, Account caller, Long domainId, boolean isRecursive, 
+    public Pair<List<UserVmJoinVO>, Integer> searchForUserVMs(Criteria c, Account caller, Long domainId, boolean isRecursive,
             List<Long> permittedAccounts, boolean listAll, ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags) {
-        Filter searchFilter = new Filter(UserVmVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
+        Filter searchFilter = new Filter(UserVmJoinVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
 
-        SearchBuilder<UserVmVO> sb = _vmDao.createSearchBuilder();
-        _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
+        //first search distinct vm id by using query criteria and pagination
+        SearchBuilder<UserVmJoinVO> sb = _vmJoinDao.createSearchBuilder();
+        sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct ids
+        _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
         
         Object id = c.getCriteria(Criteria.ID);
         Object name = c.getCriteria(Criteria.NAME);
@@ -3127,88 +3137,60 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
         sb.and("stateEQ", sb.entity().getState(), SearchCriteria.Op.EQ);
         sb.and("stateNEQ", sb.entity().getState(), SearchCriteria.Op.NEQ);
         sb.and("stateNIN", sb.entity().getState(), SearchCriteria.Op.NIN);
-        sb.and("dataCenterId", sb.entity().getDataCenterIdToDeployIn(), SearchCriteria.Op.EQ);
-        sb.and("podId", sb.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ);
+        sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
+        sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
         sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
         sb.and("hostIdEQ", sb.entity().getHostId(), SearchCriteria.Op.EQ);
-        sb.and("hostIdIN", sb.entity().getHostId(), SearchCriteria.Op.IN);
+        sb.and("hostName", sb.entity().getHostName(), SearchCriteria.Op.LIKE);
         sb.and("templateId", sb.entity().getTemplateId(), SearchCriteria.Op.EQ);
-        sb.and("isoId", sb.entity().getTemplateId(), SearchCriteria.Op.EQ);
+        sb.and("isoId", sb.entity().getIsoId(), SearchCriteria.Op.EQ);
+        sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
 
-        if (groupId != null && (Long) groupId == -1) {
-            SearchBuilder<InstanceGroupVMMapVO> vmSearch = _groupVMMapDao.createSearchBuilder();
-            vmSearch.and("instanceId", vmSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
-            sb.join("vmSearch", vmSearch, sb.entity().getId(), vmSearch.entity().getInstanceId(), JoinBuilder.JoinType.LEFTOUTER);
-        } else if (groupId != null) {
-            SearchBuilder<InstanceGroupVMMapVO> groupSearch = _groupVMMapDao.createSearchBuilder();
-            groupSearch.and("groupId", groupSearch.entity().getGroupId(), SearchCriteria.Op.EQ);
-            sb.join("groupSearch", groupSearch, sb.entity().getId(), groupSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
+        if (groupId != null && (Long) groupId != -1) {
+            sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), 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();
+                sb.or().op("key" + String.valueOf(count), sb.entity().getTagKey(), SearchCriteria.Op.EQ);
+                sb.and("value" + String.valueOf(count), sb.entity().getTagValue(), SearchCriteria.Op.EQ);
+                sb.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);
         }
 
         if (networkId != null) {
-            SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder();
-            nicSearch.and("networkId", nicSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
-
-            SearchBuilder<NetworkVO> networkSearch = _networkDao.createSearchBuilder();
-            networkSearch.and("networkId", networkSearch.entity().getId(), SearchCriteria.Op.EQ);
-            nicSearch.join("networkSearch", networkSearch, nicSearch.entity().getNetworkId(), networkSearch.entity().getId(), JoinBuilder.JoinType.INNER);
-
-            sb.join("nicSearch", nicSearch, sb.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
+            sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
         }
         
         if(vpcId != null && networkId == null){
-            SearchBuilder<NicVO> nicSearch = _nicDao.createSearchBuilder();
-
-            SearchBuilder<NetworkVO> networkSearch = _networkDao.createSearchBuilder();
-            nicSearch.join("networkSearch", networkSearch, nicSearch.entity().getNetworkId(), networkSearch.entity().getId(), JoinBuilder.JoinType.INNER);
-            
-            SearchBuilder<VpcVO> vpcSearch = _vpcDao.createSearchBuilder();
-            vpcSearch.and("vpcId", vpcSearch.entity().getId(), SearchCriteria.Op.EQ);
-            networkSearch.join("vpcSearch", vpcSearch, networkSearch.entity().getVpcId(), vpcSearch.entity().getId(), JoinBuilder.JoinType.INNER);
-            
-            sb.join("nicSearch", nicSearch, sb.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
+            sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
         }
 
         if (storageId != null) {
-            SearchBuilder<VolumeVO> volumeSearch = _volsDao.createSearchBuilder();
-            volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
-            sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
+            sb.and("poolId", sb.entity().getPoolId(), SearchCriteria.Op.EQ);
         }
 
         // populate the search criteria with the values passed in
-        SearchCriteria<UserVmVO> sc = sb.create();
-        _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
+        SearchCriteria<UserVmJoinVO> sc = sb.create();
+
+        // building ACL condition
+        _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
 
         if (tags != null && !tags.isEmpty()) {
             int count = 0;
-            sc.setJoinParameters("tagSearch", "resourceType", TaggedResourceType.UserVm.toString());
-            for (String key : tags.keySet()) {
-                sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
-                sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
+             for (String key : tags.keySet()) {
+                sc.setParameters("key" + String.valueOf(count), key);
+                sc.setParameters("value" + String.valueOf(count), tags.get(key));
                 count++;
             }
         }
 
-        if (groupId != null && (Long) groupId == -1) {
-            sc.setJoinParameters("vmSearch", "instanceId", (Object) null);
-        } else if (groupId != null) {
-            sc.setJoinParameters("groupSearch", "groupId", groupId);
+        if (groupId != null && (Long)groupId != -1) {
+            sc.setParameters("instanceGroupId", groupId);
         }
 
         if (keyword != null) {
-            SearchCriteria<UserVmVO> ssc = _vmDao.createSearchCriteria();
+            SearchCriteria<UserVmJoinVO> ssc = _vmJoinDao.createSearchCriteria();
             ssc.addOr("displayName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
             ssc.addOr("hostName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
             ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
@@ -3230,11 +3212,11 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
         }
 
         if (networkId != null) {
-            sc.setJoinParameters("nicSearch", "networkId", networkId);
+            sc.setParameters("networkId", networkId);
         }
         
         if(vpcId != null && networkId == null){
-            sc.setJoinParameters("vpcSearch", "vpcId", vpcId);
+            sc.setParameters("vpcId", vpcId);
         }
 
         if (name != null) {
@@ -3277,25 +3259,29 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
             sc.setParameters("hostIdEQ", hostId);
         } else {
             if (hostName != null) {
-                List<HostVO> hosts = _resourceMgr.listHostsByNameLike((String) hostName);
-                if (hosts != null & !hosts.isEmpty()) {
-                    Long[] hostIds = new Long[hosts.size()];
-                    for (int i = 0; i < hosts.size(); i++) {
-                        HostVO host = hosts.get(i);
-                        hostIds[i] = host.getId();
-                    }
-                    sc.setParameters("hostIdIN", (Object[]) hostIds);
-                } else {
-                    return new Pair<List<UserVmVO>, Integer>(new ArrayList<UserVmVO>(), 0);
-                }
+                sc.setParameters("hostName", hostName);
             }
         }
 
         if (storageId != null) {
-            sc.setJoinParameters("volumeSearch", "poolId", storageId);
+            sc.setParameters("poolId", storageId);
         }
-        
-        return _vmDao.searchAndCount(sc, searchFilter);
+
+        // search vm details by ids
+        Pair<List<UserVmJoinVO>, Integer> uniqueVmPair =  _vmJoinDao.searchAndCount(sc, searchFilter);
+        Integer count = uniqueVmPair.second();
+        if ( count.intValue() == 0 ){
+            // handle empty result cases
+            return uniqueVmPair;
+        }
+        List<UserVmJoinVO> uniqueVms = uniqueVmPair.first();
+        Long[] vmIds = new Long[uniqueVms.size()];
+        int i = 0;
+        for (UserVmJoinVO v : uniqueVms ){
+            vmIds[i++] = v.getId();
+        }
+        List<UserVmJoinVO> vms = _vmJoinDao.searchByIds(vmIds);
+        return new Pair<List<UserVmJoinVO>, Integer>(vms, count);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/vm/dao/DomainRouterJoinDao.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/dao/DomainRouterJoinDao.java b/server/src/com/cloud/vm/dao/DomainRouterJoinDao.java
new file mode 100644
index 0000000..c7c749b
--- /dev/null
+++ b/server/src/com/cloud/vm/dao/DomainRouterJoinDao.java
@@ -0,0 +1,36 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.vm.dao;
+
+import java.util.List;
+
+import com.cloud.api.response.DomainRouterResponse;
+import com.cloud.api.view.vo.DomainRouterJoinVO;
+import com.cloud.network.router.VirtualRouter;
+import com.cloud.user.Account;
+import com.cloud.utils.db.GenericDao;
+
+public interface DomainRouterJoinDao extends GenericDao<DomainRouterJoinVO, Long> {
+
+    DomainRouterResponse newDomainRouterResponse(DomainRouterJoinVO uvo, Account caller);
+
+    DomainRouterResponse setDomainRouterResponse(DomainRouterResponse userVmData, DomainRouterJoinVO uvo);
+
+    List<DomainRouterJoinVO> newDomainRouterView(VirtualRouter vr);
+
+    List<DomainRouterJoinVO> searchByIds(Long... ids);
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/vm/dao/DomainRouterJoinDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/dao/DomainRouterJoinDaoImpl.java b/server/src/com/cloud/vm/dao/DomainRouterJoinDaoImpl.java
new file mode 100644
index 0000000..a3f7d77
--- /dev/null
+++ b/server/src/com/cloud/vm/dao/DomainRouterJoinDaoImpl.java
@@ -0,0 +1,231 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.vm.dao;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Hashtable;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+
+import com.cloud.api.ApiDBUtils;
+import com.cloud.api.ApiResponseHelper;
+import com.cloud.api.response.DomainRouterResponse;
+import com.cloud.api.response.NicResponse;
+import com.cloud.api.response.ResourceTagResponse;
+import com.cloud.api.response.SecurityGroupResponse;
+import com.cloud.api.response.UserVmResponse;
+import com.cloud.api.view.vo.DomainRouterJoinVO;
+import com.cloud.dc.DataCenter;
+import com.cloud.network.Network;
+import com.cloud.network.Networks.TrafficType;
+import com.cloud.network.router.VirtualRouter;
+import com.cloud.offering.ServiceOffering;
+import com.cloud.user.Account;
+import com.cloud.uservm.UserVm;
+import com.cloud.utils.Pair;
+import com.cloud.utils.db.Attribute;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.JoinBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.NicProfile;
+
+
+@Local(value={DomainRouterJoinDao.class})
+public class DomainRouterJoinDaoImpl extends GenericDaoBase<DomainRouterJoinVO, Long> implements DomainRouterJoinDao {
+    public static final Logger s_logger = Logger.getLogger(DomainRouterJoinDaoImpl.class);
+
+    private SearchBuilder<DomainRouterJoinVO> vrSearch;
+
+    private SearchBuilder<DomainRouterJoinVO> vrIdSearch;
+
+    protected DomainRouterJoinDaoImpl() {
+
+        vrSearch = createSearchBuilder();
+        vrSearch.and("idIN", vrSearch.entity().getId(), SearchCriteria.Op.IN);
+        vrSearch.done();
+
+        vrIdSearch = createSearchBuilder();
+        vrIdSearch.and("id", vrIdSearch.entity().getId(), SearchCriteria.Op.EQ);
+        vrIdSearch.done();
+
+        this._count = "select count(distinct id) from domain_router_view WHERE ";
+    }
+
+
+    @Override
+    public DomainRouterResponse newDomainRouterResponse(DomainRouterJoinVO router, Account caller) {
+        DomainRouterResponse routerResponse = new DomainRouterResponse();
+        routerResponse.setId(router.getUuid());
+        routerResponse.setZoneId(router.getDataCenterUuid());
+        routerResponse.setName(router.getHostName());
+        routerResponse.setTemplateId(router.getTemplateUuid());
+        routerResponse.setCreated(router.getCreated());
+        routerResponse.setState(router.getState());
+        routerResponse.setIsRedundantRouter(router.isRedundantRouter());
+        routerResponse.setRedundantState(router.getRedundantState().toString());
+
+        if (caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN
+                || caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {
+            if (router.getHostId() != null) {
+                routerResponse.setHostId(router.getHostUuid());
+                routerResponse.setHostName(router.getHostName());
+            }
+            routerResponse.setPodId(router.getPodUuid());
+            long nic_id = router.getNicId();
+            if (nic_id > 0) {
+                TrafficType ty = router.getTrafficType();
+                if (ty != null) {
+                    // legacy code, public/control/guest nic info is kept in
+                    // nics response object
+                    if (ty == TrafficType.Public) {
+                        routerResponse.setPublicIp(router.getIpAddress());
+                        routerResponse.setPublicMacAddress(router.getMacAddress());
+                        routerResponse.setPublicNetmask(router.getNetmask());
+                        routerResponse.setGateway(router.getGateway());
+                        routerResponse.setPublicNetworkId(router.getNetworkUuid());
+                    } else if (ty == TrafficType.Control) {
+                        routerResponse.setLinkLocalIp(router.getIpAddress());
+                        routerResponse.setLinkLocalMacAddress(router.getMacAddress());
+                        routerResponse.setLinkLocalNetmask(router.getNetmask());
+                        routerResponse.setLinkLocalNetworkId(router.getNetworkUuid());
+                    } else if (ty == TrafficType.Guest) {
+                        routerResponse.setGuestIpAddress(router.getIpAddress());
+                        routerResponse.setGuestMacAddress(router.getMacAddress());
+                        routerResponse.setGuestNetmask(router.getNetmask());
+                        routerResponse.setGuestNetworkId(router.getNetworkUuid());
+                        routerResponse.setNetworkDomain(router.getNetworkDomain());
+                    }
+                }
+
+                NicResponse nicResponse = new NicResponse();
+                nicResponse.setId(router.getNicUuid());
+                nicResponse.setIpaddress(router.getIpAddress());
+                nicResponse.setGateway(router.getGateway());
+                nicResponse.setNetmask(router.getNetmask());
+                nicResponse.setNetworkid(router.getNetworkUuid());
+                nicResponse.setNetworkName(router.getNetworkName());
+                nicResponse.setMacAddress(router.getMacAddress());
+                if (router.getBroadcastUri() != null) {
+                    nicResponse.setBroadcastUri(router.getBroadcastUri().toString());
+                }
+                if (router.getIsolationUri() != null) {
+                    nicResponse.setIsolationUri(router.getIsolationUri().toString());
+                }
+                if (router.getTrafficType() != null) {
+                    nicResponse.setTrafficType(router.getTrafficType().toString());
+                }
+                if (router.getGuestType() != null) {
+                    nicResponse.setType(router.getGuestType().toString());
+                }
+                nicResponse.setIsDefault(router.isDefaultNic());
+                nicResponse.setObjectName("nic");
+                routerResponse.addNic(nicResponse);
+            }
+        }
+
+        routerResponse.setServiceOfferingId(router.getServiceOfferingUuid());
+        routerResponse.setServiceOfferingName(router.getServiceOfferingName());
+
+        // populate owner.
+        ApiResponseHelper.populateOwner(routerResponse, router);
+
+
+        routerResponse.setDomainId(router.getDomainUuid());
+        routerResponse.setDomainName(router.getDomainName());
+
+        routerResponse.setZoneName(router.getDataCenterName());
+        routerResponse.setDns1(router.getDns1());
+        routerResponse.setDns2(router.getDns2());
+
+        routerResponse.setVpcId(router.getVpcUuid());
+
+        routerResponse.setObjectName("router");
+
+        return routerResponse;
+    }
+
+
+    @Override
+    public DomainRouterResponse setDomainRouterResponse(DomainRouterResponse vrData, DomainRouterJoinVO vr) {
+        long nic_id = vr.getNicId();
+        if (nic_id > 0) {
+            NicResponse nicResponse = new NicResponse();
+            nicResponse.setId(vr.getNicUuid());
+            nicResponse.setIpaddress(vr.getIpAddress());
+            nicResponse.setGateway(vr.getGateway());
+            nicResponse.setNetmask(vr.getNetmask());
+            nicResponse.setNetworkid(vr.getNetworkUuid());
+            nicResponse.setMacAddress(vr.getMacAddress());
+            if (vr.getBroadcastUri() != null) {
+                nicResponse.setBroadcastUri(vr.getBroadcastUri().toString());
+            }
+            if (vr.getIsolationUri() != null) {
+                nicResponse.setIsolationUri(vr.getIsolationUri().toString());
+            }
+            if (vr.getTrafficType() != null) {
+                nicResponse.setTrafficType(vr.getTrafficType().toString());
+            }
+            if (vr.getGuestType() != null) {
+                nicResponse.setType(vr.getGuestType().toString());
+            }
+            nicResponse.setIsDefault(vr.isDefaultNic());
+            nicResponse.setObjectName("nic");
+            vrData.addNic(nicResponse);
+        }
+        return vrData;
+    }
+
+
+
+
+    @Override
+    public List<DomainRouterJoinVO> searchByIds(Long... ids) {
+        SearchCriteria<DomainRouterJoinVO> sc = vrSearch.create();
+        sc.setParameters("idIN", ids);
+        return searchIncludingRemoved(sc, null, null, false);
+    }
+
+
+    @Override
+    public List<DomainRouterJoinVO> newDomainRouterView(VirtualRouter vr) {
+
+        List<DomainRouterJoinVO> uvList = new ArrayList<DomainRouterJoinVO>();
+        SearchCriteria<DomainRouterJoinVO> sc = vrIdSearch.create();
+        sc.setParameters("id", vr.getId());
+        List<DomainRouterJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
+        if (vms != null) {
+            for (DomainRouterJoinVO uvm : vms) {
+                uvList.add(uvm);
+            }
+        }
+        return uvList;
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/vm/dao/UserVmJoinDao.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/dao/UserVmJoinDao.java b/server/src/com/cloud/vm/dao/UserVmJoinDao.java
new file mode 100644
index 0000000..adeef63
--- /dev/null
+++ b/server/src/com/cloud/vm/dao/UserVmJoinDao.java
@@ -0,0 +1,38 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.vm.dao;
+
+import java.util.EnumSet;
+import java.util.List;
+
+import com.cloud.api.ApiConstants.VMDetails;
+import com.cloud.api.response.UserVmResponse;
+import com.cloud.api.view.vo.UserVmJoinVO;
+import com.cloud.user.Account;
+import com.cloud.uservm.UserVm;
+import com.cloud.utils.db.GenericDao;
+
+public interface UserVmJoinDao extends GenericDao<UserVmJoinVO, Long> {
+
+    UserVmResponse newUserVmResponse(String objectName, UserVmJoinVO userVm, EnumSet<VMDetails> details, Account caller);
+
+    UserVmResponse setUserVmResponse(UserVmResponse userVmData, UserVmJoinVO uvo);
+
+    List<UserVmJoinVO> newUserVmView(UserVm... userVms);
+
+    List<UserVmJoinVO> searchByIds(Long... ids);
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/vm/dao/UserVmJoinDaoImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/dao/UserVmJoinDaoImpl.java b/server/src/com/cloud/vm/dao/UserVmJoinDaoImpl.java
new file mode 100644
index 0000000..5b7a2c1
--- /dev/null
+++ b/server/src/com/cloud/vm/dao/UserVmJoinDaoImpl.java
@@ -0,0 +1,384 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.vm.dao;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.text.DecimalFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.EnumSet;
+import java.util.Hashtable;
+import java.util.List;
+
+import javax.ejb.Local;
+
+import org.apache.log4j.Logger;
+
+import com.cloud.api.ApiDBUtils;
+import com.cloud.api.ApiConstants.VMDetails;
+import com.cloud.api.response.NicResponse;
+import com.cloud.api.response.ResourceTagResponse;
+import com.cloud.api.response.SecurityGroupResponse;
+import com.cloud.api.response.UserVmResponse;
+import com.cloud.api.view.vo.UserVmJoinVO;
+import com.cloud.user.Account;
+import com.cloud.uservm.UserVm;
+import com.cloud.utils.Pair;
+import com.cloud.utils.db.Attribute;
+import com.cloud.utils.db.DB;
+import com.cloud.utils.db.Filter;
+import com.cloud.utils.db.GenericDaoBase;
+import com.cloud.utils.db.JoinBuilder;
+import com.cloud.utils.db.SearchBuilder;
+import com.cloud.utils.db.SearchCriteria;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.vm.VmStats;
+
+
+@Local(value={UserVmJoinDao.class})
+public class UserVmJoinDaoImpl extends GenericDaoBase<UserVmJoinVO, Long> implements UserVmJoinDao {
+    public static final Logger s_logger = Logger.getLogger(UserVmJoinDaoImpl.class);
+
+    private static final int VM_DETAILS_BATCH_SIZE=100;
+
+    private SearchBuilder<UserVmJoinVO> VmDetailSearch;
+
+    protected UserVmJoinDaoImpl() {
+
+        VmDetailSearch = createSearchBuilder();
+        VmDetailSearch.and("idIN", VmDetailSearch.entity().getId(), SearchCriteria.Op.IN);
+        VmDetailSearch.done();
+
+        this._count = "select count(distinct id) from user_vm_view WHERE ";
+    }
+
+    public UserVmResponse newUserVmResponse(String objectName, UserVmJoinVO userVm, EnumSet<VMDetails> details, Account caller) {
+        UserVmResponse userVmResponse = new UserVmResponse();
+
+        if (userVm.getHypervisorType() != null){
+            userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
+        }
+        userVmResponse.setId(userVm.getUuid());
+        userVmResponse.setName(userVm.getName());
+
+        userVmResponse.setDisplayName(userVm.getDisplayName());
+
+        if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
+            userVmResponse.setProjectId(userVm.getProjectUuid());
+            userVmResponse.setProjectName(userVm.getProjectName());
+        } else {
+            userVmResponse.setAccountName(userVm.getAccountName());
+        }
+
+        userVmResponse.setDomainId(userVm.getDomainUuid());
+        userVmResponse.setDomainName(userVm.getDomainName());
+
+        userVmResponse.setCreated(userVm.getCreated());
+
+        if (userVm.getState() != null) {
+            userVmResponse.setState(userVm.getState().toString());
+        }
+        userVmResponse.setHaEnable(userVm.isHaEnabled());
+        if (details.contains(VMDetails.all) || details.contains(VMDetails.group)) {
+            userVmResponse.setGroupId(userVm.getInstanceGroupUuid());
+            userVmResponse.setGroup(userVm.getInstanceGroupName());
+        }
+        userVmResponse.setZoneId(userVm.getDataCenterUuid());
+        userVmResponse.setZoneName(userVm.getDataCenterName());
+        if ((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN)) {
+            userVmResponse.setInstanceName(userVm.getInstanceName());
+            userVmResponse.setHostId(userVm.getHostUuid());
+            userVmResponse.setHostName(userVm.getHostName());
+        }
+
+        if (details.contains(VMDetails.all) || details.contains(VMDetails.tmpl)) {
+            userVmResponse.setTemplateId(userVm.getTemplateUuid());
+            userVmResponse.setTemplateName(userVm.getTemplateName());
+            userVmResponse.setTemplateDisplayText(userVm.getTemplateDisplayText());
+            userVmResponse.setPasswordEnabled(userVm.isPasswordEnabled());
+        }
+        if (details.contains(VMDetails.all) || details.contains(VMDetails.iso)) {
+            userVmResponse.setIsoId(userVm.getIsoUuid());
+            userVmResponse.setIsoName(userVm.getIsoName());
+            userVmResponse.setIsoDisplayText(userVm.getIsoDisplayText());
+        }
+        if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff)) {
+            userVmResponse.setServiceOfferingId(userVm.getServiceOfferingUuid());
+            userVmResponse.setServiceOfferingName(userVm.getServiceOfferingName());
+            userVmResponse.setCpuNumber(userVm.getCpu());
+            userVmResponse.setCpuSpeed(userVm.getSpeed());
+            userVmResponse.setMemory(userVm.getRamSize());
+        }
+        userVmResponse.setGuestOsId(userVm.getGuestOsUuid());
+        if (details.contains(VMDetails.all) || details.contains(VMDetails.volume)) {
+            userVmResponse.setRootDeviceId(userVm.getVolumeDeviceId());
+            if (userVm.getVolumeType() != null) {
+                userVmResponse.setRootDeviceType(userVm.getVolumeType().toString());
+            }
+        }
+        userVmResponse.setPassword(userVm.getPassword());
+        userVmResponse.setJobUuid(userVm.getJobUuid());
+        userVmResponse.setJobStatus(userVm.getJobStatus());
+        //userVmResponse.setForVirtualNetwork(userVm.getForVirtualNetwork());
+
+        userVmResponse.setPublicIpId(userVm.getPublicIpUuid());
+        userVmResponse.setPublicIp(userVm.getPublicIpAddress());
+        userVmResponse.setKeyPairName(userVm.getKeypairName());
+
+        if (details.contains(VMDetails.all) || details.contains(VMDetails.stats)) {
+            DecimalFormat decimalFormat = new DecimalFormat("#.##");
+            // stats calculation
+            String cpuUsed = null;
+            VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
+            if (vmStats != null) {
+                float cpuUtil = (float) vmStats.getCPUUtilization();
+                cpuUsed = decimalFormat.format(cpuUtil) + "%";
+                userVmResponse.setCpuUsed(cpuUsed);
+
+                Double networkKbRead = Double.valueOf(vmStats.getNetworkReadKBs());
+                userVmResponse.setNetworkKbsRead(networkKbRead.longValue());
+
+                Double networkKbWrite = Double.valueOf(vmStats.getNetworkWriteKBs());
+                userVmResponse.setNetworkKbsWrite(networkKbWrite.longValue());
+            }
+        }
+
+        if (details.contains(VMDetails.all) || details.contains(VMDetails.secgrp)) {
+            Long securityGroupId = userVm.getSecurityGroupId();
+            if (securityGroupId != null && securityGroupId.longValue() != 0) {
+                SecurityGroupResponse resp = new SecurityGroupResponse();
+                resp.setId(userVm.getSecurityGroupId()); // TODO: change
+                                                         // SecurityGroupResponse
+                                                         // IdentityProxy
+                resp.setName(userVm.getSecurityGroupName());
+                resp.setDescription(userVm.getSecurityGroupDescription());
+                resp.setObjectName("securitygroup");
+                if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
+                    resp.setProjectId(userVm.getProjectId()); // TODO: pending
+                                                              // SecurityGroupResponse
+                                                              // uuid change
+                    resp.setProjectName(userVm.getProjectName());
+                } else {
+                    resp.setAccountName(userVm.getAccountName());
+                }
+                userVmResponse.addSecurityGroup(resp);
+            }
+        }
+
+        if (details.contains(VMDetails.all) || details.contains(VMDetails.nics)) {
+            long nic_id = userVm.getNicId();
+            if (nic_id > 0) {
+                NicResponse nicResponse = new NicResponse();
+                nicResponse.setId(userVm.getNicUuid());
+                nicResponse.setIpaddress(userVm.getIpAddress());
+                nicResponse.setGateway(userVm.getGateway());
+                nicResponse.setNetmask(userVm.getNetmask());
+                nicResponse.setNetworkid(userVm.getNetworkUuid());
+                nicResponse.setMacAddress(userVm.getMacAddress());
+                if (userVm.getBroadcastUri() != null) {
+                    nicResponse.setBroadcastUri(userVm.getBroadcastUri().toString());
+                }
+                if (userVm.getIsolationUri() != null) {
+                    nicResponse.setIsolationUri(userVm.getIsolationUri().toString());
+                }
+                if (userVm.getTrafficType() != null) {
+                    nicResponse.setTrafficType(userVm.getTrafficType().toString());
+                }
+                if (userVm.getGuestType() != null) {
+                    nicResponse.setType(userVm.getGuestType().toString());
+                }
+                nicResponse.setIsDefault(userVm.isDefaultNic());
+                nicResponse.setObjectName("nic");
+                userVmResponse.addNic(nicResponse);
+            }
+        }
+
+        // update tag information
+        long tag_id = userVm.getTagId();
+        if (tag_id > 0) {
+            ResourceTagResponse tag = new ResourceTagResponse();
+            tag.setKey(userVm.getTagKey());
+            tag.setValue(userVm.getTagValue());
+            if (userVm.getTagResourceType() != null) {
+                tag.setResourceType(userVm.getTagResourceType().toString());
+            }
+            tag.setId(userVm.getTagResourceUuid()); // tag resource uuid
+            tag.setCustomer(userVm.getTagCustomer());
+            // TODO: assuming tagAccountId and tagDomainId are the same as VM
+            // accountId and domainId
+            tag.setDomainId(userVm.getTagDomainId());
+            if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
+                tag.setProjectId(userVm.getProjectId());
+                tag.setProjectName(userVm.getProjectName());
+            } else {
+                tag.setAccountName(userVm.getAccountName());
+            }
+            tag.setDomainId(userVm.getDomainId()); // TODO: pending tag resource
+                                                // response uuid change
+            tag.setDomainName(userVm.getDomainName());
+
+            tag.setObjectName("tag");
+            userVmResponse.addTag(tag);
+        }
+        userVmResponse.setObjectName(objectName);
+
+        return userVmResponse;
+       }
+
+    public UserVmResponse setUserVmResponse(UserVmResponse userVmData, UserVmJoinVO uvo) {
+        Long securityGroupId = uvo.getSecurityGroupId();
+        if (securityGroupId != null && securityGroupId.longValue() != 0) {
+            SecurityGroupResponse resp = new SecurityGroupResponse();
+            resp.setId(uvo.getSecurityGroupId()); // TODO: change
+                                                  // SecurityGroupResponse
+                                                  // IdentityProxy
+            resp.setName(uvo.getSecurityGroupName());
+            resp.setDescription(uvo.getSecurityGroupDescription());
+            resp.setObjectName("securitygroup");
+            if (uvo.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
+                resp.setProjectId(uvo.getProjectId()); // TODO: pending
+                                                       // SecurityGroupResponse
+                                                       // uuid change
+                resp.setProjectName(uvo.getProjectName());
+            } else {
+                resp.setAccountName(uvo.getAccountName());
+            }
+            userVmData.addSecurityGroup(resp);
+        }
+
+        long nic_id = uvo.getNicId();
+        if (nic_id > 0) {
+            NicResponse nicResponse = new NicResponse();
+            nicResponse.setId(uvo.getNicUuid());
+            nicResponse.setIpaddress(uvo.getIpAddress());
+            nicResponse.setGateway(uvo.getGateway());
+            nicResponse.setNetmask(uvo.getNetmask());
+            nicResponse.setNetworkid(uvo.getNetworkUuid());
+            nicResponse.setMacAddress(uvo.getMacAddress());
+            if (uvo.getBroadcastUri() != null) {
+                nicResponse.setBroadcastUri(uvo.getBroadcastUri().toString());
+            }
+            if (uvo.getIsolationUri() != null) {
+                nicResponse.setIsolationUri(uvo.getIsolationUri().toString());
+            }
+            if (uvo.getTrafficType() != null) {
+                nicResponse.setTrafficType(uvo.getTrafficType().toString());
+            }
+            if (uvo.getGuestType() != null) {
+                nicResponse.setType(uvo.getGuestType().toString());
+            }
+            nicResponse.setIsDefault(uvo.isDefaultNic());
+            nicResponse.setObjectName("nic");
+            userVmData.addNic(nicResponse);
+        }
+
+        long tag_id = uvo.getTagId();
+        if (tag_id > 0) {
+            ResourceTagResponse tag = new ResourceTagResponse();
+            tag.setKey(uvo.getTagKey());
+            tag.setValue(uvo.getTagValue());
+            if (uvo.getTagResourceType() != null) {
+                tag.setResourceType(uvo.getTagResourceType().toString());
+            }
+            tag.setId(uvo.getTagResourceUuid()); // tag resource uuid
+            tag.setCustomer(uvo.getTagCustomer());
+            // TODO: assuming tagAccountId and tagDomainId are the same as VM
+            // accountId and domainId
+            tag.setDomainId(uvo.getTagDomainId());
+            if (uvo.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
+                tag.setProjectId(uvo.getProjectId());
+                tag.setProjectName(uvo.getProjectName());
+            } else {
+                tag.setAccountName(uvo.getAccountName());
+            }
+            tag.setDomainId(uvo.getDomainId()); // TODO: pending tag resource
+                                                // response uuid change
+            tag.setDomainName(uvo.getDomainName());
+
+            tag.setObjectName("tag");
+            userVmData.addTag(tag);
+        }
+        return userVmData;
+    }
+
+
+    @Override
+    public List<UserVmJoinVO> searchByIds(Long... vmIds) {
+        SearchCriteria<UserVmJoinVO> sc = VmDetailSearch.create();
+        sc.setParameters("idIN", vmIds);
+        return searchIncludingRemoved(sc, null, null, false);
+    }
+
+    @Override
+    public List<UserVmJoinVO> newUserVmView(UserVm... userVms) {
+
+        int curr_index = 0;
+
+        Hashtable<Long, UserVm> userVmDataHash = new Hashtable<Long, UserVm>();
+        for (UserVm vm : userVms){
+            if ( !userVmDataHash.containsKey(vm.getId())){
+                userVmDataHash.put(vm.getId(), vm);
+            }
+        }
+
+        List<UserVmJoinVO> uvList = new ArrayList<UserVmJoinVO>();
+        List<Long> userVmIdList = new ArrayList(userVmDataHash.keySet());
+         if (userVmIdList.size() > VM_DETAILS_BATCH_SIZE) {
+            while ((curr_index + VM_DETAILS_BATCH_SIZE) <= userVmIdList.size()) {
+                // set current ids
+                Long[] vmIds = new Long[VM_DETAILS_BATCH_SIZE];
+                for (int k = 0, j = curr_index; j < curr_index + VM_DETAILS_BATCH_SIZE; j++, k++) {
+                    vmIds[k] = userVmIdList.get(j);
+                }
+                SearchCriteria<UserVmJoinVO> sc = VmDetailSearch.create();
+                sc.setParameters("idIN", vmIds);
+                List<UserVmJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
+                if (vms != null) {
+                    for (UserVmJoinVO uvm : vms) {
+                        uvList.add(uvm);
+                    }
+                }
+                curr_index += VM_DETAILS_BATCH_SIZE;
+            }
+        }
+
+        if (curr_index < userVmIdList.size()) {
+            int batch_size = (userVmIdList.size() - curr_index);
+            // set the ids value
+            Long[] vmIds = new Long[batch_size];
+            for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
+                vmIds[k] = userVmIdList.get(j);
+            }
+            SearchCriteria<UserVmJoinVO> sc = VmDetailSearch.create();
+            sc.setParameters("idIN", vmIds);
+            List<UserVmJoinVO> vms = searchIncludingRemoved(sc, null, null, false);
+            if (vms != null) {
+                for (UserVmJoinVO uvm : vms) {
+                    UserVm vm = userVmDataHash.get(uvm.getId());
+                    assert vm != null : "We should not find details of vm not in the passed UserVm list";
+                    uvList.add(uvm);
+                }
+            }
+        }
+        return uvList;
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/test/com/cloud/keystore/KeystoreTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/keystore/KeystoreTest.java b/server/test/com/cloud/keystore/KeystoreTest.java
index 3a0bb71..a0ffb68 100644
--- a/server/test/com/cloud/keystore/KeystoreTest.java
+++ b/server/test/com/cloud/keystore/KeystoreTest.java
@@ -153,7 +153,7 @@ public class KeystoreTest extends TestCase {
 */    
     public void testUuid() {
     	UserVmResponse vm = new UserVmResponse();
-    	vm.setId(3L);
+        vm.setId(Long.toString(3L));
 /*    	
     	vm.setAccountName("admin");
     	vm.setName("i-2-3-KY");

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/test/com/cloud/user/MockAccountManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/user/MockAccountManagerImpl.java b/server/test/com/cloud/user/MockAccountManagerImpl.java
index b8d9043..3b1adc6 100644
--- a/server/test/com/cloud/user/MockAccountManagerImpl.java
+++ b/server/test/com/cloud/user/MockAccountManagerImpl.java
@@ -30,6 +30,7 @@ import org.apache.cloudstack.api.user.account.command.ListAccountsCmd;
 import com.cloud.api.commands.RegisterCmd;
 import com.cloud.api.commands.UpdateAccountCmd;
 import org.apache.cloudstack.api.admin.user.command.UpdateUserCmd;
+import com.cloud.api.view.vo.ControlledViewEntity;
 import com.cloud.domain.Domain;
 import com.cloud.exception.ConcurrentOperationException;
 import com.cloud.exception.PermissionDeniedException;
@@ -334,6 +335,18 @@ public class MockAccountManagerImpl implements Manager, AccountManager, AccountS
         // TODO Auto-generated method stub
     }
 
+    @Override
+    public void buildACLViewSearchBuilder(SearchBuilder<? extends ControlledViewEntity> sb, Long domainId,
+            boolean isRecursive, List<Long> permittedAccounts, ListProjectResourcesCriteria listProjectResourcesCriteria) {
+        // TODO Auto-generated method stub
+    }
+
+    @Override
+    public void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledViewEntity> sc, Long domainId,
+            boolean isRecursive, List<Long> permittedAccounts, ListProjectResourcesCriteria listProjectResourcesCriteria) {
+        // TODO Auto-generated method stub
+    }
+
     /* (non-Javadoc)
      * @see com.cloud.user.AccountService#getUserByApiKey(java.lang.String)
      */

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/test/com/cloud/vm/MockUserVmManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/vm/MockUserVmManagerImpl.java b/server/test/com/cloud/vm/MockUserVmManagerImpl.java
index 71e3871..d936095 100644
--- a/server/test/com/cloud/vm/MockUserVmManagerImpl.java
+++ b/server/test/com/cloud/vm/MockUserVmManagerImpl.java
@@ -44,6 +44,7 @@ import org.apache.cloudstack.api.user.vm.command.RestoreVMCmd;
 import org.apache.cloudstack.api.user.vm.command.StartVMCmd;
 import org.apache.cloudstack.api.user.vm.command.UpdateVMCmd;
 import org.apache.cloudstack.api.user.vm.command.UpgradeVMCmd;
+import com.cloud.api.view.vo.UserVmJoinVO;
 import com.cloud.dc.DataCenter;
 import com.cloud.deploy.DeployDestination;
 import com.cloud.exception.ConcurrentOperationException;
@@ -192,7 +193,7 @@ public class MockUserVmManagerImpl implements UserVmManager, UserVmService, Mana
     }
 
     @Override
-    public Pair<List<UserVmVO>, Integer> searchForUserVMs(Criteria c, Account caller, Long domainId, boolean isRecursive, List<Long> permittedAccounts, boolean listAll, ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags) {
+    public Pair<List<UserVmJoinVO>, Integer> searchForUserVMs(Criteria c, Account caller, Long domainId, boolean isRecursive, List<Long> permittedAccounts, boolean listAll, ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags) {
         // TODO Auto-generated method stub
         return null;
     }
@@ -401,7 +402,7 @@ public class MockUserVmManagerImpl implements UserVmManager, UserVmService, Mana
 	}
 
     @Override
-    public Pair<List<? extends UserVm>, Integer> searchForUserVMs(ListVMsCmd cmd) {
+    public Pair<List<UserVmJoinVO>, Integer> searchForUserVMs(ListVMsCmd cmd) {
         // TODO Auto-generated method stub
         return null;
     }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/setup/db/create-schema.sql
----------------------------------------------------------------------
diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql
index fff084e..3f45285 100755
--- a/setup/db/create-schema.sql
+++ b/setup/db/create-schema.sql
@@ -2510,3 +2510,218 @@ INSERT INTO `cloud`.`counter` (id, source, name, value,created) VALUES (3,'snmp'
 INSERT INTO `cloud`.`counter` (id, source, name, value,created) VALUES (100,'netscaler','Response Time - microseconds', 'RESPTIME', now());
 
 SET foreign_key_checks = 1;
+
+
+--- DB views for list api ---
+DROP VIEW IF EXISTS `cloud`.`user_vm_view`;
+CREATE VIEW `cloud`.`user_vm_view` AS
+select
+vm_instance.id id,
+vm_instance.name name,
+user_vm.display_name display_name,
+user_vm.user_data user_data,
+account.id account_id,
+account.uuid account_uuid,
+account.account_name account_name,
+account.type account_type,
+domain.id domain_id,
+domain.uuid domain_uuid,
+domain.name domain_name,
+domain.path domain_path,
+projects.id project_id,
+projects.uuid project_uuid,
+projects.name project_name,
+instance_group.id instance_group_id,
+instance_group.uuid instance_group_uuid,
+instance_group.name instance_group_name,
+vm_instance.uuid uuid,
+vm_instance.last_host_id last_host_id,
+vm_instance.vm_type type,
+vm_instance.vnc_password vnc_password,
+vm_instance.limit_cpu_use limit_cpu_use,
+vm_instance.created created,
+vm_instance.state state,
+vm_instance.removed removed,
+vm_instance.ha_enabled ha_enabled,
+vm_instance.hypervisor_type hypervisor_type,
+vm_instance.instance_name instance_name,
+vm_instance.guest_os_id guest_os_id,
+guest_os.uuid guest_os_uuid,
+vm_instance.pod_id pod_id,
+host_pod_ref.uuid pod_uuid,
+vm_instance.private_ip_address private_ip_address,
+vm_instance.private_mac_address private_mac_address,
+vm_instance.vm_type vm_type,
+data_center.id data_center_id,
+data_center.uuid data_center_uuid,
+data_center.name data_center_name,
+data_center.is_security_group_enabled security_group_enabled,
+host.id host_id,
+host.uuid host_uuid,
+host.name host_name,
+vm_template.id template_id,
+vm_template.uuid template_uuid,
+vm_template.name template_name,
+vm_template.display_text template_display_text,
+vm_template.enable_password password_enabled,
+iso.id iso_id,
+iso.uuid iso_uuid,
+iso.name iso_name,
+iso.display_text iso_display_text,
+service_offering.id service_offering_id,
+disk_offering.uuid service_offering_uuid,
+service_offering.cpu cpu,
+service_offering.speed speed,
+service_offering.ram_size ram_size,
+disk_offering.name service_offering_name,
+storage_pool.id pool_id,
+storage_pool.uuid pool_uuid,
+storage_pool.pool_type pool_type,
+volumes.id volume_id,
+volumes.uuid volume_uuid,
+volumes.device_id volume_device_id,
+volumes.volume_type volume_type,
+security_group.id security_group_id,
+security_group.uuid security_group_uuid,
+security_group.name security_group_name,
+security_group.description security_group_description,
+nics.id nic_id,
+nics.uuid nic_uuid,
+nics.network_id network_id,
+nics.ip4_address ip_address,
+nics.default_nic is_default_nic,
+nics.gateway gateway,
+nics.netmask netmask,
+nics.mac_address mac_address,
+nics.broadcast_uri broadcast_uri,
+nics.isolation_uri isolation_uri,
+vpc.id vpc_id,
+vpc.uuid vpc_uuid,
+networks.uuid network_uuid,
+networks.traffic_type traffic_type,
+networks.guest_type guest_type,
+user_ip_address.id public_ip_id,
+user_ip_address.uuid public_ip_uuid,
+user_ip_address.public_ip_address public_ip_address,
+ssh_keypairs.keypair_name keypair_name,
+resource_tags.id tag_id,
+resource_tags.uuid tag_uuid,
+resource_tags.key tag_key,
+resource_tags.value tag_value,
+resource_tags.domain_id tag_domain_id,
+resource_tags.account_id tag_account_id,
+resource_tags.resource_id tag_resource_id,
+resource_tags.resource_uuid tag_resource_uuid,
+resource_tags.resource_type tag_resource_type,
+resource_tags.customer tag_customer,
+async_job.id job_id,
+async_job.uuid job_uuid,
+async_job.job_status job_status,
+async_job.account_id job_account_id
+from user_vm
+inner join vm_instance on vm_instance.id = user_vm.id and vm_instance.removed is NULL
+inner join account on vm_instance.account_id=account.id
+inner join domain on vm_instance.domain_id=domain.id
+left join guest_os on vm_instance.guest_os_id = guest_os.id
+left join host_pod_ref on vm_instance.pod_id = host_pod_ref.id
+left join projects on projects.project_account_id = account.id
+left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id
+left join instance_group on instance_group_vm_map.group_id=instance_group.id
+left join data_center on vm_instance.data_center_id=data_center.id
+left join host on vm_instance.host_id=host.id
+left join vm_template on vm_instance.vm_template_id=vm_template.id
+left join vm_template iso on iso.id=user_vm.iso_id
+left join service_offering on vm_instance.service_offering_id=service_offering.id
+left join disk_offering  on vm_instance.service_offering_id=disk_offering.id
+left join volumes on vm_instance.id=volumes.instance_id
+left join storage_pool on volumes.pool_id=storage_pool.id
+left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id
+left join security_group on security_group_vm_map.security_group_id=security_group.id
+left join nics on vm_instance.id=nics.instance_id
+left join networks on nics.network_id=networks.id
+left join vpc on networks.vpc_id = vpc.id
+left join user_ip_address on user_ip_address.vm_id=vm_instance.id
+left join user_vm_details on user_vm_details.vm_id=vm_instance.id and user_vm_details.name = "SSH.PublicKey"
+left join ssh_keypairs on ssh_keypairs.public_key = user_vm_details.value
+left join resource_tags on resource_tags.resource_id = vm_instance.id and resource_tags.resource_type = "UserVm"
+left join async_job on async_job.instance_id = vm_instance.id and async_job.instance_type = "VirtualMachine" and async_job.job_status = 0;
+
+DROP VIEW IF EXISTS `cloud`.`domain_router_view`;
+CREATE VIEW domain_router_view AS
+select
+vm_instance.id id,
+vm_instance.name name,
+account.id account_id,
+account.uuid account_uuid,
+account.account_name account_name,
+account.type account_type,
+domain.id domain_id,
+domain.uuid domain_uuid,
+domain.name domain_name,
+domain.path domain_path,
+projects.id project_id,
+projects.uuid project_uuid,
+projects.name project_name,
+vm_instance.uuid uuid,
+vm_instance.created created,
+vm_instance.state state,
+vm_instance.removed removed,
+vm_instance.pod_id pod_id,
+vm_instance.instance_name instance_name,
+host_pod_ref.uuid pod_uuid,
+data_center.id data_center_id,
+data_center.uuid data_center_uuid,
+data_center.name data_center_name,
+data_center.dns1 dns1,
+data_center.dns2 dns2,
+host.id host_id,
+host.uuid host_uuid,
+host.name host_name,
+vm_template.id template_id,
+vm_template.uuid template_uuid,
+service_offering.id service_offering_id,
+disk_offering.uuid service_offering_uuid,
+disk_offering.name service_offering_name,
+nics.id nic_id,
+nics.uuid nic_uuid,
+nics.network_id network_id,
+nics.ip4_address ip_address,
+nics.default_nic is_default_nic,
+nics.gateway gateway,
+nics.netmask netmask,
+nics.mac_address mac_address,
+nics.broadcast_uri broadcast_uri,
+nics.isolation_uri isolation_uri,
+vpc.id vpc_id,
+vpc.uuid vpc_uuid,
+networks.uuid network_uuid,
+networks.name network_name,
+networks.network_domain network_domain,
+networks.traffic_type traffic_type,
+networks.guest_type guest_type,
+async_job.id job_id,
+async_job.uuid job_uuid,
+async_job.job_status job_status,
+async_job.account_id job_account_id,
+domain_router.template_version template_version,
+domain_router.scripts_version scripts_version,
+domain_router.is_redundant_router is_redundant_router,
+domain_router.redundant_state redundant_state,
+domain_router.stop_pending stop_pending
+from domain_router
+inner join vm_instance on vm_instance.id = domain_router.id
+inner join account on vm_instance.account_id=account.id
+inner join domain on vm_instance.domain_id=domain.id
+left join host_pod_ref on vm_instance.pod_id = host_pod_ref.id
+left join projects on projects.project_account_id = account.id
+left join data_center on vm_instance.data_center_id=data_center.id
+left join host on vm_instance.host_id=host.id
+left join vm_template on vm_instance.vm_template_id=vm_template.id
+left join service_offering on vm_instance.service_offering_id=service_offering.id
+left join disk_offering  on vm_instance.service_offering_id=disk_offering.id
+left join volumes on vm_instance.id=volumes.instance_id
+left join storage_pool on volumes.pool_id=storage_pool.id
+left join nics on vm_instance.id=nics.instance_id
+left join networks on nics.network_id=networks.id
+left join vpc on networks.vpc_id = vpc.id
+left join async_job on async_job.instance_id = vm_instance.id and async_job.instance_type = "DomainRouter" and async_job.job_status = 0;

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/utils/src/com/cloud/utils/db/GenericDaoBase.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/db/GenericDaoBase.java b/utils/src/com/cloud/utils/db/GenericDaoBase.java
index 8d5cb96..be0fe89 100755
--- a/utils/src/com/cloud/utils/db/GenericDaoBase.java
+++ b/utils/src/com/cloud/utils/db/GenericDaoBase.java
@@ -50,6 +50,7 @@ import javax.persistence.EmbeddedId;
 import javax.persistence.EntityExistsException;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
+import javax.persistence.Table;
 import javax.persistence.TableGenerator;
 
 import net.sf.cglib.proxy.Callback;
@@ -1653,6 +1654,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> implements Gene
     @DB(txn=false)
     protected void setField(final Object entity, final ResultSet rs, ResultSetMetaData meta, final int index) throws SQLException {
         Attribute attr = _allColumns.get(new Pair<String, String>(meta.getTableName(index), meta.getColumnName(index)));
+        if ( attr == null ){
+            // work around for mysql bug to return original table name instead of view name in db view case
+            Table tbl = entity.getClass().getSuperclass().getAnnotation(Table.class);
+            if ( tbl != null ){
+                attr = _allColumns.get(new Pair<String, String>(tbl.name(), meta.getColumnLabel(index)));
+            }
+        }
         assert (attr != null) : "How come I can't find " + meta.getCatalogName(index) + "." + meta.getColumnName(index);
         setField(entity, attr.field, rs, index);
     }
@@ -1798,7 +1806,8 @@ public abstract class GenericDaoBase<T, ID extends Serializable> implements Gene
             }
         }
 
-        List<Object> groupByValues = addGroupBy(str, sc);
+        // we have to disable group by in getting count, since count for groupBy clause will be different.
+        //List<Object> groupByValues = addGroupBy(str, sc);
         final Transaction txn = Transaction.currentTxn();
         final String sql = str.toString();
 
@@ -1816,11 +1825,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> implements Gene
                 i = addJoinAttributes(i, pstmt, joins);
             }
             
+            /*
             if (groupByValues != null) {
                 for (Object value : groupByValues) {
                     pstmt.setObject(i++, value);
                 }
             }
+            */
 
             final ResultSet rs = pstmt.executeQuery();
             while (rs.next()) {


Mime
View raw message