Search in sources :

Example 1 with VMNetworkMapVO

use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.

the class VMNetworkMapDaoImpl method getNetworks.

@Override
public List<Long> getNetworks(long vmId) {
    SearchCriteria<VMNetworkMapVO> sc = VmIdSearch.create();
    sc.setParameters("vmId", vmId);
    List<VMNetworkMapVO> results = search(sc, null);
    List<Long> networks = new ArrayList<Long>(results.size());
    for (VMNetworkMapVO result : results) {
        networks.add(result.getNetworkId());
    }
    return networks;
}
Also used : VMNetworkMapVO(org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO) ArrayList(java.util.ArrayList)

Example 2 with VMNetworkMapVO

use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.

the class VMNetworkMapDaoImpl method findByVmAndNetworkId.

@Override
public VMNetworkMapVO findByVmAndNetworkId(long vmId, long networkId) {
    SearchCriteria<VMNetworkMapVO> sc = VmNetworkSearch.create();
    sc.setParameters("vmId", vmId);
    sc.setParameters("networkId", networkId);
    VMNetworkMapVO network = findOneBy(sc);
    return network;
}
Also used : VMNetworkMapVO(org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO)

Example 3 with VMNetworkMapVO

use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.

the class NetworkOrchestrator method releaseNic.

@DB
protected void releaseNic(final VirtualMachineProfile vmProfile, final long nicId) throws ConcurrentOperationException, ResourceUnavailableException {
    final Pair<Network, NicProfile> networkToRelease = Transaction.execute(new TransactionCallback<Pair<Network, NicProfile>>() {

        @Override
        public Pair<Network, NicProfile> doInTransaction(final TransactionStatus status) {
            final NicVO nic = _nicDao.lockRow(nicId, true);
            if (nic == null) {
                throw new ConcurrentOperationException("Unable to acquire lock on nic " + nic);
            }
            final Nic.State originalState = nic.getState();
            final NetworkVO network = _networksDao.findById(nic.getNetworkId());
            if (originalState == Nic.State.Reserved || originalState == Nic.State.Reserving) {
                if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start) {
                    final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
                    nic.setState(Nic.State.Releasing);
                    _nicDao.update(nic.getId(), nic);
                    final NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vmProfile.getHypervisorType(), network));
                    if (guru.release(profile, vmProfile, nic.getReservationId())) {
                        applyProfileToNicForRelease(nic, profile);
                        nic.setState(Nic.State.Allocated);
                        if (originalState == Nic.State.Reserved) {
                            updateNic(nic, network.getId(), -1);
                        } else {
                            _nicDao.update(nic.getId(), nic);
                        }
                    }
                    // Perform release on network elements
                    return new Pair<Network, NicProfile>(network, profile);
                } else {
                    nic.setState(Nic.State.Allocated);
                    updateNic(nic, network.getId(), -1);
                }
            }
            return null;
        }
    });
    // cleanup the entry in vm_network_map
    if (vmProfile.getType().equals(VirtualMachine.Type.User)) {
        final NicVO nic = _nicDao.findById(nicId);
        if (nic != null) {
            final NetworkVO vmNetwork = _networksDao.findById(nic.getNetworkId());
            final VMNetworkMapVO vno = _vmNetworkMapDao.findByVmAndNetworkId(vmProfile.getVirtualMachine().getId(), vmNetwork.getId());
            if (vno != null) {
                _vmNetworkMapDao.remove(vno.getId());
            }
        }
    }
    if (networkToRelease != null) {
        final Network network = networkToRelease.first();
        final NicProfile profile = networkToRelease.second();
        final List<Provider> providersToImplement = getNetworkProviders(network.getId());
        for (final NetworkElement element : networkElements) {
            if (providersToImplement.contains(element.getProvider())) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Asking " + element.getName() + " to release " + profile);
                }
                // NOTE: Context appear to never be used in release method
                // implementations. Consider removing it from interface Element
                element.release(network, profile, vmProfile, null);
            }
        }
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkGuru(com.cloud.network.guru.NetworkGuru) TransactionStatus(com.cloud.utils.db.TransactionStatus) NicProfile(com.cloud.vm.NicProfile) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) DnsServiceProvider(com.cloud.network.element.DnsServiceProvider) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider) LoadBalancingServiceProvider(com.cloud.network.element.LoadBalancingServiceProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) Provider(com.cloud.network.Network.Provider) NetworkElement(com.cloud.network.element.NetworkElement) ConfigDriveNetworkElement(com.cloud.network.element.ConfigDriveNetworkElement) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) VMNetworkMapVO(org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO) NicVO(com.cloud.vm.NicVO) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB)

Example 4 with VMNetworkMapVO

use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.

the class NetworkOrchestrator method createNicForVm.

