Search in sources :

Example 1 with Pair

use of com.cloud.utils.Pair in project CloudStack-archive by CloudStack-extras.

the class IPAddressUsageParser method parse.

public static boolean parse(AccountVO account, Date startDate, Date endDate) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Parsing IP Address usage for account: " + account.getId());
    }
    if ((endDate == null) || endDate.after(new Date())) {
        endDate = new Date();
    }
    // - query usage_ip_address table with the following criteria:
    //     - look for an entry for accountId with start date in the given range
    //     - look for an entry for accountId with end date in the given range
    //     - look for an entry for accountId with end date null (currently running vm or owned IP)
    //     - look for an entry for accountId with start date before given range *and* end date after given range
    List<UsageIPAddressVO> usageIPAddress = m_usageIPAddressDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate);
    if (usageIPAddress.isEmpty()) {
        s_logger.debug("No IP Address usage for this period");
        return true;
    }
    // This map has both the running time *and* the usage amount.
    Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
    Map<String, IpInfo> IPMap = new HashMap<String, IpInfo>();
    // loop through all the usage IPs, create a usage record for each
    for (UsageIPAddressVO usageIp : usageIPAddress) {
        long IpId = usageIp.getId();
        String key = "" + IpId;
        // store the info in the IP map
        IPMap.put(key, new IpInfo(usageIp.getZoneId(), IpId, usageIp.getAddress(), usageIp.isSourceNat(), usageIp.isSystem()));
        Date IpAssignDate = usageIp.getAssigned();
        Date IpReleaseDeleteDate = usageIp.getReleased();
        if ((IpReleaseDeleteDate == null) || IpReleaseDeleteDate.after(endDate)) {
            IpReleaseDeleteDate = endDate;
        }
        // clip the start date to the beginning of our aggregation range if the vm has been running for a while
        if (IpAssignDate.before(startDate)) {
            IpAssignDate = startDate;
        }
        // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
        long currentDuration = (IpReleaseDeleteDate.getTime() - IpAssignDate.getTime()) + 1;
        updateIpUsageData(usageMap, key, usageIp.getId(), currentDuration);
    }
    for (String ipIdKey : usageMap.keySet()) {
        Pair<Long, Long> ipTimeInfo = usageMap.get(ipIdKey);
        long useTime = ipTimeInfo.second().longValue();
        // Only create a usage record if we have a runningTime of bigger than zero.
        if (useTime > 0L) {
            IpInfo info = IPMap.get(ipIdKey);
            createUsageRecord(info.getZoneId(), useTime, startDate, endDate, account, info.getIpId(), info.getIPAddress(), info.isSourceNat(), info.isSystem);
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) UsageIPAddressVO(com.cloud.usage.UsageIPAddressVO) Date(java.util.Date) Pair(com.cloud.utils.Pair)

Example 2 with Pair

use of com.cloud.utils.Pair in project CloudStack-archive by CloudStack-extras.

the class LoadBalancerUsageParser method parse.

public static boolean parse(AccountVO account, Date startDate, Date endDate) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Parsing all LoadBalancerPolicy usage events for account: " + account.getId());
    }
    if ((endDate == null) || endDate.after(new Date())) {
        endDate = new Date();
    }
    // - query usage_volume table with the following criteria:
    //     - look for an entry for accountId with start date in the given range
    //     - look for an entry for accountId with end date in the given range
    //     - look for an entry for accountId with end date null (currently running vm or owned IP)
    //     - look for an entry for accountId with start date before given range *and* end date after given range
    List<UsageLoadBalancerPolicyVO> usageLBs = m_usageLoadBalancerPolicyDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
    if (usageLBs.isEmpty()) {
        s_logger.debug("No load balancer usage events for this period");
        return true;
    }
    // This map has both the running time *and* the usage amount.
    Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
    Map<String, LBInfo> lbMap = new HashMap<String, LBInfo>();
    // loop through all the load balancer policies, create a usage record for each
    for (UsageLoadBalancerPolicyVO usageLB : usageLBs) {
        long lbId = usageLB.getId();
        String key = "" + lbId;
        lbMap.put(key, new LBInfo(lbId, usageLB.getZoneId()));
        Date lbCreateDate = usageLB.getCreated();
        Date lbDeleteDate = usageLB.getDeleted();
        if ((lbDeleteDate == null) || lbDeleteDate.after(endDate)) {
            lbDeleteDate = endDate;
        }
        // clip the start date to the beginning of our aggregation range if the vm has been running for a while
        if (lbCreateDate.before(startDate)) {
            lbCreateDate = startDate;
        }
        // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
        long currentDuration = (lbDeleteDate.getTime() - lbCreateDate.getTime()) + 1;
        updateLBUsageData(usageMap, key, usageLB.getId(), currentDuration);
    }
    for (String lbIdKey : usageMap.keySet()) {
        Pair<Long, Long> sgtimeInfo = usageMap.get(lbIdKey);
        long useTime = sgtimeInfo.second().longValue();
        // Only create a usage record if we have a runningTime of bigger than zero.
        if (useTime > 0L) {
            LBInfo info = lbMap.get(lbIdKey);
            createUsageRecord(UsageTypes.LOAD_BALANCER_POLICY, useTime, startDate, endDate, account, info.getId(), info.getZoneId());
        }
    }
    return true;
}
Also used : UsageLoadBalancerPolicyVO(com.cloud.usage.UsageLoadBalancerPolicyVO) HashMap(java.util.HashMap) Date(java.util.Date) Pair(com.cloud.utils.Pair)

