Search in sources :

Example 1 with NetworkElement

use of com.cloud.network.element.NetworkElement in project cloudstack by apache.

the class NetworkModelTest method testCapabilityForProvider.

@Test
public void testCapabilityForProvider() {
    NetworkModelImpl modelImpl = spy(NetworkModelImpl.class);
    Set<Provider> providers = new HashSet<>();
    providers.add(Provider.NuageVsp);
    NetworkElement nuageVspElement = mock(NetworkElement.class);
    HashMap<Network.Service, Map<Network.Capability, String>> nuageVspCap = new HashMap<Network.Service, Map<Network.Capability, String>>();
    HashMap<Network.Capability, String> nuageVspConnectivity = new HashMap<Network.Capability, String>();
    nuageVspConnectivity.put(Network.Capability.NoVlan, "FindMe");
    nuageVspConnectivity.put(Network.Capability.PublicAccess, "");
    nuageVspCap.put(Network.Service.Connectivity, nuageVspConnectivity);
    when(nuageVspElement.getName()).thenReturn("NuageVsp");
    doReturn(nuageVspCap).when(nuageVspElement).getCapabilities();
    doReturn(nuageVspElement).when(modelImpl).getElementImplementingProvider("NuageVsp");
    try {
        modelImpl.checkCapabilityForProvider(providers, Network.Service.UserData, null, null);
        Assert.fail();
    } catch (UnsupportedServiceException e) {
        Assert.assertEquals(e.getMessage(), "Service " + Network.Service.UserData.getName() + " is not supported by the element=NuageVsp implementing Provider=" + Provider.NuageVsp.getName());
    }
    try {
        modelImpl.checkCapabilityForProvider(providers, Network.Service.Connectivity, Network.Capability.ElasticIp, null);
        Assert.fail();
    } catch (UnsupportedServiceException e) {
        Assert.assertEquals(e.getMessage(), "Service " + Network.Service.Connectivity.getName() + " doesn't have capability " + Network.Capability.ElasticIp.getName() + " for element=NuageVsp implementing Provider=" + Provider.NuageVsp.getName());
    }
    try {
        modelImpl.checkCapabilityForProvider(providers, Network.Service.Connectivity, Network.Capability.PublicAccess, "NonExistingVal");
        Assert.fail();
    } catch (UnsupportedServiceException e) {
        Assert.assertEquals(e.getMessage(), "Service Connectivity doesn't have capability PublicAccess for element=NuageVsp implementing Provider=NuageVsp");
    }
    modelImpl.checkCapabilityForProvider(providers, Network.Service.Connectivity, Network.Capability.NoVlan, "FindMe");
    NetworkElement nuageVspElement2 = mock(NetworkElement.class);
    doReturn(null).when(nuageVspElement).getCapabilities();
    try {
        modelImpl.checkCapabilityForProvider(providers, Network.Service.Connectivity, Network.Capability.PublicAccess, "");
        Assert.fail();
    } catch (UnsupportedServiceException e) {
        Assert.assertEquals(e.getMessage(), "Service Connectivity is not supported by the element=NuageVsp implementing Provider=NuageVsp");
    }
}
Also used : HashMap(java.util.HashMap) Provider(com.cloud.network.Network.Provider) NetworkElement(com.cloud.network.element.NetworkElement) UnsupportedServiceException(com.cloud.exception.UnsupportedServiceException) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with NetworkElement

use of com.cloud.network.element.NetworkElement in project cloudstack by apache.

the class NetworkOrchestrator method implementNetworkElementsAndResources.

