You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by mc...@apache.org on 2013/06/12 20:22:57 UTC

[48/50] [abbrv] Merge branch 'master' (up to commit c30d9be3cea30339cfff40c1002906634291b373) into object_store.

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/api/ApiResponseHelper.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/api/ApiResponseHelper.java
index cf11b41,bcc1605..36ed9a5
--- a/server/src/com/cloud/api/ApiResponseHelper.java
+++ b/server/src/com/cloud/api/ApiResponseHelper.java
@@@ -947,18 -946,9 +949,18 @@@ public class ApiResponseHelper implemen
      }
  
      @Override
 +    public ImageStoreResponse createImageStoreResponse(ImageStore os) {
 +        List<ImageStoreJoinVO> viewStores = ApiDBUtils.newImageStoreView(os);
 +        List<ImageStoreResponse> listStores = ViewResponseHelper
 +                .createImageStoreResponse(viewStores.toArray(new ImageStoreJoinVO[viewStores.size()]));
 +        assert listStores != null && listStores.size() == 1 : "There should be one image data store returned";
 +        return listStores.get(0);
 +    }
 +
 +    @Override
-     public StoragePoolForMigrationResponse createStoragePoolForMigrationResponse(StoragePool pool) {
+     public StoragePoolResponse createStoragePoolForMigrationResponse(StoragePool pool) {
          List<StoragePoolJoinVO> viewPools = ApiDBUtils.newStoragePoolView(pool);
-         List<StoragePoolForMigrationResponse> listPools = ViewResponseHelper.createStoragePoolForMigrationResponse(
+         List<StoragePoolResponse> listPools = ViewResponseHelper.createStoragePoolForMigrationResponse(
                  viewPools.toArray(new StoragePoolJoinVO[viewPools.size()]));
          assert listPools != null && listPools.size() == 1 : "There should be one storage pool returned";
          return listPools.get(0);
@@@ -1681,152 -1870,7 +1683,154 @@@
          return ApiDBUtils.newEventResponse(vEvent);
      }
  
+ 
 +    private List<CapacityVO> sumCapacities(List<? extends Capacity> hostCapacities) {
 +        Map<String, Long> totalCapacityMap = new HashMap<String, Long>();
 +        Map<String, Long> usedCapacityMap = new HashMap<String, Long>();
 +
 +        Set<Long> poolIdsToIgnore = new HashSet<Long>();
 +        Criteria c = new Criteria();
 +        // TODO: implement
 +        List<? extends StoragePoolVO> allStoragePools = ApiDBUtils.searchForStoragePools(c);
 +        for (StoragePoolVO pool : allStoragePools) {
 +            StoragePoolType poolType = pool.getPoolType();
 +            if (!(poolType.isShared())) {// All the non shared storages
 +                                         // shouldn't show up in the capacity
 +                                         // calculation
 +                poolIdsToIgnore.add(pool.getId());
 +            }
 +        }
 +
 +        float cpuOverprovisioningFactor = ApiDBUtils.getCpuOverprovisioningFactor();
 +
 +        // collect all the capacity types, sum allocated/used and sum
 +        // total...get one capacity number for each
 +        for (Capacity capacity : hostCapacities) {
 +
 +            // check if zone exist
 +            DataCenter zone = ApiDBUtils.findZoneById(capacity.getDataCenterId());
 +            if (zone == null) {
 +                continue;
 +            }
 +
 +            short capacityType = capacity.getCapacityType();
 +
 +            // If local storage then ignore
 +            if ((capacityType == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED || capacityType == Capacity.CAPACITY_TYPE_STORAGE)
 +                    && poolIdsToIgnore.contains(capacity.getHostOrPoolId())) {
 +                continue;
 +            }
 +
 +            String key = capacity.getCapacityType() + "_" + capacity.getDataCenterId();
 +            String keyForPodTotal = key + "_-1";
 +
 +            boolean sumPodCapacity = false;
 +            if (capacity.getPodId() != null) {
 +                key += "_" + capacity.getPodId();
 +                sumPodCapacity = true;
 +            }
 +
 +            Long totalCapacity = totalCapacityMap.get(key);
 +            Long usedCapacity = usedCapacityMap.get(key);
 +
 +            // reset overprovisioning factor to 1
 +            float overprovisioningFactor = 1;
 +            if (capacityType == Capacity.CAPACITY_TYPE_CPU) {
 +                overprovisioningFactor = cpuOverprovisioningFactor;
 +            }
 +
 +            if (totalCapacity == null) {
 +                totalCapacity = new Long((long) (capacity.getTotalCapacity() * overprovisioningFactor));
 +            } else {
 +                totalCapacity = new Long((long) (capacity.getTotalCapacity() * overprovisioningFactor)) + totalCapacity;
 +            }
 +
 +            if (usedCapacity == null) {
 +                usedCapacity = new Long(capacity.getUsedCapacity());
 +            } else {
 +                usedCapacity = new Long(capacity.getUsedCapacity() + usedCapacity);
 +            }
 +
 +            if (capacityType == Capacity.CAPACITY_TYPE_CPU || capacityType == Capacity.CAPACITY_TYPE_MEMORY) { // Reserved
 +                // Capacity
 +                // accounts
 +                // for
 +                // stopped
 +                // vms
 +                // that
 +                // have been
 +                // stopped
 +                // within
 +                // an
 +                // interval
 +                usedCapacity += capacity.getReservedCapacity();
 +            }
 +
 +            totalCapacityMap.put(key, totalCapacity);
 +            usedCapacityMap.put(key, usedCapacity);
 +
 +            if (sumPodCapacity) {
 +                totalCapacity = totalCapacityMap.get(keyForPodTotal);
 +                usedCapacity = usedCapacityMap.get(keyForPodTotal);
 +
 +                overprovisioningFactor = 1;
 +                if (capacityType == Capacity.CAPACITY_TYPE_CPU) {
 +                    overprovisioningFactor = cpuOverprovisioningFactor;
 +                }
 +
 +                if (totalCapacity == null) {
 +                    totalCapacity = new Long((long) (capacity.getTotalCapacity() * overprovisioningFactor));
 +                } else {
 +                    totalCapacity = new Long((long) (capacity.getTotalCapacity() * overprovisioningFactor)) + totalCapacity;
 +                }
 +
 +                if (usedCapacity == null) {
 +                    usedCapacity = new Long(capacity.getUsedCapacity());
 +                } else {
 +                    usedCapacity = new Long(capacity.getUsedCapacity() + usedCapacity);
 +                }
 +
 +                if (capacityType == Capacity.CAPACITY_TYPE_CPU || capacityType == Capacity.CAPACITY_TYPE_MEMORY) { // Reserved
 +                    // Capacity
 +                    // accounts
 +                    // for
 +                    // stopped
 +                    // vms
 +                    // that
 +                    // have
 +                    // been
 +                    // stopped
 +                    // within
 +                    // an
 +                    // interval
 +                    usedCapacity += capacity.getReservedCapacity();
 +                }
 +
 +                totalCapacityMap.put(keyForPodTotal, totalCapacity);
 +                usedCapacityMap.put(keyForPodTotal, usedCapacity);
 +            }
 +        }
 +
 +        List<CapacityVO> summedCapacities = new ArrayList<CapacityVO>();
 +        for (String key : totalCapacityMap.keySet()) {
 +            CapacityVO summedCapacity = new CapacityVO();
 +
 +            StringTokenizer st = new StringTokenizer(key, "_");
 +            summedCapacity.setCapacityType(Short.parseShort(st.nextToken()));
 +            summedCapacity.setDataCenterId(Long.parseLong(st.nextToken()));
 +            if (st.hasMoreTokens()) {
 +                summedCapacity.setPodId(Long.parseLong(st.nextToken()));
 +            }
 +
 +            summedCapacity.setTotalCapacity(totalCapacityMap.get(key));
 +            summedCapacity.setUsedCapacity(usedCapacityMap.get(key));
 +
 +            summedCapacities.add(summedCapacity);
 +        }
 +        return summedCapacities;
 +    }
 +
++
      @Override
      public List<CapacityResponse> createCapacityResponse(List<? extends Capacity> result, DecimalFormat format) {
          List<CapacityResponse> capacityResponses = new ArrayList<CapacityResponse>();
@@@ -3252,151 -3322,165 +3271,164 @@@
          return response;
      }
  
 -
 -    @Override
 -    public UsageRecordResponse createUsageResponse(Usage usageRecord) {
 -        UsageRecordResponse usageRecResponse = new UsageRecordResponse();
 -
 -        Account account = ApiDBUtils.findAccountByIdIncludingRemoved(usageRecord.getAccountId());
 -        if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
 -            //find the project
 -            Project project = ApiDBUtils.findProjectByProjectAccountIdIncludingRemoved(account.getId());
 -            usageRecResponse.setProjectId(project.getUuid());
 -            usageRecResponse.setProjectName(project.getName());
 -        } else {
 -            usageRecResponse.setAccountId(account.getUuid());
 -            usageRecResponse.setAccountName(account.getAccountName());
 -        }
 -
 -        Domain domain = ApiDBUtils.findDomainById(usageRecord.getDomainId());
 -        if (domain != null) {
 -            usageRecResponse.setDomainId(domain.getUuid());
 -        }
 -
 -        if (usageRecord.getZoneId() != null) {
 -            DataCenter zone = ApiDBUtils.findZoneById(usageRecord.getZoneId());
 -            if (zone != null) {
 -                usageRecResponse.setZoneId(zone.getUuid());
 -            }
 -        }
 -        usageRecResponse.setDescription(usageRecord.getDescription());
 -        usageRecResponse.setUsage(usageRecord.getUsageDisplay());
 -        usageRecResponse.setUsageType(usageRecord.getUsageType());
 -        if (usageRecord.getVmInstanceId() != null) {
 -            VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getVmInstanceId());
 -            usageRecResponse.setVirtualMachineId(vm.getUuid());
 -        }
 -        usageRecResponse.setVmName(usageRecord.getVmName());
 -        if (usageRecord.getTemplateId() != null) {
 -            VMTemplateVO template = ApiDBUtils.findTemplateById(usageRecord.getTemplateId());
 -            if (template != null) {
 -                usageRecResponse.setTemplateId(template.getUuid());
 -            }
 -        }
 -
 -        if(usageRecord.getUsageType() == UsageTypes.RUNNING_VM || usageRecord.getUsageType() == UsageTypes.ALLOCATED_VM){
 -            ServiceOfferingVO svcOffering = _entityMgr.findByIdIncludingRemoved(ServiceOfferingVO.class, usageRecord.getOfferingId().toString());
 -            //Service Offering Id
 -            usageRecResponse.setOfferingId(svcOffering.getUuid());
 -            //VM Instance ID
 -            VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
 -            usageRecResponse.setUsageId(vm.getUuid());
 -            //Hypervisor Type
 -            usageRecResponse.setType(usageRecord.getType());
 -
 -        } else if(usageRecord.getUsageType() == UsageTypes.IP_ADDRESS){
 -            //isSourceNAT
 -            usageRecResponse.setSourceNat((usageRecord.getType().equals("SourceNat"))?true:false);
 -            //isSystem
 -            usageRecResponse.setSystem((usageRecord.getSize() == 1)?true:false);
 -            //IP Address ID
 -            IPAddressVO ip = _entityMgr.findByIdIncludingRemoved(IPAddressVO.class, usageRecord.getUsageId().toString());
 -            usageRecResponse.setUsageId(ip.getUuid());
 -
 -        } else if(usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_SENT || usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_RECEIVED){
 -            //Device Type
 -            usageRecResponse.setType(usageRecord.getType());
 -            if(usageRecord.getType().equals("DomainRouter")){
 -                //Domain Router Id
 -                VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
 -                usageRecResponse.setUsageId(vm.getUuid());
 -            } else {
 -                //External Device Host Id
 -                HostVO host = _entityMgr.findByIdIncludingRemoved(HostVO.class, usageRecord.getUsageId().toString());
 -                usageRecResponse.setUsageId(host.getUuid());
 -            }
 -            //Network ID
 -            NetworkVO network = _entityMgr.findByIdIncludingRemoved(NetworkVO.class, usageRecord.getNetworkId().toString());
 -            usageRecResponse.setNetworkId(network.getUuid());
 +	@Override
 +	public UsageRecordResponse createUsageResponse(Usage usageRecord) {
 +		UsageRecordResponse usageRecResponse = new UsageRecordResponse();
 +
 +		Account account = ApiDBUtils.findAccountByIdIncludingRemoved(usageRecord.getAccountId());
 +		if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
 +			//find the project
 +			Project project = ApiDBUtils.findProjectByProjectAccountIdIncludingRemoved(account.getId());
 +			usageRecResponse.setProjectId(project.getUuid());
 +			usageRecResponse.setProjectName(project.getName());
 +		} else {
 +			usageRecResponse.setAccountId(account.getUuid());
 +			usageRecResponse.setAccountName(account.getAccountName());
 +		}
 +
 +		Domain domain = ApiDBUtils.findDomainById(usageRecord.getDomainId());
 +		if (domain != null) {
 +			usageRecResponse.setDomainId(domain.getUuid());
 +		}
 +
 +		if (usageRecord.getZoneId() != null) {
 +			DataCenter zone = ApiDBUtils.findZoneById(usageRecord.getZoneId());
 +			if (zone != null) {
 +				usageRecResponse.setZoneId(zone.getUuid());
 +			}
 +		}
 +		usageRecResponse.setDescription(usageRecord.getDescription());
 +		usageRecResponse.setUsage(usageRecord.getUsageDisplay());
 +		usageRecResponse.setUsageType(usageRecord.getUsageType());
 +		if (usageRecord.getVmInstanceId() != null) {
 +			VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getVmInstanceId());
 +			usageRecResponse.setVirtualMachineId(vm.getUuid());
 +		}
 +		usageRecResponse.setVmName(usageRecord.getVmName());
 +		if (usageRecord.getTemplateId() != null) {
 +			VMTemplateVO template = ApiDBUtils.findTemplateById(usageRecord.getTemplateId());
 +			if (template != null) {
 +				usageRecResponse.setTemplateId(template.getUuid());
 +			}
 +		}
 +
 +		if(usageRecord.getUsageType() == UsageTypes.RUNNING_VM || usageRecord.getUsageType() == UsageTypes.ALLOCATED_VM){
 +			ServiceOfferingVO svcOffering = _entityMgr.findByIdIncludingRemoved(ServiceOfferingVO.class, usageRecord.getOfferingId().toString());
 +			//Service Offering Id
 +			usageRecResponse.setOfferingId(svcOffering.getUuid());
 +			//VM Instance ID
 +			VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
 +			usageRecResponse.setUsageId(vm.getUuid());
 +			//Hypervisor Type
 +			usageRecResponse.setType(usageRecord.getType());
 +
 +		} else if(usageRecord.getUsageType() == UsageTypes.IP_ADDRESS){
 +			//isSourceNAT
 +			usageRecResponse.setSourceNat((usageRecord.getType().equals("SourceNat"))?true:false);
 +			//isSystem
 +			usageRecResponse.setSystem((usageRecord.getSize() == 1)?true:false);
 +			//IP Address ID
 +			IPAddressVO ip = _entityMgr.findByIdIncludingRemoved(IPAddressVO.class, usageRecord.getUsageId().toString());
 +			usageRecResponse.setUsageId(ip.getUuid());
 +
 +		} else if(usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_SENT || usageRecord.getUsageType() == UsageTypes.NETWORK_BYTES_RECEIVED){
 +			//Device Type
 +			usageRecResponse.setType(usageRecord.getType());
 +			if(usageRecord.getType().equals("DomainRouter")){
 +				//Domain Router Id
 +				VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
 +				usageRecResponse.setUsageId(vm.getUuid());
 +			} else {
 +				//External Device Host Id
 +				HostVO host = _entityMgr.findByIdIncludingRemoved(HostVO.class, usageRecord.getUsageId().toString());
 +				usageRecResponse.setUsageId(host.getUuid());
 +			}
 +			//Network ID
 +			NetworkVO network = _entityMgr.findByIdIncludingRemoved(NetworkVO.class, usageRecord.getNetworkId().toString());
 +			usageRecResponse.setNetworkId(network.getUuid());
  
+         } else if(usageRecord.getUsageType() == UsageTypes.VM_DISK_IO_READ || usageRecord.getUsageType() == UsageTypes.VM_DISK_IO_WRITE ||
+                   usageRecord.getUsageType() == UsageTypes.VM_DISK_BYTES_READ || usageRecord.getUsageType() == UsageTypes.VM_DISK_BYTES_WRITE){
+             //Device Type
+             usageRecResponse.setType(usageRecord.getType());
+             //VM Instance Id
+             VMInstanceVO vm = _entityMgr.findByIdIncludingRemoved(VMInstanceVO.class, usageRecord.getUsageId().toString());
+             usageRecResponse.setUsageId(vm.getUuid());
+             //Volume ID
+             VolumeVO volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
+             usageRecResponse.setUsageId(volume.getUuid());
+ 
 -        } else if(usageRecord.getUsageType() == UsageTypes.VOLUME){
 -            //Volume ID
 -            VolumeVO volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
 -            usageRecResponse.setUsageId(volume.getUuid());
 -            //Volume Size
 -            usageRecResponse.setSize(usageRecord.getSize());
 -            //Disk Offering Id
 -            if(usageRecord.getOfferingId() != null){
 -                DiskOfferingVO diskOff = _entityMgr.findByIdIncludingRemoved(DiskOfferingVO.class, usageRecord.getOfferingId().toString());
 -                usageRecResponse.setOfferingId(diskOff.getUuid());
 -            }
 -
 -        } else if(usageRecord.getUsageType() == UsageTypes.TEMPLATE || usageRecord.getUsageType() == UsageTypes.ISO){
 -            //Template/ISO ID
 -            VMTemplateVO tmpl = _entityMgr.findByIdIncludingRemoved(VMTemplateVO.class, usageRecord.getUsageId().toString());
 -            usageRecResponse.setUsageId(tmpl.getUuid());
 -            //Template/ISO Size
 -            usageRecResponse.setSize(usageRecord.getSize());
 -
 -        } else if(usageRecord.getUsageType() == UsageTypes.SNAPSHOT){
 -            //Snapshot ID
 -            SnapshotVO snap = _entityMgr.findByIdIncludingRemoved(SnapshotVO.class, usageRecord.getUsageId().toString());
 -            usageRecResponse.setUsageId(snap.getUuid());
 -            //Snapshot Size
 -            usageRecResponse.setSize(usageRecord.getSize());
 -
 -        } else if(usageRecord.getUsageType() == UsageTypes.LOAD_BALANCER_POLICY){
 -            //Load Balancer Policy ID
 +		} else if(usageRecord.getUsageType() == UsageTypes.VOLUME){
 +			//Volume ID
 +			VolumeVO volume = _entityMgr.findByIdIncludingRemoved(VolumeVO.class, usageRecord.getUsageId().toString());
 +			usageRecResponse.setUsageId(volume.getUuid());
 +			//Volume Size
 +			usageRecResponse.setSize(usageRecord.getSize());
 +			//Disk Offering Id
 +			if(usageRecord.getOfferingId() != null){
 +				DiskOfferingVO diskOff = _entityMgr.findByIdIncludingRemoved(DiskOfferingVO.class, usageRecord.getOfferingId().toString());
 +				usageRecResponse.setOfferingId(diskOff.getUuid());
 +			}
 +
 +		} else if(usageRecord.getUsageType() == UsageTypes.TEMPLATE || usageRecord.getUsageType() == UsageTypes.ISO){
 +			//Template/ISO ID
 +			VMTemplateVO tmpl = _entityMgr.findByIdIncludingRemoved(VMTemplateVO.class, usageRecord.getUsageId().toString());
 +			usageRecResponse.setUsageId(tmpl.getUuid());
 +			//Template/ISO Size
 +			usageRecResponse.setSize(usageRecord.getSize());
 +
 +		} else if(usageRecord.getUsageType() == UsageTypes.SNAPSHOT){
 +			//Snapshot ID
 +			SnapshotVO snap = _entityMgr.findByIdIncludingRemoved(SnapshotVO.class, usageRecord.getUsageId().toString());
 +			usageRecResponse.setUsageId(snap.getUuid());
 +			//Snapshot Size
 +			usageRecResponse.setSize(usageRecord.getSize());
 +
 +		} else if(usageRecord.getUsageType() == UsageTypes.LOAD_BALANCER_POLICY){
 +			//Load Balancer Policy ID
- 			usageRecResponse.setUsageId(usageRecord.getUsageId().toString());
- 
+             LoadBalancerVO lb = _entityMgr.findByIdIncludingRemoved(LoadBalancerVO.class, usageRecord.getUsageId().toString());
+             usageRecResponse.setUsageId(lb.getUuid());
 -        } else if(usageRecord.getUsageType() == UsageTypes.PORT_FORWARDING_RULE){
 -            //Port Forwarding Rule ID
 +		} else if(usageRecord.getUsageType() == UsageTypes.PORT_FORWARDING_RULE){
 +			//Port Forwarding Rule ID
- 			usageRecResponse.setUsageId(usageRecord.getUsageId().toString());
+             PortForwardingRuleVO pf = _entityMgr.findByIdIncludingRemoved(PortForwardingRuleVO.class, usageRecord.getUsageId().toString());
+             usageRecResponse.setUsageId(pf.getUuid());
  
 -        } else if(usageRecord.getUsageType() == UsageTypes.NETWORK_OFFERING){
 -            //Network Offering Id
 -            NetworkOfferingVO netOff = _entityMgr.findByIdIncludingRemoved(NetworkOfferingVO.class, usageRecord.getOfferingId().toString());
 -            usageRecResponse.setOfferingId(netOff.getUuid());
 -            //is Default
 -            usageRecResponse.setDefault((usageRecord.getUsageId() == 1)? true:false);
 +		} else if(usageRecord.getUsageType() == UsageTypes.NETWORK_OFFERING){
 +			//Network Offering Id
 +			NetworkOfferingVO netOff = _entityMgr.findByIdIncludingRemoved(NetworkOfferingVO.class, usageRecord.getOfferingId().toString());
 +			usageRecResponse.setOfferingId(netOff.getUuid());
 +			//is Default
 +			usageRecResponse.setDefault((usageRecord.getUsageId() == 1)? true:false);
  
 -        } else if(usageRecord.getUsageType() == UsageTypes.VPN_USERS){
 -            //VPN User ID
 +		} else if(usageRecord.getUsageType() == UsageTypes.VPN_USERS){
 +			//VPN User ID
- 			usageRecResponse.setUsageId(usageRecord.getUsageId().toString());
+             VpnUserVO vpnUser = _entityMgr.findByIdIncludingRemoved(VpnUserVO.class, usageRecord.getUsageId().toString());
+             usageRecResponse.setUsageId(vpnUser.getUuid());
  
 -        } else if(usageRecord.getUsageType() == UsageTypes.SECURITY_GROUP){
 -            //Security Group Id
 -            SecurityGroupVO sg = _entityMgr.findByIdIncludingRemoved(SecurityGroupVO.class, usageRecord.getUsageId().toString());
 -            usageRecResponse.setUsageId(sg.getUuid());
 -        }
 -
 -        if (usageRecord.getRawUsage() != null) {
 -            DecimalFormat decimalFormat = new DecimalFormat("###########.######");
 -            usageRecResponse.setRawUsage(decimalFormat.format(usageRecord.getRawUsage()));
 -        }
 -
 -        if (usageRecord.getStartDate() != null) {
 -            usageRecResponse.setStartDate(getDateStringInternal(usageRecord.getStartDate()));
 -        }
 -        if (usageRecord.getEndDate() != null) {
 -            usageRecResponse.setEndDate(getDateStringInternal(usageRecord.getEndDate()));
 -        }
 -
 -        return usageRecResponse;
 -    }
 -
 +		} else if(usageRecord.getUsageType() == UsageTypes.SECURITY_GROUP){
 +			//Security Group Id
 +			SecurityGroupVO sg = _entityMgr.findByIdIncludingRemoved(SecurityGroupVO.class, usageRecord.getUsageId().toString());
 +			usageRecResponse.setUsageId(sg.getUuid());
 +		}
 +
 +		if (usageRecord.getRawUsage() != null) {
 +			DecimalFormat decimalFormat = new DecimalFormat("###########.######");
 +			usageRecResponse.setRawUsage(decimalFormat.format(usageRecord.getRawUsage()));
 +		}
 +
 +		if (usageRecord.getStartDate() != null) {
 +			usageRecResponse.setStartDate(getDateStringInternal(usageRecord.getStartDate()));
 +		}
 +		if (usageRecord.getEndDate() != null) {
 +			usageRecResponse.setEndDate(getDateStringInternal(usageRecord.getEndDate()));
 +		}
 +
 +		return usageRecResponse;
 +	}
  
      public String getDateStringInternal(Date inputDate) {
 -        if (inputDate == null) return null;
 +        if (inputDate == null)
 +            return null;
  
          TimeZone tz = _usageSvc.getUsageTimezone();
          Calendar cal = Calendar.getInstance(tz);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/api/query/QueryManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/api/query/QueryManagerImpl.java
index 331404c,beda75e..b0d8d9a
--- a/server/src/com/cloud/api/query/QueryManagerImpl.java
+++ b/server/src/com/cloud/api/query/QueryManagerImpl.java
@@@ -1032,7 -996,7 +1036,7 @@@ public class QueryManagerImpl extends M
          return response;
      }
  
