Search in sources :

Example 16 with NicVO

use of com.cloud.vm.NicVO in project cloudstack by apache.

the class BaremetalDhcpElement method prepare.

@Override
@DB
public boolean prepare(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    Host host = dest.getHost();
    if (vm.getType() != Type.User || vm.getHypervisorType() != HypervisorType.BareMetal) {
        return false;
    }
    nic.setMacAddress(host.getPrivateMacAddress());
    NicVO vo = _nicDao.findById(nic.getId());
    assert vo != null : "Where ths nic " + nic.getId() + " going???";
    vo.setMacAddress(nic.getMacAddress());
    _nicDao.update(vo.getId(), vo);
    return true;
}
Also used : Host(com.cloud.host.Host) NicVO(com.cloud.vm.NicVO) DB(com.cloud.utils.db.DB)

Example 17 with NicVO

use of com.cloud.vm.NicVO in project cloudstack by apache.

the class ContrailGuru method reserve.

/**
     * Allocate the ip address (and mac) for the specified VM device.
     */
@Override
public void reserve(NicProfile nic, Network network, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
    s_logger.debug("reserve NicProfile on network id: " + network.getId() + " " + network.getName());
    s_logger.debug("deviceId: " + nic.getDeviceId());
    NicVO nicVO = _nicDao.findById(nic.getId());
    assert nicVO != null;
    VirtualNetworkModel vnModel = _manager.getDatabase().lookupVirtualNetwork(network.getUuid(), _manager.getCanonicalName(network), network.getTrafficType());
    /* Network must have been implemented */
    assert vnModel != null;
    VirtualMachineModel vmModel = _manager.getDatabase().lookupVirtualMachine(vm.getUuid());
    if (vmModel == null) {
        VMInstanceVO vmVo = (VMInstanceVO) vm.getVirtualMachine();
        vmModel = new VirtualMachineModel(vmVo, vm.getUuid());
        vmModel.setProperties(_manager.getModelController(), vmVo);
    }
    VMInterfaceModel vmiModel = vmModel.getVMInterface(nicVO.getUuid());
    if (vmiModel == null) {
        vmiModel = new VMInterfaceModel(nicVO.getUuid());
        vmiModel.addToVirtualMachine(vmModel);
        vmiModel.addToVirtualNetwork(vnModel);
    }
    try {
        vmiModel.build(_manager.getModelController(), (VMInstanceVO) vm.getVirtualMachine(), nicVO);
        vmiModel.setActive();
    } catch (IOException ex) {
        s_logger.error("virtual-machine-interface set", ex);
        return;
    }
    InstanceIpModel ipModel = vmiModel.getInstanceIp();
    if (ipModel == null) {
        ipModel = new InstanceIpModel(vm.getInstanceName(), nic.getDeviceId());
        ipModel.addToVMInterface(vmiModel);
    } else {
        s_logger.debug("Reuse existing instance-ip object on " + ipModel.getName());
    }
    if (nic.getIPv4Address() != null) {
        s_logger.debug("Nic using existing IP address " + nic.getIPv4Address());
        ipModel.setAddress(nic.getIPv4Address());
    }
    try {
        vmModel.update(_manager.getModelController());
    } catch (Exception ex) {
        s_logger.warn("virtual-machine update", ex);
        return;
    }
    _manager.getDatabase().getVirtualMachines().add(vmModel);
    VirtualMachineInterface vmi = vmiModel.getVMInterface();
    // allocate mac address
    if (nic.getMacAddress() == null) {
        MacAddressesType macs = vmi.getMacAddresses();
        if (macs == null) {
            s_logger.debug("no mac address is allocated for Nic " + nicVO.getUuid());
        } else {
            s_logger.info("VMI " + _manager.getVifNameByVmUuid(vm.getUuid(), nicVO.getDeviceId()) + " got mac address: " + macs.getMacAddress().get(0));
            nic.setMacAddress(macs.getMacAddress().get(0));
        }
    }
    if (nic.getIPv4Address() == null) {
        s_logger.debug("Allocated IP address " + ipModel.getAddress());
        nic.setIPv4Address(ipModel.getAddress());
        if (network.getCidr() != null) {
            nic.setIPv4Netmask(NetUtils.cidr2Netmask(network.getCidr()));
        }
        nic.setIPv4Gateway(network.getGateway());
        nic.setFormat(AddressFormat.Ip4);
    }
}
Also used : VirtualMachineInterface(net.juniper.contrail.api.types.VirtualMachineInterface) VMInterfaceModel(org.apache.cloudstack.network.contrail.model.VMInterfaceModel) VirtualMachineModel(org.apache.cloudstack.network.contrail.model.VirtualMachineModel) MacAddressesType(net.juniper.contrail.api.types.MacAddressesType) VMInstanceVO(com.cloud.vm.VMInstanceVO) IOException(java.io.IOException) NicVO(com.cloud.vm.NicVO) InstanceIpModel(org.apache.cloudstack.network.contrail.model.InstanceIpModel) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) VirtualNetworkModel(org.apache.cloudstack.network.contrail.model.VirtualNetworkModel)

