Search in sources :

Example 41 with NetworkElement

use of com.cloud.network.element.NetworkElement in project cosmic by MissionCriticalCloud.

the class NetworkOrchestrator method prepareNic.

@Override
public NicProfile prepareNic(final VirtualMachineProfile vmProfile, final DeployDestination dest, final ReservationContext context, final long nicId, final Network network) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
    final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vmProfile.getId());
    final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
    final NicVO nic = _nicDao.findById(nicId);
    NicProfile profile;
    if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start) {
        nic.setState(Nic.State.Reserving);
        nic.setReservationId(context.getReservationId());
        _nicDao.update(nic.getId(), nic);
        URI broadcastUri = nic.getBroadcastUri();
        if (broadcastUri == null) {
            broadcastUri = network.getBroadcastUri();
        }
        final URI isolationUri = nic.getIsolationUri();
        profile = new NicProfile(nic, network, broadcastUri, isolationUri, networkRate, _networkModel.getNetworkTag(vmProfile.getHypervisorType(), network));
        guru.reserve(profile, network, vmProfile, dest, context);
        nic.setIPv4Address(profile.getIPv4Address());
        nic.setAddressFormat(profile.getFormat());
        nic.setIPv6Address(profile.getIPv6Address());
        nic.setMacAddress(profile.getMacAddress());
        nic.setIsolationUri(profile.getIsolationUri());
        nic.setBroadcastUri(profile.getBroadCastUri());
        nic.setReserver(guru.getName());
        nic.setState(Nic.State.Reserved);
        nic.setIPv4Netmask(profile.getIPv4Netmask());
        nic.setIPv4Gateway(profile.getIPv4Gateway());
        if (profile.getReservationStrategy() != null) {
            nic.setReservationStrategy(profile.getReservationStrategy());
        }
        updateNic(nic, network.getId(), 1);
    } else {
        profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, _networkModel.getNetworkTag(vmProfile.getHypervisorType(), network));
        guru.updateNicProfile(profile, network);
        nic.setState(Nic.State.Reserved);
        updateNic(nic, network.getId(), 1);
    }
    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 (s_logger.isDebugEnabled()) {
                s_logger.debug("Asking " + element.getName() + " to prepare for " + nic);
            }
            if (!prepareElement(element, network, profile, vmProfile, dest, context)) {
                throw new InsufficientAddressCapacityException("unable to configure the dhcp service, due to insufficiant address capacity", Network.class, network.getId());
            }
        }
    }
    guru.updateNicProfile(profile, network);
    return profile;
}
Also used : NetworkElement(com.cloud.network.element.NetworkElement) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkGuru(com.cloud.network.guru.NetworkGuru) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) NicProfile(com.cloud.vm.NicProfile) NicVO(com.cloud.vm.NicVO) URI(java.net.URI) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) LoadBalancingServiceProvider(com.cloud.network.element.LoadBalancingServiceProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider) Provider(com.cloud.network.Network.Provider)

Example 42 with NetworkElement

use of com.cloud.network.element.NetworkElement in project cosmic by MissionCriticalCloud.

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)) {
        // 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.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);
    }
}
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) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) LoadBalancingServiceProvider(com.cloud.network.element.LoadBalancingServiceProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider) Provider(com.cloud.network.Network.Provider) NetworkElement(com.cloud.network.element.NetworkElement) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NicVO(com.cloud.vm.NicVO)

Example 43 with NetworkElement

use of com.cloud.network.element.NetworkElement in project cosmic by MissionCriticalCloud.

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.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, 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) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) LoadBalancingServiceProvider(com.cloud.network.element.LoadBalancingServiceProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider) Provider(com.cloud.network.Network.Provider) NetworkElement(com.cloud.network.element.NetworkElement) RedundantState(com.cloud.network.router.VirtualRouter.RedundantState) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) VMNetworkMapVO(com.cloud.engine.cloud.entity.api.db.VMNetworkMapVO) NicVO(com.cloud.vm.NicVO) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB)

Example 44 with NetworkElement

use of com.cloud.network.element.NetworkElement in project cosmic by MissionCriticalCloud.

the class NetworkOrchestrator method removeNic.