--    
++
      @Override
      public ListResponse<DomainRouterResponse> searchForInternalLbVms(ListInternalLBVMsCmd cmd) {
          Pair<List<DomainRouterJoinVO>, Integer> result = searchForRoutersInternal(cmd, cmd.getId(), cmd.getRouterName(),
@@@ -1048,7 -1012,7 +1052,7 @@@
  
      private Pair<List<DomainRouterJoinVO>, Integer> searchForRoutersInternal(BaseListProjectAndAccountResourcesCmd cmd, Long id,
              String name, String state, Long zoneId, Long podId, Long hostId, String keyword, Long networkId, Long vpcId, Boolean forVpc, String role, String zoneType) {
--       
++
  
          Account caller = UserContext.current().getCaller();
          List<Long> permittedAccounts = new ArrayList<Long>();
@@@ -1140,7 -1105,7 +1144,7 @@@
          if (vpcId != null) {
              sc.setParameters("vpcId", vpcId);
          }
--        
++
          if (role != null) {
              sc.setParameters("role", role);
          }
@@@ -2494,311 -2400,11 +2523,312 @@@
      }
  
      @Override
 +    public ListResponse<TemplateResponse> listTemplates(ListTemplatesCmd cmd) {
 +        Pair<List<TemplateJoinVO>, Integer> result = searchForTemplatesInternal(cmd);
 +        ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
 +
 +        List<TemplateResponse> templateResponses = ViewResponseHelper.createTemplateResponse(result.first().toArray(new TemplateJoinVO[result.first().size()]));
 +        response.setResponses(templateResponses, result.second());
 +        return response;
 +    }
 +
 +    private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(ListTemplatesCmd cmd) {
 +        TemplateFilter templateFilter = TemplateFilter.valueOf(cmd.getTemplateFilter());
 +        Long id = cmd.getId();
 +        Map<String, String> tags = cmd.getTags();
 +        Account caller = UserContext.current().getCaller();
 +
 +        boolean listAll = false;
 +        if (templateFilter != null && templateFilter == TemplateFilter.all) {
 +            if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
 +                throw new InvalidParameterValueException("Filter " + TemplateFilter.all + " can be specified by admin only");
 +            }
 +            listAll = true;
 +        }
 +
 +        List<Long> permittedAccountIds = new ArrayList<Long>();
 +        Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(
 +                cmd.getDomainId(), cmd.isRecursive(), null);
 +        _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccountIds, domainIdRecursiveListProject,
 +                listAll, false);
 +        ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
 +        List<Account> permittedAccounts = new ArrayList<Account>();
 +        for (Long accountId : permittedAccountIds) {
 +            permittedAccounts.add(_accountMgr.getAccount(accountId));
 +        }
 +
 +        boolean showDomr = ((templateFilter != TemplateFilter.selfexecutable) && (templateFilter != TemplateFilter.featured));
 +        HypervisorType hypervisorType = HypervisorType.getType(cmd.getHypervisor());
 +
 +        return searchForTemplatesInternal(id, cmd.getTemplateName(), cmd.getKeyword(), templateFilter, false, null, cmd.getPageSizeVal(), cmd.getStartIndex(),
 +                cmd.getZoneId(), hypervisorType, showDomr, cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags);
 +    }
 +
 +    private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long templateId, String name, String keyword,
 +            TemplateFilter templateFilter, boolean isIso, Boolean bootable, Long pageSize, Long startIndex, Long zoneId, HypervisorType hyperType,
 +            boolean showDomr, boolean onlyReady, List<Account> permittedAccounts, Account caller,
 +            ListProjectResourcesCriteria listProjectResourcesCriteria, Map<String, String> tags) {
 +        VMTemplateVO template = null;
 +
 +        Boolean isAscending = Boolean.parseBoolean(_configDao.getValue("sortkey.algorithm"));
 +        isAscending = (isAscending == null ? true : isAscending);
 +        Filter searchFilter = new Filter(TemplateJoinVO.class, "sortKey", isAscending, startIndex, pageSize);
 +        SearchCriteria<TemplateJoinVO> sc = _templateJoinDao.createSearchCriteria();
 +
 +        // verify templateId parameter and specially handle it
 +        if (templateId != null) {
 +            template = _templateDao.findById(templateId);
 +            if (template == null) {
 +                throw new InvalidParameterValueException("Please specify a valid template ID.");
 +            }// If ISO requested then it should be ISO.
 +            if (isIso && template.getFormat() != ImageFormat.ISO) {
 +                s_logger.error("Template Id " + templateId + " is not an ISO");
 +                InvalidParameterValueException ex = new InvalidParameterValueException("Specified Template Id is not an ISO");
-                 ex.addProxyObject(template, templateId, "templateId");
++                ex.addProxyObject(template.getUuid(), "templateId");
 +                throw ex;
 +            }// If ISO not requested then it shouldn't be an ISO.
 +            if (!isIso && template.getFormat() == ImageFormat.ISO) {
 +                s_logger.error("Incorrect format of the template id " + templateId);
 +                InvalidParameterValueException ex = new InvalidParameterValueException("Incorrect format " + template.getFormat()
 +                        + " of the specified template id");
-                 ex.addProxyObject(template, templateId, "templateId");
++                ex.addProxyObject(template.getUuid(), "templateId");
 +                throw ex;
 +            }
 +
 +            // if template is not public, perform permission check here
 +            if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
 +                Account owner = _accountMgr.getAccount(template.getAccountId());
 +                _accountMgr.checkAccess(caller, null, true, owner);
 +            }
 +
 +            // if templateId is specified, then we will just use the id to
 +            // search and ignore other query parameters
 +            sc.addAnd("id", SearchCriteria.Op.EQ, templateId);
 +        } else {
 +
 +            DomainVO domain = null;
 +            if (!permittedAccounts.isEmpty()) {
 +                domain = _domainDao.findById(permittedAccounts.get(0).getDomainId());
 +            } else {
 +                domain = _domainDao.findById(DomainVO.ROOT_DOMAIN);
 +            }
 +
 +            List<HypervisorType> hypers = null;
 +            if (!isIso) {
 +                hypers = _resourceMgr.listAvailHypervisorInZone(null, null);
 +            }
 +
 +            // add criteria for project or not
 +            if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
 +                sc.addAnd("accountType", SearchCriteria.Op.NEQ, Account.ACCOUNT_TYPE_PROJECT);
 +            } else if (listProjectResourcesCriteria == ListProjectResourcesCriteria.ListProjectResourcesOnly) {
 +                sc.addAnd("accountType", SearchCriteria.Op.EQ, Account.ACCOUNT_TYPE_PROJECT);
 +            }
 +
 +            // add criteria for domain path in case of domain admin
 +            if ((templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable)
 +                    && (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN)) {
 +                sc.addAnd("domainPath", SearchCriteria.Op.LIKE, domain.getPath() + "%");
 +            }
 +
 +            List<Long> relatedDomainIds = new ArrayList<Long>();
 +            List<Long> permittedAccountIds = new ArrayList<Long>();
 +            if (!permittedAccounts.isEmpty()) {
 +                for (Account account : permittedAccounts) {
 +                    permittedAccountIds.add(account.getId());
 +                    DomainVO accountDomain = _domainDao.findById(account.getDomainId());
 +
 +                    // get all parent domain ID's all the way till root domain
 +                    DomainVO domainTreeNode = accountDomain;
 +                    relatedDomainIds.add(domainTreeNode.getId());
 +                    while (domainTreeNode.getParent() != null) {
 +                        domainTreeNode = _domainDao.findById(domainTreeNode.getParent());
 +                        relatedDomainIds.add(domainTreeNode.getId());
 +                    }
 +
 +                    // get all child domain ID's
 +                    if (_accountMgr.isAdmin(account.getType())) {
 +                        List<DomainVO> allChildDomains = _domainDao.findAllChildren(accountDomain.getPath(), accountDomain.getId());
 +                        for (DomainVO childDomain : allChildDomains) {
 +                            relatedDomainIds.add(childDomain.getId());
 +                        }
 +                    }
 +                }
 +            }
 +
 +            if (!isIso) {
 +                // add hypervisor criteria for template case
 +                if (hypers != null && !hypers.isEmpty()) {
 +                    String[] relatedHypers = new String[hypers.size()];
 +                    for (int i = 0; i < hypers.size(); i++) {
 +                        relatedHypers[i] = hypers.get(i).toString();
 +                    }
 +                    sc.addAnd("hypervisorType", SearchCriteria.Op.IN, relatedHypers);
 +                }
 +            }
 +
 +            // control different template filters
 +            if (templateFilter == TemplateFilter.featured || templateFilter == TemplateFilter.community) {
 +                sc.addAnd("publicTemplate", SearchCriteria.Op.EQ, true);
 +                if (templateFilter == TemplateFilter.featured) {
 +                    sc.addAnd("featured", SearchCriteria.Op.EQ, true);
 +                } else {
 +                    sc.addAnd("featured", SearchCriteria.Op.EQ, false);
 +                }
 +                if (!permittedAccounts.isEmpty()) {
 +                    SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
 +                    scc.addOr("domainId", SearchCriteria.Op.IN, relatedDomainIds.toArray());
 +                    scc.addOr("domainId", SearchCriteria.Op.NULL);
 +                    sc.addAnd("domainId", SearchCriteria.Op.SC, scc);
 +                }
 +            } else if (templateFilter == TemplateFilter.self || templateFilter == TemplateFilter.selfexecutable) {
 +                if (!permittedAccounts.isEmpty()) {
 +                    sc.addAnd("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
 +                }
 +            } else if (templateFilter == TemplateFilter.sharedexecutable || templateFilter == TemplateFilter.shared) {
 +                SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
 +                scc.addOr("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
 +                scc.addOr("sharedAccountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
 +                sc.addAnd("accountId", SearchCriteria.Op.SC, scc);
 +            } else if (templateFilter == TemplateFilter.executable) {
 +                SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
 +                scc.addOr("publicTemplate", SearchCriteria.Op.EQ, true);
 +                if (!permittedAccounts.isEmpty()) {
 +                    scc.addOr("accountId", SearchCriteria.Op.IN, permittedAccountIds.toArray());
 +                }
 +                sc.addAnd("publicTemplate", SearchCriteria.Op.SC, scc);
 +            }
 +
 +            // add tags criteria
 +            if (tags != null && !tags.isEmpty()) {
 +                SearchCriteria<TemplateJoinVO> scc = _templateJoinDao.createSearchCriteria();
 +                int count = 0;
 +                for (String key : tags.keySet()) {
 +                    SearchCriteria<TemplateJoinVO> scTag = _templateJoinDao.createSearchCriteria();
 +                    scTag.addAnd("tagKey", SearchCriteria.Op.EQ, key);
 +                    scTag.addAnd("tagValue", SearchCriteria.Op.EQ, tags.get(key));
 +                    if (isIso) {
 +                        scTag.addAnd("tagResourceType", SearchCriteria.Op.EQ, TaggedResourceType.ISO);
 +                    } else {
 +                        scTag.addAnd("tagResourceType", SearchCriteria.Op.EQ, TaggedResourceType.Template);
 +                    }
 +                    scc.addOr("tagKey", SearchCriteria.Op.SC, scTag);
 +                    count++;
 +                }
 +                sc.addAnd("tagKey", SearchCriteria.Op.SC, scc);
 +            }
 +
 +            // other criteria
 +
 +            if (keyword != null) {
 +                sc.addAnd("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
 +            } else if (name != null) {
 +                sc.addAnd("name", SearchCriteria.Op.EQ, name);
 +            }
 +
 +            if (isIso) {
 +                sc.addAnd("format", SearchCriteria.Op.EQ, "ISO");
 +
 +            } else {
 +                sc.addAnd("format", SearchCriteria.Op.NEQ, "ISO");
 +            }
 +
 +            if (!hyperType.equals(HypervisorType.None)) {
 +                sc.addAnd("hypervisorType", SearchCriteria.Op.EQ, hyperType);
 +            }
 +
 +            if (bootable != null) {
 +                sc.addAnd("bootable", SearchCriteria.Op.EQ, bootable);
 +            }
 +
 +            if (onlyReady) {
 +                sc.addAnd("state", SearchCriteria.Op.EQ, TemplateState.Ready);
 +            }
 +
 +            if (zoneId != null) {
 +                sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
 +            }
 +
 +            if (!showDomr) {
 +                // excluding system template
 +                sc.addAnd("templateType", SearchCriteria.Op.NEQ, Storage.TemplateType.SYSTEM);
 +            }
 +        }
 +
 +        // don't return removed template, this should not be needed since we
 +        // changed annotation for removed field in TemplateJoinVO.
 +        // sc.addAnd("removed", SearchCriteria.Op.NULL);
 +
 +        // search unique templates and find details by Ids
 +        Pair<List<TemplateJoinVO>, Integer> uniqueTmplPair = _templateJoinDao.searchAndCount(sc, searchFilter);
 +        Integer count = uniqueTmplPair.second();
 +        if (count.intValue() == 0) {
 +            // empty result
 +            return uniqueTmplPair;
 +        }
 +        List<TemplateJoinVO> uniqueTmpls = uniqueTmplPair.first();
 +        Long[] vrIds = new Long[uniqueTmpls.size()];
 +        int i = 0;
 +        for (TemplateJoinVO v : uniqueTmpls) {
 +            vrIds[i++] = v.getId();
 +        }
 +        List<TemplateJoinVO> vrs = _templateJoinDao.searchByIds(vrIds);
 +        return new Pair<List<TemplateJoinVO>, Integer>(vrs, count);
 +
 +        // TODO: revisit the special logic for iso search in
 +        // VMTemplateDaoImpl.searchForTemplates and understand why we need to
 +        // specially handle ISO. The original logic is very twisted and no idea
 +        // about what the code was doing.
 +
 +    }
 +
 +    @Override
 +    public ListResponse<TemplateResponse> listIsos(ListIsosCmd cmd) {
 +        Pair<List<TemplateJoinVO>, Integer> result = searchForIsosInternal(cmd);
 +        ListResponse<TemplateResponse> response = new ListResponse<TemplateResponse>();
 +
 +        List<TemplateResponse> templateResponses = ViewResponseHelper.createIsoResponse(result.first().toArray(new TemplateJoinVO[result.first().size()]));
 +        response.setResponses(templateResponses, result.second());
 +        return response;
 +    }
 +
 +    private Pair<List<TemplateJoinVO>, Integer> searchForIsosInternal(ListIsosCmd cmd) {
 +        TemplateFilter isoFilter = TemplateFilter.valueOf(cmd.getIsoFilter());
 +        Long id = cmd.getId();
 +        Map<String, String> tags = cmd.getTags();
 +        Account caller = UserContext.current().getCaller();
 +
 +        boolean listAll = false;
 +        if (isoFilter != null && isoFilter == TemplateFilter.all) {
 +            if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL) {
 +                throw new InvalidParameterValueException("Filter " + TemplateFilter.all + " can be specified by admin only");
 +            }
 +            listAll = true;
 +        }
 +
 +        List<Long> permittedAccountIds = new ArrayList<Long>();
 +        Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(
 +                cmd.getDomainId(), cmd.isRecursive(), null);
 +        _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccountIds, domainIdRecursiveListProject,
 +                listAll, false);
 +        ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
 +        List<Account> permittedAccounts = new ArrayList<Account>();
 +        for (Long accountId : permittedAccountIds) {
 +            permittedAccounts.add(_accountMgr.getAccount(accountId));
 +        }
 +
 +        HypervisorType hypervisorType = HypervisorType.getType(cmd.getHypervisor());
 +
 +        return searchForTemplatesInternal(cmd.getId(), cmd.getIsoName(), cmd.getKeyword(), isoFilter, true, cmd.isBootable(), cmd.getPageSizeVal(),
 +                cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, true, cmd.listInReadyState(), permittedAccounts, caller,
 +                listProjectResourcesCriteria, tags);
 +    }
 +
      public ListResponse<AffinityGroupResponse> listAffinityGroups(Long affinityGroupId, String affinityGroupName,
-             String affinityGroupType, Long vmId, Long startIndex, Long pageSize) {
+             String affinityGroupType, Long vmId, String accountName, Long domainId, boolean isRecursive,
+             boolean listAll, Long startIndex, Long pageSize) {
          Pair<List<AffinityGroupJoinVO>, Integer> result = listAffinityGroupsInternal(affinityGroupId,
-                 affinityGroupName, affinityGroupType, vmId, startIndex, pageSize);
+                 affinityGroupName, affinityGroupType, vmId, accountName, domainId, isRecursive, listAll, startIndex, pageSize);
          ListResponse<AffinityGroupResponse> response = new ListResponse<AffinityGroupResponse>();
          List<AffinityGroupResponse> agResponses = ViewResponseHelper.createAffinityGroupResponses(result.first());
          response.setResponses(agResponses, result.second());

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/api/query/ViewResponseHelper.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/api/query/ViewResponseHelper.java
index af8455b,a61da69..b98cea3
--- a/server/src/com/cloud/api/query/ViewResponseHelper.java
+++ b/server/src/com/cloud/api/query/ViewResponseHelper.java
@@@ -29,10 -46,9 +46,10 @@@ import org.apache.cloudstack.api.respon
  import org.apache.cloudstack.api.response.DiskOfferingResponse;
  import org.apache.cloudstack.api.response.DomainRouterResponse;
  import org.apache.cloudstack.api.response.EventResponse;
