Search in sources :

Example 31 with PhysicalNetwork

use of com.cloud.network.PhysicalNetwork in project cloudstack by apache.

the class NetworkOrchestrator method createGuestNetwork.

@Override
@DB
public Network createGuestNetwork(final long networkOfferingId, final String name, final String displayText, final String gateway, final String cidr, String vlanId, String networkDomain, final Account owner, final Long domainId, final PhysicalNetwork pNtwk, final long zoneId, final ACLType aclType, Boolean subdomainAccess, final Long vpcId, final String ip6Gateway, final String ip6Cidr, final Boolean isDisplayNetworkEnabled, final String isolatedPvlan) throws ConcurrentOperationException, InsufficientCapacityException, ResourceAllocationException {
    final NetworkOfferingVO ntwkOff = _networkOfferingDao.findById(networkOfferingId);
    // this method supports only guest network creation
    if (ntwkOff.getTrafficType() != TrafficType.Guest) {
        s_logger.warn("Only guest networks can be created using this method");
        return null;
    }
    final boolean updateResourceCount = resourceCountNeedsUpdate(ntwkOff, aclType);
    //check resource limits
    if (updateResourceCount) {
        _resourceLimitMgr.checkResourceLimit(owner, ResourceType.network, isDisplayNetworkEnabled);
    }
    // Validate network offering
    if (ntwkOff.getState() != NetworkOffering.State.Enabled) {
        // see NetworkOfferingVO
        final InvalidParameterValueException ex = new InvalidParameterValueException("Can't use specified network offering id as its stat is not " + NetworkOffering.State.Enabled);
        ex.addProxyObject(ntwkOff.getUuid(), "networkOfferingId");
        throw ex;
    }
    // Validate physical network
    if (pNtwk.getState() != PhysicalNetwork.State.Enabled) {
        // see PhysicalNetworkVO.java
        final InvalidParameterValueException ex = new InvalidParameterValueException("Specified physical network id is" + " in incorrect state:" + pNtwk.getState());
        ex.addProxyObject(pNtwk.getUuid(), "physicalNetworkId");
        throw ex;
    }
    boolean ipv6 = false;
    if (ip6Gateway != null && ip6Cidr != null) {
        ipv6 = true;
    }
    // Validate zone
    final DataCenterVO zone = _dcDao.findById(zoneId);
    if (zone.getNetworkType() == NetworkType.Basic) {
        if (ipv6) {
            throw new InvalidParameterValueException("IPv6 is not supported in Basic zone");
        }
        // In Basic zone the network should have aclType=Domain, domainId=1, subdomainAccess=true
        if (aclType == null || aclType != ACLType.Domain) {
            throw new InvalidParameterValueException("Only AclType=Domain can be specified for network creation in Basic zone");
        }
        // Only one guest network is supported in Basic zone
        final List<NetworkVO> guestNetworks = _networksDao.listByZoneAndTrafficType(zone.getId(), TrafficType.Guest);
        if (!guestNetworks.isEmpty()) {
            throw new InvalidParameterValueException("Can't have more than one Guest network in zone with network type " + NetworkType.Basic);
        }
        // if zone is basic, only Shared network offerings w/o source nat service are allowed
        if (!(ntwkOff.getGuestType() == GuestType.Shared && !_networkModel.areServicesSupportedByNetworkOffering(ntwkOff.getId(), Service.SourceNat))) {
            throw new InvalidParameterValueException("For zone of type " + NetworkType.Basic + " only offerings of " + "guestType " + GuestType.Shared + " with disabled " + Service.SourceNat.getName() + " service are allowed");
        }
        if (domainId == null || domainId != Domain.ROOT_DOMAIN) {
            throw new InvalidParameterValueException("Guest network in Basic zone should be dedicated to ROOT domain");
        }
        if (subdomainAccess == null) {
            subdomainAccess = true;
        } else if (!subdomainAccess) {
            throw new InvalidParameterValueException("Subdomain access should be set to true for the" + " guest network in the Basic zone");
        }
        if (vlanId == null) {
            vlanId = Vlan.UNTAGGED;
        } else {
            if (!vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) {
                throw new InvalidParameterValueException("Only vlan " + Vlan.UNTAGGED + " can be created in " + "the zone of type " + NetworkType.Basic);
            }
        }
    } else if (zone.getNetworkType() == NetworkType.Advanced) {
        if (zone.isSecurityGroupEnabled()) {
            if (ipv6) {
                throw new InvalidParameterValueException("IPv6 is not supported with security group!");
            }
            if (isolatedPvlan != null) {
                throw new InvalidParameterValueException("Isolated Private VLAN is not supported with security group!");
            }
            // enabled zone
            if (ntwkOff.getGuestType() != GuestType.Shared) {
                throw new InvalidParameterValueException("Only shared guest network can be created in security group enabled zone");
            }
            if (_networkModel.areServicesSupportedByNetworkOffering(ntwkOff.getId(), Service.SourceNat)) {
                throw new InvalidParameterValueException("Service SourceNat is not allowed in security group enabled zone");
            }
            if (!_networkModel.areServicesSupportedByNetworkOffering(ntwkOff.getId(), Service.SecurityGroup)) {
                throw new InvalidParameterValueException("network must have SecurityGroup provider in security group enabled zone");
            }
        }
        //don't allow eip/elb networks in Advance zone
        if (ntwkOff.getElasticIp() || ntwkOff.getElasticLb()) {
            throw new InvalidParameterValueException("Elastic IP and Elastic LB services are supported in zone of type " + NetworkType.Basic);
        }
    }
    //TODO(VXLAN): Support VNI specified
    // VlanId can be specified only when network offering supports it
    final boolean vlanSpecified = vlanId != null;
    if (vlanSpecified != ntwkOff.getSpecifyVlan()) {
        if (vlanSpecified) {
            throw new InvalidParameterValueException("Can't specify vlan; corresponding offering says specifyVlan=false");
        } else {
            throw new InvalidParameterValueException("Vlan has to be specified; corresponding offering says specifyVlan=true");
        }
    }
    if (vlanSpecified) {
        //don't allow to specify vlan tag used by physical network for dynamic vlan allocation
        if (_dcDao.findVnet(zoneId, pNtwk.getId(), vlanId).size() > 0) {
            throw new InvalidParameterValueException("The VLAN tag " + vlanId + " is already being used for dynamic vlan allocation for the guest network in zone " + zone.getName());
        }
        if (!UuidUtils.validateUUID(vlanId)) {
            final String uri = BroadcastDomainType.fromString(vlanId).toString();
            // For Isolated networks, don't allow to create network with vlan that already exists in the zone
            if (ntwkOff.getGuestType() == GuestType.Isolated) {
                if (_networksDao.countByZoneAndUri(zoneId, uri) > 0) {
                    throw new InvalidParameterValueException("Network with vlan " + vlanId + " already exists in zone " + zoneId);
                } else {
                    final List<DataCenterVnetVO> dcVnets = _datacenterVnetDao.findVnet(zoneId, vlanId.toString());
                    //the vnet is not coming from the data center vnet table, so the list can be empty
                    if (!dcVnets.isEmpty()) {
                        final DataCenterVnetVO dcVnet = dcVnets.get(0);
                        // Fail network creation if specified vlan is dedicated to a different account
                        if (dcVnet.getAccountGuestVlanMapId() != null) {
                            final Long accountGuestVlanMapId = dcVnet.getAccountGuestVlanMapId();
                            final AccountGuestVlanMapVO map = _accountGuestVlanMapDao.findById(accountGuestVlanMapId);
                            if (map.getAccountId() != owner.getAccountId()) {
                                throw new InvalidParameterValueException("Vlan " + vlanId + " is dedicated to a different account");
                            }
                        // Fail network creation if owner has a dedicated range of vlans but the specified vlan belongs to the system pool
                        } else {
                            final List<AccountGuestVlanMapVO> maps = _accountGuestVlanMapDao.listAccountGuestVlanMapsByAccount(owner.getAccountId());
                            if (maps != null && !maps.isEmpty()) {
                                final int vnetsAllocatedToAccount = _datacenterVnetDao.countVnetsAllocatedToAccount(zoneId, owner.getAccountId());
                                final int vnetsDedicatedToAccount = _datacenterVnetDao.countVnetsDedicatedToAccount(zoneId, owner.getAccountId());
                                if (vnetsAllocatedToAccount < vnetsDedicatedToAccount) {
                                    throw new InvalidParameterValueException("Specified vlan " + vlanId + " doesn't belong" + " to the vlan range dedicated to the owner " + owner.getAccountName());
                                }
                            }
                        }
                    }
                }
            } else {
                // shared network with same Vlan ID in the zone
                if (_networksDao.countByZoneUriAndGuestType(zoneId, uri, GuestType.Isolated) > 0) {
                    throw new InvalidParameterValueException("There is a isolated/shared network with vlan id: " + vlanId + " already exists " + "in zone " + zoneId);
                }
            }
        }
    }
    // If networkDomain is not specified, take it from the global configuration
    if (_networkModel.areServicesSupportedByNetworkOffering(networkOfferingId, Service.Dns)) {
        final Map<Network.Capability, String> dnsCapabilities = _networkModel.getNetworkOfferingServiceCapabilities(_entityMgr.findById(NetworkOffering.class, networkOfferingId), Service.Dns);
        final String isUpdateDnsSupported = dnsCapabilities.get(Capability.AllowDnsSuffixModification);
        if (isUpdateDnsSupported == null || !Boolean.valueOf(isUpdateDnsSupported)) {
            if (networkDomain != null) {
                // TBD: NetworkOfferingId and zoneId. Send uuids instead.
                throw new InvalidParameterValueException("Domain name change is not supported by network offering id=" + networkOfferingId + " in zone id=" + zoneId);
            }
        } else {
            if (networkDomain == null) {
                // 1) Get networkDomain from the corresponding account/domain/zone
                if (aclType == ACLType.Domain) {
                    networkDomain = _networkModel.getDomainNetworkDomain(domainId, zoneId);
                } else if (aclType == ACLType.Account) {
                    networkDomain = _networkModel.getAccountNetworkDomain(owner.getId(), zoneId);
                }
                // 2) If null, generate networkDomain using domain suffix from the global config variables
                if (networkDomain == null) {
                    networkDomain = "cs" + Long.toHexString(owner.getId()) + GuestDomainSuffix.valueIn(zoneId);
                }
            } else {
                // validate network domain
                if (!NetUtils.verifyDomainName(networkDomain)) {
                    throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain " + "label must be between 1 and 63 characters long, can contain ASCII letters 'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
                }
            }
        }
    }
    // In Advance zone Cidr for Shared networks and Isolated networks w/o source nat service can't be NULL - 2.2.x
    // limitation, remove after we introduce support for multiple ip ranges
    // with different Cidrs for the same Shared network
    final boolean cidrRequired = zone.getNetworkType() == NetworkType.Advanced && ntwkOff.getTrafficType() == TrafficType.Guest && (ntwkOff.getGuestType() == GuestType.Shared || ntwkOff.getGuestType() == GuestType.Isolated && !_networkModel.areServicesSupportedByNetworkOffering(ntwkOff.getId(), Service.SourceNat));
    if (cidr == null && ip6Cidr == null && cidrRequired) {
        throw new InvalidParameterValueException("StartIp/endIp/gateway/netmask are required when create network of" + " type " + Network.GuestType.Shared + " and network of type " + GuestType.Isolated + " with service " + Service.SourceNat.getName() + " disabled");
    }
    // No cidr can be specified in Basic zone
    if (zone.getNetworkType() == NetworkType.Basic && cidr != null) {
        throw new InvalidParameterValueException("StartIp/endIp/gateway/netmask can't be specified for zone of type " + NetworkType.Basic);
    }
    // Check if cidr is RFC1918 compliant if the network is Guest Isolated for IPv4
    if (cidr != null && ntwkOff.getGuestType() == Network.GuestType.Isolated && ntwkOff.getTrafficType() == TrafficType.Guest) {
        if (!NetUtils.validateGuestCidr(cidr)) {
            throw new InvalidParameterValueException("Virtual Guest Cidr " + cidr + " is not RFC 1918 or 6598 compliant");
        }
    }
    final String networkDomainFinal = networkDomain;
    final String vlanIdFinal = vlanId;
    final Boolean subdomainAccessFinal = subdomainAccess;
    final Network network = Transaction.execute(new TransactionCallback<Network>() {

        @Override
        public Network doInTransaction(final TransactionStatus status) {
            Long physicalNetworkId = null;
            if (pNtwk != null) {
                physicalNetworkId = pNtwk.getId();
            }
            final DataCenterDeployment plan = new DataCenterDeployment(zoneId, null, null, null, null, physicalNetworkId);
            final NetworkVO userNetwork = new NetworkVO();
            userNetwork.setNetworkDomain(networkDomainFinal);
            if (cidr != null && gateway != null) {
                userNetwork.setCidr(cidr);
                userNetwork.setGateway(gateway);
            }
            if (ip6Cidr != null && ip6Gateway != null) {
                userNetwork.setIp6Cidr(ip6Cidr);
                userNetwork.setIp6Gateway(ip6Gateway);
            }
            if (vlanIdFinal != null) {
                if (isolatedPvlan == null) {
                    URI uri = null;
                    if (UuidUtils.validateUUID(vlanIdFinal)) {
                        //Logical router's UUID provided as VLAN_ID
                        //Set transient field
                        userNetwork.setVlanIdAsUUID(vlanIdFinal);
                    } else {
                        uri = BroadcastDomainType.fromString(vlanIdFinal);
                    }
                    userNetwork.setBroadcastUri(uri);
                    if (!vlanIdFinal.equalsIgnoreCase(Vlan.UNTAGGED)) {
                        userNetwork.setBroadcastDomainType(BroadcastDomainType.Vlan);
                    } else {
                        userNetwork.setBroadcastDomainType(BroadcastDomainType.Native);
                    }
                } else {
                    if (vlanIdFinal.equalsIgnoreCase(Vlan.UNTAGGED)) {
                        throw new InvalidParameterValueException("Cannot support pvlan with untagged primary vlan!");
                    }
                    userNetwork.setBroadcastUri(NetUtils.generateUriForPvlan(vlanIdFinal, isolatedPvlan));
                    userNetwork.setBroadcastDomainType(BroadcastDomainType.Pvlan);
                }
            }
            final List<? extends Network> networks = setupNetwork(owner, ntwkOff, userNetwork, plan, name, displayText, true, domainId, aclType, subdomainAccessFinal, vpcId, isDisplayNetworkEnabled);
            Network network = null;
            if (networks == null || networks.isEmpty()) {
                throw new CloudRuntimeException("Fail to create a network");
            } else {
                if (networks.size() > 0 && networks.get(0).getGuestType() == Network.GuestType.Isolated && networks.get(0).getTrafficType() == TrafficType.Guest) {
                    Network defaultGuestNetwork = networks.get(0);
                    for (final Network nw : networks) {
                        if (nw.getCidr() != null && nw.getCidr().equals(zone.getGuestNetworkCidr())) {
                            defaultGuestNetwork = nw;
                        }
                    }
                    network = defaultGuestNetwork;
                } else {
                    // For shared network
                    network = networks.get(0);
                }
            }
            if (updateResourceCount) {
                _resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.network, isDisplayNetworkEnabled);
            }
            return network;
        }
    });
    CallContext.current().setEventDetails("Network Id: " + network.getId());
    CallContext.current().putContextParameter(Network.class, network.getUuid());
    return network;
}
Also used : AccountGuestVlanMapVO(com.cloud.network.dao.AccountGuestVlanMapVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) URI(java.net.URI) DataCenterVnetVO(com.cloud.dc.DataCenterVnetVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ArrayList(java.util.ArrayList) List(java.util.List) DataCenterVO(com.cloud.dc.DataCenterVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) DataCenterDeployment(com.cloud.deploy.DataCenterDeployment) Capability(com.cloud.network.Network.Capability) NetworkOffering(com.cloud.offering.NetworkOffering) NetworkOfferingVO(com.cloud.offerings.NetworkOfferingVO) DB(com.cloud.utils.db.DB)

Example 32 with PhysicalNetwork

use of com.cloud.network.PhysicalNetwork in project cloudstack by apache.

the class ListPhysicalNetworksCmd method execute.

@Override
public void execute() {
    Pair<List<? extends PhysicalNetwork>, Integer> result = _networkService.searchPhysicalNetworks(getId(), getZoneId(), this.getKeyword(), this.getStartIndex(), this.getPageSizeVal(), getNetworkName());
    if (result != null) {
        ListResponse<PhysicalNetworkResponse> response = new ListResponse<PhysicalNetworkResponse>();
        List<PhysicalNetworkResponse> networkResponses = new ArrayList<PhysicalNetworkResponse>();
        for (PhysicalNetwork network : result.first()) {
            PhysicalNetworkResponse networkResponse = _responseGenerator.createPhysicalNetworkResponse(network);
            networkResponses.add(networkResponse);
        }
        response.setResponses(networkResponses, result.second());
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to search for physical networks");
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) PhysicalNetworkResponse(org.apache.cloudstack.api.response.PhysicalNetworkResponse) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 33 with PhysicalNetwork

use of com.cloud.network.PhysicalNetwork in project cloudstack by apache.

the class UpdatePhysicalNetworkCmd method execute.

@Override
public void execute() {
    PhysicalNetwork result = _networkService.updatePhysicalNetwork(getId(), getNetworkSpeed(), getTags(), getVlan(), getState());
    if (result != null) {
        PhysicalNetworkResponse response = _responseGenerator.createPhysicalNetworkResponse(result);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    }
}
Also used : PhysicalNetworkResponse(org.apache.cloudstack.api.response.PhysicalNetworkResponse) PhysicalNetwork(com.cloud.network.PhysicalNetwork)

Example 34 with PhysicalNetwork

use of com.cloud.network.PhysicalNetwork in project cloudstack by apache.

the class CreatePhysicalNetworkCmd method create.

@Override
public void create() throws ResourceAllocationException {
    PhysicalNetwork result = _networkService.createPhysicalNetwork(getZoneId(), getVlan(), getNetworkSpeed(), getIsolationMethods(), getBroadcastDomainRange(), getDomainId(), getTags(), getNetworkName());
    if (result != null) {
        setEntityId(result.getId());
        setEntityUuid(result.getUuid());
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create physical network entity");
    }
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) PhysicalNetwork(com.cloud.network.PhysicalNetwork)

Aggregations

PhysicalNetwork (com.cloud.network.PhysicalNetwork)34 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 ArrayList (java.util.ArrayList)8 HostVO (com.cloud.host.HostVO)7 Network (com.cloud.network.Network)7 Host (com.cloud.host.Host)6 DB (com.cloud.utils.db.DB)6 DataCenterVO (com.cloud.dc.DataCenterVO)5 TransactionStatus (com.cloud.utils.db.TransactionStatus)5 DataCenter (com.cloud.dc.DataCenter)4 ActionEvent (com.cloud.event.ActionEvent)4 NetworkVO (com.cloud.network.dao.NetworkVO)4 Account (com.cloud.user.Account)4 List (java.util.List)4 ConfigurationException (javax.naming.ConfigurationException)4 ServerApiException (com.cloud.api.ServerApiException)3 PhysicalNetworkResponse (com.cloud.api.response.PhysicalNetworkResponse)3 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)3 NetworkOffering (com.cloud.offering.NetworkOffering)3