Example 3 with Pair

use of com.cloud.utils.Pair in project CloudStack-archive by CloudStack-extras.

the class VolumeUsageParser method parse.

public static boolean parse(AccountVO account, Date startDate, Date endDate) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Parsing all Volume usage events for account: " + account.getId());
    }
    if ((endDate == null) || endDate.after(new Date())) {
        endDate = new Date();
    }
    // - query usage_volume table with the following criteria:
    //     - look for an entry for accountId with start date in the given range
    //     - look for an entry for accountId with end date in the given range
    //     - look for an entry for accountId with end date null (currently running vm or owned IP)
    //     - look for an entry for accountId with start date before given range *and* end date after given range
    List<UsageVolumeVO> usageUsageVols = m_usageVolumeDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
    if (usageUsageVols.isEmpty()) {
        s_logger.debug("No volume usage events for this period");
        return true;
    }
    // This map has both the running time *and* the usage amount.
    Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
    Map<String, VolInfo> diskOfferingMap = new HashMap<String, VolInfo>();
    // loop through all the usage volumes, create a usage record for each
    for (UsageVolumeVO usageVol : usageUsageVols) {
        long volId = usageVol.getId();
        Long doId = usageVol.getDiskOfferingId();
        long zoneId = usageVol.getZoneId();
        Long templateId = usageVol.getTemplateId();
        long size = usageVol.getSize();
        String key = "" + volId;
        diskOfferingMap.put(key, new VolInfo(volId, zoneId, doId, templateId, size));
        Date volCreateDate = usageVol.getCreated();
        Date volDeleteDate = usageVol.getDeleted();
        if ((volDeleteDate == null) || volDeleteDate.after(endDate)) {
            volDeleteDate = endDate;
        }
        // clip the start date to the beginning of our aggregation range if the vm has been running for a while
        if (volCreateDate.before(startDate)) {
            volCreateDate = startDate;
        }
        // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
        long currentDuration = (volDeleteDate.getTime() - volCreateDate.getTime()) + 1;
        updateVolUsageData(usageMap, key, usageVol.getId(), currentDuration);
    }
    for (String volIdKey : usageMap.keySet()) {
        Pair<Long, Long> voltimeInfo = usageMap.get(volIdKey);
        long useTime = voltimeInfo.second().longValue();
        // Only create a usage record if we have a runningTime of bigger than zero.
        if (useTime > 0L) {
            VolInfo info = diskOfferingMap.get(volIdKey);
            createUsageRecord(UsageTypes.VOLUME, useTime, startDate, endDate, account, info.getVolumeId(), info.getZoneId(), info.getDiskOfferingId(), info.getTemplateId(), info.getSize());
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) Date(java.util.Date) UsageVolumeVO(com.cloud.usage.UsageVolumeVO) Pair(com.cloud.utils.Pair)

Example 4 with Pair

use of com.cloud.utils.Pair in project CloudStack-archive by CloudStack-extras.

the class VPNUserUsageParser method parse.

public static boolean parse(AccountVO account, Date startDate, Date endDate) {
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Parsing all VPN user usage events for account: " + account.getId());
    }
    if ((endDate == null) || endDate.after(new Date())) {
        endDate = new Date();
    }
    List<UsageVPNUserVO> usageVUs = m_usageVPNUserDao.getUsageRecords(account.getId(), account.getDomainId(), startDate, endDate, false, 0);
    if (usageVUs.isEmpty()) {
        s_logger.debug("No VPN user usage events for this period");
        return true;
    }
    // This map has both the running time *and* the usage amount.
    Map<String, Pair<Long, Long>> usageMap = new HashMap<String, Pair<Long, Long>>();
    Map<String, VUInfo> vuMap = new HashMap<String, VUInfo>();
    // loop through all the VPN user usage, create a usage record for each
    for (UsageVPNUserVO usageVU : usageVUs) {
        long userId = usageVU.getUserId();
        String userName = usageVU.getUsername();
        String key = "" + userId + "VU" + userName;
        vuMap.put(key, new VUInfo(userId, usageVU.getZoneId(), userName));
        Date vuCreateDate = usageVU.getCreated();
        Date vuDeleteDate = usageVU.getDeleted();
        if ((vuDeleteDate == null) || vuDeleteDate.after(endDate)) {
            vuDeleteDate = endDate;
        }
        // clip the start date to the beginning of our aggregation range if the vm has been running for a while
        if (vuCreateDate.before(startDate)) {
            vuCreateDate = startDate;
        }
        // make sure this is an inclusive check for milliseconds (i.e. use n - m + 1 to find total number of millis to charge)
        long currentDuration = (vuDeleteDate.getTime() - vuCreateDate.getTime()) + 1;
        updateVUUsageData(usageMap, key, usageVU.getUserId(), currentDuration);
    }
    for (String vuIdKey : usageMap.keySet()) {
        Pair<Long, Long> vutimeInfo = usageMap.get(vuIdKey);
        long useTime = vutimeInfo.second().longValue();
        // Only create a usage record if we have a runningTime of bigger than zero.
        if (useTime > 0L) {
            VUInfo info = vuMap.get(vuIdKey);
            createUsageRecord(UsageTypes.VPN_USERS, useTime, startDate, endDate, account, info.getUserId(), info.getUserName(), info.getZoneId());
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) UsageVPNUserVO(com.cloud.usage.UsageVPNUserVO) Date(java.util.Date) Pair(com.cloud.utils.Pair)

Example 5 with Pair

use of com.cloud.utils.Pair in project CloudStack-archive by CloudStack-extras.

the class BaseCmd method writeSubObject.

@SuppressWarnings("rawtypes")
private void writeSubObject(StringBuffer sb, String tagName, List tagList, String responseType, int objectCount) {
    if (RESPONSE_TYPE_JSON.equalsIgnoreCase(responseType)) {
        sb.append(((objectCount == 0) ? "\"" + tagName + "\" : [  { " : ", { "));
    } else {
        sb.append("<" + tagName + ">");
    }
    int i = 0;
    for (Object tag : tagList) {
        if (tag instanceof Pair) {
            Pair nameValuePair = (Pair) tag;
            writeNameValuePair(sb, (String) nameValuePair.first(), nameValuePair.second(), responseType, i++);
        }
    }
    if (RESPONSE_TYPE_JSON.equalsIgnoreCase(responseType)) {
        sb.append("}");
    } else {
        sb.append("</" + tagName + ">");
    }
}
Also used : Pair(com.cloud.utils.Pair)

Aggregations

Pair (com.cloud.utils.Pair)513 ArrayList (java.util.ArrayList)274 List (java.util.List)170 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)138 Filter (com.cloud.utils.db.Filter)117 Account (com.cloud.user.Account)113 HashMap (java.util.HashMap)113 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)70 Ternary (com.cloud.utils.Ternary)56 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)51 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)49 Map (java.util.Map)47 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)43 SSHKeyPair (com.cloud.user.SSHKeyPair)39 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)38 DB (com.cloud.utils.db.DB)35 Date (java.util.Date)34 ConfigurationException (javax.naming.ConfigurationException)33 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)30 VolumeVO (com.cloud.storage.VolumeVO)29