cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bhais...@apache.org
Subject [2/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/api/src/org/apache/cloudstack/api/admin/router/command/ListRoutersCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/admin/router/command/ListRoutersCmd.java b/api/src/org/apache/cloudstack/api/admin/router/command/ListRoutersCmd.java
index 1659bea..e517dc1 100644
--- a/api/src/org/apache/cloudstack/api/admin/router/command/ListRoutersCmd.java
+++ b/api/src/org/apache/cloudstack/api/admin/router/command/ListRoutersCmd.java
@@ -17,6 +17,7 @@
 package org.apache.cloudstack.api.admin.router.command;
 
 import java.util.ArrayList;
+import java.util.EnumSet;
 import java.util.List;
 
 import org.apache.log4j.Logger;
@@ -26,8 +27,17 @@ import org.apache.cloudstack.api.BaseListProjectAndAccountResourcesCmd;
 import org.apache.cloudstack.api.IdentityMapper;
 import org.apache.cloudstack.api.Implementation;
 import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.ApiConstants.VMDetails;
+
+import com.cloud.api.response.UserVmResponse;
+import com.cloud.api.view.vo.DomainRouterJoinVO;
+import com.cloud.api.view.vo.UserVmJoinVO;
+
 import com.cloud.api.response.DomainRouterResponse;
 import com.cloud.api.response.ListResponse;
+import com.cloud.api.response.UserVmResponse;
+import com.cloud.api.view.vo.DomainRouterJoinVO;
+import com.cloud.api.view.vo.UserVmJoinVO;
 import com.cloud.async.AsyncJob;
 import com.cloud.network.router.VirtualRouter;
 import com.cloud.utils.Pair;
@@ -130,15 +140,10 @@ public class ListRoutersCmd extends BaseListProjectAndAccountResourcesCmd {
 
     @Override
     public void execute(){
-        Pair<List<? extends VirtualRouter>, Integer> result = _mgr.searchForRouters(this);
+        Pair<List<DomainRouterJoinVO>, Integer> result = _mgr.searchForRouters(this);
         ListResponse<DomainRouterResponse> response = new ListResponse<DomainRouterResponse>();
-        List<DomainRouterResponse> routerResponses = new ArrayList<DomainRouterResponse>();
-        for (VirtualRouter router : result.first()) {
-            DomainRouterResponse routerResponse = _responseGenerator.createDomainRouterResponse(router);
-            routerResponse.setObjectName("router");
-            routerResponses.add(routerResponse);
-        }
 
+        List<DomainRouterResponse> routerResponses = _responseGenerator.createDomainRouterResponse(result.first().toArray(new DomainRouterJoinVO[result.first().size()]));
         response.setResponses(routerResponses, result.second());
         response.setResponseName(getCommandName());
         this.setResponseObject(response);

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/api/src/org/apache/cloudstack/api/user/vm/command/ListVMsCmd.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/user/vm/command/ListVMsCmd.java b/api/src/org/apache/cloudstack/api/user/vm/command/ListVMsCmd.java
index 4acee5c..9ed9bb3 100644
--- a/api/src/org/apache/cloudstack/api/user/vm/command/ListVMsCmd.java
+++ b/api/src/org/apache/cloudstack/api/user/vm/command/ListVMsCmd.java
@@ -32,8 +32,8 @@ import com.cloud.api.response.ListResponse;
 import com.cloud.api.response.UserVmResponse;
 import com.cloud.async.AsyncJob;
 import com.cloud.exception.InvalidParameterValueException;
-import com.cloud.uservm.UserVm;
 import com.cloud.utils.Pair;
+import com.cloud.api.view.vo.UserVmJoinVO;
 
 
 @Implementation(description="List the virtual machines owned by the account.", responseObject=UserVmResponse.class)
@@ -199,16 +199,10 @@ public class ListVMsCmd extends BaseListTaggedResourcesCmd {
 
     @Override
     public void execute(){
-        Pair<List<? extends UserVm>, Integer> result = _userVmService.searchForUserVMs(this);
+        Pair<List<UserVmJoinVO>, Integer> result = _userVmService.searchForUserVMs(this);
         ListResponse<UserVmResponse> response = new ListResponse<UserVmResponse>();
         EnumSet<VMDetails> details = getDetails();
-        List<UserVmResponse> vmResponses;
-        if (details.contains(VMDetails.all)){ // for all use optimized version
-            vmResponses = _responseGenerator.createUserVmResponse("virtualmachine", result.first().toArray(new UserVm[result.first().size()]));
-        }
-        else {
-             vmResponses = _responseGenerator.createUserVmResponse("virtualmachine", getDetails(), result.first().toArray(new UserVm[result.first().size()]));
-        }
+        List<UserVmResponse> vmResponses = _responseGenerator.createUserVmResponse("virtualmachine", getDetails(), result.first().toArray(new UserVmJoinVO[result.first().size()]));
         response.setResponses(vmResponses, result.second());
         response.setResponseName(getCommandName());
         this.setResponseObject(response);

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/api/test/src/com/cloud/api/commands/test/ListRoutersCmdTest.java
----------------------------------------------------------------------
diff --git a/api/test/src/com/cloud/api/commands/test/ListRoutersCmdTest.java b/api/test/src/com/cloud/api/commands/test/ListRoutersCmdTest.java
new file mode 100644
index 0000000..d922474
--- /dev/null
+++ b/api/test/src/com/cloud/api/commands/test/ListRoutersCmdTest.java
@@ -0,0 +1,170 @@
+// 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 src.com.cloud.api.commands.test;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+
+
+import com.cloud.api.ResponseGenerator;
+import com.cloud.api.ServerApiException;
+import com.cloud.api.commands.ListRoutersCmd;
+import com.cloud.api.response.DomainRouterResponse;
+import com.cloud.api.response.ListResponse;
+import com.cloud.api.view.vo.DomainRouterJoinVO;
+import com.cloud.server.ManagementService;
+import com.cloud.utils.Pair;
+
+
+public class ListRoutersCmdTest extends TestCase {
+
+    private ListRoutersCmd listVrCmd;
+    private ManagementService mgrService;
+    private ResponseGenerator responseGenerator;
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Before
+    public void setUp() {
+        listVrCmd = new ListRoutersCmd();
+        mgrService = Mockito.mock(ManagementService.class);
+        responseGenerator = Mockito.mock(ResponseGenerator.class);
+
+        listVrCmd._mgr = mgrService;
+        listVrCmd._responseGenerator = responseGenerator;
+    }
+
+    @Test
+    public void testExecuteForSingleResult() throws Exception {
+
+        List<DomainRouterJoinVO> vrList = new ArrayList<DomainRouterJoinVO>();
+        DomainRouterJoinVO vr = new DomainRouterJoinVO();
+        String uuid = UUID.randomUUID().toString();
+        vr.setUuid(uuid);
+        vrList.add(vr);
+
+        List<DomainRouterResponse> respList = new ArrayList<DomainRouterResponse>();
+        DomainRouterResponse resp = new DomainRouterResponse();
+        resp.setId(uuid);
+        respList.add(resp);
+
+        Mockito.when(mgrService.searchForRouters(listVrCmd))
+                .thenReturn(new Pair<List<DomainRouterJoinVO>, Integer>(vrList, 1));
+        Mockito.when(responseGenerator.createDomainRouterResponse(vr)).thenReturn(respList);
+
+
+        try {
+            listVrCmd.execute();
+            ListResponse<DomainRouterResponse> listResp = (ListResponse<DomainRouterResponse>)listVrCmd.getResponseObject();
+            assertNotNull(listResp);
+            assertEquals(1, listResp.getCount().intValue());
+            List<DomainRouterResponse> vrResp = listResp.getResponses();
+            assertTrue(vrResp != null && vrResp.size() == 1);
+            DomainRouterResponse v = vrResp.get(0);
+            assertEquals(uuid, v.getId());
+        } catch (ServerApiException exception) {
+            assertEquals("Failed to list domain routers",
+                    exception.getDescription());
+        }
+    }
+
+
+    @Test
+    public void testExecuteForPagedResult() throws Exception {
+
+
+        List<DomainRouterJoinVO> vrList = new ArrayList<DomainRouterJoinVO>();
+        DomainRouterJoinVO vr1 = new DomainRouterJoinVO();
+        String uuid1 = UUID.randomUUID().toString();
+        vr1.setUuid(uuid1);
+        vrList.add(vr1);
+        DomainRouterJoinVO vr2 = new DomainRouterJoinVO();
+        String uuid2 = UUID.randomUUID().toString();
+        vrList.add(vr2);
+
+        List<DomainRouterResponse> respList = new ArrayList<DomainRouterResponse>();
+        DomainRouterResponse resp1 = new DomainRouterResponse();
+        resp1.setId(uuid1);
+        respList.add(resp1);
+        DomainRouterResponse resp2 = new DomainRouterResponse();
+        resp2.setId(uuid2);
+        respList.add(resp2);
+
+        // without paging
+        Mockito.when(mgrService.searchForRouters(listVrCmd))
+                    .thenReturn(new Pair<List<DomainRouterJoinVO>, Integer>(vrList, 2));
+        Mockito.when(responseGenerator.createDomainRouterResponse(vr1, vr2)).thenReturn(respList);
+        try {
+            listVrCmd.execute();
+            ListResponse<DomainRouterResponse> listResp = (ListResponse<DomainRouterResponse>)listVrCmd.getResponseObject();
+            assertNotNull(listResp);
+            assertEquals(2, listResp.getCount().intValue());
+            List<DomainRouterResponse> vrResp = listResp.getResponses();
+            assertTrue(vrResp != null && vrResp.size() == 2);
+        } catch (ServerApiException exception) {
+            assertEquals("Failed to list domain routers without pagination",
+                    exception.getDescription());
+        }
+
+        // with pagination
+        List<DomainRouterJoinVO> pVrList = new ArrayList<DomainRouterJoinVO>();
+        pVrList.add(vr1);
+
+        List<DomainRouterResponse> pRespList = new ArrayList<DomainRouterResponse>();
+        pRespList.add(resp1);
+
+        listVrCmd = new ListRoutersCmd() {
+            public Integer getPage() {
+                return 1;
+            }
+
+            public Integer getPageSize() {
+                return 1;
+            }
+
+        };
+        Mockito.when(mgrService.searchForRouters(listVrCmd))
+                .thenReturn(new Pair<List<DomainRouterJoinVO>, Integer>(pVrList, 2));
+        Mockito.when(responseGenerator.createDomainRouterResponse(vr1)).thenReturn(pRespList);
+
+        try {
+            listVrCmd.execute();
+            ListResponse<DomainRouterResponse> listResp = (ListResponse<DomainRouterResponse>)listVrCmd.getResponseObject();
+            assertNotNull(listResp);
+            assertEquals(2, listResp.getCount().intValue());
+            List<DomainRouterResponse> vmResp = listResp.getResponses();
+            assertTrue(vmResp != null && vmResp.size() == 1);
+            DomainRouterResponse v = vmResp.get(0);
+            assertEquals(uuid1, v.getId());
+        } catch (ServerApiException exception) {
+            assertEquals("Failed to list domain routers with pagination",
+                    exception.getDescription());
+        }
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/api/test/src/com/cloud/api/commands/test/ListVmsCmdTest.java
----------------------------------------------------------------------
diff --git a/api/test/src/com/cloud/api/commands/test/ListVmsCmdTest.java b/api/test/src/com/cloud/api/commands/test/ListVmsCmdTest.java
new file mode 100644
index 0000000..ca6d95e
--- /dev/null
+++ b/api/test/src/com/cloud/api/commands/test/ListVmsCmdTest.java
@@ -0,0 +1,173 @@
+// 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 src.com.cloud.api.commands.test;
+
+import java.util.ArrayList;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+
+import com.cloud.api.ApiConstants;
+import com.cloud.api.ApiConstants.VMDetails;
+import com.cloud.api.BaseCmd;
+import com.cloud.api.ResponseGenerator;
+import com.cloud.api.ServerApiException;
+import com.cloud.api.commands.ListVMsCmd;
+import com.cloud.api.response.ListResponse;
+import com.cloud.api.response.UserVmResponse;
+import com.cloud.api.view.vo.UserVmJoinVO;
+import com.cloud.utils.Pair;
+import com.cloud.vm.UserVmService;
+
+public class ListVmsCmdTest extends TestCase {
+
+    private ListVMsCmd listVmCmd;
+    private UserVmService userVmService;
+    private ResponseGenerator responseGenerator;
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Before
+    public void setUp() {
+        listVmCmd = new ListVMsCmd();
+        userVmService = Mockito.mock(UserVmService.class);
+        responseGenerator = Mockito.mock(ResponseGenerator.class);
+
+        listVmCmd._userVmService = userVmService;
+        listVmCmd._responseGenerator = responseGenerator;
+    }
+
+    @Test
+    public void testExecuteForSingleResult() throws Exception {
+
+        List<UserVmJoinVO> vmList = new ArrayList<UserVmJoinVO>();
+        UserVmJoinVO vm = new UserVmJoinVO();
+        String uuid = UUID.randomUUID().toString();
+        vm.setUuid(uuid);
+        vmList.add(vm);
+
+        List<UserVmResponse> respList = new ArrayList<UserVmResponse>();
+        UserVmResponse resp = new UserVmResponse();
+        resp.setId(uuid);
+        respList.add(resp);
+
+        Mockito.when(userVmService.searchForUserVMs(listVmCmd))
+                .thenReturn(new Pair<List<UserVmJoinVO>, Integer>(vmList, 1));
+        Mockito.when(responseGenerator.createUserVmResponse("virtualmachine", EnumSet.of(VMDetails.all), vm)).thenReturn(respList);
+
+
+        try {
+            listVmCmd.execute();
+            ListResponse<UserVmResponse> listResp = (ListResponse<UserVmResponse>)listVmCmd.getResponseObject();
+            assertNotNull(listResp);
+            assertEquals(1, listResp.getCount().intValue());
+            List<UserVmResponse> vmResp = listResp.getResponses();
+            assertTrue(vmResp != null && vmResp.size() == 1);
+            UserVmResponse v = vmResp.get(0);
+            assertEquals(uuid, v.getId());
+        } catch (ServerApiException exception) {
+            assertEquals("Failed to list user vms",
+                    exception.getDescription());
+        }
+    }
+
+
+    @Test
+    public void testExecuteForPagedResult() throws Exception {
+
+
+        List<UserVmJoinVO> vmList = new ArrayList<UserVmJoinVO>();
+        UserVmJoinVO vm1 = new UserVmJoinVO();
+        String uuid1 = UUID.randomUUID().toString();
+        vm1.setUuid(uuid1);
+        vmList.add(vm1);
+        UserVmJoinVO vm2 = new UserVmJoinVO();
+        String uuid2 = UUID.randomUUID().toString();
+        vmList.add(vm2);
+
+        List<UserVmResponse> respList = new ArrayList<UserVmResponse>();
+        UserVmResponse resp1 = new UserVmResponse();
+        resp1.setId(uuid1);
+        respList.add(resp1);
+        UserVmResponse resp2 = new UserVmResponse();
+        resp2.setId(uuid2);
+        respList.add(resp2);
+
+        // without paging
+        Mockito.when(userVmService.searchForUserVMs(listVmCmd))
+                    .thenReturn(new Pair<List<UserVmJoinVO>, Integer>(vmList, 2));
+        Mockito.when(responseGenerator.createUserVmResponse("virtualmachine", EnumSet.of(VMDetails.all), vm1, vm2)).thenReturn(respList);
+        try {
+            listVmCmd.execute();
+            ListResponse<UserVmResponse> listResp = (ListResponse<UserVmResponse>)listVmCmd.getResponseObject();
+            assertNotNull(listResp);
+            assertEquals(2, listResp.getCount().intValue());
+            List<UserVmResponse> vmResp = listResp.getResponses();
+            assertTrue(vmResp != null && vmResp.size() == 2);
+        } catch (ServerApiException exception) {
+            assertEquals("Failed to list user vms without pagination",
+                    exception.getDescription());
+        }
+
+        // with pagination
+        List<UserVmJoinVO> pVmList = new ArrayList<UserVmJoinVO>();
+        pVmList.add(vm1);
+
+        List<UserVmResponse> pRespList = new ArrayList<UserVmResponse>();
+        pRespList.add(resp1);
+
+        listVmCmd = new ListVMsCmd() {
+            public Integer getPage() {
+                return 1;
+            }
+
+            public Integer getPageSize() {
+                return 1;
+            }
+
+        };
+        Mockito.when(userVmService.searchForUserVMs(listVmCmd))
+                .thenReturn(new Pair<List<UserVmJoinVO>, Integer>(pVmList, 2));
+        Mockito.when(responseGenerator.createUserVmResponse("virtualmachine", EnumSet.of(VMDetails.all), vm1)).thenReturn(pRespList);
+
+        try {
+            listVmCmd.execute();
+            ListResponse<UserVmResponse> listResp = (ListResponse<UserVmResponse>)listVmCmd.getResponseObject();
+            assertNotNull(listResp);
+            assertEquals(2, listResp.getCount().intValue());
+            List<UserVmResponse> vmResp = listResp.getResponses();
+            assertTrue(vmResp != null && vmResp.size() == 1);
+            UserVmResponse v = vmResp.get(0);
+            assertEquals(uuid1, v.getId());
+        } catch (ServerApiException exception) {
+            assertEquals("Failed to list user vms with pagination",
+                    exception.getDescription());
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/api/ApiDBUtils.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiDBUtils.java b/server/src/com/cloud/api/ApiDBUtils.java
index 3b5f634..5dc2412 100755
--- a/server/src/com/cloud/api/ApiDBUtils.java
+++ b/server/src/com/cloud/api/ApiDBUtils.java
@@ -17,11 +17,16 @@
 package com.cloud.api;
 
 import java.util.ArrayList;
-import java.util.Hashtable;
+import java.util.EnumSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.cloudstack.api.ApiConstants.VMDetails;
+import com.cloud.api.response.DomainRouterResponse;
+import com.cloud.api.response.UserVmResponse;
+import com.cloud.api.view.vo.DomainRouterJoinVO;
+import com.cloud.api.view.vo.UserVmJoinVO;
 import com.cloud.async.AsyncJobManager;
 import com.cloud.async.AsyncJobVO;
 import com.cloud.capacity.CapacityVO;
@@ -84,6 +89,7 @@ import com.cloud.network.dao.NetworkDomainDao;
 import com.cloud.network.dao.NetworkRuleConfigDao;
 import com.cloud.network.dao.Site2SiteVpnGatewayDao;
 import com.cloud.network.dao.Site2SiteCustomerGatewayDao;
+import com.cloud.network.router.VirtualRouter;
 import com.cloud.network.security.SecurityGroup;
 import com.cloud.network.security.SecurityGroupManager;
 import com.cloud.network.security.SecurityGroupVO;
@@ -160,9 +166,11 @@ import com.cloud.vm.VirtualMachine;
 import com.cloud.vm.VmStats;
 import com.cloud.vm.dao.ConsoleProxyDao;
 import com.cloud.vm.dao.DomainRouterDao;
+import com.cloud.vm.dao.DomainRouterJoinDao;
 import com.cloud.vm.dao.UserVmDao;
 import com.cloud.vm.dao.UserVmData;
 import com.cloud.vm.dao.UserVmDetailsDao;
+import com.cloud.vm.dao.UserVmJoinDao;
 import com.cloud.vm.dao.VMInstanceDao;
 
 public class ApiDBUtils {
@@ -181,6 +189,7 @@ public class ApiDBUtils {
     private static DiskOfferingDao _diskOfferingDao;
     private static DomainDao _domainDao;
     private static DomainRouterDao _domainRouterDao;
+    private static DomainRouterJoinDao _domainRouterJoinDao;
     private static GuestOSDao _guestOSDao;
     private static GuestOSCategoryDao _guestOSCategoryDao;
     private static HostDao _hostDao;
@@ -200,6 +209,7 @@ public class ApiDBUtils {
     private static UserDao _userDao;
     private static UserStatisticsDao _userStatsDao;
     private static UserVmDao _userVmDao;
+    private static UserVmJoinDao _userVmJoinDao;
     private static VlanDao _vlanDao;
     private static VolumeDao _volumeDao;
     private static Site2SiteVpnGatewayDao _site2SiteVpnGatewayDao;
@@ -246,6 +256,7 @@ public class ApiDBUtils {
         _diskOfferingDao = locator.getDao(DiskOfferingDao.class);
         _domainDao = locator.getDao(DomainDao.class);
         _domainRouterDao = locator.getDao(DomainRouterDao.class);
+        _domainRouterJoinDao = locator.getDao(DomainRouterJoinDao.class);
         _guestOSDao = locator.getDao(GuestOSDao.class);
         _guestOSCategoryDao = locator.getDao(GuestOSCategoryDao.class);
         _hostDao = locator.getDao(HostDao.class);
@@ -264,6 +275,7 @@ public class ApiDBUtils {
         _userDao = locator.getDao(UserDao.class);
         _userStatsDao = locator.getDao(UserStatisticsDao.class);
         _userVmDao = locator.getDao(UserVmDao.class);
+        _userVmJoinDao = locator.getDao(UserVmJoinDao.class);
         _vlanDao = locator.getDao(VlanDao.class);
         _volumeDao = locator.getDao(VolumeDao.class);
         _site2SiteVpnGatewayDao = locator.getDao(Site2SiteVpnGatewayDao.class);
@@ -344,7 +356,7 @@ public class ApiDBUtils {
         return _ms.getVersion();
     }
 
-    public static List<UserVmVO> searchForUserVMs(Criteria c, List<Long> permittedAccounts) {
+    public static List<UserVmJoinVO> searchForUserVMs(Criteria c, List<Long> permittedAccounts) {
         return _userVmMgr.searchForUserVMs(c, _accountDao.findById(Account.ACCOUNT_ID_SYSTEM),
                 null, false, permittedAccounts, false, null, null).first();
     }
@@ -745,10 +757,6 @@ public class ApiDBUtils {
         return _firewallCidrsDao.getSourceCidrs(id);
     }
 
-    public static Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> vmData){
-        return _userVmDao.listVmDetails(vmData);
-    }
-
     public static Account getProjectOwner(long projectId) {
         return _projectMgr.getProjectOwner(projectId);
     }
@@ -889,3 +897,33 @@ public class ApiDBUtils {
     public static CounterVO getCounter(long counterId) {
         return _counterDao.findById(counterId);
     }}
+
+
+    ///////////////////////////////////////////////////////////////////////
+    //  Newly Added Utility Methods for List API refactoring             //
+    ///////////////////////////////////////////////////////////////////////
+
+    public static DomainRouterResponse newDomainRouterResponse(DomainRouterJoinVO vr, Account caller) {
+        return _domainRouterJoinDao.newDomainRouterResponse(vr, caller);
+    }
+
+    public static DomainRouterResponse fillRouterDetails(DomainRouterResponse vrData, DomainRouterJoinVO vr){
+         return _domainRouterJoinDao.setDomainRouterResponse(vrData, vr);
+    }
+
+    public static List<DomainRouterJoinVO> newDomainRouterView(VirtualRouter vr){
+        return _domainRouterJoinDao.newDomainRouterView(vr);
+    }
+
+    public static UserVmResponse newUserVmResponse(String objectName, UserVmJoinVO userVm, EnumSet<VMDetails> details, Account caller) {
+        return _userVmJoinDao.newUserVmResponse(objectName, userVm, details, caller);
+    }
+
+    public static UserVmResponse fillVmDetails(UserVmResponse vmData, UserVmJoinVO vm){
+         return _userVmJoinDao.setUserVmResponse(vmData, vm);
+    }
+
+    public static List<UserVmJoinVO> newUserVmView(UserVm... userVms){
+        return _userVmJoinDao.newUserVmView(userVms);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/api/ApiResponseHelper.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java
index 24fb480..84950d3 100755
--- a/server/src/com/cloud/api/ApiResponseHelper.java
+++ b/server/src/com/cloud/api/ApiResponseHelper.java
@@ -63,6 +63,7 @@ import com.cloud.api.response.FirewallResponse;
 import com.cloud.api.response.FirewallRuleResponse;
 import com.cloud.api.response.HostResponse;
 import com.cloud.api.response.HypervisorCapabilitiesResponse;
+import com.cloud.api.response.ControlledViewEntityResponse;
 import com.cloud.api.response.IPAddressResponse;
 import com.cloud.api.response.InstanceGroupResponse;
 import com.cloud.api.response.IpForwardingRuleResponse;
@@ -115,6 +116,9 @@ import com.cloud.api.response.VpcOfferingResponse;
 import com.cloud.api.response.VpcResponse;
 import com.cloud.api.response.VpnUsersResponse;
 import com.cloud.api.response.ZoneResponse;
+import com.cloud.api.view.vo.DomainRouterJoinVO;
+import com.cloud.api.view.vo.ControlledViewEntity;
+import com.cloud.api.view.vo.UserVmJoinVO;
 import com.cloud.async.AsyncJob;
 import com.cloud.capacity.Capacity;
 import com.cloud.capacity.CapacityVO;
@@ -360,12 +364,12 @@ public class ApiResponseHelper implements ResponseGenerator {
         List<Long> permittedAccounts = new ArrayList<Long>();
         permittedAccounts.add(account.getId());
 
-        List<? extends UserVm> virtualMachines = ApiDBUtils.searchForUserVMs(new Criteria(), permittedAccounts);
+        List<UserVmJoinVO> virtualMachines = ApiDBUtils.searchForUserVMs(new Criteria(), permittedAccounts);
 
         // get Running/Stopped VMs
-        for (Iterator<? extends UserVm> iter = virtualMachines.iterator(); iter.hasNext();) {
+        for (Iterator<UserVmJoinVO> iter = virtualMachines.iterator(); iter.hasNext();) {
             // count how many stopped/running vms we have
-            UserVm vm = iter.next();
+            UserVmJoinVO vm = iter.next();
 
             if (vm.getState() == State.Stopped) {
                 vmStopped++;
@@ -1386,400 +1390,72 @@ public class ApiResponseHelper implements ResponseGenerator {
 
     @Override
     public List<UserVmResponse> createUserVmResponse(String objectName, EnumSet<VMDetails> details, UserVm... userVms) {
-        Account caller = UserContext.current().getCaller();
-        Map<Long, DataCenter> dataCenters = new HashMap<Long, DataCenter>();
-        Map<Long, Host> hosts = new HashMap<Long, Host>();
-        Map<Long, VMTemplateVO> templates = new HashMap<Long, VMTemplateVO>();
-        Map<Long, ServiceOffering> serviceOfferings = new HashMap<Long, ServiceOffering>();
-        Map<Long, Network> networks = new HashMap<Long, Network>();
-
-        List<UserVmResponse> vmResponses = new ArrayList<UserVmResponse>();
-
-        for (UserVm userVm : userVms) {
-            UserVmResponse userVmResponse = new UserVmResponse();
-            Account acct = ApiDBUtils.findAccountById(Long.valueOf(userVm.getAccountId()));
-            if (acct != null) {
-                userVmResponse.setAccountName(acct.getAccountName());
-                userVmResponse.setDomainId(acct.getDomainId());
-                userVmResponse.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName());
-            }
-
-            userVmResponse.setId(userVm.getId());
-            userVmResponse.setName(userVm.getHostName());
-            userVmResponse.setCreated(userVm.getCreated());
-
-            userVmResponse.setHaEnable(userVm.isHaEnabled());
-
-            if (userVm.getDisplayName() != null) {
-                userVmResponse.setDisplayName(userVm.getDisplayName());
-            } else {
-                userVmResponse.setDisplayName(userVm.getHostName());
-            }
-
-            if (caller.getType() == Account.ACCOUNT_TYPE_ADMIN) {
-                userVmResponse.setInstanceName(userVm.getInstanceName());
-            }
-
-
-            if (userVm.getPassword() != null) {
-                userVmResponse.setPassword(userVm.getPassword());
-            }
-
-            if (details.contains(VMDetails.all) || details.contains(VMDetails.group)) {
-                InstanceGroupVO group = ApiDBUtils.findInstanceGroupForVM(userVm.getId());
-                if (group != null) {
-                    userVmResponse.setGroup(group.getName());
-                    userVmResponse.setGroupId(group.getId());
-                }
-
-            }
-
-            // Data Center Info
-            DataCenter zone = dataCenters.get(userVm.getDataCenterIdToDeployIn());
-            if (zone == null) {
-                zone = ApiDBUtils.findZoneById(userVm.getDataCenterIdToDeployIn());
-                dataCenters.put(zone.getId(), zone);
-            }
-
-            userVmResponse.setZoneId(zone.getId());
-            userVmResponse.setZoneName(zone.getName());
-
-            // if user is an admin, display host id
-            if (((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN)) && (userVm.getHostId() != null)) {
-                Host host = hosts.get(userVm.getHostId());
-
-                if (host == null) {
-                    host = ApiDBUtils.findHostById(userVm.getHostId());
-                    hosts.put(host.getId(), host);
-                }
-
-                userVmResponse.setHostId(host.getId());
-                userVmResponse.setHostName(host.getName());
-            }
-
-            if (userVm.getState() != null) {
-                if (userVm.getHostId() != null) {
-                    Host host = hosts.get(userVm.getHostId());
-
-                    if (host == null) {
-                        host = ApiDBUtils.findHostById(userVm.getHostId());
-                        hosts.put(host.getId(), host);
-                    }
-                    if (host.getStatus() != com.cloud.host.Status.Up) {
-                        userVmResponse.setState(VirtualMachine.State.Unknown.toString());
-                    } else {
-                        userVmResponse.setState(userVm.getState().toString());
-                    }
-                } else {
-                    userVmResponse.setState(userVm.getState().toString());
-                }
-            }
-
-            if (userVm.getHypervisorType() != null) {
-                userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
-            }
-
-            if (details.contains(VMDetails.all) || details.contains(VMDetails.tmpl)) {
-                // Template Info
-                VMTemplateVO template = templates.get(userVm.getTemplateId());
-                if (template == null) {
-                    template = ApiDBUtils.findTemplateById(userVm.getTemplateId());
-                    if (template != null) {
-                        templates.put(template.getId(), template);
-                    }
-                }
-
-                if (template != null) {
-                    userVmResponse.setTemplateId(userVm.getTemplateId());
-                    userVmResponse.setTemplateName(template.getName());
-                    userVmResponse.setTemplateDisplayText(template.getDisplayText());
-                    userVmResponse.setPasswordEnabled(template.getEnablePassword());
-                } else {
-                    userVmResponse.setTemplateId(-1L);
-                    userVmResponse.setTemplateName("ISO Boot");
-                    userVmResponse.setTemplateDisplayText("ISO Boot");
-                    userVmResponse.setPasswordEnabled(false);
-                }
-            }
-
-            if (details.contains(VMDetails.all) || details.contains(VMDetails.iso)) {
-                // ISO Info
-                VMTemplateVO iso = templates.get(userVm.getIsoId());
-                if (iso == null) {
-                    iso = ApiDBUtils.findTemplateById(userVm.getIsoId());
-                    if (iso != null) {
-                        templates.put(iso.getId(), iso);
-                    }
-                }
-
-                if (iso != null) {
-                    userVmResponse.setIsoId(iso.getId());
-                    userVmResponse.setIsoName(iso.getName());
-                }
-            }
-
-            if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff)) {
-                // Service Offering Info
-                ServiceOffering offering = serviceOfferings.get(userVm.getServiceOfferingId());
-
-                if (offering == null) {
-                    offering = ApiDBUtils.findServiceOfferingById(userVm.getServiceOfferingId());
-                    serviceOfferings.put(offering.getId(), offering);
-                }
-
-                userVmResponse.setServiceOfferingId(offering.getId());
-                userVmResponse.setServiceOfferingName(offering.getName());
-                userVmResponse.setCpuNumber(offering.getCpu());
-                userVmResponse.setCpuSpeed(offering.getSpeed());
-                userVmResponse.setMemory(offering.getRamSize());
-            }
-
-            if (details.contains(VMDetails.all) || details.contains(VMDetails.volume)) {
-                VolumeVO rootVolume = ApiDBUtils.findRootVolume(userVm.getId());
-                if (rootVolume != null) {
-                    userVmResponse.setRootDeviceId(rootVolume.getDeviceId());
-                    String rootDeviceType = "Not created";
-                    if (rootVolume.getPoolId() != null) {
-                        StoragePoolVO storagePool = ApiDBUtils.findStoragePoolById(rootVolume.getPoolId());
-                        rootDeviceType = storagePool.getPoolType().toString();
-                    }
-                    userVmResponse.setRootDeviceType(rootDeviceType);
-                }
-            }
-
-            if (details.contains(VMDetails.all) || details.contains(VMDetails.stats)) {
-                // stats calculation
-                DecimalFormat decimalFormat = new DecimalFormat("#.##");
-                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());
-                }
-            }
-
-            userVmResponse.setGuestOsId(userVm.getGuestOSId());
-
-            if (details.contains(VMDetails.all) || details.contains(VMDetails.secgrp)) {
-                // security groups - list only when zone is security group enabled
-                if (zone.isSecurityGroupEnabled()) {
-                    List<SecurityGroupVO> securityGroups = ApiDBUtils.getSecurityGroupsForVm(userVm.getId());
-                    List<SecurityGroupResponse> securityGroupResponse = new ArrayList<SecurityGroupResponse>();
-                    for (SecurityGroupVO grp : securityGroups) {
-                        SecurityGroupResponse resp = new SecurityGroupResponse();
-                        resp.setId(grp.getId());
-                        resp.setName(grp.getName());
-                        resp.setDescription(grp.getDescription());
-                        resp.setObjectName("securitygroup");
-                        securityGroupResponse.add(resp);
-                    }
-                    userVmResponse.setSecurityGroupList(securityGroupResponse);
-                }
-            }
-
-            if (details.contains(VMDetails.all) || details.contains(VMDetails.nics)) {
-                List<NicProfile> nicProfiles = ApiDBUtils.getNics(userVm);
-                List<NicResponse> nicResponses = new ArrayList<NicResponse>();
-                for (NicProfile singleNicProfile : nicProfiles) {
-                    NicResponse nicResponse = new NicResponse();
-                    nicResponse.setId(singleNicProfile.getId());
-                    nicResponse.setIpaddress(singleNicProfile.getIp4Address());
-                    nicResponse.setGateway(singleNicProfile.getGateway());
-                    nicResponse.setNetmask(singleNicProfile.getNetmask());
-                    nicResponse.setNetworkid(singleNicProfile.getNetworkId());
-                    nicResponse.setNetworkName(ApiDBUtils.findNetworkById(singleNicProfile.getNetworkId()).getName());
-                    if (acct.getType() == Account.ACCOUNT_TYPE_ADMIN) {
-                        if (singleNicProfile.getBroadCastUri() != null) {
-                            nicResponse.setBroadcastUri(singleNicProfile.getBroadCastUri().toString());
-                        }
-                        if (singleNicProfile.getIsolationUri() != null) {
-                            nicResponse.setIsolationUri(singleNicProfile.getIsolationUri().toString());
-                        }
-                    }
-
-                    // Long networkId = singleNicProfile.getNetworkId();
-                    Network network = networks.get(singleNicProfile.getNetworkId());
-                    if (network == null) {
-                        network = ApiDBUtils.findNetworkById(singleNicProfile.getNetworkId());
-                        networks.put(singleNicProfile.getNetworkId(), network);
-                    }
+        List<UserVmJoinVO> viewVms = ApiDBUtils.newUserVmView(userVms);
+        return createUserVmResponse(objectName, details, viewVms.toArray(new UserVmJoinVO[viewVms.size()]));
 
-                    nicResponse.setTrafficType(network.getTrafficType().toString());
-                    nicResponse.setType(network.getGuestType().toString());
-                    nicResponse.setIsDefault(singleNicProfile.isDefaultNic());
-                    nicResponse.setObjectName("nic");
-                    nicResponses.add(nicResponse);
-                }
-                userVmResponse.setNics(nicResponses);
-            }
-
-            //set tag information
-            List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(TaggedResourceType.UserVm, userVm.getId());
-            List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
-            for (ResourceTag tag : tags) {
-                ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
-                tagResponses.add(tagResponse);
-            }
-            userVmResponse.setTags(tagResponses);
-
-            IpAddress ip = ApiDBUtils.findIpByAssociatedVmId(userVm.getId());
-            if (ip != null) {
-                userVmResponse.setPublicIpId(ip.getId());
-                userVmResponse.setPublicIp(ip.getAddress().addr());
-            }
+    }
 
-            userVmResponse.setObjectName(objectName);
-            vmResponses.add(userVmResponse);
-        }
+    @Override
+    public List<UserVmResponse> createUserVmResponse(String objectName, UserVm... userVms) {
+        List<UserVmJoinVO> viewVms = ApiDBUtils.newUserVmView(userVms);
+        return createUserVmResponse(objectName, viewVms.toArray(new UserVmJoinVO[viewVms.size()]));
+    }
 
-        return vmResponses;
+    @Override
+    public List<UserVmResponse> createUserVmResponse(String objectName, UserVmJoinVO... userVms) {
+        return createUserVmResponse(objectName, EnumSet.of(VMDetails.all), userVms);
     }
 
     @Override
-    public List<UserVmResponse> createUserVmResponse(String objectName, UserVm... userVms) {
+    public List<UserVmResponse> createUserVmResponse(String objectName, EnumSet<VMDetails> details, UserVmJoinVO... userVms) {
         Account caller = UserContext.current().getCaller();
-        boolean caller_is_admin = ((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN));
 
-        Hashtable<Long, UserVmData> vmDataList = new Hashtable<Long, UserVmData>();
+        s_logger.debug(">>>Converting UserVm VO to UserVmResponse");
+        Hashtable<Long, UserVmResponse> vmDataList = new Hashtable<Long, UserVmResponse>();
         // Initialise the vmdatalist with the input data
-        for (UserVm userVm : userVms) {
-            UserVmData userVmData = newUserVmData(userVm);
-            vmDataList.put(userVm.getId(), userVmData);
-        }
-
-        vmDataList = ApiDBUtils.listVmDetails(vmDataList);
-
-        // initialize vmresponse from vmdatalist
-        List<UserVmResponse> vmResponses = new ArrayList<UserVmResponse>();
-        DecimalFormat decimalFormat = new DecimalFormat("#.##");
-        for (UserVmData uvd : vmDataList.values()) {
-            UserVmResponse userVmResponse = newUserVmResponse(uvd, caller_is_admin);
 
-            // stats calculation
-            String cpuUsed = null;
-            // VmStats vmStats = ApiDBUtils.getVmStatistics(userVmResponse.getId());
-            VmStats vmStats = ApiDBUtils.getVmStatistics(uvd.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());
+        for (UserVmJoinVO userVm : userVms) {
+            UserVmResponse userVmData = vmDataList.get(userVm.getId());
+            if ( userVmData == null ){
+                // first time encountering this vm
+                userVmData = ApiDBUtils.newUserVmResponse(objectName, userVm, details, caller);
+            } else{
+                // update nics, securitygroups, tags for 1 to many mapping fields
+                userVmData = ApiDBUtils.fillVmDetails(userVmData, userVm);
             }
-            userVmResponse.setObjectName(objectName);
-
-            vmResponses.add(userVmResponse);
+            vmDataList.put(userVm.getId(), userVmData);
         }
-        return vmResponses;
+        s_logger.debug(">>>Done with creating vm response");
+        return new ArrayList<UserVmResponse>(vmDataList.values());
     }
 
     @Override
     public DomainRouterResponse createDomainRouterResponse(VirtualRouter router) {
-        Account caller = UserContext.current().getCaller();
-        Map<Long, ServiceOffering> serviceOfferings = new HashMap<Long, ServiceOffering>();
-
-        DomainRouterResponse routerResponse = new DomainRouterResponse();
-        routerResponse.setId(router.getId());
-        routerResponse.setZoneId(router.getDataCenterIdToDeployIn());
-        routerResponse.setName(router.getHostName());
-        routerResponse.setTemplateId(router.getTemplateId());
-        routerResponse.setCreated(router.getCreated());
-        routerResponse.setState(router.getState());
-        routerResponse.setIsRedundantRouter(router.getIsRedundantRouter());
-        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.getHostId());
-                routerResponse.setHostName(ApiDBUtils.findHostById(router.getHostId()).getName());
-            }
-            routerResponse.setPodId(router.getPodIdToDeployIn());
-            List<NicProfile> nicProfiles = ApiDBUtils.getNics(router);
-            List<NicResponse> nicResponses = new ArrayList<NicResponse>();
-            for (NicProfile singleNicProfile : nicProfiles) {
-                Network network = ApiDBUtils.findNetworkById(singleNicProfile.getNetworkId());
-                //legacy code, public/control/guest nic info is kept in nics response object
-                if (network != null) {
-                    if (network.getTrafficType() == TrafficType.Public) {
-                        routerResponse.setPublicIp(singleNicProfile.getIp4Address());
-                        routerResponse.setPublicMacAddress(singleNicProfile.getMacAddress());
-                        routerResponse.setPublicNetmask(singleNicProfile.getNetmask());
-                        routerResponse.setGateway(singleNicProfile.getGateway());
-                        routerResponse.setPublicNetworkId(singleNicProfile.getNetworkId());
-                    } else if (network.getTrafficType() == TrafficType.Control) {
-                        routerResponse.setLinkLocalIp(singleNicProfile.getIp4Address());
-                        routerResponse.setLinkLocalMacAddress(singleNicProfile.getMacAddress());
-                        routerResponse.setLinkLocalNetmask(singleNicProfile.getNetmask());
-                        routerResponse.setLinkLocalNetworkId(singleNicProfile.getNetworkId());
-                    } else if (network.getTrafficType() == TrafficType.Guest) {
-                        routerResponse.setGuestIpAddress(singleNicProfile.getIp4Address());
-                        routerResponse.setGuestMacAddress(singleNicProfile.getMacAddress());
-                        routerResponse.setGuestNetmask(singleNicProfile.getNetmask());
-                        routerResponse.setGuestNetworkId(singleNicProfile.getNetworkId());
-                        routerResponse.setNetworkDomain(network.getNetworkDomain());
-                    }
-
-                    NicResponse nicResponse = new NicResponse();
-                    nicResponse.setId(singleNicProfile.getId());
-                    nicResponse.setIpaddress(singleNicProfile.getIp4Address());
-                    nicResponse.setGateway(singleNicProfile.getGateway());
-                    nicResponse.setNetmask(singleNicProfile.getNetmask());
-                    nicResponse.setNetworkid(singleNicProfile.getNetworkId());
-                    nicResponse.setNetworkName(network.getName());
-                    
-                    if (singleNicProfile.getBroadCastUri() != null) {
-                        nicResponse.setBroadcastUri(singleNicProfile.getBroadCastUri().toString());
-                    }
-                    if (singleNicProfile.getIsolationUri() != null) {
-                        nicResponse.setIsolationUri(singleNicProfile.getIsolationUri().toString());
-                    }
+        List<DomainRouterJoinVO> viewVrs = ApiDBUtils.newDomainRouterView(router);
+        List<DomainRouterResponse> listVrs = createDomainRouterResponse(viewVrs.toArray(new DomainRouterJoinVO[viewVrs.size()]));
+        assert listVrs != null && listVrs.size() == 1 : "There should be one virtual router returned";
+        return listVrs.get(0);
+    }
 
-                    nicResponse.setTrafficType(network.getTrafficType().toString());
-                    if (network.getGuestType() != null) {
-                        nicResponse.setType(network.getGuestType().toString());  
-                    }
-                    nicResponse.setIsDefault(singleNicProfile.isDefaultNic());
-                    nicResponse.setObjectName("nic");
-                    nicResponses.add(nicResponse);  
-                }
+    @Override
+    public List<DomainRouterResponse> createDomainRouterResponse(DomainRouterJoinVO... routers) {
+        Account caller = UserContext.current().getCaller();
+        Hashtable<Long, DomainRouterResponse> vrDataList = new Hashtable<Long, DomainRouterResponse>();
+        // Initialise the vrdatalist with the input data
+        for (DomainRouterJoinVO vr : routers) {
+            DomainRouterResponse vrData = vrDataList.get(vr.getId());
+            if ( vrData == null ){
+                // first time encountering this vm
+                vrData = ApiDBUtils.newDomainRouterResponse(vr, caller);
             }
-            routerResponse.setNics(nicResponses);
-        }
-
-        // Service Offering Info
-        ServiceOffering offering = serviceOfferings.get(router.getServiceOfferingId());
-
-        if (offering == null) {
-            offering = ApiDBUtils.findServiceOfferingById(router.getServiceOfferingId());
-            serviceOfferings.put(offering.getId(), offering);
-        }
-        routerResponse.setServiceOfferingId(offering.getId());
-        routerResponse.setServiceOfferingName(offering.getName());
-
-        populateOwner(routerResponse, router);
-
-        DataCenter zone = ApiDBUtils.findZoneById(router.getDataCenterIdToDeployIn());
-        if (zone != null) {
-            routerResponse.setZoneName(zone.getName());
-            routerResponse.setDns1(zone.getDns1());
-            routerResponse.setDns2(zone.getDns2());
+            else{
+                // update nics for 1 to many mapping fields
+                vrData = ApiDBUtils.fillRouterDetails(vrData, vr);
+            }
+            vrDataList.put(vr.getId(), vrData);
         }
-        
-        routerResponse.setVpcId(router.getVpcId());
-
-        routerResponse.setObjectName("domainrouter");
-        return routerResponse;
+        return new ArrayList<DomainRouterResponse>(vrDataList.values());
     }
 
     @Override
@@ -3209,141 +2885,6 @@ public class ApiResponseHelper implements ResponseGenerator {
         return response;
     }
 
-    public UserVmData newUserVmData(UserVm userVm) {
-        UserVmData userVmData = new UserVmData();
-        userVmData.setId(userVm.getId());
-        userVmData.setName(userVm.getHostName());
-        userVmData.setCreated(userVm.getCreated());
-        userVmData.setGuestOsId(userVm.getGuestOSId());
-        userVmData.setHaEnable(userVm.isHaEnabled());
-        if (userVm.getState() != null) {
-            userVmData.setState(userVm.getState().toString());
-        }
-        if (userVm.getDisplayName() != null) {
-            userVmData.setDisplayName(userVm.getDisplayName());
-        } else {
-            userVmData.setDisplayName(userVm.getHostName());
-        }
-        userVmData.setInstanceName(userVm.getInstanceName());
-
-        userVmData.setDomainId(userVm.getDomainId());
-
-        if (userVm.getHypervisorType() != null) {
-            userVmData.setHypervisor(userVm.getHypervisorType().toString());
-        }
-
-        if (userVm.getPassword() != null) {
-            userVmData.setPassword(userVm.getPassword());
-        }
-        return userVmData;
-    }
-
-    public UserVmResponse newUserVmResponse(UserVmData userVmData, boolean caller_is_admin) {
-        UserVmResponse userVmResponse = new UserVmResponse();
-
-        userVmResponse.setHypervisor(userVmData.getHypervisor());
-        userVmResponse.setId(userVmData.getId());
-        userVmResponse.setName(userVmData.getName());
-
-        userVmResponse.setDisplayName(userVmData.getDisplayName());
-
-        populateAccount(userVmResponse, userVmData.getAccountId());
-        populateDomain(userVmResponse, userVmData.getDomainId());
-
-        userVmResponse.setCreated(userVmData.getCreated());
-        userVmResponse.setState(userVmData.getState());
-        userVmResponse.setHaEnable(userVmData.getHaEnable());
-        userVmResponse.setGroupId(userVmData.getGroupId());
-        userVmResponse.setGroup(userVmData.getGroup());
-        userVmResponse.setZoneId(userVmData.getZoneId());
-        userVmResponse.setZoneName(userVmData.getZoneName());
-        if (caller_is_admin) {
-            userVmResponse.setInstanceName(userVmData.getInstanceName());
-            userVmResponse.setHostId(userVmData.getHostId());
-            userVmResponse.setHostName(userVmData.getHostName());
-        }
-        userVmResponse.setTemplateId(userVmData.getTemplateId());
-        userVmResponse.setTemplateName(userVmData.getTemplateName());
-        userVmResponse.setTemplateDisplayText(userVmData.getTemplateDisplayText());
-        userVmResponse.setPasswordEnabled(userVmData.getPasswordEnabled());
-        userVmResponse.setIsoId(userVmData.getIsoId());
-        userVmResponse.setIsoName(userVmData.getIsoName());
-        userVmResponse.setIsoDisplayText(userVmData.getIsoDisplayText());
-        userVmResponse.setServiceOfferingId(userVmData.getServiceOfferingId());
-        userVmResponse.setServiceOfferingName(userVmData.getServiceOfferingName());
-        userVmResponse.setCpuNumber(userVmData.getCpuNumber());
-        userVmResponse.setCpuSpeed(userVmData.getCpuSpeed());
-        userVmResponse.setMemory(userVmData.getMemory());
-        userVmResponse.setCpuUsed(userVmData.getCpuUsed());
-        userVmResponse.setNetworkKbsRead(userVmData.getNetworkKbsRead());
-        userVmResponse.setNetworkKbsWrite(userVmData.getNetworkKbsWrite());
-        userVmResponse.setGuestOsId(userVmData.getGuestOsId());
-        userVmResponse.setRootDeviceId(userVmData.getRootDeviceId());
-        userVmResponse.setRootDeviceType(userVmData.getRootDeviceType());
-        userVmResponse.setPassword(userVmData.getPassword());
-        userVmResponse.setJobId(userVmData.getJobId());
-        userVmResponse.setJobStatus(userVmData.getJobStatus());
-        userVmResponse.setForVirtualNetwork(userVmData.getForVirtualNetwork());
-
-        Set<SecurityGroupResponse> securityGroupResponse = new HashSet<SecurityGroupResponse>();
-        for (SecurityGroupData sgd : userVmData.getSecurityGroupList()) {
-            if (sgd.getId() != null) {
-                SecurityGroupResponse sgr = new SecurityGroupResponse();
-                sgr.setId(sgd.getId());
-                sgr.setName(sgd.getName());
-                sgr.setDescription(sgd.getDescription());
-
-                Account account = ApiDBUtils.findAccountByNameDomain(sgd.getAccountName(), sgd.getDomainId());
-                if (account != null) {
-                    populateAccount(sgr, account.getId());
-                    populateDomain(sgr, account.getDomainId());
-                }
-
-                sgr.setObjectName(sgd.getObjectName());
-                securityGroupResponse.add(sgr);
-            }
-        }
-        userVmResponse.setSecurityGroupList(new ArrayList<SecurityGroupResponse>(securityGroupResponse));
-
-        Set<NicResponse> nicResponses = new HashSet<NicResponse>();
-        for (NicData nd : userVmData.getNics()) {
-            NicResponse nr = new NicResponse();
-            nr.setId(nd.getId());
-            nr.setNetworkid(nd.getNetworkid());
-            nr.setNetmask(nd.getNetmask());
-            nr.setGateway(nd.getGateway());
-            nr.setIpaddress(nd.getIpaddress());
-            nr.setIsolationUri(nd.getIsolationUri());
-            nr.setBroadcastUri(nd.getBroadcastUri());
-            nr.setTrafficType(nd.getTrafficType());
-            nr.setType(nd.getType());
-            nr.setIsDefault(nd.getIsDefault());
-            nr.setMacAddress(nd.getMacAddress());
-            nr.setObjectName(nd.getObjectName());
-            nicResponses.add(nr);
-        }
-        userVmResponse.setNics(new ArrayList<NicResponse>(nicResponses));
-        userVmResponse.setPublicIpId(userVmData.getPublicIpId());
-        userVmResponse.setPublicIp(userVmData.getPublicIp());
-
-        //set tag information
-        List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(TaggedResourceType.UserVm, userVmData.getId());
-        List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
-        for (ResourceTag tag : tags) {
-            ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
-            tagResponses.add(tagResponse);
-        }
-        userVmResponse.setTags(tagResponses);
-
-        UserVmDetailVO userVmDetail =  ApiDBUtils.findPublicKeyByVmId(userVmData.getId());
-        if (userVmDetail != null && userVmDetail.getValue() != null) {
-            String keyPairName = ApiDBUtils.getKeyPairName(userVmDetail.getValue());
-            userVmResponse.setKeyPairName(keyPairName);
-        }
-
-        return userVmResponse;
-    }
-
     @Override
     public HypervisorCapabilitiesResponse createHypervisorCapabilitiesResponse(HypervisorCapabilities hpvCapabilities) {
         HypervisorCapabilitiesResponse hpvCapabilitiesResponse = new HypervisorCapabilitiesResponse();
@@ -3355,6 +2896,8 @@ public class ApiResponseHelper implements ResponseGenerator {
         return hpvCapabilitiesResponse;
     }
 
+    // TODO: we may need to refactor once ControlledEntityResponse and ControlledEntity id to uuid conversion are all done.
+    // currently code is scattered in
     private void populateOwner(ControlledEntityResponse response, ControlledEntity object) {
         Account account = ApiDBUtils.findAccountByIdIncludingRemoved(object.getAccountId());
 
@@ -3372,6 +2915,19 @@ public class ApiResponseHelper implements ResponseGenerator {
         response.setDomainName(domain.getName());
     }
 
+    public static void populateOwner(ControlledViewEntityResponse response, ControlledViewEntity object) {
+
+        if (object.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
+            response.setProjectId(object.getProjectUuid());
+            response.setProjectName(object.getProjectName());
+        } else {
+            response.setAccountName(object.getAccountName());
+        }
+
+        response.setDomainId(object.getDomainUuid());
+        response.setDomainName(object.getDomainName());
+    }
+
     private void populateAccount(ControlledEntityResponse response, long accountId) {
         Account account = ApiDBUtils.findAccountByIdIncludingRemoved(accountId);
         if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/api/ApiServer.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiServer.java b/server/src/com/cloud/api/ApiServer.java
index 2c4f7cf..d75c8e1 100755
--- a/server/src/com/cloud/api/ApiServer.java
+++ b/server/src/com/cloud/api/ApiServer.java
@@ -78,6 +78,8 @@ import org.apache.http.protocol.ResponseDate;
 import org.apache.http.protocol.ResponseServer;
 import org.apache.log4j.Logger;
 
+import org.apache.cloudstack.api.admin.router.command.ListRoutersCmd;
+import org.apache.cloudstack.api.user.vm.command.ListVMsCmd;
 import com.cloud.acl.ControlledEntity;
 import com.cloud.api.response.ApiResponseSerializer;
 import com.cloud.api.response.ExceptionResponse;
@@ -435,7 +437,7 @@ public class ApiServer implements HttpRequestHandler {
 
             // if the command is of the listXXXCommand, we will need to also return the
             // the job id and status if possible
-            if (cmdObj instanceof BaseListCmd) {
+            if (cmdObj instanceof BaseListCmd && !(cmdObj instanceof ListVMsCmd) && !(cmdObj instanceof ListRoutersCmd)) {
                 buildAsyncListResponse((BaseListCmd) cmdObj, caller);
             }
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/api/response/ApiResponseSerializer.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/response/ApiResponseSerializer.java b/server/src/com/cloud/api/response/ApiResponseSerializer.java
index 70d7edd..cc2725e 100644
--- a/server/src/com/cloud/api/response/ApiResponseSerializer.java
+++ b/server/src/com/cloud/api/response/ApiResponseSerializer.java
@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Date;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -210,8 +211,8 @@ public class ApiResponseSerializer {
                     if (isAsync) {
                         sb.append("</jobresult>");
                     }
-                } else if (fieldValue instanceof List<?>) {
-                    List<?> subResponseList = (List<Object>) fieldValue;                    
+                } else if (fieldValue instanceof Collection<?>) {
+                    Collection<?> subResponseList = (Collection<Object>) fieldValue;
                     boolean usedUuidList = false;
                     for (Object value : subResponseList) {
                         if (value instanceof ResponseObject) {

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/configuration/DefaultComponentLibrary.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/configuration/DefaultComponentLibrary.java b/server/src/com/cloud/configuration/DefaultComponentLibrary.java
index ef61044..ec543ed 100755
--- a/server/src/com/cloud/configuration/DefaultComponentLibrary.java
+++ b/server/src/com/cloud/configuration/DefaultComponentLibrary.java
@@ -198,11 +198,13 @@ import com.cloud.vm.ItWorkDaoImpl;
 import com.cloud.vm.UserVmManagerImpl;
 import com.cloud.vm.dao.ConsoleProxyDaoImpl;
 import com.cloud.vm.dao.DomainRouterDaoImpl;
+import com.cloud.vm.dao.DomainRouterJoinDaoImpl;
 import com.cloud.vm.dao.InstanceGroupDaoImpl;
 import com.cloud.vm.dao.InstanceGroupVMMapDaoImpl;
 import com.cloud.vm.dao.NicDaoImpl;
 import com.cloud.vm.dao.SecondaryStorageVmDaoImpl;
 import com.cloud.vm.dao.UserVmDaoImpl;
+import com.cloud.vm.dao.UserVmJoinDaoImpl;
 import com.cloud.vm.dao.UserVmDetailsDaoImpl;
 import com.cloud.vm.dao.VMInstanceDaoImpl;
 
@@ -216,6 +218,8 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com
         addDao("HostDao", HostDaoImpl.class);
         addDao("VMInstanceDao", VMInstanceDaoImpl.class);
         addDao("UserVmDao", UserVmDaoImpl.class);
+        addDao("UserVmJoinDao", UserVmJoinDaoImpl.class);
+        addDao("DomainRouterJoinDao", DomainRouterJoinDaoImpl.class);
         ComponentInfo<? extends GenericDao<?, ? extends Serializable>> info = addDao("ServiceOfferingDao", ServiceOfferingDaoImpl.class);
         info.addParameter("cache.size", "50");
         info.addParameter("cache.time.to.live", "600");
@@ -359,6 +363,7 @@ public class DefaultComponentLibrary extends ComponentLibraryBase implements Com
         if (_daos.size() == 0) {
             populateDaos();
         }
+        //FIXME: Incorrect method return definition
         return _daos;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/server/ManagementServerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java
index 7a8d562..cced763 100755
--- a/server/src/com/cloud/server/ManagementServerImpl.java
+++ b/server/src/com/cloud/server/ManagementServerImpl.java
@@ -98,6 +98,8 @@ import org.apache.cloudstack.api.user.vmgroup.command.UpdateVMGroupCmd;
 import com.cloud.api.commands.UpgradeSystemVMCmd;
 import com.cloud.api.commands.UploadCustomCertificateCmd;
 import com.cloud.api.response.ExtractResponse;
+import com.cloud.api.view.vo.DomainRouterJoinVO;
+import com.cloud.api.view.vo.UserVmJoinVO;
 import com.cloud.async.AsyncJob;
 import com.cloud.async.AsyncJobExecutor;
 import com.cloud.async.AsyncJobManager;
@@ -236,6 +238,7 @@ import com.cloud.utils.db.Filter;
 import com.cloud.utils.db.GlobalLock;
 import com.cloud.utils.db.JoinBuilder;
 import com.cloud.utils.db.JoinBuilder.JoinType;
+import com.cloud.utils.db.SearchCriteria.Func;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
 import com.cloud.utils.db.Transaction;
@@ -258,6 +261,7 @@ import com.cloud.vm.VirtualMachineProfile;
 import com.cloud.vm.VirtualMachineProfileImpl;
 import com.cloud.vm.dao.ConsoleProxyDao;
 import com.cloud.vm.dao.DomainRouterDao;
+import com.cloud.vm.dao.DomainRouterJoinDao;
 import com.cloud.vm.dao.InstanceGroupDao;
 import com.cloud.vm.dao.NicDao;
 import com.cloud.vm.dao.SecondaryStorageVmDao;
@@ -275,6 +279,7 @@ public class ManagementServerImpl implements ManagementServer {
     private final AlertManager _alertMgr;
     private final IPAddressDao _publicIpAddressDao;
     private final DomainRouterDao _routerDao;
+    private final DomainRouterJoinDao _routerJoinDao;
     private final ConsoleProxyDao _consoleProxyDao;
     private final ClusterDao _clusterDao;
     private final SecondaryStorageVmDao _secStorageVmDao;
@@ -349,6 +354,7 @@ public class ManagementServerImpl implements ManagementServer {
         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);
         _dcDao = locator.getDao(DataCenterDao.class);
         _vlanDao = locator.getDao(VlanDao.class);
@@ -1635,7 +1641,7 @@ public class ManagementServerImpl implements ManagementServer {
     }
 
     @Override
-    public Pair<List<? extends VirtualRouter>, Integer> searchForRouters(ListRoutersCmd cmd) {
+    public Pair<List<DomainRouterJoinVO>, Integer> searchForRouters(ListRoutersCmd cmd) {
         Long id = cmd.getId();
         String name = cmd.getRouterName();
         String state = cmd.getState();
@@ -1655,16 +1661,19 @@ public class ManagementServerImpl implements ManagementServer {
         Long domainId = domainIdRecursiveListProject.first();
         Boolean isRecursive = domainIdRecursiveListProject.second();
         ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
-        Filter searchFilter = new Filter(DomainRouterVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
-        SearchBuilder<DomainRouterVO> sb = _routerDao.createSearchBuilder();
-        _accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
+        // no default orderBy
+        //Filter searchFilter = new Filter(DomainRouterJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
+        Filter searchFilter = new Filter(DomainRouterJoinVO.class, null, true, cmd.getStartIndex(), cmd.getPageSizeVal());
+        SearchBuilder<DomainRouterJoinVO> sb = _routerJoinDao.createSearchBuilder();
+        sb.select(null, Func.DISTINCT, sb.entity().getId()); // select distinct ids to get number of records with pagination
+        _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
 
         sb.and("name", sb.entity().getHostName(), SearchCriteria.Op.LIKE);
         sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
         sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.IN);
         sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
-        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("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
         sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
         
@@ -1677,20 +1686,14 @@ public class ManagementServerImpl implements ManagementServer {
         }
 
         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);
         }
-        
-        SearchCriteria<DomainRouterVO> sc = sb.create();
-        _accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
+
+        SearchCriteria<DomainRouterJoinVO> sc = sb.create();
+        _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
 
         if (keyword != null) {
-            SearchCriteria<DomainRouterVO> ssc = _routerDao.createSearchCriteria();
+            SearchCriteria<DomainRouterJoinVO> ssc = _routerJoinDao.createSearchCriteria();
             ssc.addOr("hostName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
             ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
             ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
@@ -1730,8 +1733,21 @@ public class ManagementServerImpl implements ManagementServer {
             sc.setParameters("vpcId", vpcId);
         }
 
-        Pair<List<DomainRouterVO>, Integer> result = _routerDao.searchAndCount(sc, searchFilter);
-        return new Pair<List<? extends VirtualRouter>, Integer>(result.first(), result.second());
+        // search VR details by ids
+        Pair<List<DomainRouterJoinVO>, Integer> uniqueVrPair =  _routerJoinDao.searchAndCount(sc, searchFilter);
+        Integer count = uniqueVrPair.second();
+        if ( count.intValue() == 0 ) {
+            // empty result
+            return uniqueVrPair;
+        }
+        List<DomainRouterJoinVO> uniqueVrs = uniqueVrPair.first();
+        Long[] vrIds = new Long[uniqueVrs.size()];
+        int i = 0;
+        for (DomainRouterJoinVO v : uniqueVrs ){
+            vrIds[i++] = v.getId();
+        }
+        List<DomainRouterJoinVO> vrs =  _routerJoinDao.searchByIds(vrIds);
+        return new Pair<List<DomainRouterJoinVO>, Integer>(vrs, count);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/user/AccountManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/user/AccountManager.java b/server/src/com/cloud/user/AccountManager.java
index 90a34ad..3cc7d12 100755
--- a/server/src/com/cloud/user/AccountManager.java
+++ b/server/src/com/cloud/user/AccountManager.java
@@ -21,6 +21,7 @@ import java.util.Map;
 
 import com.cloud.acl.ControlledEntity;
 import com.cloud.acl.SecurityChecker.AccessType;
+import com.cloud.api.view.vo.ControlledViewEntity;
 import com.cloud.domain.Domain;
 import com.cloud.exception.ConcurrentOperationException;
 import com.cloud.exception.PermissionDeniedException;
@@ -93,9 +94,15 @@ public interface AccountManager extends AccountService {
 	void buildACLSearchBuilder(SearchBuilder<? extends ControlledEntity> sb, Long domainId,
 			boolean isRecursive, List<Long> permittedAccounts, ListProjectResourcesCriteria listProjectResourcesCriteria);
 
+    void buildACLViewSearchBuilder(SearchBuilder<? extends ControlledViewEntity> sb, Long domainId,
+            boolean isRecursive, List<Long> permittedAccounts, ListProjectResourcesCriteria listProjectResourcesCriteria);
+
 	void buildACLSearchCriteria(SearchCriteria<? extends ControlledEntity> sc,
 			Long domainId, boolean isRecursive, List<Long> permittedAccounts, ListProjectResourcesCriteria listProjectResourcesCriteria);
 
+    void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledViewEntity> sc,
+            Long domainId, boolean isRecursive, List<Long> permittedAccounts, ListProjectResourcesCriteria listProjectResourcesCriteria);
+
 	void buildACLSearchParameters(Account caller, Long id,
 			String accountName, Long projectId, List<Long> permittedAccounts, Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject, boolean listAll, boolean forProjectInvitation);
    

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/user/AccountManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java
index ab04a9c..59b7584 100755
--- a/server/src/com/cloud/user/AccountManagerImpl.java
+++ b/server/src/com/cloud/user/AccountManagerImpl.java
@@ -41,6 +41,7 @@ import org.apache.commons.codec.binary.Base64;
 import org.apache.log4j.Logger;
 
 import com.cloud.acl.ControlledEntity;
+import com.cloud.api.view.vo.ControlledViewEntity;
 import com.cloud.acl.SecurityChecker;
 import com.cloud.acl.SecurityChecker.AccessType;
 import com.cloud.api.ApiDBUtils;
@@ -2380,8 +2381,51 @@ public class AccountManagerImpl implements AccountManager, AccountService, Manag
         }
     }
 
-	@Override
-	public UserAccount getUserByApiKey(String apiKey) {
-		return _userAccountDao.getUserByApiKey(apiKey);
-	}
+    @Override
+    public void buildACLViewSearchBuilder(SearchBuilder<? extends ControlledViewEntity> sb, Long domainId,
+            boolean isRecursive, List<Long> permittedAccounts, ListProjectResourcesCriteria listProjectResourcesCriteria) {
+
+        sb.and("accountIdIN", sb.entity().getAccountId(), SearchCriteria.Op.IN);
+        sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
+
+        if (((permittedAccounts.isEmpty()) && (domainId != null) && isRecursive)) {
+            // if accountId isn't specified, we can do a domain match for the
+            // admin case if isRecursive is true
+            sb.and("domainPath", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
+        }
+
+        if (listProjectResourcesCriteria != null) {
+            if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.ListProjectResourcesOnly) {
+                sb.and("accountType", sb.entity().getAccountType(), SearchCriteria.Op.EQ);
+            } else if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.SkipProjectResources) {
+                sb.and("accountType", sb.entity().getAccountType(), SearchCriteria.Op.NEQ);
+            }
+        }
+
+    }
+
+    @Override
+    public void buildACLViewSearchCriteria(SearchCriteria<? extends ControlledViewEntity> sc,
+            Long domainId, boolean isRecursive, List<Long> permittedAccounts, ListProjectResourcesCriteria listProjectResourcesCriteria) {
+
+        if (listProjectResourcesCriteria != null) {
+            sc.setParameters("accountType", Account.ACCOUNT_TYPE_PROJECT);
+        }
+
+        if (!permittedAccounts.isEmpty()) {
+            sc.setParameters("accountIdIN", permittedAccounts.toArray());
+        } else if (domainId != null) {
+            DomainVO domain = _domainDao.findById(domainId);
+            if (isRecursive) {
+                sc.setParameters("domainPath", domain.getPath() + "%");
+            } else {
+                sc.setParameters("domainId", domainId);
+            }
+        }
+    }
+
+    @Override
+    public UserAccount getUserByApiKey(String apiKey) {
+        return _userAccountDao.getUserByApiKey(apiKey);
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/b0ce8fd4/server/src/com/cloud/vm/UserVmManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/vm/UserVmManager.java b/server/src/com/cloud/vm/UserVmManager.java
index 4ce9bfe..1a2f508 100755
--- a/server/src/com/cloud/vm/UserVmManager.java
+++ b/server/src/com/cloud/vm/UserVmManager.java
@@ -21,6 +21,7 @@ import java.util.List;
 import java.util.Map;
 
 import com.cloud.agent.api.VmStatsEntry;
+import com.cloud.api.view.vo.UserVmJoinVO;
 import com.cloud.exception.ConcurrentOperationException;
 import com.cloud.exception.InsufficientCapacityException;
 import com.cloud.exception.ResourceUnavailableException;
@@ -98,7 +99,7 @@ public interface UserVmManager extends VirtualMachineGuru<UserVmVO>, UserVmServi
      * @param tags TODO
      * @return List of UserVMs + count
      */
-	Pair<List<UserVmVO>, Integer> searchForUserVMs(Criteria c, Account caller, Long domainId, boolean isRecursive, List<Long> permittedAccounts, boolean listAll, ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags);
+    Pair<List<UserVmJoinVO>, Integer> searchForUserVMs(Criteria c, Account caller, Long domainId, boolean isRecursive, List<Long> permittedAccounts, boolean listAll, ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags);
 
     String getChecksum(Long hostId, String templatePath);
     


Mime
View raw message