@Override
public void implementNetworkElementsAndResources(final DeployDestination dest, final ReservationContext context, final Network network, final NetworkOffering offering) throws ConcurrentOperationException, InsufficientAddressCapacityException, ResourceUnavailableException, InsufficientCapacityException {
    // Associate a source NAT IP (if one isn't already associated with the network) if this is a
    // 1) 'Isolated' or 'Shared' guest virtual network in the advance zone
    // 2) network has sourceNat service
    // 3) network offering does not support a shared source NAT rule
    final boolean sharedSourceNat = offering.isSharedSourceNat();
    final DataCenter zone = _dcDao.findById(network.getDataCenterId());
    if (!sharedSourceNat && _networkModel.areServicesSupportedInNetwork(network.getId(), Service.SourceNat) && (network.getGuestType() == Network.GuestType.Isolated || network.getGuestType() == Network.GuestType.Shared && zone.getNetworkType() == NetworkType.Advanced)) {
        List<IPAddressVO> ips = null;
        final Account owner = _entityMgr.findById(Account.class, network.getAccountId());
        if (network.getVpcId() != null) {
            ips = _ipAddressDao.listByAssociatedVpc(network.getVpcId(), true);
            if (ips.isEmpty()) {
                final Vpc vpc = _vpcMgr.getActiveVpc(network.getVpcId());
                s_logger.debug("Creating a source nat ip for vpc " + vpc);
                _vpcMgr.assignSourceNatIpAddressToVpc(owner, vpc);
            }
        } else {
            ips = _ipAddressDao.listByAssociatedNetwork(network.getId(), true);
            if (ips.isEmpty()) {
                s_logger.debug("Creating a source nat ip for network " + network);
                _ipAddrMgr.assignSourceNatIpAddressToGuestNetwork(owner, network);
            }
        }
    }
    // get providers to implement
    final List<Provider> providersToImplement = getNetworkProviders(network.getId());
    implementNetworkElements(dest, context, network, offering, providersToImplement);
    // Reset the extra DHCP option that may have been cleared per nic.
    List<NicVO> nicVOs = _nicDao.listByNetworkId(network.getId());
    for (NicVO nicVO : nicVOs) {
        if (nicVO.getState() == Nic.State.Reserved) {
            configureExtraDhcpOptions(network, nicVO.getId());
        }
    }
    for (final NetworkElement element : networkElements) {
        if (element instanceof AggregatedCommandExecutor && providersToImplement.contains(element.getProvider())) {
            ((AggregatedCommandExecutor) element).prepareAggregatedExecution(network, dest);
        }
    }
    try {
        // reapply all the firewall/staticNat/lb rules
        s_logger.debug("Reprogramming network " + network + " as a part of network implement");
        if (!reprogramNetworkRules(network.getId(), CallContext.current().getCallingAccount(), network)) {
            s_logger.warn("Failed to re-program the network as a part of network " + network + " implement");
            // see DataCenterVO.java
            final ResourceUnavailableException ex = new ResourceUnavailableException("Unable to apply network rules as a part of network " + network + " implement", DataCenter.class, network.getDataCenterId());
            ex.addProxyObject(_entityMgr.findById(DataCenter.class, network.getDataCenterId()).getUuid());
            throw ex;
        }
        for (final NetworkElement element : networkElements) {
            if (element instanceof AggregatedCommandExecutor && providersToImplement.contains(element.getProvider())) {
                if (!((AggregatedCommandExecutor) element).completeAggregatedExecution(network, dest)) {
                    s_logger.warn("Failed to re-program the network as a part of network " + network + " implement due to aggregated commands execution failure!");
                    // see DataCenterVO.java
                    final ResourceUnavailableException ex = new ResourceUnavailableException("Unable to apply network rules as a part of network " + network + " implement", DataCenter.class, network.getDataCenterId());
                    ex.addProxyObject(_entityMgr.findById(DataCenter.class, network.getDataCenterId()).getUuid());
                    throw ex;
                }
            }
        }
    } finally {
        for (final NetworkElement element : networkElements) {
            if (element instanceof AggregatedCommandExecutor && providersToImplement.contains(element.getProvider())) {
                ((AggregatedCommandExecutor) element).cleanupAggregatedExecution(network, dest);
            }
        }
    }
}
Also used : Account(com.cloud.user.Account) AggregatedCommandExecutor(com.cloud.network.element.AggregatedCommandExecutor) Vpc(com.cloud.network.vpc.Vpc) 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) DataCenter(com.cloud.dc.DataCenter) NetworkElement(com.cloud.network.element.NetworkElement) ConfigDriveNetworkElement(com.cloud.network.element.ConfigDriveNetworkElement) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) IPAddressVO(com.cloud.network.dao.IPAddressVO) NicVO(com.cloud.vm.NicVO)

Example 3 with NetworkElement

use of com.cloud.network.element.NetworkElement 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 NetworkElement

use of com.cloud.network.element.NetworkElement 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)

Example 5 with NetworkElement

use of com.cloud.network.element.NetworkElement in project cloudstack by apache.

the class NetworkOrchestrator method setHypervisorHostname.

@Override
public void setHypervisorHostname(VirtualMachineProfile vm, DeployDestination dest, boolean migrationSuccessful) throws ResourceUnavailableException {
    final List<NicVO> nics = _nicDao.listByVmId(vm.getId());
    for (final NicVO nic : nics) {
        final NetworkVO network = _networksDao.findById(nic.getNetworkId());
        final Integer networkRate = _networkModel.getNetworkRate(network.getId(), vm.getId());
        final NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vm.getHypervisorType(), network));
        for (final NetworkElement element : networkElements) {
            if (_networkModel.areServicesSupportedInNetwork(network.getId(), Service.UserData) && element instanceof UserDataServiceProvider) {
                if (element instanceof ConfigDriveNetworkElement && !migrationSuccessful || element instanceof VirtualRouterElement && migrationSuccessful) {
                    final UserDataServiceProvider sp = (UserDataServiceProvider) element;
                    if (!sp.saveHypervisorHostname(profile, network, vm, dest)) {
                        throw new CloudRuntimeException("Failed to Add hypervisor hostname");
                    }
                }
            }
        }
    }
}
Also used : PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) NetworkElement(com.cloud.network.element.NetworkElement) ConfigDriveNetworkElement(com.cloud.network.element.ConfigDriveNetworkElement) VirtualRouterElement(com.cloud.network.element.VirtualRouterElement) ConfigDriveNetworkElement(com.cloud.network.element.ConfigDriveNetworkElement) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) NicProfile(com.cloud.vm.NicProfile) NicVO(com.cloud.vm.NicVO) UserDataServiceProvider(com.cloud.network.element.UserDataServiceProvider)

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