Search in sources :

Example 31 with NetworkGuru

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

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 InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, 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 = null;
    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.isSecurityGroupSupportedInNetwork(network), _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.setIPv6Cidr(profile.getIPv6Cidr());
        nic.setIPv6Gateway(profile.getIPv6Gateway());
        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.isSecurityGroupSupportedInNetwork(network), _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());
            }
        }
    }
    profile.setSecurityGroupEnabled(_networkModel.isSecurityGroupSupportedInNetwork(network));
    guru.updateNicProfile(profile, network);
    configureExtraDhcpOptions(network, nicId);
    return profile;
}
Also used : NetworkElement(com.cloud.network.element.NetworkElement) ConfigDriveNetworkElement(com.cloud.network.element.ConfigDriveNetworkElement) 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) 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)

Example 32 with NetworkGuru

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

the class NetworkOrchestrator method prepare.

@Override
public void prepare(final VirtualMachineProfile vmProfile, final DeployDestination dest, final ReservationContext context) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
    final List<NicVO> nics = _nicDao.listByVmId(vmProfile.getId());
    // we have to implement default nics first - to ensure that default network elements start up first in multiple
    // nics case
    // (need for setting DNS on Dhcp to domR's Ip4 address)
    Collections.sort(nics, new Comparator<NicVO>() {

        @Override
        public int compare(final NicVO nic1, final NicVO nic2) {
            final boolean isDefault1 = nic1.isDefaultNic();
            final boolean isDefault2 = nic2.isDefaultNic();
            return isDefault1 ^ isDefault2 ? isDefault1 ^ true ? 1 : -1 : 0;
        }
    });
    for (final NicVO nic : nics) {
        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());
        }
        final NetworkVO network = implemented.second();
        final NicProfile profile = prepareNic(vmProfile, dest, context, nic.getId(), network);
        vmProfile.addNic(profile);
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NetworkGuru(com.cloud.network.guru.NetworkGuru) NicProfile(com.cloud.vm.NicProfile) NicVO(com.cloud.vm.NicVO)

Example 33 with NetworkGuru

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

the class NetworkOrchestrator method rollbackNicForMigration.

@Override
public void rollbackNicForMigration(final VirtualMachineProfile src, final VirtualMachineProfile dst) {
    for (final NicProfile nicDst : dst.getNics()) {
        final NetworkVO network = _networksDao.findById(nicDst.getNetworkId());
        final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
        final NicProfile nicSrc = findNicProfileById(src, nicDst.getId());
        final ReservationContext src_context = new ReservationContextImpl(nicSrc.getReservationId(), null, null);
        final ReservationContext dst_context = new ReservationContextImpl(nicDst.getReservationId(), null, null);
        if (guru instanceof NetworkMigrationResponder) {
            ((NetworkMigrationResponder) guru).rollbackMigration(nicDst, network, dst, src_context, dst_context);
        }
        if (network.getGuestType() == Network.GuestType.L2 && src.getType() == VirtualMachine.Type.User) {
            _userVmMgr.setupVmForPvlan(true, dst.getVirtualMachine().getHostId(), nicDst);
        }
        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) {
                    ((NetworkMigrationResponder) element).rollbackMigration(nicDst, network, dst, src_context, dst_context);
                }
            }
        }
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkMigrationResponder(com.cloud.network.NetworkMigrationResponder) NetworkElement(com.cloud.network.element.NetworkElement) ConfigDriveNetworkElement(com.cloud.network.element.ConfigDriveNetworkElement) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) 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)

Example 34 with NetworkGuru

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

the class NetworkOrchestrator method implementNetwork.

