Search in sources :

Example 81 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class ApiResponseHelper method createSite2SiteVpnConnectionResponse.

@Override
public Site2SiteVpnConnectionResponse createSite2SiteVpnConnectionResponse(Site2SiteVpnConnection result) {
    Site2SiteVpnConnectionResponse response = new Site2SiteVpnConnectionResponse();
    response.setId(result.getUuid());
    response.setPassive(result.isPassive());
    Long vpnGatewayId = result.getVpnGatewayId();
    if (vpnGatewayId != null) {
        Site2SiteVpnGateway vpnGateway = ApiDBUtils.findVpnGatewayById(vpnGatewayId);
        if (vpnGateway != null) {
            response.setVpnGatewayId(vpnGateway.getUuid());
            long ipId = vpnGateway.getAddrId();
            IPAddressVO ipObj = ApiDBUtils.findIpAddressById(ipId);
            response.setIp(ipObj.getAddress().addr());
        }
    }
    Long customerGatewayId = result.getCustomerGatewayId();
    if (customerGatewayId != null) {
        Site2SiteCustomerGateway customerGateway = ApiDBUtils.findCustomerGatewayById(customerGatewayId);
        if (customerGateway != null) {
            response.setCustomerGatewayId(customerGateway.getUuid());
            response.setGatewayIp(customerGateway.getGatewayIp());
            response.setGuestCidrList(customerGateway.getGuestCidrList());
            response.setIpsecPsk(customerGateway.getIpsecPsk());
            response.setIkePolicy(customerGateway.getIkePolicy());
            response.setEspPolicy(customerGateway.getEspPolicy());
            response.setIkeLifetime(customerGateway.getIkeLifetime());
            response.setEspLifetime(customerGateway.getEspLifetime());
            response.setDpd(customerGateway.getDpd());
            response.setEncap(customerGateway.getEncap());
        }
    }
    populateAccount(response, result.getAccountId());
    populateDomain(response, result.getDomainId());
    response.setState(result.getState().toString());
    response.setCreated(result.getCreated());
    response.setRemoved(result.getRemoved());
    response.setForDisplay(result.isDisplay());
    response.setObjectName("vpnconnection");
    return response;
}
Also used : Site2SiteVpnGateway(com.cloud.network.Site2SiteVpnGateway) Site2SiteVpnConnectionResponse(org.apache.cloudstack.api.response.Site2SiteVpnConnectionResponse) IPAddressVO(com.cloud.network.dao.IPAddressVO) Site2SiteCustomerGateway(com.cloud.network.Site2SiteCustomerGateway)

Example 82 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class ApiResponseHelper method createLoadBalancerResponse.

