Search in sources :

Example 6 with NetworkRuleConflictException

use of com.cloud.legacymodel.exceptions.NetworkRuleConflictException in project cosmic by MissionCriticalCloud.

the class CreatePortForwardingRuleCmd method create.

@Override
public void create() {
    // cidr list parameter is deprecated
    if (cidrlist != null) {
        throw new InvalidParameterValueException("Parameter cidrList is deprecated; if you need to open firewall rule for the specific cidr, please refer to createFirewallRule command");
    }
    final Ip privateIp = getVmSecondaryIp();
    if (privateIp != null) {
        if (!NetUtils.isValidIp4(privateIp.toString())) {
            throw new InvalidParameterValueException("Invalid vm ip address");
        }
    }
    try {
        final PortForwardingRule result = _rulesService.createPortForwardingRule(this, virtualMachineId, privateIp, getOpenFirewall(), isDisplay());
        setEntityId(result.getId());
        setEntityUuid(result.getUuid());
    } catch (final NetworkRuleConflictException ex) {
        s_logger.info("Network rule conflict: ", ex);
        s_logger.trace("Network Rule Conflict: ", ex);
        throw new ServerApiException(ApiErrorCode.NETWORK_RULE_CONFLICT_ERROR, ex.getMessage());
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Ip(com.cloud.legacymodel.network.Ip) PortForwardingRule(com.cloud.legacymodel.network.PortForwardingRule) NetworkRuleConflictException(com.cloud.legacymodel.exceptions.NetworkRuleConflictException)

Example 7 with NetworkRuleConflictException

use of com.cloud.legacymodel.exceptions.NetworkRuleConflictException in project cosmic by MissionCriticalCloud.

the class DeleteSslCertCmd method execute.

// ///////////////////////////////////////////////////
// ///////////// API Implementation///////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
    try {
        _certService.deleteSslCert(this);
        final SuccessResponse rsp = new SuccessResponse();
        rsp.setResponseName(getCommandName());
        rsp.setObjectName("success");
        this.setResponseObject(rsp);
    } catch (final Exception e) {
        throw new CloudRuntimeException(e);
    }
}
Also used : SuccessResponse(com.cloud.api.response.SuccessResponse) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ServerApiException(com.cloud.api.ServerApiException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) NetworkRuleConflictException(com.cloud.legacymodel.exceptions.NetworkRuleConflictException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException)

Example 8 with NetworkRuleConflictException

use of com.cloud.legacymodel.exceptions.NetworkRuleConflictException in project cosmic by MissionCriticalCloud.

the class CreateLBStickinessPolicyCmd method create.

@Override
public void create() {
    try {
        final StickinessPolicy result = _lbService.createLBStickinessPolicy(this);
        this.setEntityId(result.getId());
        this.setEntityUuid(result.getUuid());
    } catch (final NetworkRuleConflictException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.NETWORK_RULE_CONFLICT_ERROR, e.getMessage());
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) NetworkRuleConflictException(com.cloud.legacymodel.exceptions.NetworkRuleConflictException) StickinessPolicy(com.cloud.network.rules.StickinessPolicy)

Example 9 with NetworkRuleConflictException

use of com.cloud.legacymodel.exceptions.NetworkRuleConflictException in project cosmic by MissionCriticalCloud.

the class LoadBalancingRulesManagerImpl method createPublicLoadBalancer.

@DB
@Override
public LoadBalancer createPublicLoadBalancer(final String xId, final String name, final String description, final int srcPort, final int destPort, final long sourceIpId, final String protocol, final String algorithm, final boolean openFirewall, final CallContext caller, final String lbProtocol, final Boolean forDisplay, final int clientTimeout, final int serverTimeout) throws NetworkRuleConflictException {
    if (!NetUtils.isValidPort(destPort)) {
        throw new InvalidParameterValueException("privatePort is an invalid value: " + destPort);
    }
    if ((algorithm == null) || !NetUtils.isValidAlgorithm(algorithm)) {
        throw new InvalidParameterValueException("Invalid algorithm: " + algorithm);
    }
    final IPAddressVO ipAddr = _ipAddressDao.findById(sourceIpId);
    // make sure ip address exists
    if (ipAddr == null || !ipAddr.readyToUse()) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule, invalid IP address id specified");
        if (ipAddr == null) {
            ex.addProxyObject(String.valueOf(sourceIpId), "sourceIpId");
        } else {
            ex.addProxyObject(ipAddr.getUuid(), "sourceIpId");
        }
        throw ex;
    } else if (ipAddr.isOneToOneNat()) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule; specified sourceip id has static nat enabled");
        ex.addProxyObject(ipAddr.getUuid(), "sourceIpId");
        throw ex;
    }
    _accountMgr.checkAccess(caller.getCallingAccount(), null, true, ipAddr);
    final Long networkId = ipAddr.getAssociatedWithNetworkId();
    if (networkId == null) {
        final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to create load balancer rule ; specified sourceip id is not associated with any network");
        ex.addProxyObject(ipAddr.getUuid(), "sourceIpId");
        throw ex;
    }
    // verify that lb service is supported by the network
    isLbServiceSupportedInNetwork(networkId, Scheme.Public);
    _firewallMgr.validateFirewallRule(caller.getCallingAccount(), ipAddr, srcPort, srcPort, protocol, Purpose.LoadBalancing, FirewallRuleType.User, networkId, null);
    final LoadBalancerVO newRule = new LoadBalancerVO(xId, name, description, sourceIpId, srcPort, destPort, algorithm, networkId, ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId(), lbProtocol, clientTimeout, serverTimeout);
    // verify rule is supported by Lb provider of the network
    final Ip sourceIp = getSourceIp(newRule);
    final LoadBalancingRule loadBalancing = new LoadBalancingRule(newRule, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), sourceIp, null, lbProtocol);
    if (!validateLbRule(loadBalancing)) {
        throw new InvalidParameterValueException("LB service provider cannot support this rule");
    }
    return Transaction.execute(new TransactionCallbackWithException<LoadBalancerVO, NetworkRuleConflictException>() {

        @Override
        public LoadBalancerVO doInTransaction(final TransactionStatus status) throws NetworkRuleConflictException {
            LoadBalancerVO newRule = new LoadBalancerVO(xId, name, description, sourceIpId, srcPort, destPort, algorithm, networkId, ipAddr.getAllocatedToAccountId(), ipAddr.getAllocatedInDomainId(), lbProtocol, clientTimeout, serverTimeout);
            if (forDisplay != null) {
                newRule.setDisplay(forDisplay);
            }
            // verify rule is supported by Lb provider of the network
            final Ip sourceIp = getSourceIp(newRule);
            final LoadBalancingRule loadBalancing = new LoadBalancingRule(newRule, new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), sourceIp, null, lbProtocol);
            if (!validateLbRule(loadBalancing)) {
                throw new InvalidParameterValueException("LB service provider cannot support this rule");
            }
            newRule = _lbDao.persist(newRule);
            // create rule for all CIDRs
            if (openFirewall) {
                _firewallMgr.createRuleForAllCidrs(sourceIpId, caller.getCallingAccount(), srcPort, srcPort, protocol, null, null, newRule.getId(), networkId);
            }
            boolean success = true;
            try {
                _firewallMgr.detectRulesConflict(newRule);
                if (!_firewallDao.setStateToAdd(newRule)) {
                    throw new CloudRuntimeException("Unable to update the state to add for " + newRule);
                }
                s_logger.debug("Load balancer " + newRule.getId() + " for Ip address id=" + sourceIpId + ", public port " + srcPort + ", private port " + destPort + " is added successfully.");
                CallContext.current().setEventDetails("Load balancer Id: " + newRule.getId());
                return newRule;
            } catch (final Exception e) {
                success = false;
                if (e instanceof NetworkRuleConflictException) {
                    throw (NetworkRuleConflictException) e;
                }
                throw new CloudRuntimeException("Unable to add rule for ip address id=" + newRule.getSourceIpAddressId(), e);
            } finally {
                if (!success && newRule != null) {
                    _firewallMgr.revokeRelatedFirewallRule(newRule.getId(), false);
                    removeLBRule(newRule);
                }
            }
        }
    });
}
Also used : LoadBalancingRule(com.cloud.legacymodel.network.LoadBalancingRule) Ip(com.cloud.legacymodel.network.Ip) PublicIp(com.cloud.network.addr.PublicIp) LoadBalancerVO(com.cloud.network.dao.LoadBalancerVO) ArrayList(java.util.ArrayList) TransactionStatus(com.cloud.utils.db.TransactionStatus) NetworkRuleConflictException(com.cloud.legacymodel.exceptions.NetworkRuleConflictException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) NetworkRuleConflictException(com.cloud.legacymodel.exceptions.NetworkRuleConflictException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) IPAddressVO(com.cloud.network.dao.IPAddressVO) DB(com.cloud.utils.db.DB)

