Search in sources :

Example 6 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class ConfigurationManagerImpl method editZone.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_ZONE_EDIT, eventDescription = "editing zone", async = false)
public DataCenter editZone(final UpdateZoneCmd cmd) {
    // Parameter validation as from execute() method in V1
    final Long zoneId = cmd.getId();
    String zoneName = cmd.getZoneName();
    String dns1 = cmd.getDns1();
    String dns2 = cmd.getDns2();
    String ip6Dns1 = cmd.getIp6Dns1();
    String ip6Dns2 = cmd.getIp6Dns2();
    String internalDns1 = cmd.getInternalDns1();
    String internalDns2 = cmd.getInternalDns2();
    String guestCidr = cmd.getGuestCidrAddress();
    final List<String> dnsSearchOrder = cmd.getDnsSearchOrder();
    final Boolean isPublic = cmd.isPublic();
    final String allocationStateStr = cmd.getAllocationState();
    final String dhcpProvider = cmd.getDhcpProvider();
    final Map<?, ?> detailsMap = cmd.getDetails();
    final String networkDomain = cmd.getDomain();
    final Boolean localStorageEnabled = cmd.getLocalStorageEnabled();
    final Map<String, String> newDetails = new HashMap<String, String>();
    if (detailsMap != null) {
        final Collection<?> zoneDetailsCollection = detailsMap.values();
        final Iterator<?> iter = zoneDetailsCollection.iterator();
        while (iter.hasNext()) {
            final HashMap<?, ?> detail = (HashMap<?, ?>) iter.next();
            final String key = (String) detail.get("key");
            final String value = (String) detail.get("value");
            if (key == null || value == null) {
                throw new InvalidParameterValueException("Invalid Zone Detail specified, fields 'key' and 'value' cannot be null, please specify details in the form:  details[0].key=XXX&details[0].value=YYY");
            }
            // validate the zone detail keys are known keys
            /*
                 * if(!ZoneConfig.doesKeyExist(key)){ throw new
                 * InvalidParameterValueException
                 * ("Invalid Zone Detail parameter: "+ key); }
                 */
            newDetails.put(key, value);
        }
    }
    // add the domain prefix list to details if not null
    if (dnsSearchOrder != null) {
        for (final String dom : dnsSearchOrder) {
            if (!NetUtils.verifyDomainName(dom)) {
                throw new InvalidParameterValueException("Invalid network domain suffixes. 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 \"-\"");
            }
        }
        newDetails.put(ZoneConfig.DnsSearchOrder.getName(), StringUtils.join(dnsSearchOrder, ","));
    }
    final DataCenterVO zone = _zoneDao.findById(zoneId);
    if (zone == null) {
        throw new InvalidParameterValueException("unable to find zone by id " + zoneId);
    }
    if (zoneName == null) {
        zoneName = zone.getName();
    }
    if (guestCidr != null && !NetUtils.validateGuestCidr(guestCidr)) {
        throw new InvalidParameterValueException("Please enter a valid guest cidr");
    }
    // Make sure the zone exists
    if (!validZone(zoneId)) {
        throw new InvalidParameterValueException("A zone with ID: " + zoneId + " does not exist.");
    }
    final String oldZoneName = zone.getName();
    if (zoneName == null) {
        zoneName = oldZoneName;
    }
    if (dns1 == null) {
        dns1 = zone.getDns1();
    }
    if (dns2 == null) {
        dns2 = zone.getDns2();
    }
    if (ip6Dns1 == null) {
        ip6Dns1 = zone.getIp6Dns1();
    }
    if (ip6Dns2 == null) {
        ip6Dns2 = zone.getIp6Dns2();
    }
    if (internalDns1 == null) {
        internalDns1 = zone.getInternalDns1();
    }
    if (internalDns2 == null) {
        internalDns2 = zone.getInternalDns2();
    }
    if (guestCidr == null) {
        guestCidr = zone.getGuestNetworkCidr();
    }
    // validate network domain
    if (networkDomain != null && !networkDomain.isEmpty()) {
        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 \"-\"");
        }
    }
    final boolean checkForDuplicates = !zoneName.equals(oldZoneName);
    // not allowing updating
    checkZoneParameters(zoneName, dns1, dns2, internalDns1, internalDns2, checkForDuplicates, null, allocationStateStr, ip6Dns1, ip6Dns2);
    // domain associated with
    // a zone, once created
    zone.setName(zoneName);
    zone.setDns1(dns1);
    zone.setDns2(dns2);
    zone.setIp6Dns1(ip6Dns1);
    zone.setIp6Dns2(ip6Dns2);
    zone.setInternalDns1(internalDns1);
    zone.setInternalDns2(internalDns2);
    zone.setGuestNetworkCidr(guestCidr);
    if (localStorageEnabled != null) {
        zone.setLocalStorageEnabled(localStorageEnabled.booleanValue());
    }
    if (networkDomain != null) {
        if (networkDomain.isEmpty()) {
            zone.setDomain(null);
        } else {
            zone.setDomain(networkDomain);
        }
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            final Map<String, String> updatedDetails = new HashMap<String, String>();
            _zoneDao.loadDetails(zone);
            if (zone.getDetails() != null) {
                updatedDetails.putAll(zone.getDetails());
            }
            updatedDetails.putAll(newDetails);
            zone.setDetails(updatedDetails);
            if (allocationStateStr != null && !allocationStateStr.isEmpty()) {
                final Grouping.AllocationState allocationState = Grouping.AllocationState.valueOf(allocationStateStr);
                if (allocationState == Grouping.AllocationState.Enabled) {
                    // check if zone has necessary trafficTypes before enabling
                    try {
                        PhysicalNetwork mgmtPhyNetwork;
                        // zone should have a physical network with management
                        // traffiType
                        mgmtPhyNetwork = _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Management);
                        if (NetworkType.Advanced == zone.getNetworkType() && !zone.isSecurityGroupEnabled()) {
                            // advanced zone without SG should have a physical
                            // network with public Thpe
                            _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Public);
                        }
                        try {
                            _networkModel.getDefaultPhysicalNetworkByZoneAndTrafficType(zoneId, TrafficType.Storage);
                        } catch (final InvalidParameterValueException noStorage) {
                            final PhysicalNetworkTrafficTypeVO mgmtTraffic = _trafficTypeDao.findBy(mgmtPhyNetwork.getId(), TrafficType.Management);
                            _networkSvc.addTrafficTypeToPhysicalNetwork(mgmtPhyNetwork.getId(), TrafficType.Storage.toString(), "vlan", mgmtTraffic.getXenNetworkLabel(), mgmtTraffic.getKvmNetworkLabel(), mgmtTraffic.getVmwareNetworkLabel(), mgmtTraffic.getSimulatorNetworkLabel(), mgmtTraffic.getVlan(), mgmtTraffic.getHypervNetworkLabel(), mgmtTraffic.getOvm3NetworkLabel());
                            s_logger.info("No storage traffic type was specified by admin, create default storage traffic on physical network " + mgmtPhyNetwork.getId() + " with same configure of management traffic type");
                        }
                    } catch (final InvalidParameterValueException ex) {
                        throw new InvalidParameterValueException("Cannot enable this Zone since: " + ex.getMessage());
                    }
                }
                zone.setAllocationState(allocationState);
            }
            if (dhcpProvider != null) {
                zone.setDhcpProvider(dhcpProvider);
            }
            // update a private zone to public; not vice versa
            if (isPublic != null && isPublic) {
                zone.setDomainId(null);
                zone.setDomain(null);
                // release the dedication for this zone
                final DedicatedResourceVO resource = _dedicatedDao.findByZoneId(zoneId);
                Long resourceId = null;
                if (resource != null) {
                    resourceId = resource.getId();
                    if (!_dedicatedDao.remove(resourceId)) {
                        throw new CloudRuntimeException("Failed to delete dedicated Zone Resource " + resourceId);
                    }
                    // find the group associated and check if there are any more
                    // resources under that group
                    final List<DedicatedResourceVO> resourcesInGroup = _dedicatedDao.listByAffinityGroupId(resource.getAffinityGroupId());
                    if (resourcesInGroup.isEmpty()) {
                        // delete the group
                        _affinityGroupService.deleteAffinityGroup(resource.getAffinityGroupId(), null, null, null, null);
                    }
                }
            }
            if (!_zoneDao.update(zoneId, zone)) {
                throw new CloudRuntimeException("Failed to edit zone. Please contact Cloud Support.");
            }
        }
    });
    return zone;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) HashMap(java.util.HashMap) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) PhysicalNetworkTrafficTypeVO(com.cloud.network.dao.PhysicalNetworkTrafficTypeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) AllocationState(com.cloud.org.Grouping.AllocationState) PhysicalNetwork(com.cloud.network.PhysicalNetwork) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) List(java.util.List) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) Map(java.util.Map) HashMap(java.util.HashMap) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 7 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class ConfigurationManagerImpl method checkPodCidrSubnets.

