Search in sources :

Example 41 with Service

use of com.cloud.legacymodel.network.Network.Service in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method getIpToServices.

/* Get a list of IPs, classify them by service */
protected Map<PublicIp, Set<Service>> getIpToServices(final List<PublicIp> publicIps, final boolean rulesRevoked, final boolean includingFirewall) {
    final Map<PublicIp, Set<Service>> ipToServices = new HashMap<>();
    if (publicIps != null && !publicIps.isEmpty()) {
        final Set<Long> networkSNAT = new HashSet<>();
        for (final PublicIp ip : publicIps) {
            Set<Service> services = ipToServices.get(ip);
            if (services == null) {
                services = new HashSet<>();
            }
            if (ip.isSourceNat()) {
                if (!networkSNAT.contains(ip.getAssociatedWithNetworkId())) {
                    services.add(Service.SourceNat);
                    networkSNAT.add(ip.getAssociatedWithNetworkId());
                } else {
                    final CloudRuntimeException ex = new CloudRuntimeException("Multiple generic soure NAT IPs provided for network");
                    // see the IPAddressVO.java class.
                    final IPAddressVO ipAddr = ApiDBUtils.findIpAddressById(ip.getAssociatedWithNetworkId());
                    String ipAddrUuid = ip.getAssociatedWithNetworkId().toString();
                    if (ipAddr != null) {
                        ipAddrUuid = ipAddr.getUuid();
                    }
                    ex.addProxyObject(ipAddrUuid, "networkId");
                    throw ex;
                }
            }
            ipToServices.put(ip, services);
            // provider
            if (ip.getState() == State.Allocating) {
                continue;
            }
            // check if any active rules are applied on the public IP
            Set<Purpose> purposes = getPublicIpPurposeInRules(ip, false, includingFirewall);
            // Firewall rules didn't cover static NAT
            if (ip.isOneToOneNat() && ip.getAssociatedWithVmId() != null) {
                if (purposes == null) {
                    purposes = new HashSet<>();
                }
                purposes.add(Purpose.StaticNat);
            }
            if (purposes == null || purposes.isEmpty()) {
                // since no active rules are there check if any rules are applied on the public IP but are in
                // revoking state
                purposes = getPublicIpPurposeInRules(ip, true, includingFirewall);
                if (ip.isOneToOneNat()) {
                    if (purposes == null) {
                        purposes = new HashSet<>();
                    }
                    purposes.add(Purpose.StaticNat);
                }
                if (purposes == null || purposes.isEmpty()) {
                    // IP is not being used for any purpose so skip IPAssoc to network service provider
                    continue;
                } else {
                    if (rulesRevoked) {
                        // no active rules/revoked rules are associated with this public IP, so remove the
                        // association with the provider
                        ip.setState(State.Releasing);
                    } else {
                        if (ip.getState() == State.Releasing) {
                            // rules are not revoked yet, so don't let the network service provider revoke the IP
                            // association
                            // mark IP is allocated so that IP association will not be removed from the provider
                            ip.setState(State.Allocated);
                        }
                    }
                }
            }
            if (purposes.contains(Purpose.StaticNat)) {
                services.add(Service.StaticNat);
            }
            if (purposes.contains(Purpose.LoadBalancing)) {
                services.add(Service.Lb);
            }
            if (purposes.contains(Purpose.PortForwarding)) {
                services.add(Service.PortForwarding);
            }
            if (purposes.contains(Purpose.Vpn)) {
                services.add(Service.Vpn);
            }
            if (purposes.contains(Purpose.Firewall)) {
                services.add(Service.Firewall);
            }
            if (services.isEmpty()) {
                continue;
            }
            ipToServices.put(ip, services);
        }
    }
    return ipToServices;
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) PublicIp(com.cloud.network.addr.PublicIp) HashMap(java.util.HashMap) NetworkOrchestrationService(com.cloud.engine.orchestration.service.NetworkOrchestrationService) LoadBalancingRulesService(com.cloud.network.lb.LoadBalancingRulesService) Service(com.cloud.legacymodel.network.Network.Service) ResourceLimitService(com.cloud.user.ResourceLimitService) Purpose(com.cloud.legacymodel.network.FirewallRule.Purpose) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) IPAddressVO(com.cloud.network.dao.IPAddressVO) HashSet(java.util.HashSet)

Example 42 with Service

use of com.cloud.legacymodel.network.Network.Service in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method canIpsUseOffering.