- import org.apache.cloudstack.api.response.HostResponse;
  import org.apache.cloudstack.api.response.HostForMigrationResponse;
+ import org.apache.cloudstack.api.response.HostResponse;
  import org.apache.cloudstack.api.response.InstanceGroupResponse;
 +import org.apache.cloudstack.api.response.ImageStoreResponse;
  import org.apache.cloudstack.api.response.ProjectAccountResponse;
  import org.apache.cloudstack.api.response.ProjectInvitationResponse;
  import org.apache.cloudstack.api.response.ProjectResponse;
@@@ -40,38 -56,16 +57,20 @@@ import org.apache.cloudstack.api.respon
  import org.apache.cloudstack.api.response.SecurityGroupResponse;
  import org.apache.cloudstack.api.response.ServiceOfferingResponse;
  import org.apache.cloudstack.api.response.StoragePoolResponse;
 +import org.apache.cloudstack.api.response.TemplateResponse;
- import org.apache.cloudstack.api.response.StoragePoolForMigrationResponse;
  import org.apache.cloudstack.api.response.UserResponse;
  import org.apache.cloudstack.api.response.UserVmResponse;
  import org.apache.cloudstack.api.response.VolumeResponse;
  import org.apache.cloudstack.api.response.ZoneResponse;
  import org.apache.log4j.Logger;
  