@Override
@DB
public Pair<NetworkGuru, NetworkVO> implementNetwork(final long networkId, final DeployDestination dest, final ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    final Pair<NetworkGuru, NetworkVO> implemented = new Pair<NetworkGuru, NetworkVO>(null, null);
    NetworkVO network = _networksDao.findById(networkId);
    final NetworkGuru guru = AdapterBase.getAdapterByName(networkGurus, network.getGuruName());
    if (isNetworkImplemented(network)) {
        s_logger.debug("Network id=" + networkId + " is already implemented");
        implemented.set(guru, network);
        return implemented;
    }
    // Acquire lock only when network needs to be implemented
    network = _networksDao.acquireInLockTable(networkId, NetworkLockTimeout.value());
    if (network == null) {
        // see NetworkVO.java
        final ConcurrentOperationException ex = new ConcurrentOperationException("Unable to acquire network configuration");
        ex.addProxyObject(_entityMgr.findById(Network.class, networkId).getUuid());
        throw ex;
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Lock is acquired for network id " + networkId + " as a part of network implement");
    }
    try {
        if (isNetworkImplemented(network)) {
            s_logger.debug("Network id=" + networkId + " is already implemented");
            implemented.set(guru, network);
            return implemented;
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Asking " + guru.getName() + " to implement " + network);
        }
        final NetworkOfferingVO offering = _networkOfferingDao.findById(network.getNetworkOfferingId());
        network.setReservationId(context.getReservationId());
        if (isSharedNetworkWithServices(network)) {
            network.setState(Network.State.Implementing);
        } else {
            stateTransitTo(network, Event.ImplementNetwork);
        }
        final Network result = guru.implement(network, offering, dest, context);
        network.setCidr(result.getCidr());
        network.setBroadcastUri(result.getBroadcastUri());
        network.setGateway(result.getGateway());
        network.setMode(result.getMode());
        network.setPhysicalNetworkId(result.getPhysicalNetworkId());
        _networksDao.update(networkId, network);
        // implement network elements and re-apply all the network rules
        implementNetworkElementsAndResources(dest, context, network, offering);
        long dcId = dest.getDataCenter().getId();
        if (networkMeetsPersistenceCriteria(network, offering, false)) {
            setupPersistentNetwork(network, offering, dcId);
        }
        if (isSharedNetworkWithServices(network)) {
            network.setState(Network.State.Implemented);
        } else {
            stateTransitTo(network, Event.OperationSucceeded);
        }
        network.setRestartRequired(false);
        _networksDao.update(network.getId(), network);
        implemented.set(guru, network);
        return implemented;
    } catch (final NoTransitionException e) {
        s_logger.error(e.getMessage());
        return new Pair<NetworkGuru, NetworkVO>(null, null);
    } catch (final CloudRuntimeException | OperationTimedoutException e) {
        s_logger.error("Caught exception: " + e.getMessage());
        return new Pair<NetworkGuru, NetworkVO>(null, null);
    } finally {
        if (implemented.first() == null) {
            s_logger.debug("Cleaning up because we're unable to implement the network " + network);
            try {
                if (isSharedNetworkWithServices(network)) {
                    network.setState(Network.State.Shutdown);
                    _networksDao.update(networkId, network);
                } else {
                    stateTransitTo(network, Event.OperationFailed);
                }
            } catch (final NoTransitionException e) {
                s_logger.error(e.getMessage());
            }
            try {
                shutdownNetwork(networkId, context, false);
            } catch (final Exception e) {
                // Don't throw this exception as it would hide the original thrown exception, just log
                s_logger.error("Exception caught while shutting down a network as part of a failed implementation", e);
            }
        }
        _networksDao.releaseFromLockTable(networkId);
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Lock is released for network id " + networkId + " as a part of network implement");
        }
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) NetworkGuru(com.cloud.network.guru.NetworkGuru) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConnectionException(com.cloud.exception.ConnectionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB)

Example 35 with NetworkGuru

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

the class NetworkOrchestrator method startNetwork.

@Override
public boolean startNetwork(final long networkId, final DeployDestination dest, final ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    // Check if network exists
    final NetworkVO network = _networksDao.findById(networkId);
    if (network == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Network with specified id doesn't exist");
        ex.addProxyObject(String.valueOf(networkId), "networkId");
        throw ex;
    }
    // implement the network
    s_logger.debug("Starting network " + network + "...");
    final Pair<NetworkGuru, NetworkVO> implementedNetwork = implementNetwork(networkId, dest, context);
    if (implementedNetwork == null || implementedNetwork.first() == null) {
        s_logger.warn("Failed to start the network " + network);
        return false;
    } else {
        return true;
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) NetworkGuru(com.cloud.network.guru.NetworkGuru)

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