Example 18 with NicVO

use of com.cloud.vm.NicVO in project cloudstack by apache.

the class InternalLBVMManagerTest method setUp.

@Override
@Before
public void setUp() {
    //mock system offering creation as it's used by configure() method called by initComponentsLifeCycle
    Mockito.when(_accountMgr.getAccount(1L)).thenReturn(new AccountVO());
    ServiceOfferingVO off = new ServiceOfferingVO("alena", 1, 1, 1, 1, 1, false, "alena", Storage.ProvisioningType.THIN, false, false, null, false, VirtualMachine.Type.InternalLoadBalancerVm, false);
    off = setId(off, 1);
    List<ServiceOfferingVO> list = new ArrayList<ServiceOfferingVO>();
    list.add(off);
    list.add(off);
    Mockito.when(_svcOffDao.createSystemServiceOfferings(Matchers.anyString(), Matchers.anyString(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyInt(), Matchers.anyBoolean(), Matchers.anyString(), Matchers.any(ProvisioningType.class), Matchers.anyBoolean(), Matchers.anyString(), Matchers.anyBoolean(), Matchers.any(VirtualMachine.Type.class), Matchers.anyBoolean())).thenReturn(list);
    ComponentContext.initComponentsLifeCycle();
    vm = new DomainRouterVO(1L, off.getId(), 1, "alena", 1, HypervisorType.XenServer, 1, 1, 1, 1, false, null, false, false, VirtualMachine.Type.InternalLoadBalancerVm, null);
    vm.setRole(Role.INTERNAL_LB_VM);
    vm = setId(vm, 1);
    vm.setPrivateIpAddress("10.2.2.2");
    final NicVO nic = new NicVO("somereserver", 1L, 1L, VirtualMachine.Type.InternalLoadBalancerVm);
    nic.setIPv4Address(requestedIp);
    final List<DomainRouterVO> emptyList = new ArrayList<DomainRouterVO>();
    final List<DomainRouterVO> nonEmptyList = new ArrayList<DomainRouterVO>();
    nonEmptyList.add(vm);
    Mockito.when(_domainRouterDao.listByNetworkAndRole(invalidNtwkId, Role.INTERNAL_LB_VM)).thenReturn(emptyList);
    Mockito.when(_domainRouterDao.listByNetworkAndRole(validNtwkId, Role.INTERNAL_LB_VM)).thenReturn(nonEmptyList);
    Mockito.when(_nicDao.findByNtwkIdAndInstanceId(validNtwkId, 1)).thenReturn(nic);
    Mockito.when(_nicDao.findByNtwkIdAndInstanceId(invalidNtwkId, 1)).thenReturn(nic);
    final Answer answer = new Answer(null, true, null);
    final Answer[] answers = new Answer[1];
    answers[0] = answer;
    try {
        Mockito.when(_agentMgr.send(Matchers.anyLong(), Matchers.any(Commands.class))).thenReturn(answers);
    } catch (final AgentUnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (final OperationTimedoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    createNetwork();
    Mockito.when(_ntwkModel.getNetwork(Matchers.anyLong())).thenReturn(ntwk);
    Mockito.when(_itMgr.toNicTO(Matchers.any(NicProfile.class), Matchers.any(HypervisorType.class))).thenReturn(null);
    Mockito.when(_domainRouterDao.findById(Matchers.anyLong())).thenReturn(vm);
    final DataCenterVO dc = new DataCenterVO(1L, null, null, null, null, null, null, null, null, null, NetworkType.Advanced, null, null);
    Mockito.when(_dcDao.findById(Matchers.anyLong())).thenReturn(dc);
    final NetworkOfferingVO networkOfferingVO = new NetworkOfferingVO();
    networkOfferingVO.setConcurrentConnections(500);
    Mockito.when(_offeringDao.findById(Matchers.anyLong())).thenReturn(networkOfferingVO);
    Mockito.when(_domainRouterDao.findById(validVmId)).thenReturn(vm);
    Mockito.when(_domainRouterDao.findById(invalidVmId)).thenReturn(null);
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) ArrayList(java.util.ArrayList) NicProfile(com.cloud.vm.NicProfile) AccountVO(com.cloud.user.AccountVO) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ProvisioningType(com.cloud.storage.Storage.ProvisioningType) Answer(com.cloud.agent.api.Answer) NetworkType(com.cloud.dc.DataCenter.NetworkType) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ProvisioningType(com.cloud.storage.Storage.ProvisioningType) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) NicVO(com.cloud.vm.NicVO) DomainRouterVO(com.cloud.vm.DomainRouterVO) Before(org.junit.Before)

Example 19 with NicVO

use of com.cloud.vm.NicVO in project cloudstack by apache.

the class InternalLoadBalancerVMManagerImpl method getInternalLbControlIp.

protected String getInternalLbControlIp(final long internalLbVmId) {
    String controlIpAddress = null;
    final List<NicVO> nics = _nicDao.listByVmId(internalLbVmId);
    for (final NicVO nic : nics) {
        final Network ntwk = _ntwkModel.getNetwork(nic.getNetworkId());
        if (ntwk.getTrafficType() == TrafficType.Control) {
            controlIpAddress = nic.getIPv4Address();
        }
    }
    if (controlIpAddress == null) {
        s_logger.warn("Unable to find Internal LB control ip in its attached NICs!. Internal LB vm: " + internalLbVmId);
        final DomainRouterVO internalLbVm = _internalLbVmDao.findById(internalLbVmId);
        return internalLbVm.getPrivateIpAddress();
    }
    return controlIpAddress;
}
Also used : Network(com.cloud.network.Network) NicVO(com.cloud.vm.NicVO) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 20 with NicVO

use of com.cloud.vm.NicVO in project cloudstack by apache.

the class NuageVspGuestNetworkGuru method deallocate.

@Override
@DB
public void deallocate(Network network, NicProfile nic, VirtualMachineProfile vm) {
    boolean lockedNetwork = lockNetworkForUserVm(network, vm);
    if (lockedNetwork && s_logger.isDebugEnabled()) {
        s_logger.debug("Locked network " + network.getId() + " for deallocation of user VM " + vm.getInstanceName());
    }
    try {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Handling deallocate() call back, which is called when a VM is destroyed or interface is removed, " + "to delete VM Interface with IP " + nic.getIPv4Address() + " from a VM " + vm.getInstanceName() + " with state " + vm.getVirtualMachine().getState());
        }
        NicVO nicFromDb = _nicDao.findById(nic.getId());
        VspNetwork vspNetwork = _nuageVspEntityBuilder.buildVspNetwork(vm.getVirtualMachine().getDomainId(), network);
        VspVm vspVm = _nuageVspEntityBuilder.buildVspVm(vm.getVirtualMachine(), network);
        VspNic vspNic = _nuageVspEntityBuilder.buildVspNic(nicFromDb.getUuid(), nic);
        HostVO nuageVspHost = _nuageVspManager.getNuageVspHost(network.getPhysicalNetworkId());
        DeallocateVmVspCommand cmd = new DeallocateVmVspCommand(vspNetwork, vspVm, vspNic);
        Answer answer = _agentMgr.easySend(nuageVspHost.getId(), cmd);
        if (answer == null || !answer.getResult()) {
            s_logger.error("DeallocateVmNuageVspCommand for VM " + vm.getUuid() + " failed on Nuage VSD " + nuageVspHost.getDetail("hostname"));
            if ((null != answer) && (null != answer.getDetails())) {
                s_logger.error(answer.getDetails());
            }
        }
        // because it is still allocated for the VR.
        if (vspNetwork.isShared() && VirtualMachine.Type.User.equals(vm.getType()) && nic.getIPv4Address().equals(vspNetwork.getVirtualRouterIp())) {
            nic.deallocate();
        } else {
            super.deallocate(network, nic, vm);
        }
    } finally {
        if (network != null && lockedNetwork) {
            _networkDao.releaseFromLockTable(network.getId());
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Unlocked network " + network.getId() + " for deallocation of user VM " + vm.getInstanceName());
            }
        }
    }
}
Also used : Answer(com.cloud.agent.api.Answer) DeallocateVmVspCommand(com.cloud.agent.api.guru.DeallocateVmVspCommand) VspVm(net.nuage.vsp.acs.client.api.model.VspVm) VspNetwork(net.nuage.vsp.acs.client.api.model.VspNetwork) VspNic(net.nuage.vsp.acs.client.api.model.VspNic) NicVO(com.cloud.vm.NicVO) HostVO(com.cloud.host.HostVO) DB(com.cloud.utils.db.DB)

Aggregations

NicVO (com.cloud.vm.NicVO)86 NetworkVO (com.cloud.network.dao.NetworkVO)33 ArrayList (java.util.ArrayList)21 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)18 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 NicProfile (com.cloud.vm.NicProfile)15 VMInstanceVO (com.cloud.vm.VMInstanceVO)13 DataCenterVO (com.cloud.dc.DataCenterVO)12 Commands (com.cloud.agent.manager.Commands)11 Network (com.cloud.network.Network)11 HostVO (com.cloud.host.HostVO)10 UserVmVO (com.cloud.vm.UserVmVO)10 Answer (com.cloud.agent.api.Answer)9 NetworkGuru (com.cloud.network.guru.NetworkGuru)9 Nic (com.cloud.vm.Nic)9 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)9 Test (org.junit.Test)9 DataCenter (com.cloud.dc.DataCenter)7 IPAddressVO (com.cloud.network.dao.IPAddressVO)7 VirtualRouter (com.cloud.network.router.VirtualRouter)7