@Override
public NicProfile createNicForVm(final Network network, final NicProfile requested, final ReservationContext context, final VirtualMachineProfile vmProfile, final boolean prepare) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
    final VirtualMachine vm = vmProfile.getVirtualMachine();
    final DataCenter dc = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
    final Host host = _hostDao.findById(vm.getHostId());
    final DeployDestination dest = new DeployDestination(dc, null, null, host);
    NicProfile nic = getNicProfileForVm(network, requested, vm);
    // 1) allocate nic (if needed) Always allocate if it is a user vm
    if (nic == null || vmProfile.getType() == VirtualMachine.Type.User) {
        final int deviceId = _nicDao.getFreeDeviceId(vm.getId());
        nic = allocateNic(requested, network, false, deviceId, vmProfile).first();
        if (nic == null) {
            throw new CloudRuntimeException("Failed to allocate nic for vm " + vm + " in network " + network);
        }
        // Update vm_network_map table
        if (vmProfile.getType() == VirtualMachine.Type.User) {
            final VMNetworkMapVO vno = new VMNetworkMapVO(vm.getId(), network.getId());
            _vmNetworkMapDao.persist(vno);
        }
        s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network);
    }
    // 2) prepare nic
    if (prepare) {
        final Pair<NetworkGuru, NetworkVO> implemented = implementNetwork(nic.getNetworkId(), dest, context, vmProfile.getVirtualMachine().getType() == Type.DomainRouter);
        if (implemented == null || implemented.first() == null) {
            s_logger.warn("Failed to implement network id=" + nic.getNetworkId() + " as a part of preparing nic id=" + nic.getId());
            throw new CloudRuntimeException("Failed to implement network id=" + nic.getNetworkId() + " as a part preparing nic id=" + nic.getId());
        }
        nic = prepareNic(vmProfile, dest, context, nic.getId(), implemented.second());
        s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network);
    }
    return nic;
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) DataCenter(com.cloud.dc.DataCenter) DeployDestination(com.cloud.deploy.DeployDestination) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkGuru(com.cloud.network.guru.NetworkGuru) VMNetworkMapVO(org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO) Host(com.cloud.host.Host) NicProfile(com.cloud.vm.NicProfile) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 5 with VMNetworkMapVO

use of org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO in project cloudstack by apache.

the class NetworkMigrationManagerImpl method migrateNicsInDB.

private Boolean migrateNicsInDB(NicVO originalNic, Network networkInNewPhysicalNet, DataCenter dc, ReservationContext context) {
    s_logger.debug("migrating nics in database.");
    UserVmVO vmVO = _vmDao.findById(originalNic.getInstanceId());
    VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
    NicProfile nicProfile = new NicProfile(originalNic, networkInNewPhysicalNet, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(networkInNewPhysicalNet), null);
    try {
        nicProfile = _networkMgr.allocateNic(nicProfile, networkInNewPhysicalNet, originalNic.isDefaultNic(), nicProfile.getDeviceId(), vmProfile).first();
    } catch (InsufficientVirtualNetworkCapacityException | InsufficientAddressCapacityException e) {
        throw new CloudRuntimeException("Allocation of new nicProfile failed during migration", e);
    }
    // Update vm_network_map table
    if (vmProfile.getType() == VirtualMachine.Type.User) {
        final VMNetworkMapVO vno = new VMNetworkMapVO(vmVO.getId(), networkInNewPhysicalNet.getId());
        _vmNetworkMapDao.persist(vno);
    }
    NicVO newNic = _nicDao.findById(nicProfile.getId());
    copyNicDetails(originalNic.getId(), newNic.getId());
    // Update nic uuid here
    moveServices(originalNic, newNic);
    if (originalNic.getState() == Nic.State.Reserved) {
        final VirtualMachine vm = vmProfile.getVirtualMachine();
        final Host host = _hostDao.findById(vm.getHostId());
        final DeployDestination dest = new DeployDestination(dc, null, null, host);
        try {
            nicProfile = _networkMgr.prepareNic(vmProfile, dest, context, nicProfile.getId(), networkInNewPhysicalNet);
            _itMgr.replugNic(networkInNewPhysicalNet, _itMgr.toNicTO(nicProfile, host.getHypervisorType()), _itMgr.toVmTO(vmProfile), dest.getHost());
        } catch (ResourceUnavailableException | InsufficientCapacityException e) {
            throw new CloudRuntimeException("Migration of Nic failed", e);
        }
    }
    // Mark the old nic as removed
    markAsNonDefault(originalNic);
    _networkMgr.removeNic(vmProfile, originalNic);
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Nic is migrated successfully for vm " + vmVO + " to " + networkInNewPhysicalNet);
    }
    return true;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) Host(com.cloud.host.Host) NicProfile(com.cloud.vm.NicProfile) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DeployDestination(com.cloud.deploy.DeployDestination) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) VMNetworkMapVO(org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NicVO(com.cloud.vm.NicVO) VirtualMachine(com.cloud.vm.VirtualMachine)

Aggregations

VMNetworkMapVO (org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO)6 NicProfile (com.cloud.vm.NicProfile)3 DeployDestination (com.cloud.deploy.DeployDestination)2 Host (com.cloud.host.Host)2 NetworkVO (com.cloud.network.dao.NetworkVO)2 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)2 NetworkGuru (com.cloud.network.guru.NetworkGuru)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 NicVO (com.cloud.vm.NicVO)2 VirtualMachine (com.cloud.vm.VirtualMachine)2 DataCenter (com.cloud.dc.DataCenter)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)1 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)1 InsufficientVirtualNetworkCapacityException (com.cloud.exception.InsufficientVirtualNetworkCapacityException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 Network (com.cloud.network.Network)1 Provider (com.cloud.network.Network.Provider)1 PhysicalNetwork (com.cloud.network.PhysicalNetwork)1 ConfigDriveNetworkElement (com.cloud.network.element.ConfigDriveNetworkElement)1