- import com.cloud.api.ApiDBUtils;
- import com.cloud.api.query.vo.AccountJoinVO;
- import com.cloud.api.query.vo.AffinityGroupJoinVO;
- import com.cloud.api.query.vo.AsyncJobJoinVO;
- import com.cloud.api.query.vo.DataCenterJoinVO;
- import com.cloud.api.query.vo.DiskOfferingJoinVO;
- import com.cloud.api.query.vo.DomainRouterJoinVO;
- import com.cloud.api.query.vo.EventJoinVO;
- import com.cloud.api.query.vo.HostJoinVO;
++
 +import com.cloud.api.query.vo.ImageStoreJoinVO;
- import com.cloud.api.query.vo.InstanceGroupJoinVO;
- import com.cloud.api.query.vo.ProjectAccountJoinVO;
- import com.cloud.api.query.vo.ProjectInvitationJoinVO;
- import com.cloud.api.query.vo.ProjectJoinVO;
- import com.cloud.api.query.vo.ResourceTagJoinVO;
- import com.cloud.api.query.vo.SecurityGroupJoinVO;
- import com.cloud.api.query.vo.ServiceOfferingJoinVO;
- import com.cloud.api.query.vo.StoragePoolJoinVO;
 +import com.cloud.api.query.vo.TemplateJoinVO;
