super();
}
@Override
public final VirtualMachineTO implement(VirtualMachineProfile vm) {
VirtualMachineTO to = toVirtualMachineTO(vm);
List<NicProfile> nicProfiles = vm.getNics();
if(vm.getVirtualMachine().getType() == VirtualMachine.Type.DomainRouter) {
NicProfile publicNicProfile = null;
NicProfile controlNicProfile = null;
NicProfile profile = null;
for(NicProfile nicProfile : nicProfiles) {
if(nicProfile.getTrafficType() == TrafficType.Public) {
publicNicProfile = nicProfile;
break;
}
else if (nicProfile.getTrafficType() == TrafficType.Control) {
controlNicProfile = nicProfile;
}
}
if(publicNicProfile != null || controlNicProfile != null) {
NicTO[] nics = to.getNics();
// reserve extra NICs
NicTO[] expandedNics = new NicTO[MaxNicSupported];
int i = 0;
int deviceId = -1;
for(i = 0; i < nics.length; i++) {
expandedNics[i] = nics[i];
if(nics[i].getDeviceId() > deviceId)
deviceId = nics[i].getDeviceId();
}
deviceId++;
long networkId = 0;
if(publicNicProfile != null ) {
networkId= publicNicProfile.getNetworkId();
profile = publicNicProfile;
}
else {
networkId = controlNicProfile.getNetworkId();
profile = controlNicProfile;
}
NetworkVO network = _networkDao.findById(networkId);
// for Hyperv Hot Nic plug is not supported and it will support upto 8 nics.
// creating the VR with extra nics (actual nics(3) + extra nics) will be 8
for(; i < MaxNicSupported; i++) {
NicTO nicTo = new NicTO();
nicTo.setDeviceId(deviceId++);
nicTo.setBroadcastType(BroadcastDomainType.Vlan);
nicTo.setType(TrafficType.Public);
nicTo.setIp("0.0.0.0");
nicTo.setNetmask("255.255.255.255");
nicTo.setName(profile.getName());
try {
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(networkId);
nicTo.setMac(mac);
} catch (InsufficientAddressCapacityException e) {
throw new CloudRuntimeException("unable to allocate mac address on network: " + networkId);
}
nicTo.setDns1(profile.getDns1());
nicTo.setDns2(profile.getDns2());
if (publicNicProfile != null && publicNicProfile.getGateway() != null) {
nicTo.setGateway(publicNicProfile.getGateway());
} else {
nicTo.setGateway(network.getGateway());
}
nicTo.setDefaultNic(false);
nicTo.setBroadcastUri(profile.getBroadCastUri());
nicTo.setIsolationuri(profile.getIsolationUri());
Integer networkRate = _networkMgr.getNetworkRate(network.getId(), null);
nicTo.setNetworkRateMbps(networkRate);
expandedNics[i] = nicTo;
}
to.setNics(expandedNics);
}
StringBuffer sbMacSequence = new StringBuffer();
for(NicTO nicTo : sortNicsByDeviceId(to.getNics())) {
sbMacSequence.append(nicTo.getMac()).append("|");
}
if (!sbMacSequence.toString().isEmpty()) {
sbMacSequence.deleteCharAt(sbMacSequence.length() - 1);
String bootArgs = to.getBootArgs();
to.setBootArgs(bootArgs + " nic_macs=" + sbMacSequence.toString());
}
}
// Determine the VM's OS description
GuestOSVO guestOS = _guestOsDao.findById(vm.getVirtualMachine().getGuestOSId());
to.setOs(guestOS.getDisplayName());
return to;
}