Search in sources :

Example 6 with NicVO

use of com.cloud.vm.NicVO 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) 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 7 with NicVO

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

the class NetworkOrchestrator method listVmNics.

@Override
public List<? extends Nic> listVmNics(final long vmId, final Long nicId, final Long networkId) {
    List<NicVO> result = null;
    if (nicId == null && networkId == null) {
        result = _nicDao.listByVmId(vmId);
    } else {
        result = _nicDao.listByVmIdAndNicIdAndNtwkId(vmId, nicId, networkId);
    }
    for (final NicVO nic : result) {
        if (_networkModel.isProviderForNetwork(Provider.NiciraNvp, nic.getNetworkId())) {
            //For NSX Based networks, add nsxlogicalswitch, nsxlogicalswitchport to each result
            s_logger.info("Listing NSX logical switch and logical switch por for each nic");
            final NetworkVO network = _networksDao.findById(nic.getNetworkId());
            final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
            final NetworkGuruAdditionalFunctions guruFunctions = (NetworkGuruAdditionalFunctions) guru;
            final Map<String, ? extends Object> nsxParams = guruFunctions.listAdditionalNicParams(nic.getUuid());
            if (nsxParams != null) {
                final String lswitchUuuid = nsxParams.containsKey(NetworkGuruAdditionalFunctions.NSX_LSWITCH_UUID) ? (String) nsxParams.get(NetworkGuruAdditionalFunctions.NSX_LSWITCH_UUID) : null;
                final String lswitchPortUuuid = nsxParams.containsKey(NetworkGuruAdditionalFunctions.NSX_LSWITCHPORT_UUID) ? (String) nsxParams.get(NetworkGuruAdditionalFunctions.NSX_LSWITCHPORT_UUID) : null;
                nic.setNsxLogicalSwitchUuid(lswitchUuuid);
                nic.setNsxLogicalSwitchPortUuid(lswitchPortUuuid);
            }
        }
    }
    return result;
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkGuru(com.cloud.network.guru.NetworkGuru) NicVO(com.cloud.vm.NicVO) NetworkGuruAdditionalFunctions(com.cloud.network.guru.NetworkGuruAdditionalFunctions)

Example 8 with NicVO

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

the class NetworkOrchestrator method reallocate.

@DB
@Override
public boolean reallocate(final VirtualMachineProfile vm, final DataCenterDeployment dest) throws InsufficientCapacityException, ConcurrentOperationException {
    final VMInstanceVO vmInstance = _vmDao.findById(vm.getId());
    final DataCenterVO dc = _dcDao.findById(vmInstance.getDataCenterId());
    if (dc.getNetworkType() == NetworkType.Basic) {
        final List<NicVO> nics = _nicDao.listByVmId(vmInstance.getId());
        final NetworkVO network = _networksDao.findById(nics.get(0).getNetworkId());
        final LinkedHashMap<Network, List<? extends NicProfile>> profiles = new LinkedHashMap<Network, List<? extends NicProfile>>();
        profiles.put(network, new ArrayList<NicProfile>());
        Transaction.execute(new TransactionCallbackWithExceptionNoReturn<InsufficientCapacityException>() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) throws InsufficientCapacityException {
                cleanupNics(vm);
                allocate(vm, profiles);
            }
        });
    }
    return true;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) NicProfile(com.cloud.vm.NicProfile) LinkedHashMap(java.util.LinkedHashMap) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ArrayList(java.util.ArrayList) List(java.util.List) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) NicVO(com.cloud.vm.NicVO) DB(com.cloud.utils.db.DB)

Example 9 with NicVO

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

the class NetworkOrchestrator method allocateNic.

