if (nodeGroupEntity.getNodes() == null) {
nodeGroupEntity.setNodes(new LinkedList<NodeEntity>());
}
boolean insert = false;
NodeEntity nodeEntity =
getClusterEntityMgr()
.findByName(nodeGroupEntity, vNode.getVmName());
// if node already exists, replace the value with new one from vNode.
if (nodeEntity == null) {
nodeEntity = new NodeEntity();
nodeGroupEntity.getNodes().add(nodeEntity);
insert = true;
}
nodeEntity.setVmName(vNode.getVmName());
setNodeStatus(nodeEntity, vNode);
if (vNode.getVmMobId() == null && nodeEntity.getMoId() != null) {
vNode.setVmMobId(nodeEntity.getMoId());
}
// set node version
nodeEntity.setVersion(cluster.getVersion());
//set vc resource pool entity
nodeEntity.setVcRp(rpDao.findByClusterAndRp(vNode.getTargetVcCluster(),
vNode.getTargetRp()));
// set ipconfigs field even IPs are not yet retrieved, otherwise if
// Tomcat crashes, we will lost the ipconfigs template
for (NicSpec nicSpec : vNode.getNics().values()) {
NetworkEntity networkEntity =
networkMgr.getNetworkEntityByName(nicSpec.getNetworkName());
NicEntity nicEntity = nodeEntity.findNic(networkEntity);
if (nicEntity == null) {
nicEntity = new NicEntity();
nodeEntity.getNics().add(nicEntity);
}
nicEntity.setIpv4Address(nicSpec.getIpv4Address());
nicEntity.setIpv6Address(nicSpec.getIpv6Address());
nicEntity.setMacAddress(nicSpec.getMacAddress());
nicEntity.setNetTrafficDefs(nicSpec.getNetTrafficDefinitionSet());
nicEntity.setNetworkEntity(networkEntity);
nicEntity.setNodeEntity(nodeEntity);
if (vNode.getVmMobId() != null) {
VcVmUtil.populateNicInfo(nicEntity, vNode.getVmMobId(),
networkEntity.getPortGroup());
}
}
if (vNode.isFinished()) {
nodeEntity.setActionFailed(!vNode.isSuccess());
nodeEntity.setErrMessage(vNode.getErrMessage());
}
if (vNode.getVmMobId() != null) {
nodeEntity.setMoId(vNode.getVmMobId());
nodeEntity.setRack(vNode.getTargetRack());
nodeEntity.setHostName(vNode.getTargetHost());
nodeEntity.setGuestHostName(vNode.getGuestHostName());
nodeEntity.setCpuNum(vNode.getCpu());
nodeEntity.setMemorySize((long) vNode.getMem());
// set disk entities, include system/swap/data disk
Set<DiskEntity> diskEntities = nodeEntity.getDisks();
// system disk
DiskEntity systemDisk = nodeEntity.findSystemDisk();
if (systemDisk == null)
systemDisk = new DiskEntity(nodeEntity.getVmName() + ".vmdk");
systemDisk.setDiskType(DiskType.SYSTEM_DISK.getType());
systemDisk.setExternalAddress(DiskEntity
.getSystemDiskExternalAddress());
systemDisk.setNodeEntity(nodeEntity);
systemDisk.setDatastoreName(vNode.getTargetDs());
VcVmUtil.populateDiskInfo(systemDisk, vNode.getVmMobId());
diskEntities.add(systemDisk);
// swap and data disk
for (Disk disk : vNode.getVmSchema().diskSchema.getDisks()) {
DiskEntity newDisk = nodeEntity.findDisk(disk.name);
if (newDisk == null) {
newDisk = new DiskEntity(disk.name);
diskEntities.add(newDisk);
}
newDisk.setSizeInMB(disk.initialSizeMB);
newDisk.setAllocType(disk.allocationType.toString());
newDisk.setDatastoreName(disk.datastore);
newDisk.setDiskType(disk.type);
newDisk.setExternalAddress(disk.externalAddress);
newDisk.setNodeEntity(nodeEntity);
// get vm object and find the vmdk path
VcVmUtil.populateDiskInfo(newDisk, vNode.getVmMobId());
}
}
nodeEntity.setNodeGroup(nodeGroupEntity);
if (insert) {
getClusterEntityMgr().insert(nodeEntity);
} else {
getClusterEntityMgr().update(nodeEntity);