@Override
public void checkPodCidrSubnets(final long dcId, final Long podIdToBeSkipped, final String cidr) {
    // For each pod, return an error if any of the following is true:
    // The pod's CIDR subnet conflicts with the CIDR subnet of any other pod
    // Check if the CIDR conflicts with the Guest Network or other pods
    long skipPod = 0;
    if (podIdToBeSkipped != null) {
        skipPod = podIdToBeSkipped;
    }
    final HashMap<Long, List<Object>> currentPodCidrSubnets = _podDao.getCurrentPodCidrSubnets(dcId, skipPod);
    final List<Object> newCidrPair = new ArrayList<Object>();
    newCidrPair.add(0, getCidrAddress(cidr));
    newCidrPair.add(1, (long) getCidrSize(cidr));
    currentPodCidrSubnets.put(new Long(-1), newCidrPair);
    final DataCenterVO dcVo = _zoneDao.findById(dcId);
    final String guestNetworkCidr = dcVo.getGuestNetworkCidr();
    // Guest cidr can be null for Basic zone
    String guestIpNetwork = null;
    Long guestCidrSize = null;
    if (guestNetworkCidr != null) {
        final String[] cidrTuple = guestNetworkCidr.split("\\/");
        guestIpNetwork = NetUtils.getIpRangeStartIpFromCidr(cidrTuple[0], Long.parseLong(cidrTuple[1]));
        guestCidrSize = Long.parseLong(cidrTuple[1]);
    }
    final String zoneName = getZoneName(dcId);
    // Iterate through all pods in this zone
    for (final Long podId : currentPodCidrSubnets.keySet()) {
        String podName;
        if (podId.longValue() == -1) {
            podName = "newPod";
        } else {
            podName = getPodName(podId.longValue());
        }
        final List<Object> cidrPair = currentPodCidrSubnets.get(podId);
        final String cidrAddress = (String) cidrPair.get(0);
        final long cidrSize = ((Long) cidrPair.get(1)).longValue();
        long cidrSizeToUse = -1;
        if (guestCidrSize == null || cidrSize < guestCidrSize) {
            cidrSizeToUse = cidrSize;
        } else {
            cidrSizeToUse = guestCidrSize;
        }
        String cidrSubnet = NetUtils.getCidrSubNet(cidrAddress, cidrSizeToUse);
        if (guestNetworkCidr != null) {
            final String guestSubnet = NetUtils.getCidrSubNet(guestIpNetwork, cidrSizeToUse);
            // Check that cidrSubnet does not equal guestSubnet
            if (cidrSubnet.equals(guestSubnet)) {
                if (podName.equals("newPod")) {
                    throw new InvalidParameterValueException("The subnet of the pod you are adding conflicts with the subnet of the Guest IP Network. Please specify a different CIDR.");
                } else {
                    throw new InvalidParameterValueException("Warning: The subnet of pod " + podName + " in zone " + zoneName + " conflicts with the subnet of the Guest IP Network. Please change either the pod's CIDR or the Guest IP Network's subnet, and re-run install-vmops-management.");
                }
            }
        }
        // Iterate through the rest of the pods
        for (final Long otherPodId : currentPodCidrSubnets.keySet()) {
            if (podId.equals(otherPodId)) {
                continue;
            }
            // Check that cidrSubnet does not equal otherCidrSubnet
            final List<Object> otherCidrPair = currentPodCidrSubnets.get(otherPodId);
            final String otherCidrAddress = (String) otherCidrPair.get(0);
            final long otherCidrSize = ((Long) otherCidrPair.get(1)).longValue();
            if (cidrSize < otherCidrSize) {
                cidrSizeToUse = cidrSize;
            } else {
                cidrSizeToUse = otherCidrSize;
            }
            cidrSubnet = NetUtils.getCidrSubNet(cidrAddress, cidrSizeToUse);
            final String otherCidrSubnet = NetUtils.getCidrSubNet(otherCidrAddress, cidrSizeToUse);
            if (cidrSubnet.equals(otherCidrSubnet)) {
                final String otherPodName = getPodName(otherPodId.longValue());
                if (podName.equals("newPod")) {
                    throw new InvalidParameterValueException("The subnet of the pod you are adding conflicts with the subnet of pod " + otherPodName + " in zone " + zoneName + ". Please specify a different CIDR.");
                } else {
                    throw new InvalidParameterValueException("Warning: The pods " + podName + " and " + otherPodName + " in zone " + zoneName + " have conflicting CIDR subnets. Please change the CIDR of one of these pods.");
                }
            }
        }
    }
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class ControlNetworkGuru method release.

@Override
public boolean release(NicProfile nic, VirtualMachineProfile vm, String reservationId) {
    assert nic.getTrafficType() == TrafficType.Control;
    HypervisorType hType = vm.getHypervisorType();
    if (((hType == HypervisorType.VMware) || (hType == HypervisorType.Hyperv)) && isRouterVm(vm)) {
        long dcId = vm.getVirtualMachine().getDataCenterId();
        DataCenterVO dcVo = _dcDao.findById(dcId);
        if (dcVo.getNetworkType() != NetworkType.Basic) {
            super.release(nic, vm, reservationId);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Released nic: " + nic);
            }
            return true;
        } else {
            nic.deallocate();
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Released nic: " + nic);
            }
            return true;
        }
    }
    _dcDao.releaseLinkLocalIpAddress(nic.getId(), reservationId);
    nic.deallocate();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Released nic: " + nic);
    }
    return true;
}
Also used : HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) DataCenterVO(com.cloud.dc.DataCenterVO)