@Override
public LoadBalancerResponse createLoadBalancerResponse(LoadBalancer loadBalancer) {
    LoadBalancerResponse lbResponse = new LoadBalancerResponse();
    lbResponse.setId(loadBalancer.getUuid());
    lbResponse.setName(loadBalancer.getName());
    lbResponse.setDescription(loadBalancer.getDescription());
    List<String> cidrs = ApiDBUtils.findFirewallSourceCidrs(loadBalancer.getId());
    lbResponse.setCidrList(StringUtils.join(cidrs, ","));
    IPAddressVO publicIp = ApiDBUtils.findIpAddressById(loadBalancer.getSourceIpAddressId());
    lbResponse.setPublicIpId(publicIp.getUuid());
    lbResponse.setPublicIp(publicIp.getAddress().addr());
    lbResponse.setPublicPort(Integer.toString(loadBalancer.getSourcePortStart()));
    lbResponse.setPrivatePort(Integer.toString(loadBalancer.getDefaultPortStart()));
    lbResponse.setAlgorithm(loadBalancer.getAlgorithm());
    lbResponse.setLbProtocol(loadBalancer.getLbProtocol());
    lbResponse.setForDisplay(loadBalancer.isDisplay());
    FirewallRule.State state = loadBalancer.getState();
    String stateToSet = state.toString();
    if (state.equals(FirewallRule.State.Revoke)) {
        stateToSet = "Deleting";
    }
    lbResponse.setState(stateToSet);
    populateOwner(lbResponse, loadBalancer);
    DataCenter zone = ApiDBUtils.findZoneById(publicIp.getDataCenterId());
    if (zone != null) {
        lbResponse.setZoneId(zone.getUuid());
    }
    //set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.LoadBalancer, loadBalancer.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    lbResponse.setTags(tagResponses);
    Network ntwk = ApiDBUtils.findNetworkById(loadBalancer.getNetworkId());
    lbResponse.setNetworkId(ntwk.getUuid());
    lbResponse.setObjectName("loadbalancer");
    return lbResponse;
}
Also used : ArrayList(java.util.ArrayList) GlobalLoadBalancerResponse(org.apache.cloudstack.api.response.GlobalLoadBalancerResponse) ApplicationLoadBalancerResponse(org.apache.cloudstack.api.response.ApplicationLoadBalancerResponse) LoadBalancerResponse(org.apache.cloudstack.api.response.LoadBalancerResponse) DataCenter(com.cloud.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ResourceTagResponse(org.apache.cloudstack.api.response.ResourceTagResponse) IPAddressVO(com.cloud.network.dao.IPAddressVO) FirewallRule(com.cloud.network.rules.FirewallRule)

Example 83 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class DirectNetworkGuru method trash.

@Override
@DB
public boolean trash(Network network, NetworkOffering offering) {
    //Have to remove all placeholder nics
    try {
        long id = network.getId();
        final List<NicVO> nics = _nicDao.listPlaceholderNicsByNetworkId(id);
        if (nics != null) {
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    for (Nic nic : nics) {
                        if (nic.getIPv4Address() != null) {
                            s_logger.debug("Releasing ip " + nic.getIPv4Address() + " of placeholder nic " + nic);
                            IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIPv4Address());
                            if (ip != null) {
                                _ipAddrMgr.markIpAsUnavailable(ip.getId());
                                _ipAddressDao.unassignIpAddress(ip.getId());
                                s_logger.debug("Removing placeholder nic " + nic);
                                _nicDao.remove(nic.getId());
                            }
                        }
                    }
                }
            });
        }
        return true;
    } catch (Exception e) {
        s_logger.error("trash. Exception:" + e.getMessage());
        throw new CloudRuntimeException("trash. Exception:" + e.getMessage(), e);
    }
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) TransactionStatus(com.cloud.utils.db.TransactionStatus) Nic(com.cloud.vm.Nic) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) IPAddressVO(com.cloud.network.dao.IPAddressVO) NicVO(com.cloud.vm.NicVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) InsufficientVirtualNetworkCapacityException(com.cloud.exception.InsufficientVirtualNetworkCapacityException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DB(com.cloud.utils.db.DB)

Example 84 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class DirectPodBasedNetworkGuru method getIp.

@DB
protected void getIp(final NicProfile nic, final Pod pod, final VirtualMachineProfile vm, final Network network) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, ConcurrentOperationException {
    final DataCenter dc = _dcDao.findById(pod.getDataCenterId());
    Transaction.execute(new TransactionCallbackWithExceptionNoReturn<InsufficientAddressCapacityException>() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) throws InsufficientAddressCapacityException {
            PublicIp ip = null;
            List<PodVlanMapVO> podRefs = _podVlanDao.listPodVlanMapsByPod(pod.getId());
            VlanVO vlan = _vlanDao.findById(podRefs.get(0).getVlanDbId());
            if (nic.getIPv4Address() == null) {
                String podRangeGateway = null;
                if (!podRefs.isEmpty()) {
                    podRangeGateway = vlan.getVlanGateway();
                }
                //Get ip address from the placeholder and don't allocate a new one
                if (vm.getType() == VirtualMachine.Type.DomainRouter) {
                    Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, pod.getId());
                    if (placeholderNic != null) {
                        IPAddressVO userIp = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), placeholderNic.getIPv4Address());
                        ip = PublicIp.createFromAddrAndVlan(userIp, _vlanDao.findById(userIp.getVlanId()));
                        s_logger.debug("Nic got an ip address " + placeholderNic.getIPv4Address() + " stored in placeholder nic for the network " + network + " and gateway " + podRangeGateway);
                    }
                }
                if (ip == null) {
                    ip = _ipAddrMgr.assignPublicIpAddress(dc.getId(), pod.getId(), vm.getOwner(), VlanType.DirectAttached, network.getId(), null, false);
                }
                nic.setIPv4Address(ip.getAddress().toString());
                nic.setFormat(AddressFormat.Ip4);
                nic.setIPv4Gateway(ip.getGateway());
                nic.setIPv4Netmask(ip.getNetmask());
                if (ip.getVlanTag() != null && ip.getVlanTag().equalsIgnoreCase(Vlan.UNTAGGED)) {
                    nic.setIsolationUri(IsolationType.Ec2.toUri(Vlan.UNTAGGED));
                    nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(Vlan.UNTAGGED));
                    nic.setBroadcastType(BroadcastDomainType.Native);
                }
                nic.setReservationId(String.valueOf(ip.getVlanTag()));
                nic.setMacAddress(ip.getMacAddress());
                //save the placeholder nic if the vm is the Virtual router
                if (vm.getType() == VirtualMachine.Type.DomainRouter) {
                    Nic placeholderNic = _networkModel.getPlaceholderNicForRouter(network, pod.getId());
                    if (placeholderNic == null) {
                        s_logger.debug("Saving placeholder nic with ip4 address " + nic.getIPv4Address() + " for the network " + network);
                        _networkMgr.savePlaceholderNic(network, nic.getIPv4Address(), null, VirtualMachine.Type.DomainRouter);
                    }
                }
            }
            /**
                 * Calculate the IPv6 Address the Instance will obtain using SLAAC and IPv6 EUI-64
                 *
                 * Linux, FreeBSD and Windows all calculate the same IPv6 address when configured properly.
                 *
                 * Using Router Advertisements the routers in the network should announce the IPv6 CIDR which is configured
                 * in in the vlan table in the database.
                 *
                 * This way the NIC will be populated with a IPv6 address on which the Instance is reachable.
                 */
            if (vlan.getIp6Cidr() != null) {
                if (nic.getIPv6Address() == null) {
                    s_logger.debug("Found IPv6 CIDR " + vlan.getIp6Cidr() + " for VLAN " + vlan.getId());
                    nic.setIPv6Cidr(vlan.getIp6Cidr());
                    nic.setIPv6Gateway(vlan.getIp6Gateway());
                    IPv6Address ipv6addr = NetUtils.EUI64Address(vlan.getIp6Cidr(), nic.getMacAddress());
                    s_logger.info("Calculated IPv6 address " + ipv6addr + " using EUI-64 for NIC " + nic.getUuid());
                    nic.setIPv6Address(ipv6addr.toString());
                }
            } else {
                s_logger.debug("No IPv6 CIDR configured for VLAN " + vlan.getId());
            }
        }
    });
    nic.setIPv4Dns1(dc.getDns1());
    nic.setIPv4Dns2(dc.getDns2());
}
Also used : DataCenter(com.cloud.dc.DataCenter) PublicIp(com.cloud.network.addr.PublicIp) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) IPv6Address(com.googlecode.ipv6.IPv6Address) TransactionStatus(com.cloud.utils.db.TransactionStatus) Nic(com.cloud.vm.Nic) List(java.util.List) IPAddressVO(com.cloud.network.dao.IPAddressVO) VlanVO(com.cloud.dc.VlanVO) DB(com.cloud.utils.db.DB)