private boolean canIpsUseOffering(final List<PublicIp> publicIps, final long offeringId) {
    final Map<PublicIp, Set<Service>> ipToServices = getIpToServices(publicIps, false, true);
    final Map<Service, Set<Provider>> serviceToProviders = _networkModel.getNetworkOfferingServiceProvidersMap(offeringId);
    final NetworkOfferingVO offering = _networkOfferingDao.findById(offeringId);
    // For inline mode checking, using firewall provider for LB instead, because public ip would apply on firewall provider
    if (offering.isInline()) {
        Provider firewallProvider = null;
        if (serviceToProviders.containsKey(Service.Firewall)) {
            firewallProvider = (Provider) serviceToProviders.get(Service.Firewall).toArray()[0];
        }
        final Set<Provider> p = new HashSet<>();
        p.add(firewallProvider);
        serviceToProviders.remove(Service.Lb);
        serviceToProviders.put(Service.Lb, p);
    }
    for (final PublicIp ip : ipToServices.keySet()) {
        final Set<Service> services = ipToServices.get(ip);
        Provider provider = null;
        for (final Service service : services) {
            final Set<Provider> curProviders = serviceToProviders.get(service);
            if (curProviders == null || curProviders.isEmpty()) {
                continue;
            }
            final Provider curProvider = (Provider) curProviders.toArray()[0];
            if (provider == null) {
                provider = curProvider;
                continue;
            }
            // We don't support multiple providers for one service now
            if (!provider.equals(curProvider)) {
                throw new InvalidParameterException("There would be multiple providers for IP " + ip.getAddress() + " with the new network offering!");
            }
        }
    }
    return true;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) ResultSet(java.sql.ResultSet) Set(java.util.Set) SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) PublicIp(com.cloud.network.addr.PublicIp) NetworkOrchestrationService(com.cloud.engine.orchestration.service.NetworkOrchestrationService) LoadBalancingRulesService(com.cloud.network.lb.LoadBalancingRulesService) Service(com.cloud.legacymodel.network.Network.Service) ResourceLimitService(com.cloud.user.ResourceLimitService) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) Provider(com.cloud.legacymodel.network.Network.Provider) HashSet(java.util.HashSet)

Example 43 with Service

use of com.cloud.legacymodel.network.Network.Service in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method updateNetworkServiceProvider.

@Override
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_PROVIDER_UPDATE, eventDescription = "Updating physical network ServiceProvider", async = true)
public PhysicalNetworkServiceProvider updateNetworkServiceProvider(final Long id, final String stateStr, final List<String> enabledServices) {
    final PhysicalNetworkServiceProviderVO provider = _pNSPDao.findById(id);
    if (provider == null) {
        throw new InvalidParameterValueException("Network Service Provider id=" + id + "doesn't exist in the system");
    }
    final NetworkElement element = _networkModel.getElementImplementingProvider(provider.getProviderName());
    if (element == null) {
        throw new InvalidParameterValueException("Unable to find the Network Element implementing the Service Provider '" + provider.getProviderName() + "'");
    }
    PhysicalNetworkServiceProvider.State state = null;
    if (stateStr != null && !stateStr.isEmpty()) {
        try {
            state = PhysicalNetworkServiceProvider.State.valueOf(stateStr);
        } catch (final IllegalArgumentException ex) {
            throw new InvalidParameterValueException("Unable to resolve state '" + stateStr + "' to a supported value {Enabled or Disabled}");
        }
    }
    boolean update = false;
    if (state != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("trying to update the state of the service provider id=" + id + " on physical network: " + provider.getPhysicalNetworkId() + " to state: " + stateStr);
        }
        switch(state) {
            case Enabled:
                if (element != null && element.isReady(provider)) {
                    provider.setState(PhysicalNetworkServiceProvider.State.Enabled);
                    update = true;
                } else {
                    throw new CloudRuntimeException("Provider is not ready, cannot Enable the provider, please configure the provider first");
                }
                break;
            case Disabled:
                // do we need to do anything for the provider instances before disabling?
                provider.setState(PhysicalNetworkServiceProvider.State.Disabled);
                update = true;
                break;
            case Shutdown:
                throw new InvalidParameterValueException("Updating the provider state to 'Shutdown' is not supported");
        }
    }
    if (enabledServices != null) {
        // check if services can be turned of
        if (!element.canEnableIndividualServices()) {
            throw new InvalidParameterValueException("Cannot update set of Services for this Service Provider '" + provider.getProviderName() + "'");
        }
        // validate Services
        final List<Service> services = new ArrayList<>();
        for (final String serviceName : enabledServices) {
            final Network.Service service = Network.Service.getService(serviceName);
            if (service == null) {
                throw new InvalidParameterValueException("Invalid Network Service specified=" + serviceName);
            }
            services.add(service);
        }
        // set enabled services
        provider.setEnabledServices(services);
        update = true;
    }
    if (update) {
        _pNSPDao.update(id, provider);
    }
    return provider;
}
Also used : ArrayList(java.util.ArrayList) NetworkOrchestrationService(com.cloud.engine.orchestration.service.NetworkOrchestrationService) LoadBalancingRulesService(com.cloud.network.lb.LoadBalancingRulesService) Service(com.cloud.legacymodel.network.Network.Service) ResourceLimitService(com.cloud.user.ResourceLimitService) Service(com.cloud.legacymodel.network.Network.Service) NetworkElement(com.cloud.network.element.NetworkElement) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.cloud.legacymodel.network.Network) ActionEvent(com.cloud.event.ActionEvent)