Example 10 with NetworkRuleConflictException

use of com.cloud.legacymodel.exceptions.NetworkRuleConflictException in project cosmic by MissionCriticalCloud.

the class LoadBalancingRulesManagerImpl method createPublicLoadBalancerRule.

@Override
@ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_CREATE, eventDescription = "creating load balancer")
public LoadBalancer createPublicLoadBalancerRule(final String xId, final String name, final String description, final int srcPortStart, final int srcPortEnd, final int defPortStart, final int defPortEnd, final Long ipAddrId, final String protocol, final String algorithm, final long networkId, final long lbOwnerId, final boolean openFirewall, final String lbProtocol, final Boolean forDisplay, Integer clientTimeout, Integer serverTimeout) throws NetworkRuleConflictException, InsufficientAddressCapacityException {
    final Account lbOwner = _accountMgr.getAccount(lbOwnerId);
    if (srcPortStart != srcPortEnd) {
        throw new InvalidParameterValueException("Port ranges are not supported by the load balancer");
    }
    IPAddressVO ipVO = null;
    if (ipAddrId != null) {
        ipVO = _ipAddressDao.findById(ipAddrId);
    }
    final Network network = _networkModel.getNetwork(networkId);
    LoadBalancer result = null;
    if (result == null) {
        IpAddress systemIp = null;
        final NetworkOffering off = _entityMgr.findById(NetworkOffering.class, network.getNetworkOfferingId());
        if (off.getElasticLb() && ipVO == null && network.getVpcId() == null) {
            systemIp = _ipAddrMgr.assignSystemIp(networkId, lbOwner, true, false);
            if (systemIp != null) {
                ipVO = _ipAddressDao.findById(systemIp.getId());
            }
        }
        // Validate ip address
        if (ipVO == null) {
            throw new InvalidParameterValueException("Unable to create load balance rule; can't find/allocate source IP");
        } else if (ipVO.isOneToOneNat()) {
            throw new NetworkRuleConflictException("Can't do load balance on ip address: " + ipVO.getAddress());
        }
        boolean performedIpAssoc = false;
        try {
            if (ipVO.getAssociatedWithNetworkId() == null) {
                final boolean assignToVpcNtwk = network.getVpcId() != null && ipVO.getVpcId() != null && ipVO.getVpcId().longValue() == network.getVpcId();
                if (assignToVpcNtwk) {
                    // set networkId just for verification purposes
                    _networkModel.checkIpForService(ipVO, Service.Lb, networkId);
                    s_logger.debug("The ip is not associated with the VPC network id=" + networkId + " so assigning");
                    ipVO = _ipAddrMgr.associateIPToGuestNetwork(ipAddrId, networkId, false);
                    performedIpAssoc = true;
                }
            } else {
                _networkModel.checkIpForService(ipVO, Service.Lb, null);
            }
            if (ipVO.getAssociatedWithNetworkId() == null) {
                throw new InvalidParameterValueException("Ip address " + ipVO + " is not assigned to the network " + network);
            }
            // Load default values and fallback to hardcoded if not available
            final Integer defaultClientTimeout = NumbersUtil.parseInt(_configDao.getValue(Config.DefaultLoadBalancerClientTimeout.key()), 60000);
            final Integer defaultServerTimeout = NumbersUtil.parseInt(_configDao.getValue(Config.DefaultLoadBalancerServerTimeout.key()), 60000);
            // set timeouts, use defaults if not available
            if (clientTimeout != null) {
                clientTimeout = NumbersUtil.parseInt(clientTimeout.toString(), defaultClientTimeout);
            } else {
                clientTimeout = defaultClientTimeout;
            }
            if (serverTimeout != null) {
                serverTimeout = NumbersUtil.parseInt(serverTimeout.toString(), defaultServerTimeout);
            } else {
                serverTimeout = defaultServerTimeout;
            }
            result = createPublicLoadBalancer(xId, name, description, srcPortStart, defPortStart, ipVO.getId(), protocol, algorithm, openFirewall, CallContext.current(), lbProtocol, forDisplay, clientTimeout, serverTimeout);
        } catch (final Exception ex) {
            s_logger.warn("Failed to create load balancer due to ", ex);
            if (ex instanceof NetworkRuleConflictException) {
                throw (NetworkRuleConflictException) ex;
            }
            if (ex instanceof InvalidParameterValueException) {
                throw (InvalidParameterValueException) ex;
            }
        } finally {
            if (result == null && systemIp != null) {
                s_logger.debug("Releasing system IP address " + systemIp + " as corresponding lb rule failed to create");
                _ipAddrMgr.handleSystemIpRelease(systemIp);
            }
            // release ip address if ipassoc was perfored
            if (performedIpAssoc) {
                ipVO = _ipAddressDao.findById(ipVO.getId());
                _vpcMgr.unassignIPFromVpcNetwork(ipVO.getId(), networkId);
            }
        }
    }
    if (result == null) {
        throw new CloudRuntimeException("Failed to create load balancer rule: " + name);
    }
    return result;
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) NetworkOffering(com.cloud.offering.NetworkOffering) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Network(com.cloud.legacymodel.network.Network) LoadBalancer(com.cloud.legacymodel.network.LoadBalancer) IPAddressVO(com.cloud.network.dao.IPAddressVO) IpAddress(com.cloud.network.IpAddress) NetworkRuleConflictException(com.cloud.legacymodel.exceptions.NetworkRuleConflictException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) InvalidParameterException(java.security.InvalidParameterException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) NetworkRuleConflictException(com.cloud.legacymodel.exceptions.NetworkRuleConflictException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ActionEvent(com.cloud.event.ActionEvent)

Aggregations

NetworkRuleConflictException (com.cloud.legacymodel.exceptions.NetworkRuleConflictException)26 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)14 ServerApiException (com.cloud.api.ServerApiException)12 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)10 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)9 Network (com.cloud.legacymodel.network.Network)8 IPAddressVO (com.cloud.network.dao.IPAddressVO)8 DB (com.cloud.utils.db.DB)7 TransactionStatus (com.cloud.utils.db.TransactionStatus)7 InsufficientAddressCapacityException (com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException)6 Account (com.cloud.legacymodel.user.Account)6 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)5 CallContext (com.cloud.context.CallContext)4 ActionEvent (com.cloud.event.ActionEvent)4 IpAddress (com.cloud.network.IpAddress)4 NetworkOffering (com.cloud.offering.NetworkOffering)4 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)3 ResourceAllocationException (com.cloud.legacymodel.exceptions.ResourceAllocationException)3 FirewallRule (com.cloud.legacymodel.network.FirewallRule)3 Ip (com.cloud.legacymodel.network.Ip)3