@DB
@Override
public Pair<NicProfile, Integer> allocateNic(final NicProfile requested, final Network network, final Boolean isDefaultNic, int deviceId, final VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
    final NetworkVO ntwkVO = _networksDao.findById(network.getId());
    s_logger.debug("Allocating nic for vm " + vm.getVirtualMachine() + " in network " + network + " with requested profile " + requested);
    final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, ntwkVO.getGuruName());
    if (requested != null && requested.getMode() == null) {
        requested.setMode(network.getMode());
    }
    final NicProfile profile = guru.allocate(network, requested, vm);
    if (profile == null) {
        return null;
    }
    if (isDefaultNic != null) {
        profile.setDefaultNic(isDefaultNic);
    }
    if (requested != null && requested.getMode() == null) {
        profile.setMode(requested.getMode());
    } else {
        profile.setMode(network.getMode());
    }
    NicVO vo = new NicVO(guru.getName(), vm.getId(), network.getId(), vm.getType());
    deviceId = applyProfileToNic(vo, profile, deviceId);
    vo = _nicDao.persist(vo);
    final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
    final NicProfile vmNic = new NicProfile(vo, network, vo.getBroadcastUri(), vo.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
    return new Pair<NicProfile, Integer>(vmNic, Integer.valueOf(deviceId));
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkGuru(com.cloud.network.guru.NetworkGuru) NicProfile(com.cloud.vm.NicProfile) NicVO(com.cloud.vm.NicVO) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB)

Example 10 with NicVO

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

the class VMwareGuru method finalizeExpungeNics.

@Override
public List<Command> finalizeExpungeNics(VirtualMachine vm, List<NicProfile> nics) {
    List<Command> commands = new ArrayList<Command>();
    List<NicVO> nicVOs = _nicDao.listByVmId(vm.getId());
    for (NicVO nic : nicVOs) {
        NetworkVO network = _networkDao.findById(nic.getNetworkId());
        if (network.getBroadcastDomainType() == BroadcastDomainType.Lswitch) {
            s_logger.debug("Nic " + nic.toString() + " is connected to an lswitch, cleanup required");
            NetworkVO networkVO = _networkDao.findById(nic.getNetworkId());
            // We need the traffic label to figure out which vSwitch has the
            // portgroup
            PhysicalNetworkTrafficTypeVO trafficTypeVO = _physicalNetworkTrafficTypeDao.findBy(networkVO.getPhysicalNetworkId(), networkVO.getTrafficType());
            UnregisterNicCommand unregisterNicCommand = new UnregisterNicCommand(vm.getInstanceName(), trafficTypeVO.getVmwareNetworkLabel(), UUID.fromString(nic.getUuid()));
            commands.add(unregisterNicCommand);
        }
    }
    return commands;
}
Also used : UnregisterNicCommand(com.cloud.agent.api.UnregisterNicCommand) NetworkVO(com.cloud.network.dao.NetworkVO) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) BackupSnapshotCommand(com.cloud.agent.api.BackupSnapshotCommand) CreateVolumeOVACommand(com.cloud.agent.api.storage.CreateVolumeOVACommand) CreatePrivateTemplateFromSnapshotCommand(com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand) PrepareOVAPackingCommand(com.cloud.agent.api.storage.PrepareOVAPackingCommand) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) UnregisterNicCommand(com.cloud.agent.api.UnregisterNicCommand) CopyVolumeCommand(com.cloud.agent.api.storage.CopyVolumeCommand) StorageSubSystemCommand(org.apache.cloudstack.storage.command.StorageSubSystemCommand) CreateVolumeFromSnapshotCommand(com.cloud.agent.api.CreateVolumeFromSnapshotCommand) CreateEntityDownloadURLCommand(com.cloud.agent.api.storage.CreateEntityDownloadURLCommand) Command(com.cloud.agent.api.Command) CreatePrivateTemplateFromVolumeCommand(com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand) ArrayList(java.util.ArrayList) PhysicalNetworkTrafficTypeVO(com.cloud.network.dao.PhysicalNetworkTrafficTypeVO) NicVO(com.cloud.vm.NicVO)

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