Example 44 with Service

use of com.cloud.legacymodel.network.Network.Service in project cosmic by MissionCriticalCloud.

the class NetworkServiceImpl method addProviderToPhysicalNetwork.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_SERVICE_PROVIDER_CREATE, eventDescription = "Creating Physical Network ServiceProvider", create = true)
public PhysicalNetworkServiceProvider addProviderToPhysicalNetwork(final Long physicalNetworkId, final String providerName, final Long destinationPhysicalNetworkId, final List<String> enabledServices) {
    // verify input parameters
    final PhysicalNetworkVO network = _physicalNetworkDao.findById(physicalNetworkId);
    if (network == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Physical Network with specified id doesn't exist in the system");
        ex.addProxyObject(physicalNetworkId.toString(), "physicalNetworkId");
        throw ex;
    }
    // verify input parameters
    if (destinationPhysicalNetworkId != null) {
        final PhysicalNetworkVO destNetwork = _physicalNetworkDao.findById(destinationPhysicalNetworkId);
        if (destNetwork == null) {
            final InvalidParameterValueException ex = new InvalidParameterValueException("Destination Physical Network with specified id doesn't exist in the system");
            ex.addProxyObject(destinationPhysicalNetworkId.toString(), "destinationPhysicalNetworkId");
            throw ex;
        }
    }
    if (providerName != null) {
        final Provider provider = Network.Provider.getProvider(providerName);
        if (provider == null) {
            throw new InvalidParameterValueException("Invalid Network Service Provider=" + providerName);
        }
    }
    if (_pNSPDao.findByServiceProvider(physicalNetworkId, providerName) != null) {
        // TBD: send uuid instead of physicalNetworkId.
        throw new CloudRuntimeException("The '" + providerName + "' provider already exists on physical network : " + physicalNetworkId);
    }
    // check if services can be turned off
    final NetworkElement element = _networkModel.getElementImplementingProvider(providerName);
    if (element == null) {
        throw new InvalidParameterValueException("Unable to find the Network Element implementing the Service Provider '" + providerName + "'");
    }
    List<Service> services = new ArrayList<>();
    if (enabledServices != null) {
        if (!element.canEnableIndividualServices()) {
            if (enabledServices.size() != element.getCapabilities().keySet().size()) {
                throw new InvalidParameterValueException("Cannot enable subset of Services, Please specify the complete list of Services for this Service Provider '" + providerName + "'");
            }
        }
        // validate Services
        boolean addGatewayService = false;
        for (final String serviceName : enabledServices) {
            final Network.Service service = Network.Service.getService(serviceName);
            if (service == null || service == Service.Gateway) {
                throw new InvalidParameterValueException("Invalid Network Service specified=" + serviceName);
            } else if (service == Service.SourceNat) {
                addGatewayService = true;
            }
            // check if the service is provided by this Provider
            if (!element.getCapabilities().containsKey(service)) {
                throw new InvalidParameterValueException(providerName + " Provider cannot provide this Service specified=" + serviceName);
            }
            services.add(service);
        }
        if (addGatewayService) {
            services.add(Service.Gateway);
        }
    } else {
        // enable all the default services supported by this element.
        services = new ArrayList<>(element.getCapabilities().keySet());
    }
    try {
        // Create the new physical network in the database
        PhysicalNetworkServiceProviderVO nsp = new PhysicalNetworkServiceProviderVO(physicalNetworkId, providerName);
        // set enabled services
        nsp.setEnabledServices(services);
        if (destinationPhysicalNetworkId != null) {
            nsp.setDestinationPhysicalNetworkId(destinationPhysicalNetworkId);
        }
        nsp = _pNSPDao.persist(nsp);
        return nsp;
    } catch (final Exception ex) {
        s_logger.warn("Exception: ", ex);
        throw new CloudRuntimeException("Fail to add a provider to physical network");
    }
}
Also used : ArrayList(java.util.ArrayList) NetworkOrchestrationService(com.cloud.engine.orchestration.service.NetworkOrchestrationService) LoadBalancingRulesService(com.cloud.network.lb.LoadBalancingRulesService) Service(com.cloud.legacymodel.network.Network.Service) ResourceLimitService(com.cloud.user.ResourceLimitService) Service(com.cloud.legacymodel.network.Network.Service) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) SQLException(java.sql.SQLException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) UnknownHostException(java.net.UnknownHostException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) UnsupportedServiceException(com.cloud.legacymodel.exceptions.UnsupportedServiceException) ConfigurationException(javax.naming.ConfigurationException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Provider(com.cloud.legacymodel.network.Network.Provider) NetworkElement(com.cloud.network.element.NetworkElement) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) Network(com.cloud.legacymodel.network.Network) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 45 with Service

