bug 11094: delinking api layer objects from dao layer

This commit is contained in:
Abhinandan Prateek 2011-08-19 15:21:03 +05:30 committed by root
parent 843e41752f
commit 8d4c7208a4
5 changed files with 754 additions and 15 deletions

View File

@ -119,6 +119,7 @@ import com.cloud.vm.VmStats;
import com.cloud.vm.dao.ConsoleProxyDao;
import com.cloud.vm.dao.DomainRouterDao;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.UserVmData;
public class ApiDBUtils {
private static ManagementServer _ms;
@ -596,7 +597,7 @@ public class ApiDBUtils {
return _firewallCidrsDao.getSourceCidrs(id);
}
public static UserVmResponse listVmDetails(UserVm userVm, boolean show_host){
public static UserVmData listVmDetails(UserVm userVm, boolean show_host){
return _userVmDao.listVmDetails(userVm, show_host);
}

View File

@ -58,6 +58,7 @@ import com.cloud.api.response.ListResponse;
import com.cloud.api.response.LoadBalancerResponse;
import com.cloud.api.response.NetworkOfferingResponse;
import com.cloud.api.response.NetworkResponse;
import com.cloud.api.response.NicResponse;
import com.cloud.api.response.PodResponse;
import com.cloud.api.response.ProjectResponse;
import com.cloud.api.response.RemoteAccessVpnResponse;
@ -162,6 +163,9 @@ import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachine.Type;
import com.cloud.vm.dao.UserVmData;
import com.cloud.vm.dao.UserVmData.NicData;
import com.cloud.vm.dao.UserVmData.SecurityGroupData;
import com.cloud.vm.VmStats;
public class ApiResponseHelper implements ResponseGenerator {
@ -1057,7 +1061,8 @@ public class ApiResponseHelper implements ResponseGenerator {
DecimalFormat decimalFormat = new DecimalFormat("#.##");
for (UserVm userVm : userVms) {
UserVmResponse userVmResponse = ApiDBUtils.listVmDetails(userVm, (((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN)) && (userVm.getHostId() != null)));
UserVmData userVmData = ApiDBUtils.listVmDetails(userVm, (((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN)) && (userVm.getHostId() != null)));
UserVmResponse userVmResponse = newUserVmResponse(userVmData);
// stats calculation
String cpuUsed = null;
VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
@ -2253,4 +2258,83 @@ public class ApiResponseHelper implements ResponseGenerator {
response.setObjectName("firewallrule");
return response;
}
public UserVmResponse newUserVmResponse(UserVmData userVmData){
UserVmResponse userVmResponse = new UserVmResponse();
userVmResponse.setHypervisor(userVmData.getHypervisor());
userVmResponse.setId(userVmData.getId());
userVmResponse.setName(userVmData.getName());
userVmResponse.setDisplayName(userVmData.getDisplayName());
userVmResponse.setIpAddress(userVmData.getIpAddress());
userVmResponse.setAccountName(userVmData.getAccountName());
userVmResponse.setDomainId(userVmData.getDomainId());
userVmResponse.setDomainName(userVmData.getDomainName());
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());
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()){
SecurityGroupResponse sgr = new SecurityGroupResponse();
sgr.setId(sgd.getId());
sgr.setName(sgd.getName());
sgr.setDescription(sgd.getDescription());
sgr.setAccountName(sgd.getAccountName());
sgr.setDomainId(sgd.getDomainId());
sgr.setDomainName(sgd.getDomainName());
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));
return userVmResponse;
}
}

View File

@ -71,5 +71,5 @@ public interface UserVmDao extends GenericDao<UserVmVO, Long> {
List<Long> listPodIdsHavingVmsforAccount(long zoneId, long accountId);
public Long countAllocatedVMsForAccount(long accountId);
UserVmResponse listVmDetails(UserVm userVm, boolean show_host);
UserVmData listVmDetails(UserVm userVm, boolean show_host);
}

View File

@ -31,9 +31,6 @@ import javax.ejb.Local;
import org.apache.log4j.Logger;
import com.cloud.api.response.NicResponse;
import com.cloud.api.response.SecurityGroupResponse;
import com.cloud.api.response.UserVmResponse;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.utils.component.ComponentLocator;
@ -50,6 +47,8 @@ import com.cloud.vm.NicVO;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.dao.UserVmData.NicData;
import com.cloud.vm.dao.UserVmData.SecurityGroupData;
@Local(value={UserVmDao.class})
public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements UserVmDao {
@ -343,7 +342,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
}
@Override
public UserVmResponse listVmDetails(UserVm userVm, boolean show_host){
public UserVmData listVmDetails(UserVm userVm, boolean show_host){
Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
@ -355,12 +354,12 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
ResultSet rs = pstmt.executeQuery();
boolean is_data_center_security_group_enabled=false;
Set<SecurityGroupResponse> securityGroupResponse = new HashSet<SecurityGroupResponse>();
Set<NicResponse> nicResponses = new HashSet<NicResponse>();
UserVmResponse userVmResponse = null;
Set<SecurityGroupData> securityGroupResponse = new HashSet<SecurityGroupData>();
Set<NicData> nicResponses = new HashSet<NicData>();
UserVmData userVmResponse = null;
while (rs.next()) {
if (userVmResponse==null){
userVmResponse=new UserVmResponse();
userVmResponse=new UserVmData();
userVmResponse.setId(userVm.getId());
userVmResponse.setName(userVm.getInstanceName());
userVmResponse.setCreated(userVm.getCreated());
@ -451,7 +450,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
//security_group.id, security_group.name, security_group.description, , data_center.is_security_group_enabled
if (is_data_center_security_group_enabled){
SecurityGroupResponse resp = new SecurityGroupResponse();
SecurityGroupData resp = userVmResponse.newSecurityGroupData();
resp.setId(rs.getLong("security_group.id"));
resp.setName(rs.getString("security_group.name"));
resp.setDescription(rs.getString("security_group.description"));
@ -464,7 +463,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
//"networks.traffic_type, networks.guest_type, networks.is_default from vm_instance, "
long nic_id = rs.getLong("nics.id");
if (nic_id > 0){
NicResponse nicResponse = new NicResponse();
NicData nicResponse = userVmResponse.newNicData();
nicResponse.setId(nic_id);
nicResponse.setIpaddress(rs.getString("nics.ip4_address"));
nicResponse.setGateway(rs.getString("nics.gateway"));
@ -487,8 +486,8 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
}
}
userVmResponse.setSecurityGroupList(new ArrayList<SecurityGroupResponse>(securityGroupResponse));
userVmResponse.setNics(new ArrayList<NicResponse>(nicResponses));
userVmResponse.setSecurityGroupList(new ArrayList<SecurityGroupData>(securityGroupResponse));
userVmResponse.setNics(new ArrayList<NicData>(nicResponses));
rs.close();
pstmt.close();
return userVmResponse;

View File

@ -0,0 +1,655 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.vm.dao;
import java.util.Date;
import java.util.List;
import com.cloud.api.ApiConstants;
import com.cloud.api.response.IngressRuleResponse;
import com.cloud.api.response.NicResponse;
import com.cloud.api.response.SecurityGroupResponse;
import com.cloud.serializer.Param;
import com.google.gson.annotations.SerializedName;
public class UserVmData {
private Long id;
private String name;
private String displayName;
private String ipAddress;
private String accountName;
private Long domainId;
private String domainName;
private Date created;
private String state;
private Boolean haEnable;
private Long groupId;
private String group;
private Long zoneId;
private String zoneName;
private Long hostId;
private String hostName;
private Long templateId;
private String templateName;
private String templateDisplayText;
private Boolean passwordEnabled;
private Long isoId;
private String isoName;
private String isoDisplayText;
private Long serviceOfferingId;
private String serviceOfferingName;
private Boolean forVirtualNetwork;
private Integer cpuNumber;
private Integer cpuSpeed;
private Integer memory;
private String cpuUsed;
private Long networkKbsRead;
private Long networkKbsWrite;
private Long guestOsId;
private Long rootDeviceId;
private String rootDeviceType;
private List<SecurityGroupData> securityGroupList;
private String password;
private Long jobId;
private Integer jobStatus;
private List<NicData> nics;
private String hypervisor;
public NicData newNicData(){
return new NicData();
}
public SecurityGroupData newSecurityGroupData(){
return new SecurityGroupData();
}
public String getHypervisor() {
return hypervisor;
}
public void setHypervisor(String hypervisor) {
this.hypervisor = hypervisor;
}
public Long getObjectId() {
return getId();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Long getDomainId() {
return domainId;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Boolean getHaEnable() {
return haEnable;
}
public void setHaEnable(Boolean haEnable) {
this.haEnable = haEnable;
}
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public Long getZoneId() {
return zoneId;
}
public void setZoneId(Long zoneId) {
this.zoneId = zoneId;
}
public String getZoneName() {
return zoneName;
}
public void setZoneName(String zoneName) {
this.zoneName = zoneName;
}
public Long getHostId() {
return hostId;
}
public void setHostId(Long hostId) {
this.hostId = hostId;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public Long getTemplateId() {
return templateId;
}
public void setTemplateId(Long templateId) {
this.templateId = templateId;
}
public String getTemplateName() {
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
public String getTemplateDisplayText() {
return templateDisplayText;
}
public void setTemplateDisplayText(String templateDisplayText) {
this.templateDisplayText = templateDisplayText;
}
public Boolean getPasswordEnabled() {
return passwordEnabled;
}
public void setPasswordEnabled(Boolean passwordEnabled) {
this.passwordEnabled = passwordEnabled;
}
public Long getIsoId() {
return isoId;
}
public void setIsoId(Long isoId) {
this.isoId = isoId;
}
public String getIsoName() {
return isoName;
}
public void setIsoName(String isoName) {
this.isoName = isoName;
}
public String getIsoDisplayText() {
return isoDisplayText;
}
public void setIsoDisplayText(String isoDisplayText) {
this.isoDisplayText = isoDisplayText;
}
public Long getServiceOfferingId() {
return serviceOfferingId;
}
public void setServiceOfferingId(Long serviceOfferingId) {
this.serviceOfferingId = serviceOfferingId;
}
public String getServiceOfferingName() {
return serviceOfferingName;
}
public void setServiceOfferingName(String serviceOfferingName) {
this.serviceOfferingName = serviceOfferingName;
}
public Integer getCpuNumber() {
return cpuNumber;
}
public void setCpuNumber(Integer cpuNumber) {
this.cpuNumber = cpuNumber;
}
public Integer getCpuSpeed() {
return cpuSpeed;
}
public void setCpuSpeed(Integer cpuSpeed) {
this.cpuSpeed = cpuSpeed;
}
public Integer getMemory() {
return memory;
}
public void setMemory(Integer memory) {
this.memory = memory;
}
public String getCpuUsed() {
return cpuUsed;
}
public void setCpuUsed(String cpuUsed) {
this.cpuUsed = cpuUsed;
}
public Long getNetworkKbsRead() {
return networkKbsRead;
}
public void setNetworkKbsRead(Long networkKbsRead) {
this.networkKbsRead = networkKbsRead;
}
public Long getNetworkKbsWrite() {
return networkKbsWrite;
}
public void setNetworkKbsWrite(Long networkKbsWrite) {
this.networkKbsWrite = networkKbsWrite;
}
public Long getGuestOsId() {
return guestOsId;
}
public void setGuestOsId(Long guestOsId) {
this.guestOsId = guestOsId;
}
public Long getRootDeviceId() {
return rootDeviceId;
}
public void setRootDeviceId(Long rootDeviceId) {
this.rootDeviceId = rootDeviceId;
}
public String getRootDeviceType() {
return rootDeviceType;
}
public void setRootDeviceType(String rootDeviceType) {
this.rootDeviceType = rootDeviceType;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Long getJobId() {
return jobId;
}
public void setJobId(Long jobId) {
this.jobId = jobId;
}
public Integer getJobStatus() {
return jobStatus;
}
public void setJobStatus(Integer jobStatus) {
this.jobStatus = jobStatus;
}
public Boolean getForVirtualNetwork() {
return forVirtualNetwork;
}
public void setForVirtualNetwork(Boolean forVirtualNetwork) {
this.forVirtualNetwork = forVirtualNetwork;
}
public List<NicData> getNics() {
return nics;
}
public void setNics(List<NicData> nics) {
this.nics = nics;
}
public List<SecurityGroupData> getSecurityGroupList() {
return securityGroupList;
}
public void setSecurityGroupList(List<SecurityGroupData> securityGroups) {
this.securityGroupList = securityGroups;
}
public class NicData {
private String objectName;
private Long id;
private Long networkid;
private String netmask;
private String gateway;
private String ipaddress;
private String isolationUri;
private String broadcastUri;
private String trafficType;
private String type;
private Boolean isDefault;
private String macAddress;
public String getObjectName() {
return objectName;
}
public void setObjectName(String objectName) {
this.objectName = objectName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getNetworkid() {
return networkid;
}
public void setNetworkid(Long networkid) {
this.networkid = networkid;
}
public String getNetmask() {
return netmask;
}
public void setNetmask(String netmask) {
this.netmask = netmask;
}
public String getGateway() {
return gateway;
}
public void setGateway(String gateway) {
this.gateway = gateway;
}
public String getIpaddress() {
return ipaddress;
}
public void setIpaddress(String ipaddress) {
this.ipaddress = ipaddress;
}
public String getIsolationUri() {
return isolationUri;
}
public void setIsolationUri(String isolationUri) {
this.isolationUri = isolationUri;
}
public String getBroadcastUri() {
return broadcastUri;
}
public void setBroadcastUri(String broadcastUri) {
this.broadcastUri = broadcastUri;
}
public String getTrafficType() {
return trafficType;
}
public void setTrafficType(String trafficType) {
this.trafficType = trafficType;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Boolean getIsDefault() {
return isDefault;
}
public void setIsDefault(Boolean isDefault) {
this.isDefault = isDefault;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
NicData other = (NicData) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
public class SecurityGroupData {
private String objectName;
private Long id;
private String name;
private String description;
private String accountName;
private Long domainId;
private String domainName;
private Long jobId;
private Integer jobStatus;
private List<IngressRuleResponse> ingressRules;
public String getObjectName() {
return objectName;
}
public void setObjectName(String objectName) {
this.objectName = objectName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Long getDomainId() {
return domainId;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
public List<IngressRuleResponse> getIngressRules() {
return ingressRules;
}
public void setIngressRules(List<IngressRuleResponse> ingressRules) {
this.ingressRules = ingressRules;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SecurityGroupData other = (SecurityGroupData) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
}