- import com.cloud.api.query.vo.UserAccountJoinVO;
- import com.cloud.api.query.vo.UserVmJoinVO;
- import com.cloud.api.query.vo.VolumeJoinVO;
- import com.cloud.user.Account;
- import com.cloud.user.UserContext;
+ import java.util.ArrayList;
+ import java.util.EnumSet;
+ import java.util.Hashtable;
+ import java.util.List;
  
  /**
   * Helper class to generate response from DB view VO objects.
@@@ -289,29 -283,11 +288,30 @@@ public class ViewResponseHelper 
          return new ArrayList<StoragePoolResponse>(vrDataList.values());
      }
  
++
 +    public static List<ImageStoreResponse> createImageStoreResponse(ImageStoreJoinVO... stores) {
 +        Hashtable<Long, ImageStoreResponse> vrDataList = new Hashtable<Long, ImageStoreResponse>();
 +        // Initialise the vrdatalist with the input data
 +        for (ImageStoreJoinVO vr : stores) {
 +            ImageStoreResponse vrData = vrDataList.get(vr.getId());
 +            if ( vrData == null ){
 +                // first time encountering this vm
 +                vrData = ApiDBUtils.newImageStoreResponse(vr);
 +            }
 +            else{
 +                // update tags
 +                vrData = ApiDBUtils.fillImageStoreDetails(vrData, vr);
 +            }
 +            vrDataList.put(vr.getId(), vrData);
 +        }
 +        return new ArrayList<ImageStoreResponse>(vrDataList.values());
 +    }
-     
-     public static List<StoragePoolForMigrationResponse> createStoragePoolForMigrationResponse(StoragePoolJoinVO... pools) {
-         Hashtable<Long, StoragePoolForMigrationResponse> vrDataList = new Hashtable<Long, StoragePoolForMigrationResponse>();
++
+     public static List<StoragePoolResponse> createStoragePoolForMigrationResponse(StoragePoolJoinVO... pools) {
+         Hashtable<Long, StoragePoolResponse> vrDataList = new Hashtable<Long, StoragePoolResponse>();
          // Initialise the vrdatalist with the input data
          for (StoragePoolJoinVO vr : pools) {
-             StoragePoolForMigrationResponse vrData = vrDataList.get(vr.getId());
+             StoragePoolResponse vrData = vrDataList.get(vr.getId());
              if ( vrData == null ) {
                  // first time encountering this vm
                  vrData = ApiDBUtils.newStoragePoolForMigrationResponse(vr);
@@@ -365,57 -341,6 +365,57 @@@
          return respList;
      }
  
 +    public static List<TemplateResponse> createTemplateResponse(TemplateJoinVO... templates) {
 +        Hashtable<Long, TemplateResponse> vrDataList = new Hashtable<Long, TemplateResponse>();
 +        for (TemplateJoinVO vr : templates) {
 +            TemplateResponse vrData = vrDataList.get(vr.getId());
 +            if ( vrData == null ){
 +                // first time encountering this volume
 +                vrData = ApiDBUtils.newTemplateResponse(vr);
 +            }
 +            else{
 +                // update tags
 +                vrData = ApiDBUtils.fillTemplateDetails(vrData, vr);
 +            }
 +            vrDataList.put(vr.getId(), vrData);
 +        }
 +        return new ArrayList<TemplateResponse>(vrDataList.values());
 +    }
 +
 +    public static List<TemplateResponse> createTemplateUpdateResponse(TemplateJoinVO... templates) {
 +        Hashtable<Long, TemplateResponse> vrDataList = new Hashtable<Long, TemplateResponse>();
 +        for (TemplateJoinVO vr : templates) {
 +            TemplateResponse vrData = vrDataList.get(vr.getId());
 +            if ( vrData == null ){
 +                // first time encountering this volume
 +                vrData = ApiDBUtils.newTemplateUpdateResponse(vr);
 +            }
 +            else{
 +                // update tags
 +                vrData = ApiDBUtils.fillTemplateDetails(vrData, vr);
 +            }
 +            vrDataList.put(vr.getId(), vrData);
 +        }
 +        return new ArrayList<TemplateResponse>(vrDataList.values());
 +    }
 +
 +    public static List<TemplateResponse> createIsoResponse(TemplateJoinVO... templates) {
 +        Hashtable<Long, TemplateResponse> vrDataList = new Hashtable<Long, TemplateResponse>();
 +        for (TemplateJoinVO vr : templates) {
 +            TemplateResponse vrData = vrDataList.get(vr.getId());
 +            if ( vrData == null ){
 +                // first time encountering this volume
 +                vrData = ApiDBUtils.newIsoResponse(vr);
 +            }
 +            else{
 +                // update tags
 +                vrData = ApiDBUtils.fillTemplateDetails(vrData, vr);
 +            }
 +            vrDataList.put(vr.getId(), vrData);
 +        }
 +        return new ArrayList<TemplateResponse>(vrDataList.values());
 +    }
-     
++
      public static List<AffinityGroupResponse> createAffinityGroupResponses(List<AffinityGroupJoinVO> groups) {
          Hashtable<Long, AffinityGroupResponse> vrDataList = new Hashtable<Long, AffinityGroupResponse>();
          for (AffinityGroupJoinVO vr : groups) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/api/query/dao/UserVmJoinDaoImpl.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/api/query/vo/StoragePoolJoinVO.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/api/query/vo/StoragePoolJoinVO.java
index 07347c5,29e97f4..5ec5447
--- a/server/src/com/cloud/api/query/vo/StoragePoolJoinVO.java
+++ b/server/src/com/cloud/api/query/vo/StoragePoolJoinVO.java
@@@ -32,8 -27,14 +32,10 @@@ import com.cloud.utils.db.GenericDao
  
  import org.apache.cloudstack.api.Identity;
  import org.apache.cloudstack.api.InternalIdentity;
 -import org.apache.cloudstack.engine.subsystem.api.storage.ScopeType;
 +
  
+ import com.cloud.hypervisor.Hypervisor.HypervisorType;
 -import com.cloud.org.Cluster;
 -import com.cloud.storage.Storage.StoragePoolType;
 -import com.cloud.storage.StoragePoolStatus;
 -import com.cloud.utils.db.GenericDao;
+ 
  /**
   * Storage Pool DB view.
   *

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/configuration/ConfigurationManagerImpl.java
index f0b6899,59e70cf..9005ee5
--- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
+++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java
@@@ -39,8 -39,6 +39,7 @@@ import javax.naming.NamingException
  import javax.naming.directory.DirContext;
  import javax.naming.directory.InitialDirContext;
  
 +import com.cloud.event.UsageEventUtils;
- import com.cloud.utils.db.*;
  import org.apache.cloudstack.acl.SecurityChecker;
  import org.apache.cloudstack.api.ApiConstants.LDAPParams;
  import org.apache.cloudstack.api.command.admin.config.UpdateCfgCmd;
@@@ -68,9 -66,13 +67,15 @@@ import org.apache.cloudstack.api.comman
  import org.apache.cloudstack.api.command.admin.zone.DeleteZoneCmd;
  import org.apache.cloudstack.api.command.admin.zone.UpdateZoneCmd;
  import org.apache.cloudstack.api.command.user.network.ListNetworkOfferingsCmd;
 +import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
 +import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
- import org.apache.cloudstack.region.*;
+ import org.apache.cloudstack.region.PortableIp;
+ import org.apache.cloudstack.region.PortableIpDao;
+ import org.apache.cloudstack.region.PortableIpRange;
+ import org.apache.cloudstack.region.PortableIpRangeDao;
+ import org.apache.cloudstack.region.PortableIpRangeVO;
+ import org.apache.cloudstack.region.PortableIpVO;
+ import org.apache.cloudstack.region.Region;
  import org.apache.cloudstack.region.dao.RegionDao;
  import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
  import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailVO;
@@@ -1731,13 -1753,11 +1753,11 @@@ public class ConfigurationManagerImpl e
                  // check if zone has necessary trafficTypes before enabling
                  try {
                      PhysicalNetwork mgmtPhyNetwork;
-                     if (NetworkType.Advanced == zone.getNetworkType()) {
-                         // zone should have a physical network with public and management traffiType
-                         _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Public);
-                         mgmtPhyNetwork = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Management);
-                     } else {
 -                    // zone should have a physical network with management traffiType
 -                    mgmtPhyNetwork = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Management);
 +                        // zone should have a physical network with management traffiType
 +                        mgmtPhyNetwork = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Management);
+                     if (NetworkType.Advanced == zone.getNetworkType() && ! zone.isSecurityGroupEnabled() ) {
+                         // advanced zone without SG should have a physical network with public Thpe
+                         _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Public);
                      }
  
                      try {
@@@ -3014,9 -3039,16 +3038,16 @@@
                      }
                  }
  
+             } else {
+                 // when there is no dhcp support in the network.
+                 if (!deletePublicIPRange(vlanDbId)) {
+                     return false;
 -                }
 +            }
+                 _vlanDao.expunge(vlanDbId);
+                 return  true;
 -            }
++        }
          }
-        throw new InvalidParameterValueException("One of the ips in the range is used to provide Dhcp service to this subnet. cannot delete this range as ");
+         return false;
      }
  
  
@@@ -3289,43 -3321,7 +3320,7 @@@
              throw new InvalidParameterValueException("Please ensure that your end IP is in the same subnet as your IP range's gateway, as per the IP range's netmask.");
          }
      }
 -    
 +
-     private void checkPrivateIpRangeErrors(Long podId, String startIP, String endIP) {
-         HostPodVO pod = _podDao.findById(podId);
-         if (pod == null) {
-             throw new InvalidParameterValueException("Please specify a valid pod.");
-         }
- 
-         // Check that the start and end IPs are valid
-         if (!NetUtils.isValidIp(startIP)) {
-             throw new InvalidParameterValueException("Please specify a valid start IP");
-         }
- 
-         if (endIP != null && !NetUtils.isValidIp(endIP)) {
-             throw new InvalidParameterValueException("Please specify a valid end IP");
-         }
- 
-         if (endIP != null && !NetUtils.validIpRange(startIP, endIP)) {
-             throw new InvalidParameterValueException("Please specify a valid IP range.");
-         }
- 
-         // Check that the IPs that are being added are compatible with the pod's
-         // CIDR
-         String cidrAddress = getCidrAddress(podId);
-         long cidrSize = getCidrSize(podId);
- 
-         if (endIP != null && !NetUtils.sameSubnetCIDR(startIP, endIP, cidrSize)) {
-             throw new InvalidParameterValueException("Please ensure that your start IP and end IP are in the same subnet, as per the pod's CIDR size.");
-         }
- 
-         if (!NetUtils.sameSubnetCIDR(startIP, cidrAddress, cidrSize)) {
-             throw new InvalidParameterValueException("Please ensure that your start IP is in the same subnet as the pod's CIDR address.");
-         }
- 
-         if (endIP != null && !NetUtils.sameSubnetCIDR(endIP, cidrAddress, cidrSize)) {
-             throw new InvalidParameterValueException("Please ensure that your end IP is in the same subnet as the pod's CIDR address.");
-         }
-     }
  
      private String getCidrAddress(String cidr) {
          String[] cidrPair = cidr.split("\\/");

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
index e7828ea,421e53f..5983aa7
--- a/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
+++ b/server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java
@@@ -682,19 -687,28 +682,28 @@@ public class ConsoleProxyManagerImpl ex
  
          DataCenterDeployment plan = new DataCenterDeployment(dataCenterId);
  
+         NetworkVO defaultNetwork = null;
+         if (dc.getNetworkType() == NetworkType.Advanced && dc.isSecurityGroupEnabled()) {
+             List<NetworkVO> networks = _networkDao.listByZoneSecurityGroup(dataCenterId);
+             if (networks == null || networks.size() == 0) {
+                 throw new CloudRuntimeException("Can not found security enabled network in SG Zone " + dc);
+             }
+             defaultNetwork = networks.get(0);
+         } else {
 -            TrafficType defaultTrafficType = TrafficType.Public;
 -            if (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()) {
 -                defaultTrafficType = TrafficType.Guest;
 -            }
 -            List<NetworkVO> defaultNetworks = _networkDao.listByZoneAndTrafficType(dataCenterId, defaultTrafficType);
 +        TrafficType defaultTrafficType = TrafficType.Public;
 +        if (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()) {
 +            defaultTrafficType = TrafficType.Guest;
 +        }
- 
 +        List<NetworkVO> defaultNetworks = _networkDao.listByZoneAndTrafficType(dataCenterId, defaultTrafficType);
  
+             // api should never allow this situation to happen
 -            if (defaultNetworks.size() != 1) {
 +        if (defaultNetworks.size() != 1) {
-             throw new CloudRuntimeException("Found " + defaultNetworks.size() + " networks of type " + defaultTrafficType + " when expect to find 1");
+                 throw new CloudRuntimeException("Found " + defaultNetworks.size() + " networks of type "
+                       + defaultTrafficType + " when expect to find 1");
+             }
+              defaultNetwork = defaultNetworks.get(0);
          }
  
-         NetworkVO defaultNetwork = defaultNetworks.get(0);
- 
          List<? extends NetworkOffering> offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork, NetworkOffering.SystemManagementNetwork);
          List<Pair<NetworkVO, NicProfile>> networks = new ArrayList<Pair<NetworkVO, NicProfile>>(offerings.size() + 1);
          NicProfile defaultNic = new NicProfile();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/deploy/DeploymentPlanningManagerImpl.java
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/hypervisor/HypervisorGuruBase.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/hypervisor/HypervisorGuruBase.java
index befd8c1,d8945af..5d4a580
--- a/server/src/com/cloud/hypervisor/HypervisorGuruBase.java
+++ b/server/src/com/cloud/hypervisor/HypervisorGuruBase.java
@@@ -123,7 -125,10 +127,10 @@@ public abstract class HypervisorGuruBas
          // Workaround to make sure the TO has the UUID we need for Niciri integration
          VMInstanceVO vmInstance = _virtualMachineDao.findById(to.getId());
          to.setUuid(vmInstance.getUuid());
 -
 +        
+         //
+         to.setEnableDynamicallyScaleVm(Boolean.parseBoolean(_configServer.getConfigValue(Config.EnableDynamicallyScaleVm.key(), Config.ConfigurationParameterScope.zone.toString(), vm.getDataCenterId())));
+ 
          return to;
      }
  

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/network/NetworkManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/NetworkManagerImpl.java
index e880180,d6a6450..2fd9bd0
--- a/server/src/com/cloud/network/NetworkManagerImpl.java
+++ b/server/src/com/cloud/network/NetworkManagerImpl.java
@@@ -42,14 -87,28 +87,27 @@@ import com.cloud.event.ActionEventUtils
  import com.cloud.event.EventTypes;
  import com.cloud.event.UsageEventUtils;
  import com.cloud.event.dao.UsageEventDao;
- import com.cloud.exception.*;
+ import com.cloud.exception.AccountLimitException;
+ import com.cloud.exception.ConcurrentOperationException;
+ import com.cloud.exception.ConnectionException;
+ import com.cloud.exception.InsufficientAddressCapacityException;
+ import com.cloud.exception.InsufficientCapacityException;
+ import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
+ import com.cloud.exception.InvalidParameterValueException;
+ import com.cloud.exception.PermissionDeniedException;
+ import com.cloud.exception.ResourceAllocationException;
+ import com.cloud.exception.ResourceUnavailableException;
+ import com.cloud.exception.UnsupportedServiceException;
  import com.cloud.host.Host;
 -import com.cloud.host.HostVO;
  import com.cloud.host.Status;
  import com.cloud.host.dao.HostDao;
- import com.cloud.server.ConfigurationServer;
  import com.cloud.hypervisor.Hypervisor.HypervisorType;
  import com.cloud.network.IpAddress.State;
- import com.cloud.network.Network.*;
+ import com.cloud.network.Network.Capability;
+ import com.cloud.network.Network.Event;
+ import com.cloud.network.Network.GuestType;
+ import com.cloud.network.Network.Provider;
+ import com.cloud.network.Network.Service;
  import com.cloud.network.Networks.AddressFormat;
  import com.cloud.network.Networks.BroadcastDomainType;
  import com.cloud.network.Networks.IsolationType;
@@@ -3532,9 -3603,17 +3602,17 @@@ public class NetworkManagerImpl extend
          List<IPAddressVO> ipsToRelease = _ipAddressDao.listByAssociatedNetwork(networkId, null);
          for (IPAddressVO ipToRelease : ipsToRelease) {
              if (ipToRelease.getVpcId() == null) {
+                 if (!ipToRelease.isPortable()) {
 -                    IPAddressVO ip = markIpAsUnavailable(ipToRelease.getId());
 -                    assert (ip != null) : "Unable to mark the ip address id=" + ipToRelease.getId() + " as unavailable.";
 -                } else {
 +                IPAddressVO ip = markIpAsUnavailable(ipToRelease.getId());
 +                assert (ip != null) : "Unable to mark the ip address id=" + ipToRelease.getId() + " as unavailable.";
 +            } else {
+                     // portable IP address are associated with owner, until explicitly requested to be disassociated
+                     // so as part of network clean up just break IP association with guest network
+                     ipToRelease.setAssociatedWithNetworkId(null);
+                     _ipAddressDao.update(ipToRelease.getId(), ipToRelease);
+                     s_logger.debug("Portable IP address " + ipToRelease + " is no longer associated with any network");
+                 }
+             } else {
                  _vpcMgr.unassignIPFromVpcNetwork(ipToRelease.getId(), network.getId());
              }
          }
@@@ -4042,37 -4120,39 +4119,39 @@@
      public NicProfile createNicForVm(Network network, NicProfile requested, ReservationContext context, VirtualMachineProfile<? extends VMInstanceVO> vmProfile, boolean prepare)
              throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException,
              ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
 -
 -        VirtualMachine vm = vmProfile.getVirtualMachine();
 -        DataCenter dc = _configMgr.getZone(network.getDataCenterId());
 -        Host host = _hostDao.findById(vm.getHostId()); 
 -        DeployDestination dest = new DeployDestination(dc, null, null, host);
 -
 -        NicProfile nic = getNicProfileForVm(network, requested, vm);
 -
 -        //1) allocate nic (if needed) Always allocate if it is a user vm
 -        if (nic == null || (vmProfile.getType() == VirtualMachine.Type.User)) {
 -            int deviceId = _nicDao.countNics(vm.getId());
 -            
 -            nic = allocateNic(requested, network, false, 
 -                    deviceId, vmProfile).first();
 -            
 -            if (nic == null) {
 -                throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network);
 +                
 +                VirtualMachine vm = vmProfile.getVirtualMachine();
 +                DataCenter dc = _configMgr.getZone(network.getDataCenterId());
 +                Host host = _hostDao.findById(vm.getHostId()); 
 +                DeployDestination dest = new DeployDestination(dc, null, null, host);
 +                
 +                NicProfile nic = getNicProfileForVm(network, requested, vm);
 +                
 +                //1) allocate nic (if needed) Always allocate if it is a user vm
 +                if (nic == null || (vmProfile.getType() == VirtualMachine.Type.User)) {
 +                    int deviceId = _nicDao.countNics(vm.getId());
 +                    
 +                    nic = allocateNic(requested, network, false, 
 +                            deviceId, vmProfile).first();
 +                    
 +                    if (nic == null) {
 +                        throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network);
 +                    }
 +                    
 +                    s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network); 
 +                }
 +                
 +                //2) prepare nic
 +                if (prepare) {
 +                    Pair<NetworkGuru, NetworkVO> implemented = implementNetwork(nic.getNetworkId(), dest, context);
 +                    nic = prepareNic(vmProfile, dest, context, nic.getId(), implemented.second());
 +                    s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network);
 +                }
 +                
 +                return nic;
              }
+ 
 -            s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network); 
 -        }
 -
 -        //2) prepare nic
 -        if (prepare) {
 -            Pair<NetworkGuru, NetworkVO> implemented = implementNetwork(nic.getNetworkId(), dest, context);
 -            nic = prepareNic(vmProfile, dest, context, nic.getId(), implemented.second());
 -            s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network);
 -        }
 -        
 -        return nic;
 -    }
 -
+ 
      @Override
      public List<NicProfile> getNicProfiles(VirtualMachine vm) {
          List<NicVO> nics = _nicDao.listByVmId(vm.getId());
@@@ -4153,7 -4235,8 +4234,8 @@@
          }
          return elements;
      }
 -
 +    
+ 
      @Override
      public StaticNatServiceProvider getStaticNatProviderForNetwork(Network network) {
          //only one provider per Static nat service is supoprted
@@@ -4187,7 -4271,8 +4270,8 @@@
          assert lbElement instanceof LoadBalancingServiceProvider; 
          return (LoadBalancingServiceProvider)lbElement;        
      }
 -
 +    
+ 
      @Override
      public boolean isNetworkInlineMode(Network network) {
          NetworkOfferingVO offering = _networkOfferingDao.findById(network.getNetworkOfferingId());
@@@ -4203,47 -4288,51 +4287,51 @@@
          return rules.size();
      }
  
+ 
 -    @Override
 +         @Override
      public boolean isSecondaryIpSetForNic(long nicId) {
          NicVO nic = _nicDao.findById(nicId);
          return nic.getSecondaryIp();
      }
  
-          @Override
-         public boolean removeVmSecondaryIpsOfNic(long nicId) {
+ 
+     private boolean removeVmSecondaryIpsOfNic(long nicId) {
 -       Transaction txn = Transaction.currentTxn();
 -       txn.start();
 -       List <NicSecondaryIpVO> ipList = _nicSecondaryIpDao.listByNicId(nicId);
 -       if (ipList != null) {
 -           for (NicSecondaryIpVO ip: ipList) {
 -               _nicSecondaryIpDao.remove(ip.getId());
 +           Transaction txn = Transaction.currentTxn();
 +           txn.start();
 +           List <NicSecondaryIpVO> ipList = _nicSecondaryIpDao.listByNicId(nicId);
 +           if (ipList != null) {
 +               for (NicSecondaryIpVO ip: ipList) {
 +                   _nicSecondaryIpDao.remove(ip.getId());
 +               }
 +               s_logger.debug("Revoving nic secondary ip entry ...");
             }
 -           s_logger.debug("Revoving nic secondary ip entry ...");
 -       }
 -       txn.commit();
 -       return true;
 -    }
 +           txn.commit();
 +           return true;
 +        }
  
+ 
 -    @Override
 -    public String allocatePublicIpForGuestNic(Long networkId, DataCenter dc, Pod pod,Account owner,
 -            String requestedIp) throws InsufficientAddressCapacityException {
 -        PublicIp ip = assignPublicIpAddress(dc.getId(), null, owner, VlanType.DirectAttached, networkId, requestedIp, false);
 -        if (ip == null) {
 -            s_logger.debug("There is no free public ip address");
 -            return null;
 +        @Override
 +        public String allocatePublicIpForGuestNic(Long networkId, DataCenter dc, Pod pod,Account owner,
 +                String requestedIp) throws InsufficientAddressCapacityException {
 +            PublicIp ip = assignPublicIpAddress(dc.getId(), null, owner, VlanType.DirectAttached, networkId, requestedIp, false);
 +            if (ip == null) {
 +                s_logger.debug("There is no free public ip address");
 +                return null;
 +            }
 +            Ip ipAddr = ip.getAddress();
 +            return ipAddr.addr();
          }
 -        Ip ipAddr = ip.getAddress();
 -        return ipAddr.addr();
 -    }
 -
 +        
+ 
 -    @Override
 +        @Override
-         public NicVO savePlaceholderNic(Network network, String ip4Address, Type vmType) {
+     public NicVO savePlaceholderNic(Network network, String ip4Address, String ip6Address, Type vmType) {
 -        NicVO nic = new NicVO(null, null, network.getId(), null); 
 -        nic.setIp4Address(ip4Address);
 +            NicVO nic = new NicVO(null, null, network.getId(), null); 
 +            nic.setIp4Address(ip4Address);
+         nic.setIp6Address(ip6Address);
 -        nic.setReservationStrategy(ReservationStrategy.PlaceHolder);
 -        nic.setState(Nic.State.Reserved);
 -        nic.setVmType(vmType);
 -        return _nicDao.persist(nic);
 -    }
 +            nic.setReservationStrategy(ReservationStrategy.PlaceHolder);
 +            nic.setState(Nic.State.Reserved);
 +            nic.setVmType(vmType);
 +            return _nicDao.persist(nic);
 +        }
          
   }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/18aeef3e/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
----------------------------------------------------------------------
diff --cc server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
index 4e3b903,db4786a..40db31f
--- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
+++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java
@@@ -1743,33 -1740,37 +1741,37 @@@ public class VirtualNetworkApplianceMan
              s_logger.debug("Adding nic for Virtual Router in Guest network " + guestNetwork);
              String defaultNetworkStartIp = null, defaultNetworkStartIpv6 = null;
              if (!setupPublicNetwork) {
-             	if (guestNetwork.getCidr() != null) {
 -            	Nic placeholder = _networkModel.getPlaceholderNicForRouter(guestNetwork, plan.getPodId());
 +            	    Nic placeholder = _networkModel.getPlaceholderNicForRouter(guestNetwork, plan.getPodId());
-             	    if (placeholder != null) {
-             	        s_logger.debug("Requesting ip address " + placeholder.getIp4Address() + " stored in placeholder nic for the network " + guestNetwork);
+             	if (guestNetwork.getCidr() != null) {
+             		if (placeholder != null && placeholder.getIp4Address() != null) {
+             			s_logger.debug("Requesting ipv4 address " + placeholder.getIp4Address() + " stored in placeholder nic for the network " + guestNetwork);
 -            			defaultNetworkStartIp = placeholder.getIp4Address(); 
 -            		} else {
 -            			String startIp = _networkModel.getStartIpAddress(guestNetwork.getId());
 -            			if (startIp != null && _ipAddressDao.findByIpAndSourceNetworkId(guestNetwork.getId(), startIp).getAllocatedTime() == null) {
 -            				defaultNetworkStartIp = startIp;
 -            			} else if (s_logger.isDebugEnabled()){
 +            	        defaultNetworkStartIp = placeholder.getIp4Address();
 +            	    } else {
 +            	        String startIp = _networkModel.getStartIpAddress(guestNetwork.getId());
 +                        if (startIp != null && _ipAddressDao.findByIpAndSourceNetworkId(guestNetwork.getId(), startIp).getAllocatedTime() == null) {
 +                            defaultNetworkStartIp = startIp;
 +                        } else if (s_logger.isDebugEnabled()){
-                             s_logger.debug("First ip " + startIp + " in network id=" + guestNetwork.getId() + 
+             				s_logger.debug("First ipv4 " + startIp + " in network id=" + guestNetwork.getId() + 
 -            						" is already allocated, can't use it for domain router; will get random ip address from the range");
 -            			}
 -            		}
 +                                    " is already allocated, can't use it for domain router; will get random ip address from the range");
 +                        }
 +            	    }
              	}
 -
 +            	
-             	//FIXME - get ipv6 stored in the placeholder
              	if (guestNetwork.getIp6Cidr() != null) {
+             		if (placeholder != null && placeholder.getIp6Address() != null) {
+             			s_logger.debug("Requesting ipv6 address " + placeholder.getIp6Address() + " stored in placeholder nic for the network " + guestNetwork);
+             			defaultNetworkStartIpv6 = placeholder.getIp6Address(); 
+             		} else {
 -            			String startIpv6 = _networkModel.getStartIpv6Address(guestNetwork.getId());
 -            			if (startIpv6 != null && _ipv6Dao.findByNetworkIdAndIp(guestNetwork.getId(), startIpv6) == null) {
 -            				defaultNetworkStartIpv6 = startIpv6;
 -            			} else if (s_logger.isDebugEnabled()){
 -            				s_logger.debug("First ipv6 " + startIpv6 + " in network id=" + guestNetwork.getId() + 
 -            						" is already allocated, can't use it for domain router; will get random ipv6 address from the range");
 -            			}
 +            		String startIpv6 = _networkModel.getStartIpv6Address(guestNetwork.getId());
 +            		if (startIpv6 != null && _ipv6Dao.findByNetworkIdAndIp(guestNetwork.getId(), startIpv6) == null) {
 +            			defaultNetworkStartIpv6 = startIpv6;
 +            		} else if (s_logger.isDebugEnabled()){
 +            			s_logger.debug("First ipv6 " + startIpv6 + " in network id=" + guestNetwork.getId() + 
 +            					" is already allocated, can't use it for domain router; will get random ipv6 address from the range");
              		}
              	}
              }
++            }
  
              NicProfile gatewayNic = new NicProfile(defaultNetworkStartIp, defaultNetworkStartIpv6);
              if (setupPublicNetwork) {
@@@ -2487,7 -2488,10 +2489,10 @@@
              for (NicIpAliasVO aliasVO : aliasVOs) {
                     activeIpAliasTOs.add(new IpAliasTO(aliasVO.getIp4Address(), aliasVO.getNetmask(), aliasVO.getAliasCount().toString()));
              }
+             if (revokedIpAliasTOs.size() != 0 || activeIpAliasTOs.size() != 0){
 -                createDeleteIpAliasCommand(router, revokedIpAliasTOs, activeIpAliasTOs, guestNetworkId, cmds);
 +            createDeleteIpAliasCommand(router, revokedIpAliasTOs, activeIpAliasTOs, guestNetworkId, cmds);
+                 configDnsMasq(router, _networkDao.findById(guestNetworkId), cmds);
+             }
  
          }
      }
@@@ -2796,7 -2800,13 +2801,13 @@@
                          for (VlanVO vlan : vlanList) {
                              vlanDbIdList.add(vlan.getId());
                          }
+                         if (dc.getNetworkType() == NetworkType.Basic) {
 -                            routerPublicIP = _networkMgr.assignPublicIpAddressFromVlans(router.getDataCenterId(), vm.getPodIdToDeployIn(), caller, Vlan.VlanType.DirectAttached, vlanDbIdList, nic.getNetworkId(), null, false);
 +                        routerPublicIP = _networkMgr.assignPublicIpAddressFromVlans(router.getDataCenterId(), vm.getPodIdToDeployIn(), caller, Vlan.VlanType.DirectAttached, vlanDbIdList, nic.getNetworkId(), null, false);
+                         }
+                         else {
+                             routerPublicIP = _networkMgr.assignPublicIpAddressFromVlans(router.getDataCenterId(), null, caller, Vlan.VlanType.DirectAttached, vlanDbIdList, nic.getNetworkId(), null, false);
+                         }
+ 
                          routerAliasIp = routerPublicIP.getAddress().addr();
                      }
                  }