Search in sources :

Example 1 with NetworkGuru

use of com.cloud.network.guru.NetworkGuru 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 2 with NetworkGuru

use of com.cloud.network.guru.NetworkGuru in project cloudstack by apache.

the class NetworkOrchestrator method shutdownNetwork.

@Override
@DB
public boolean shutdownNetwork(final long networkId, final ReservationContext context, final boolean cleanupElements) {
    NetworkVO network = _networksDao.findById(networkId);
    if (network.getState() == Network.State.Allocated) {
        s_logger.debug("Network is already shutdown: " + network);
        return true;
    }
    if (network.getState() != Network.State.Implemented && network.getState() != Network.State.Shutdown) {
        s_logger.debug("Network is not implemented: " + network);
        return false;
    }
    try {
        // do global lock for the network
        network = _networksDao.acquireInLockTable(networkId, NetworkLockTimeout.value());
        if (network == null) {
            s_logger.warn("Unable to acquire lock for the network " + network + " as a part of network shutdown");
            return false;
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Lock is acquired for network " + network + " as a part of network shutdown");
        }
        if (network.getState() == Network.State.Allocated) {
            s_logger.debug("Network is already shutdown: " + network);
            return true;
        }
        if (network.getState() != Network.State.Implemented && network.getState() != Network.State.Shutdown) {
            s_logger.debug("Network is not implemented: " + network);
            return false;
        }
        if (isSharedNetworkWithServices(network)) {
            network.setState(Network.State.Shutdown);
            _networksDao.update(network.getId(), network);
        } else {
            try {
                stateTransitTo(network, Event.DestroyNetwork);
            } catch (final NoTransitionException e) {
                network.setState(Network.State.Shutdown);
                _networksDao.update(network.getId(), network);
            }
        }
        final boolean success = shutdownNetworkElementsAndResources(context, cleanupElements, network);
        final NetworkVO networkFinal = network;
        final boolean result = Transaction.execute(new TransactionCallback<Boolean>() {

            @Override
            public Boolean doInTransaction(final TransactionStatus status) {
                boolean result = false;
                if (success) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Network id=" + networkId + " is shutdown successfully, cleaning up corresponding resources now.");
                    }
                    final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, networkFinal.getGuruName());
                    final NetworkProfile profile = convertNetworkToNetworkProfile(networkFinal.getId());
                    guru.shutdown(profile, _networkOfferingDao.findById(networkFinal.getNetworkOfferingId()));
                    applyProfileToNetwork(networkFinal, profile);
                    final DataCenterVO zone = _dcDao.findById(networkFinal.getDataCenterId());
                    if (isSharedNetworkOfferingWithServices(networkFinal.getNetworkOfferingId()) && zone.getNetworkType() == NetworkType.Advanced) {
                        networkFinal.setState(Network.State.Setup);
                    } else {
                        try {
                            stateTransitTo(networkFinal, Event.OperationSucceeded);
                        } catch (final NoTransitionException e) {
                            networkFinal.setState(Network.State.Allocated);
                            networkFinal.setRestartRequired(false);
                        }
                    }
                    _networksDao.update(networkFinal.getId(), networkFinal);
                    _networksDao.clearCheckForGc(networkId);
                    result = true;
                } else {
                    try {
                        stateTransitTo(networkFinal, Event.OperationFailed);
                    } catch (final NoTransitionException e) {
                        networkFinal.setState(Network.State.Implemented);
                        _networksDao.update(networkFinal.getId(), networkFinal);
                    }
                    result = false;
                }
                return result;
            }
        });
        return result;
    } finally {
        if (network != null) {
            _networksDao.releaseFromLockTable(network.getId());
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Lock is released for network " + network + " as a part of network shutdown");
            }
        }
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkProfile(com.cloud.network.NetworkProfile) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) NetworkGuru(com.cloud.network.guru.NetworkGuru) TransactionStatus(com.cloud.utils.db.TransactionStatus) DB(com.cloud.utils.db.DB)

Example 3 with NetworkGuru

use of com.cloud.network.guru.NetworkGuru 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());
    DataCenterVO dcVo = _dcDao.findById(network.getDataCenterId());
    if (dcVo.getNetworkType() == NetworkType.Basic) {
        configureNicProfileBasedOnRequestedIp(requested, profile, network);
    }
    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 : DataCenterVO(com.cloud.dc.DataCenterVO) 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 4 with NetworkGuru

use of com.cloud.network.guru.NetworkGuru 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 5 with NetworkGuru

use of com.cloud.network.guru.NetworkGuru in project cloudstack by apache.

the class NetworkOrchestrator method prepareNicForMigration.

@Override
public void prepareNicForMigration(final VirtualMachineProfile vm, final DeployDestination dest) {
    if (vm.getType().equals(VirtualMachine.Type.DomainRouter) && (vm.getHypervisorType().equals(HypervisorType.KVM) || vm.getHypervisorType().equals(HypervisorType.VMware))) {
        // Include nics hot plugged and not stored in DB
        prepareAllNicsForMigration(vm, dest);
        return;
    }
    final List<NicVO> nics = _nicDao.listByVmId(vm.getId());
    final ReservationContext context = new ReservationContextImpl(UUID.randomUUID().toString(), null, null);
    for (final NicVO nic : nics) {
        final NetworkVO network = _networksDao.findById(nic.getNetworkId());
        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.");
            }
        }
        if (network.getGuestType() == Network.GuestType.L2 && vm.getType() == VirtualMachine.Type.User) {
            _userVmMgr.setupVmForPvlan(false, vm.getVirtualMachine().getHostId(), profile);
        }
        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);
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkMigrationResponder(com.cloud.network.NetworkMigrationResponder) NetworkGuru(com.cloud.network.guru.NetworkGuru) NicProfile(com.cloud.vm.NicProfile) ReservationContextImpl(com.cloud.vm.ReservationContextImpl) ReservationContext(com.cloud.vm.ReservationContext) 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) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NicVO(com.cloud.vm.NicVO)

Aggregations

NetworkGuru (com.cloud.network.guru.NetworkGuru)44 NetworkVO (com.cloud.network.dao.NetworkVO)39 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)37 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 NicProfile (com.cloud.vm.NicProfile)23 NicVO (com.cloud.vm.NicVO)19 DhcpServiceProvider (com.cloud.network.element.DhcpServiceProvider)18 Provider (com.cloud.network.Network.Provider)16 LoadBalancingServiceProvider (com.cloud.network.element.LoadBalancingServiceProvider)16 NetworkElement (com.cloud.network.element.NetworkElement)16 StaticNatServiceProvider (com.cloud.network.element.StaticNatServiceProvider)16 UserDataServiceProvider (com.cloud.network.element.UserDataServiceProvider)16 DB (com.cloud.utils.db.DB)14 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)11 ArrayList (java.util.ArrayList)11 Network (com.cloud.network.Network)10 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)10 NetworkMigrationResponder (com.cloud.network.NetworkMigrationResponder)9 Pair (com.cloud.utils.Pair)9 TransactionStatus (com.cloud.utils.db.TransactionStatus)9