Skip to content

Commit 86e83bc

Browse files
committed
Created new response objects for userVm/domainRouter/systemVm that going to be used by API after network refactorig is in place.
1 parent f2cbdb9 commit 86e83bc

5 files changed

Lines changed: 294 additions & 4 deletions

File tree

api/src/com/cloud/dc/DataCenter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public interface DataCenter extends Grouping {
1313
String getDns1();
1414
String getDns2();
1515
String getGuestNetworkCidr();
16+
String getName();
1617
Long getDomainId();
1718
}

api/src/com/cloud/vm/Nic.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public interface Nic extends Resource {
3131

3232
String getMacAddress();
3333

34+
String getNetmask();
35+
36+
String getGateway();
37+
3438
/**
3539
* @return network profile id that this
3640
*/

server/src/com/cloud/api/ApiResponseHelper.java

Lines changed: 276 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.cloud.configuration.ResourceCount.ResourceType;
5555
import com.cloud.configuration.ResourceLimit;
5656
import com.cloud.dc.ClusterVO;
57+
import com.cloud.dc.DataCenter;
5758
import com.cloud.dc.DataCenterVO;
5859
import com.cloud.dc.HostPodVO;
5960
import com.cloud.dc.Vlan.VlanType;
@@ -67,6 +68,8 @@
6768
import com.cloud.network.FirewallRuleVO;
6869
import com.cloud.network.IPAddressVO;
6970
import com.cloud.network.LoadBalancerVO;
71+
import com.cloud.network.Network.TrafficType;
72+
import com.cloud.network.NetworkConfiguration;
7073
import com.cloud.offering.NetworkOffering.GuestIpType;
7174
import com.cloud.offering.ServiceOffering;
7275
import com.cloud.server.Criteria;
@@ -91,6 +94,7 @@
9194
import com.cloud.vm.ConsoleProxyVO;
9295
import com.cloud.vm.DomainRouter;
9396
import com.cloud.vm.InstanceGroupVO;
97+
import com.cloud.vm.Nic;
9498
import com.cloud.vm.SecondaryStorageVmVO;
9599
import com.cloud.vm.State;
96100
import com.cloud.vm.SystemVm;
@@ -377,7 +381,6 @@ public static UserVmResponse createUserVmResponse (UserVm userVm) {
377381
userVmResponse.setState(userVm.getState().toString());
378382
}
379383

380-
381384
userVmResponse.setHaEnable(userVm.isHaEnabled());
382385

383386
if (userVm.getDisplayName() != null) {
@@ -985,4 +988,276 @@ public static FirewallRuleResponse createFirewallRuleResponse(FirewallRuleVO fwR
985988
return response;
986989
}
987990

991+
992+
993+
public static UserVmResponse createUserVm2Response (UserVm userVm) {
994+
UserVmResponse userVmResponse = new UserVmResponse();
995+
Account acct = ApiDBUtils.findAccountById(Long.valueOf(userVm.getAccountId()));
996+
//FIXME - this check should be done in searchForUserVm method in ManagementServerImpl;
997+
//otherwise the number of vms returned is not going to match pageSize request parameter
998+
if ((acct != null) && (acct.getRemoved() == null)) {
999+
userVmResponse.setAccountName(acct.getAccountName());
1000+
userVmResponse.setDomainId(acct.getDomainId());
1001+
userVmResponse.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName());
1002+
} else {
1003+
return null; // the account has been deleted, skip this VM in the response
1004+
}
1005+
1006+
userVmResponse.setId(userVm.getId());
1007+
AsyncJobVO asyncJob = ApiDBUtils.findInstancePendingAsyncJob("vm_instance", userVm.getId());
1008+
if (asyncJob != null) {
1009+
userVmResponse.setJobId(asyncJob.getId());
1010+
userVmResponse.setJobStatus(asyncJob.getStatus());
1011+
}
1012+
1013+
userVmResponse.setName(userVm.getHostName());
1014+
userVmResponse.setCreated(userVm.getCreated());
1015+
1016+
if (userVm.getState() != null) {
1017+
userVmResponse.setState(userVm.getState().toString());
1018+
}
1019+
1020+
userVmResponse.setHaEnable(userVm.isHaEnabled());
1021+
1022+
if (userVm.getDisplayName() != null) {
1023+
userVmResponse.setDisplayName(userVm.getDisplayName());
1024+
} else {
1025+
userVmResponse.setDisplayName(userVm.getHostName());
1026+
}
1027+
1028+
InstanceGroupVO group = ApiDBUtils.findInstanceGroupForVM(userVm.getId());
1029+
if (group != null) {
1030+
userVmResponse.setGroup(group.getName());
1031+
userVmResponse.setGroupId(group.getId());
1032+
}
1033+
1034+
// Data Center Info
1035+
userVmResponse.setZoneId(userVm.getDataCenterId());
1036+
userVmResponse.setZoneName(ApiDBUtils.findZoneById(userVm.getDataCenterId()).getName());
1037+
1038+
Account account = UserContext.current().getAccount();
1039+
//if user is an admin, display host id
1040+
if (((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) && (userVm.getHostId() != null)) {
1041+
userVmResponse.setHostId(userVm.getHostId());
1042+
userVmResponse.setHostName(ApiDBUtils.findHostById(userVm.getHostId()).getName());
1043+
}
1044+
1045+
// Template Info
1046+
VMTemplateVO template = ApiDBUtils.findTemplateById(userVm.getTemplateId());
1047+
if (template != null) {
1048+
userVmResponse.setTemplateId(userVm.getTemplateId());
1049+
userVmResponse.setTemplateName(template.getName());
1050+
userVmResponse.setTemplateDisplayText(template.getDisplayText());
1051+
userVmResponse.setPasswordEnabled(template.getEnablePassword());
1052+
} else {
1053+
userVmResponse.setTemplateId(-1L);
1054+
userVmResponse.setTemplateName("ISO Boot");
1055+
userVmResponse.setTemplateDisplayText("ISO Boot");
1056+
userVmResponse.setPasswordEnabled(false);
1057+
}
1058+
1059+
if (userVm.getPassword() != null) {
1060+
userVmResponse.setPassword(userVm.getPassword());
1061+
}
1062+
1063+
// ISO Info
1064+
if (userVm.getIsoId() != null) {
1065+
VMTemplateVO iso = ApiDBUtils.findTemplateById(userVm.getIsoId().longValue());
1066+
if (iso != null) {
1067+
userVmResponse.setIsoId(userVm.getIsoId());
1068+
userVmResponse.setIsoName(iso.getName());
1069+
}
1070+
}
1071+
1072+
// Service Offering Info
1073+
ServiceOffering offering = ApiDBUtils.findServiceOfferingById(userVm.getServiceOfferingId());
1074+
userVmResponse.setServiceOfferingId(userVm.getServiceOfferingId());
1075+
userVmResponse.setServiceOfferingName(offering.getName());
1076+
userVmResponse.setCpuNumber(offering.getCpu());
1077+
userVmResponse.setCpuSpeed(offering.getSpeed());
1078+
userVmResponse.setMemory(offering.getRamSize());
1079+
1080+
VolumeVO rootVolume = ApiDBUtils.findRootVolume(userVm.getId());
1081+
if (rootVolume != null) {
1082+
userVmResponse.setRootDeviceId(rootVolume.getDeviceId());
1083+
String rootDeviceType = "Not created";
1084+
if (rootVolume.getPoolId() != null){
1085+
StoragePoolVO storagePool = ApiDBUtils.findStoragePoolById(rootVolume.getPoolId());
1086+
rootDeviceType = storagePool.getPoolType().toString();
1087+
}
1088+
userVmResponse.setRootDeviceType(rootDeviceType);
1089+
}
1090+
1091+
//stats calculation
1092+
DecimalFormat decimalFormat = new DecimalFormat("#.##");
1093+
String cpuUsed = null;
1094+
VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
1095+
if (vmStats != null) {
1096+
float cpuUtil = (float) vmStats.getCPUUtilization();
1097+
cpuUsed = decimalFormat.format(cpuUtil) + "%";
1098+
userVmResponse.setCpuUsed(cpuUsed);
1099+
1100+
long networkKbRead = (long)vmStats.getNetworkReadKBs();
1101+
userVmResponse.setNetworkKbsRead(networkKbRead);
1102+
1103+
long networkKbWrite = (long)vmStats.getNetworkWriteKBs();
1104+
userVmResponse.setNetworkKbsWrite(networkKbWrite);
1105+
}
1106+
1107+
userVmResponse.setGuestOsId(userVm.getGuestOSId());
1108+
//network groups
1109+
userVmResponse.setNetworkGroupList(ApiDBUtils.getNetworkGroupsNamesForVm(userVm.getId()));
1110+
1111+
List<? extends Nic> nics = BaseCmd._networkMgr.getNics(userVm);
1112+
for (Nic singleNic : nics) {
1113+
long configId = singleNic.getNetworkConfigurationId();
1114+
NetworkConfiguration networkConf = BaseCmd._networkMgr.getNetworkConfiguration(configId);
1115+
if (networkConf.getTrafficType() == TrafficType.Guest) {
1116+
userVmResponse.setIpAddress(singleNic.getIp4Address());
1117+
}
1118+
}
1119+
1120+
userVmResponse.setObjectName("virtualmachine");
1121+
return userVmResponse;
1122+
}
1123+
1124+
1125+
public static DomainRouterResponse createDomainRouter2Response (DomainRouter router) {
1126+
DomainRouterResponse routerResponse = new DomainRouterResponse();
1127+
routerResponse.setId(router.getId());
1128+
routerResponse.setZoneId(router.getDataCenterId());
1129+
routerResponse.setName(router.getHostName());
1130+
routerResponse.setPodId(router.getPodId());
1131+
routerResponse.setTemplateId(router.getTemplateId());
1132+
routerResponse.setCreated(router.getCreated());
1133+
routerResponse.setState(router.getState());
1134+
routerResponse.setNetworkDomain(router.getDomain());
1135+
1136+
if (router.getHostId() != null) {
1137+
routerResponse.setHostId(router.getHostId());
1138+
routerResponse.setHostName(ApiDBUtils.findHostById(router.getHostId()).getName());
1139+
}
1140+
1141+
Account accountTemp = ApiDBUtils.findAccountById(router.getAccountId());
1142+
if (accountTemp != null) {
1143+
routerResponse.setAccountName(accountTemp.getAccountName());
1144+
routerResponse.setDomainId(accountTemp.getDomainId());
1145+
routerResponse.setDomainName(ApiDBUtils.findDomainById(accountTemp.getDomainId()).getName());
1146+
}
1147+
1148+
AsyncJobVO asyncJob = ApiDBUtils.findInstancePendingAsyncJob("domain_router", router.getId());
1149+
if (asyncJob != null) {
1150+
routerResponse.setJobId(asyncJob.getId());
1151+
routerResponse.setJobStatus(asyncJob.getStatus());
1152+
}
1153+
1154+
List<? extends Nic> nics = BaseCmd._networkMgr.getNics(router);
1155+
for (Nic singleNic : nics) {
1156+
long configId = singleNic.getNetworkConfigurationId();
1157+
NetworkConfiguration networkConf = BaseCmd._networkMgr.getNetworkConfiguration(configId);
1158+
1159+
if (networkConf.getTrafficType() == TrafficType.Guest) {
1160+
routerResponse.setGuestIpAddress(singleNic.getIp4Address());
1161+
routerResponse.setGuestMacAddress(singleNic.getMacAddress());
1162+
routerResponse.setGuestNetmask(singleNic.getNetmask());
1163+
}
1164+
1165+
if (networkConf.getTrafficType() == TrafficType.Control) {
1166+
routerResponse.setPrivateIp(singleNic.getIp4Address());
1167+
routerResponse.setPrivateMacAddress(singleNic.getMacAddress());
1168+
routerResponse.setPrivateNetmask(singleNic.getNetmask());
1169+
}
1170+
1171+
if (networkConf.getTrafficType() == TrafficType.Public) {
1172+
routerResponse.setPublicIp(singleNic.getIp4Address());
1173+
routerResponse.setPublicMacAddress(singleNic.getMacAddress());
1174+
routerResponse.setPublicNetmask(singleNic.getNetmask());
1175+
routerResponse.setGateway(singleNic.getGateway());
1176+
}
1177+
1178+
DataCenter zone = ApiDBUtils.findZoneById(router.getDataCenterId());
1179+
if (zone != null) {
1180+
routerResponse.setZoneName(zone.getName());
1181+
routerResponse.setDns1(zone.getDns1());
1182+
routerResponse.setDns2(zone.getDns2());
1183+
}
1184+
}
1185+
1186+
routerResponse.setObjectName("domainrouter");
1187+
return routerResponse;
1188+
}
1189+
1190+
1191+
public static SystemVmResponse createSystemVm2Response (VMInstanceVO systemVM) {
1192+
SystemVmResponse vmResponse = new SystemVmResponse();
1193+
if (systemVM instanceof SystemVm) {
1194+
SystemVm vm = (SystemVm)systemVM;
1195+
1196+
vmResponse.setId(vm.getId());
1197+
vmResponse.setSystemVmType(vm.getType().toString().toLowerCase());
1198+
vmResponse.setZoneId(vm.getDataCenterId());
1199+
1200+
vmResponse.setNetworkDomain(vm.getDomain());
1201+
vmResponse.setName(vm.getHostName());
1202+
vmResponse.setPodId(vm.getPodId());
1203+
vmResponse.setTemplateId(vm.getTemplateId());
1204+
vmResponse.setCreated(vm.getCreated());
1205+
1206+
if (vm.getHostId() != null) {
1207+
vmResponse.setHostId(vm.getHostId());
1208+
vmResponse.setHostName(ApiDBUtils.findHostById(vm.getHostId()).getName());
1209+
}
1210+
1211+
if (vm.getState() != null) {
1212+
vmResponse.setState(vm.getState().toString());
1213+
}
1214+
1215+
String instanceType = "console_proxy";
1216+
if (systemVM instanceof SecondaryStorageVmVO) {
1217+
instanceType = "sec_storage_vm"; // FIXME: this should be a constant so that the async jobs get updated with the correct instance type, they are using
1218+
// different instance types at the moment
1219+
}
1220+
1221+
AsyncJobVO asyncJob = ApiDBUtils.findInstancePendingAsyncJob(instanceType, vm.getId());
1222+
if (asyncJob != null) {
1223+
vmResponse.setJobId(asyncJob.getId());
1224+
vmResponse.setJobStatus(asyncJob.getStatus());
1225+
}
1226+
1227+
// for console proxies, add the active sessions
1228+
if (systemVM instanceof ConsoleProxyVO) {
1229+
ConsoleProxyVO proxy = (ConsoleProxyVO)systemVM;
1230+
vmResponse.setActiveViewerSessions(proxy.getActiveSession());
1231+
}
1232+
1233+
DataCenter zone = ApiDBUtils.findZoneById(vm.getDataCenterId());
1234+
if (zone != null) {
1235+
vmResponse.setZoneName(zone.getName());
1236+
vmResponse.setDns1(zone.getDns1());
1237+
vmResponse.setDns2(zone.getDns2());
1238+
}
1239+
1240+
List<? extends Nic> nics = BaseCmd._networkMgr.getNics(systemVM);
1241+
for (Nic singleNic : nics) {
1242+
long configId = singleNic.getNetworkConfigurationId();
1243+
NetworkConfiguration networkConf = BaseCmd._networkMgr.getNetworkConfiguration(configId);
1244+
1245+
if (networkConf.getTrafficType() == TrafficType.Management) {
1246+
vmResponse.setPrivateIp(singleNic.getIp4Address());
1247+
vmResponse.setPrivateMacAddress(singleNic.getMacAddress());
1248+
vmResponse.setPrivateNetmask(singleNic.getNetmask());
1249+
}
1250+
1251+
if (networkConf.getTrafficType() == TrafficType.Public) {
1252+
vmResponse.setPublicIp(singleNic.getIp4Address());
1253+
vmResponse.setPublicMacAddress(singleNic.getMacAddress());
1254+
vmResponse.setPublicNetmask(singleNic.getNetmask());
1255+
vmResponse.setGateway(singleNic.getGateway());
1256+
}
1257+
}
1258+
}
1259+
1260+
vmResponse.setObjectName("systemvm");
1261+
return vmResponse;
1262+
}
9881263
}

server/src/com/cloud/network/NetworkManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@
6161
import com.cloud.utils.Pair;
6262
import com.cloud.vm.DomainRouter;
6363
import com.cloud.vm.DomainRouterVO;
64+
import com.cloud.vm.Nic;
6465
import com.cloud.vm.NicProfile;
65-
import com.cloud.vm.NicVO;
6666
import com.cloud.vm.ReservationContext;
6767
import com.cloud.vm.UserVmVO;
6868
import com.cloud.vm.VMInstanceVO;
69+
import com.cloud.vm.VirtualMachine;
6970
import com.cloud.vm.VirtualMachineProfile;
7071

7172
/**
@@ -316,7 +317,7 @@ public interface NetworkManager {
316317
void release(VirtualMachineProfile<? extends VMInstanceVO> vmProfile);
317318

318319
DomainRouter upgradeRouter(UpgradeRouterCmd cmd);
319-
List<NicVO> getNics(VMInstanceVO vm);
320+
List<? extends Nic> getNics (VirtualMachine vm);
320321

321322
List<AccountVO> getAccountsUsingNetworkConfiguration(long configurationId);
322323
AccountVO getNetworkConfigurationOwner(long configurationId);
@@ -358,4 +359,6 @@ public interface NetworkManager {
358359
boolean removeVpnUser(RemoveVpnUserCmd cmd) throws ConcurrentOperationException;
359360

360361
String getNextAvailableMacAddressInNetwork(long networkConfigurationId);
362+
363+
NetworkConfiguration getNetworkConfiguration(long id);
361364
}

server/src/com/cloud/network/NetworkManagerImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,14 @@
156156
import com.cloud.utils.net.NetUtils;
157157
import com.cloud.vm.DomainRouter;
158158
import com.cloud.vm.DomainRouterVO;
159+
import com.cloud.vm.Nic;
159160
import com.cloud.vm.NicProfile;
160161
import com.cloud.vm.NicVO;
161162
import com.cloud.vm.ReservationContext;
162163
import com.cloud.vm.State;
163164
import com.cloud.vm.UserVmVO;
164165
import com.cloud.vm.VMInstanceVO;
166+
import com.cloud.vm.VirtualMachine;
165167
import com.cloud.vm.VirtualMachineProfile;
166168
import com.cloud.vm.dao.NicDao;
167169
import com.cloud.vm.dao.UserVmDao;
@@ -2086,7 +2088,7 @@ public void release(VirtualMachineProfile<? extends VMInstanceVO> vmProfile) {
20862088
}
20872089

20882090
@Override
2089-
public List<NicVO> getNics(VMInstanceVO vm) {
2091+
public List<? extends Nic> getNics(VirtualMachine vm) {
20902092
return _nicDao.listBy(vm.getId());
20912093
}
20922094

@@ -2924,4 +2926,9 @@ public List<NetworkOfferingVO> listNetworkOfferings() {
29242926
public String getNextAvailableMacAddressInNetwork(long networkConfigurationId) {
29252927
return _networkConfigDao.getNextAvailableMacAddress(networkConfigurationId);
29262928
}
2929+
2930+
@Override
2931+
public NetworkConfiguration getNetworkConfiguration(long id) {
2932+
return _networkConfigDao.findById(id);
2933+
}
29272934
}

0 commit comments

Comments
 (0)