Example 85 with IPAddressVO

use of com.cloud.network.dao.IPAddressVO in project cloudstack by apache.

the class FirewallManagerImpl method createFirewallRule.

@DB
protected FirewallRule createFirewallRule(final Long ipAddrId, Account caller, final String xId, final Integer portStart, final Integer portEnd, final String protocol, final List<String> sourceCidrList, final Integer icmpCode, final Integer icmpType, final Long relatedRuleId, final FirewallRule.FirewallRuleType type, final Long networkId, final FirewallRule.TrafficType trafficType, final Boolean forDisplay) throws NetworkRuleConflictException {
    IPAddressVO ipAddress = null;
    if (ipAddrId != null) {
        // this for ingress firewall rule, for egress id is null
        ipAddress = _ipAddressDao.findById(ipAddrId);
        // Validate ip address
        if (ipAddress == null && type == FirewallRule.FirewallRuleType.User) {
            throw new InvalidParameterValueException("Unable to create firewall rule; " + "couldn't locate IP address by id in the system");
        }
        _networkModel.checkIpForService(ipAddress, Service.Firewall, null);
    }
    validateFirewallRule(caller, ipAddress, portStart, portEnd, protocol, Purpose.Firewall, type, networkId, trafficType);
    // icmp code and icmp type can't be passed in for any other protocol rather than icmp
    if (!protocol.equalsIgnoreCase(NetUtils.ICMP_PROTO) && (icmpCode != null || icmpType != null)) {
        throw new InvalidParameterValueException("Can specify icmpCode and icmpType for ICMP protocol only");
    }
    if (protocol.equalsIgnoreCase(NetUtils.ICMP_PROTO) && (portStart != null || portEnd != null)) {
        throw new InvalidParameterValueException("Can't specify start/end port when protocol is ICMP");
    }
    Long accountId = null;
    Long domainId = null;
    if (ipAddress != null) {
        //Ingress firewall rule
        accountId = ipAddress.getAllocatedToAccountId();
        domainId = ipAddress.getAllocatedInDomainId();
    } else if (networkId != null) {
        //egress firewall rule
        Network network = _networkModel.getNetwork(networkId);
        accountId = network.getAccountId();
        domainId = network.getDomainId();
    }
    final Long accountIdFinal = accountId;
    final Long domainIdFinal = domainId;
    return Transaction.execute(new TransactionCallbackWithException<FirewallRuleVO, NetworkRuleConflictException>() {

        @Override
        public FirewallRuleVO doInTransaction(TransactionStatus status) throws NetworkRuleConflictException {
            FirewallRuleVO newRule = new FirewallRuleVO(xId, ipAddrId, portStart, portEnd, protocol.toLowerCase(), networkId, accountIdFinal, domainIdFinal, Purpose.Firewall, sourceCidrList, icmpCode, icmpType, relatedRuleId, trafficType);
            newRule.setType(type);
            if (forDisplay != null) {
                newRule.setDisplay(forDisplay);
            }
            newRule = _firewallDao.persist(newRule);
            if (type == FirewallRuleType.User)
                detectRulesConflict(newRule);
            if (!_firewallDao.setStateToAdd(newRule)) {
                throw new CloudRuntimeException("Unable to update the state to add for " + newRule);
            }
            CallContext.current().setEventDetails("Rule Id: " + newRule.getId());
            return newRule;
        }
    });
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Network(com.cloud.network.Network) TransactionStatus(com.cloud.utils.db.TransactionStatus) IPAddressVO(com.cloud.network.dao.IPAddressVO) NetworkRuleConflictException(com.cloud.exception.NetworkRuleConflictException) FirewallRuleVO(com.cloud.network.rules.FirewallRuleVO) DB(com.cloud.utils.db.DB)

Aggregations

IPAddressVO (com.cloud.network.dao.IPAddressVO)109 ArrayList (java.util.ArrayList)43 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)42 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)39 Account (com.cloud.user.Account)37 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)32 DB (com.cloud.utils.db.DB)28 TransactionStatus (com.cloud.utils.db.TransactionStatus)26 Network (com.cloud.network.Network)25 PublicIp (com.cloud.network.addr.PublicIp)22 DataCenter (com.cloud.dc.DataCenter)17 VlanVO (com.cloud.dc.VlanVO)16 InsufficientAddressCapacityException (com.cloud.exception.InsufficientAddressCapacityException)16 List (java.util.List)15 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)14 Ip (com.cloud.utils.net.Ip)14 NetworkOffering (com.cloud.offering.NetworkOffering)13 TransactionCallbackWithException (com.cloud.utils.db.TransactionCallbackWithException)13 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)12 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)11