use of com.cloud.legacymodel.network.Network.Service in project cosmic by MissionCriticalCloud.

the class VpcManagerImpl method validateConnectivtyServiceCapabilities.

private void validateConnectivtyServiceCapabilities(final Set<Provider> providers, final Map serviceCapabilitystList) {
    if (serviceCapabilitystList != null && !serviceCapabilitystList.isEmpty()) {
        final Collection serviceCapabilityCollection = serviceCapabilitystList.values();
        final Iterator iter = serviceCapabilityCollection.iterator();
        while (iter.hasNext()) {
            final HashMap<String, String> svcCapabilityMap = (HashMap<String, String>) iter.next();
            Capability capability = null;
            final String svc = svcCapabilityMap.get(SERVICE);
            final String capabilityName = svcCapabilityMap.get(CAPABILITYTYPE);
            final String capabilityValue = svcCapabilityMap.get(CAPABILITYVALUE);
            if (capabilityName != null) {
                capability = Capability.getCapability(capabilityName);
            }
            if (capability == null || capabilityValue == null) {
                throw new InvalidParameterValueException("Invalid capability:" + capabilityName + " capability value:" + capabilityValue);
            }
            final Service usedService = Service.getService(svc);
            checkCapabilityPerServiceProvider(providers, capability, usedService);
            if (!capabilityValue.equalsIgnoreCase(TRUE_VALUE) && !capabilityValue.equalsIgnoreCase(FALSE_VALUE)) {
                throw new InvalidParameterValueException("Invalid Capability value:" + capabilityValue + " specified.");
            }
        }
    }
}
Also used : Capability(com.cloud.legacymodel.network.Network.Capability) HashMap(java.util.HashMap) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Iterator(java.util.Iterator) Collection(java.util.Collection) NetworkOrchestrationService(com.cloud.engine.orchestration.service.NetworkOrchestrationService) NetworkService(com.cloud.network.NetworkService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Service(com.cloud.legacymodel.network.Network.Service) ResourceLimitService(com.cloud.user.ResourceLimitService) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

Service (com.cloud.legacymodel.network.Network.Service)62 HashMap (java.util.HashMap)31 NetworkOrchestrationService (com.cloud.engine.orchestration.service.NetworkOrchestrationService)29 Provider (com.cloud.legacymodel.network.Network.Provider)29 ArrayList (java.util.ArrayList)29 HashSet (java.util.HashSet)29 ResourceLimitService (com.cloud.user.ResourceLimitService)27 Set (java.util.Set)24 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)18 Map (java.util.Map)15 Network (com.cloud.legacymodel.network.Network)14 NetworkElement (com.cloud.network.element.NetworkElement)14 NetworkService (com.cloud.network.NetworkService)13 SortedSet (java.util.SortedSet)12 Capability (com.cloud.legacymodel.network.Network.Capability)11 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)11 Test (org.junit.Test)11 UserDataServiceProvider (com.cloud.network.element.UserDataServiceProvider)10 ExecutorService (java.util.concurrent.ExecutorService)10 NetworkOfferingVO (com.cloud.offerings.NetworkOfferingVO)9