protected void removeNic(final VirtualMachineProfile vm, final NicVO nic) {
    if (nic.getReservationStrategy() == Nic.ReservationStrategy.Start && nic.getState() != Nic.State.Allocated) {
        // Ensure that release is performed before Nic is to be removed to avoid resource leaks.
        try {
            releaseNic(vm, nic.getId());
        } catch (final Exception ex) {
            s_logger.warn("Failed to release nic: " + nic.toString() + " as part of remove operation due to", ex);
        }
    }
    nic.setState(Nic.State.Deallocating);
    _nicDao.update(nic.getId(), nic);
    final NetworkVO network = _networksDao.findById(nic.getNetworkId());
    final NicProfile profile = new NicProfile(nic, network, null, null, null, _networkModel.getNetworkTag(vm.getHypervisorType(), network));
    /*
         * We need to release the nics with a Create ReservationStrategy here
         * because the nic is now being removed.
         */
    if (nic.getReservationStrategy() == Nic.ReservationStrategy.Create) {
        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 " + nic);
                }
                try {
                    element.release(network, profile, vm, null);
                } catch (final ConcurrentOperationException | ResourceUnavailableException ex) {
                    s_logger.warn("release failed during the nic " + nic.toString() + " removeNic due to ", ex);
                }
            }
        }
    }
    final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
    guru.deallocate(network, profile, vm);
    _nicDao.remove(nic.getId());
    s_logger.debug("Removed nic id=" + nic.getId());
    // remove the secondary ip addresses corresponding to to this nic
    if (!removeVmSecondaryIpsOfNic(nic.getId())) {
        s_logger.debug("Removing nic " + nic.getId() + " secondary ip addreses failed");
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkElement(com.cloud.network.element.NetworkElement) NetworkGuru(com.cloud.network.guru.NetworkGuru) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) NicProfile(com.cloud.vm.NicProfile) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConnectionException(com.cloud.exception.ConnectionException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) IllegalVirtualMachineException(com.cloud.exception.IllegalVirtualMachineException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) ConfigurationException(javax.naming.ConfigurationException) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider) LoadBalancingServiceProvider(com.cloud.network.element.LoadBalancingServiceProvider) StaticNatServiceProvider(com.cloud.network.element.StaticNatServiceProvider) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider) Provider(com.cloud.network.Network.Provider)

Example 45 with NetworkElement

use of com.cloud.network.element.NetworkElement in project cosmic by MissionCriticalCloud.

the class NetworkOrchestrator method getDhcpServiceProvider.

@Override
public DhcpServiceProvider getDhcpServiceProvider(final Network network) {
    final String DhcpProvider = _ntwkSrvcDao.getProviderForServiceInNetwork(network.getId(), Service.Dhcp);
    if (DhcpProvider == null) {
        s_logger.debug("Network " + network + " doesn't support service " + Service.Dhcp.getName());
        return null;
    }
    final NetworkElement element = _networkModel.getElementImplementingProvider(DhcpProvider);
    if (element instanceof DhcpServiceProvider) {
        return (DhcpServiceProvider) element;
    } else {
        return null;
    }
}
Also used : NetworkElement(com.cloud.network.element.NetworkElement) DhcpServiceProvider(com.cloud.network.element.DhcpServiceProvider)

Aggregations

NetworkElement (com.cloud.network.element.NetworkElement)69 Provider (com.cloud.network.Network.Provider)43 UserDataServiceProvider (com.cloud.network.element.UserDataServiceProvider)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)33 Service (com.cloud.network.Network.Service)29 StaticNatServiceProvider (com.cloud.network.element.StaticNatServiceProvider)27 DhcpServiceProvider (com.cloud.network.element.DhcpServiceProvider)25 LoadBalancingServiceProvider (com.cloud.network.element.LoadBalancingServiceProvider)25 UnsupportedServiceException (com.cloud.exception.UnsupportedServiceException)20 HashMap (java.util.HashMap)20 Map (java.util.Map)20 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)19 NetworkVO (com.cloud.network.dao.NetworkVO)17 ArrayList (java.util.ArrayList)17 ConfigDriveNetworkElement (com.cloud.network.element.ConfigDriveNetworkElement)16 NetworkGuru (com.cloud.network.guru.NetworkGuru)16 NicProfile (com.cloud.vm.NicProfile)16 Capability (com.cloud.network.Network.Capability)15 NicVO (com.cloud.vm.NicVO)13 NetworkOrchestrationService (org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService)13