use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class MidoNetElementTest method testAddDhcpEntry.
/*
* Test the standard case of addDhcpEntry with no errors.
*/
public void testAddDhcpEntry() {
//mockMgmt
MidonetApi api = mock(MidonetApi.class, RETURNS_DEEP_STUBS);
//mockDhcpHost
DhcpHost mockDhcpHost = mock(DhcpHost.class);
//mockHostCollection
ResourceCollection<DhcpHost> hosts = new ResourceCollection<DhcpHost>(new ArrayList<DhcpHost>());
//mockDhcpSubnet
DhcpSubnet mockSub = mock(DhcpSubnet.class);
when(mockSub.addDhcpHost()).thenReturn(mockDhcpHost);
when(mockSub.getDhcpHosts()).thenReturn(hosts);
//mockSubnetCollection
ResourceCollection mockSubnetCollection = mock(ResourceCollection.class);
when(mockSubnetCollection.get(anyInt())).thenReturn(mockSub);
//mockBridge
Bridge mockBridge = mock(Bridge.class);
when(api.addBridge().tenantId(anyString()).name(anyString()).create()).thenReturn(mockBridge);
when(mockBridge.getDhcpSubnets()).thenReturn(mockSubnetCollection);
//mockRouter
Router mockRouter = mock(Router.class);
when(api.addRouter().tenantId(anyString()).name(anyString()).create()).thenReturn(mockRouter);
//mockNetwork
Network mockNetwork = mock(Network.class);
when(mockNetwork.getAccountId()).thenReturn((long) 1);
when(mockNetwork.getGateway()).thenReturn("1.2.3.4");
when(mockNetwork.getCidr()).thenReturn("1.2.3.0/24");
when(mockNetwork.getId()).thenReturn((long) 2);
when(mockNetwork.getBroadcastDomainType()).thenReturn(Networks.BroadcastDomainType.Mido);
when(mockNetwork.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
//mockAccountDao
AccountDao mockAccountDao = mock(AccountDao.class);
AccountVO mockAccountVO = mock(AccountVO.class);
when(mockAccountDao.findById(anyLong())).thenReturn(mockAccountVO);
when(mockAccountVO.getUuid()).thenReturn("1");
//mockNic
NicProfile mockNic = mock(NicProfile.class);
when(mockNic.getIPv4Address()).thenReturn("10.10.10.170");
when(mockNic.getMacAddress()).thenReturn("02:00:73:3e:00:01");
when(mockNic.getName()).thenReturn("Fake Name");
//mockVm
@SuppressWarnings("unchecked") VirtualMachineProfile mockVm = mock(VirtualMachineProfile.class);
when(mockVm.getType()).thenReturn(VirtualMachine.Type.User);
MidoNetElement elem = new MidoNetElement();
elem.setMidonetApi(api);
elem.setAccountDao(mockAccountDao);
boolean result = false;
try {
result = elem.addDhcpEntry(mockNetwork, mockNic, mockVm, null, null);
} catch (ConcurrentOperationException e) {
fail(e.getMessage());
} catch (InsufficientCapacityException e) {
fail(e.getMessage());
} catch (ResourceUnavailableException e) {
fail(e.getMessage());
}
assertEquals(result, true);
}
use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class SecondaryStorageManagerImpl method finalizeDeployment.
@Override
public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) {
finalizeCommandsOnStart(cmds, profile);
SecondaryStorageVmVO secVm = _secStorageVmDao.findById(profile.getId());
DataCenter dc = dest.getDataCenter();
List<NicProfile> nics = profile.getNics();
for (NicProfile nic : nics) {
if ((nic.getTrafficType() == TrafficType.Public && dc.getNetworkType() == NetworkType.Advanced) || (nic.getTrafficType() == TrafficType.Guest && (dc.getNetworkType() == NetworkType.Basic || dc.isSecurityGroupEnabled()))) {
secVm.setPublicIpAddress(nic.getIPv4Address());
secVm.setPublicNetmask(nic.getIPv4Netmask());
secVm.setPublicMacAddress(nic.getMacAddress());
} else if (nic.getTrafficType() == TrafficType.Management) {
secVm.setPrivateIpAddress(nic.getIPv4Address());
secVm.setPrivateMacAddress(nic.getMacAddress());
}
}
_secStorageVmDao.update(secVm.getId(), secVm);
return true;
}
use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class SecondaryStorageManagerImpl method finalizeVirtualMachineProfile.
@Override
public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) {
SecondaryStorageVmVO vm = _secStorageVmDao.findById(profile.getId());
Map<String, String> details = _vmDetailsDao.listDetailsKeyPairs(vm.getId());
vm.setDetails(details);
DataStore secStore = _dataStoreMgr.getImageStore(dest.getDataCenter().getId());
assert (secStore != null);
StringBuilder buf = profile.getBootArgsBuilder();
buf.append(" template=domP type=secstorage");
buf.append(" host=").append(ApiServiceConfiguration.ManagementHostIPAdr.value());
buf.append(" port=").append(_mgmtPort);
buf.append(" name=").append(profile.getVirtualMachine().getHostName());
buf.append(" zone=").append(dest.getDataCenter().getId());
buf.append(" pod=").append(dest.getPod().getId());
buf.append(" guid=").append(profile.getVirtualMachine().getHostName());
buf.append(" workers=").append(_configDao.getValue("workers"));
if (_configDao.isPremium()) {
s_logger.debug("VmWare hypervisor configured, telling the ssvm to load the PremiumSecondaryStorageResource");
buf.append(" resource=com.cloud.storage.resource.PremiumSecondaryStorageResource");
} else {
buf.append(" resource=org.apache.cloudstack.storage.resource.NfsSecondaryStorageResource");
}
buf.append(" instance=SecStorage");
buf.append(" sslcopy=").append(Boolean.toString(_useSSlCopy));
buf.append(" role=").append(vm.getRole().toString());
buf.append(" mtu=").append(_secStorageVmMtuSize);
boolean externalDhcp = false;
String externalDhcpStr = _configDao.getValue("direct.attach.network.externalIpAllocator.enabled");
if (externalDhcpStr != null && externalDhcpStr.equalsIgnoreCase("true")) {
externalDhcp = true;
}
if (Boolean.valueOf(_configDao.getValue("system.vm.random.password"))) {
buf.append(" vmpassword=").append(_configDao.getValue("system.vm.password"));
}
for (NicProfile nic : profile.getNics()) {
int deviceId = nic.getDeviceId();
if (nic.getIPv4Address() == null) {
buf.append(" eth").append(deviceId).append("mask=").append("0.0.0.0");
buf.append(" eth").append(deviceId).append("ip=").append("0.0.0.0");
} else {
buf.append(" eth").append(deviceId).append("ip=").append(nic.getIPv4Address());
buf.append(" eth").append(deviceId).append("mask=").append(nic.getIPv4Netmask());
}
if (nic.isDefaultNic()) {
buf.append(" gateway=").append(nic.getIPv4Gateway());
}
if (nic.getTrafficType() == TrafficType.Management) {
String mgmt_cidr = _configDao.getValue(Config.ManagementNetwork.key());
if (NetUtils.isValidCIDR(mgmt_cidr)) {
buf.append(" mgmtcidr=").append(mgmt_cidr);
}
buf.append(" localgw=").append(dest.getPod().getGateway());
buf.append(" private.network.device=").append("eth").append(deviceId);
} else if (nic.getTrafficType() == TrafficType.Public) {
buf.append(" public.network.device=").append("eth").append(deviceId);
} else if (nic.getTrafficType() == TrafficType.Storage) {
buf.append(" storageip=").append(nic.getIPv4Address());
buf.append(" storagenetmask=").append(nic.getIPv4Netmask());
buf.append(" storagegateway=").append(nic.getIPv4Gateway());
}
}
/* External DHCP mode */
if (externalDhcp) {
buf.append(" bootproto=dhcp");
}
DataCenterVO dc = _dcDao.findById(profile.getVirtualMachine().getDataCenterId());
buf.append(" internaldns1=").append(dc.getInternalDns1());
if (dc.getInternalDns2() != null) {
buf.append(" internaldns2=").append(dc.getInternalDns2());
}
buf.append(" dns1=").append(dc.getDns1());
if (dc.getDns2() != null) {
buf.append(" dns2=").append(dc.getDns2());
}
Integer nfsVersion = imageStoreDetailsUtil != null ? imageStoreDetailsUtil.getNfsVersion(secStore.getId()) : null;
buf.append(" nfsVersion=").append(nfsVersion);
String bootArgs = buf.toString();
if (s_logger.isDebugEnabled()) {
s_logger.debug("Boot Args for " + profile + ": " + bootArgs);
}
return true;
}
use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class SecondaryStorageManagerImpl method createSecStorageVmInstance.
protected Map<String, Object> createSecStorageVmInstance(long dataCenterId, SecondaryStorageVm.Role role) {
DataStore secStore = _dataStoreMgr.getImageStore(dataCenterId);
if (secStore == null) {
String msg = "No secondary storage available in zone " + dataCenterId + ", cannot create secondary storage vm";
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
long id = _secStorageVmDao.getNextInSequence(Long.class, "id");
String name = VirtualMachineName.getSystemVmName(id, _instance, "s").intern();
Account systemAcct = _accountMgr.getSystemAccount();
DataCenterDeployment plan = new DataCenterDeployment(dataCenterId);
DataCenter dc = _dcDao.findById(plan.getDataCenterId());
NetworkVO defaultNetwork = getDefaultNetworkForCreation(dc);
List<? extends NetworkOffering> offerings = null;
if (_sNwMgr.isStorageIpRangeAvailable(dataCenterId)) {
offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork, NetworkOffering.SystemManagementNetwork, NetworkOffering.SystemStorageNetwork);
} else {
offerings = _networkModel.getSystemAccountNetworkOfferings(NetworkOffering.SystemControlNetwork, NetworkOffering.SystemManagementNetwork);
}
LinkedHashMap<Network, List<? extends NicProfile>> networks = new LinkedHashMap<Network, List<? extends NicProfile>>(offerings.size() + 1);
NicProfile defaultNic = new NicProfile();
defaultNic.setDefaultNic(true);
defaultNic.setDeviceId(2);
try {
networks.put(_networkMgr.setupNetwork(systemAcct, _networkOfferingDao.findById(defaultNetwork.getNetworkOfferingId()), plan, null, null, false).get(0), new ArrayList<NicProfile>(Arrays.asList(defaultNic)));
for (NetworkOffering offering : offerings) {
networks.put(_networkMgr.setupNetwork(systemAcct, offering, plan, null, null, false).get(0), new ArrayList<NicProfile>());
}
} catch (ConcurrentOperationException e) {
s_logger.info("Unable to setup due to concurrent operation. " + e);
return new HashMap<String, Object>();
}
VMTemplateVO template = null;
HypervisorType availableHypervisor = _resourceMgr.getAvailableHypervisor(dataCenterId);
template = _templateDao.findSystemVMReadyTemplate(dataCenterId, availableHypervisor);
if (template == null) {
throw new CloudRuntimeException("Not able to find the System templates or not downloaded in zone " + dataCenterId);
}
ServiceOfferingVO serviceOffering = _serviceOffering;
if (serviceOffering == null) {
serviceOffering = _offeringDao.findDefaultSystemOffering(ServiceOffering.ssvmDefaultOffUniqueName, ConfigurationManagerImpl.SystemVMUseLocalStorage.valueIn(dataCenterId));
}
SecondaryStorageVmVO secStorageVm = new SecondaryStorageVmVO(id, serviceOffering.getId(), name, template.getId(), template.getHypervisorType(), template.getGuestOSId(), dataCenterId, systemAcct.getDomainId(), systemAcct.getId(), _accountMgr.getSystemUser().getId(), role, serviceOffering.getOfferHA());
secStorageVm.setDynamicallyScalable(template.isDynamicallyScalable());
secStorageVm = _secStorageVmDao.persist(secStorageVm);
try {
_itMgr.allocate(name, template, serviceOffering, networks, plan, null);
secStorageVm = _secStorageVmDao.findById(secStorageVm.getId());
} catch (InsufficientCapacityException e) {
s_logger.warn("InsufficientCapacity", e);
throw new CloudRuntimeException("Insufficient capacity exception", e);
}
Map<String, Object> context = new HashMap<String, Object>();
context.put("secStorageVmId", secStorageVm.getId());
return context;
}
use of com.cloud.vm.NicProfile in project cloudstack by apache.
the class NetworkOrchestrator method prepareAllNicsForMigration.
/*
Prepare All Nics for migration including the nics dynamically created and not stored in DB
This is a temporary workaround work KVM migration
Once clean fix is added by stored dynamically nics is DB, this workaround won't be needed
*/
@Override
public void prepareAllNicsForMigration(final VirtualMachineProfile vm, final DeployDestination dest) {
final List<NicVO> nics = _nicDao.listByVmId(vm.getId());
final ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null);
Long guestNetworkId = null;
for (final NicVO nic : nics) {
final NetworkVO network = _networksDao.findById(nic.getNetworkId());
if (network.getTrafficType().equals(TrafficType.Guest) && network.getGuestType().equals(GuestType.Isolated)) {
guestNetworkId = network.getId();
}
final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
final NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
if (guru instanceof NetworkMigrationResponder) {
if (!((NetworkMigrationResponder) guru).prepareMigration(profile, network, vm, dest, context)) {
// XXX: Transaction error
s_logger.error("NetworkGuru " + guru + " prepareForMigration failed.");
}
}
final List<Provider> providersToImplement = getNetworkProviders(network.getId());
for (final NetworkElement element : networkElements) {
if (providersToImplement.contains(element.getProvider())) {
if (!_networkModel.isProviderEnabledInPhysicalNetwork(_networkModel.getPhysicalNetworkId(network), element.getProvider().getName())) {
throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + " either doesn't exist or is not enabled in physical network id: " + network.getPhysicalNetworkId());
}
if (element instanceof NetworkMigrationResponder) {
if (!((NetworkMigrationResponder) element).prepareMigration(profile, network, vm, dest, context)) {
// XXX: Transaction error
s_logger.error("NetworkElement " + element + " prepareForMigration failed.");
}
}
}
}
guru.updateNicProfile(profile, network);
vm.addNic(profile);
}
final List<String> addedURIs = new ArrayList<String>();
if (guestNetworkId != null) {
final List<IPAddressVO> publicIps = _ipAddressDao.listByAssociatedNetwork(guestNetworkId, null);
for (final IPAddressVO userIp : publicIps) {
final PublicIp publicIp = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
final URI broadcastUri = BroadcastDomainType.Vlan.toUri(publicIp.getVlanTag());
final long ntwkId = publicIp.getNetworkId();
final Nic nic = _nicDao.findByNetworkIdInstanceIdAndBroadcastUri(ntwkId, vm.getId(), broadcastUri.toString());
if (nic == null && !addedURIs.contains(broadcastUri.toString())) {
//Nic details are not available in DB
//Create nic profile for migration
s_logger.debug("Creating nic profile for migration. BroadcastUri: " + broadcastUri.toString() + " NetworkId: " + ntwkId + " Vm: " + vm.getId());
final NetworkVO network = _networksDao.findById(ntwkId);
_networkModel.getNetworkRate(network.getId(), vm.getId());
final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
final NicProfile profile = new NicProfile();
//dummyId
profile.setDeviceId(255);
profile.setIPv4Address(userIp.getAddress().toString());
profile.setIPv4Netmask(publicIp.getNetmask());
profile.setIPv4Gateway(publicIp.getGateway());
profile.setMacAddress(publicIp.getMacAddress());
profile.setBroadcastType(network.getBroadcastDomainType());
profile.setTrafficType(network.getTrafficType());
profile.setBroadcastUri(broadcastUri);
profile.setIsolationUri(Networks.IsolationType.Vlan.toUri(publicIp.getVlanTag()));
profile.setSecurityGroupEnabled(_networkModel.isSecurityGroupSupportedInNetwork(network));
profile.setName(_networkModel.getNetworkTag(vm.getHypervisorType(), network));
profile.setNetworId(network.getId());
guru.updateNicProfile(profile, network);
vm.addNic(profile);
addedURIs.add(broadcastUri.toString());
}
}
}
}
Aggregations