Example 9 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class VirtualRouterElement method applyLBRules.

@Override
public boolean applyLBRules(final Network network, final List<LoadBalancingRule> rules) throws ResourceUnavailableException {
    boolean result = true;
    if (canHandle(network, Service.Lb)) {
        if (!canHandleLbRules(rules)) {
            return false;
        }
        final List<DomainRouterVO> routers = getRouters(network);
        if (routers == null || routers.isEmpty()) {
            s_logger.debug("Virtual router elemnt doesn't need to apply lb rules on the backend; virtual " + "router doesn't exist in the network " + network.getId());
            return true;
        }
        final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
        final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
        for (final DomainRouterVO domainRouterVO : routers) {
            result = result && networkTopology.applyLoadBalancingRules(network, rules, domainRouterVO);
        }
    }
    return result;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) NetworkTopology(org.apache.cloudstack.network.topology.NetworkTopology) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Example 10 with DataCenterVO

use of com.cloud.dc.DataCenterVO in project cloudstack by apache.

the class VirtualRouterElement method applyPFRules.

@Override
public boolean applyPFRules(final Network network, final List<PortForwardingRule> rules) throws ResourceUnavailableException {
    boolean result = true;
    if (canHandle(network, Service.PortForwarding)) {
        final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
        if (routers == null || routers.isEmpty()) {
            s_logger.debug("Virtual router elemnt doesn't need to apply firewall rules on the backend; virtual " + "router doesn't exist in the network " + network.getId());
            return true;
        }
        final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
        final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
        for (final DomainRouterVO domainRouterVO : routers) {
            result = result && networkTopology.applyFirewallRules(network, rules, domainRouterVO);
        }
    }
    return result;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) NetworkTopology(org.apache.cloudstack.network.topology.NetworkTopology) DomainRouterVO(com.cloud.vm.DomainRouterVO)

Aggregations

DataCenterVO (com.cloud.dc.DataCenterVO)214 ArrayList (java.util.ArrayList)60 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)54 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)53 HostVO (com.cloud.host.HostVO)42 Account (com.cloud.user.Account)37 NetworkVO (com.cloud.network.dao.NetworkVO)35 DomainRouterVO (com.cloud.vm.DomainRouterVO)33 HostPodVO (com.cloud.dc.HostPodVO)32 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)30 ClusterVO (com.cloud.dc.ClusterVO)27 NetworkTopology (org.apache.cloudstack.network.topology.NetworkTopology)27 DB (com.cloud.utils.db.DB)26 Network (com.cloud.network.Network)25 HashMap (java.util.HashMap)25 ConfigurationException (javax.naming.ConfigurationException)25 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)20 Test (org.junit.Test)20 NicProfile (com.cloud.vm.NicProfile)19 ActionEvent (com.cloud.event.ActionEvent)18