Search in sources :

Example 1 with UsageEventVO

use of com.cloud.event.UsageEventVO in project cloudstack by apache.

the class Upgrade218to22 method convertSnapshotEvent.

private UsageEventVO convertSnapshotEvent(EventVO event, Connection conn) throws IOException, SQLException {
    Properties snapEventParams = new Properties();
    long snapId = -1L;
    long snapSize = -1L;
    Long zoneId = 0L;
    UsageEventVO usageEvent = null;
    snapEventParams.load(new StringReader(event.getParameters()));
    snapId = Long.parseLong(snapEventParams.getProperty("id"));
    String snapshotName = snapEventParams.getProperty("ssName");
    String size = snapEventParams.getProperty("size");
    if (size != null) {
        snapSize = Long.parseLong(size);
    }
    String zoneString = snapEventParams.getProperty("dcId");
    if (zoneString != null) {
        zoneId = Long.parseLong(zoneString);
    }
    Long accountId = event.getAccountId();
    // Get snapshot info (there was a bug in 2.1.x - accountId is 0, and data_center info is not present in events table
    if (accountId.longValue() == 0L || zoneId.longValue() == 0L) {
        try (PreparedStatement pstmt = conn.prepareStatement("SELECT zone_id, account_id from usage_event where resource_id=? and type like '%SNAPSHOT%'")) {
            pstmt.setLong(1, snapId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    zoneId = rs.getLong(1);
                    accountId = rs.getLong(2);
                }
            }
            if (EventTypes.EVENT_SNAPSHOT_CREATE.equals(event.getType())) {
                usageEvent = new UsageEventVO(EventTypes.EVENT_SNAPSHOT_CREATE, accountId, zoneId, snapId, snapshotName, null, null, snapSize);
            } else if (EventTypes.EVENT_SNAPSHOT_DELETE.equals(event.getType())) {
                usageEvent = new UsageEventVO(EventTypes.EVENT_SNAPSHOT_DELETE, accountId, zoneId, snapId, snapshotName, null, null, 0L);
            }
        }
    }
    return usageEvent;
}
Also used : StringReader(java.io.StringReader) ResultSet(java.sql.ResultSet) UsageEventVO(com.cloud.event.UsageEventVO) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties)

Example 2 with UsageEventVO

use of com.cloud.event.UsageEventVO in project cloudstack by apache.

the class Upgrade218to22 method convertVMEvent.

private UsageEventVO convertVMEvent(EventVO event) throws IOException {
    Properties vmEventParams = new Properties();
    UsageEventVO usageEvent = null;
    long vmId = -1L;
    // service offering id
    long soId = -1L;
    long zoneId = -1L;
    String eventParams = event.getParameters();
    if (eventParams != null) {
        vmEventParams.load(new StringReader(eventParams));
        vmId = Long.parseLong(vmEventParams.getProperty("id"));
        soId = Long.parseLong(vmEventParams.getProperty("soId"));
        zoneId = Long.parseLong(vmEventParams.getProperty("dcId"));
    }
    if (EventTypes.EVENT_VM_START.equals(event.getType())) {
        long templateId = 0;
        String tId = vmEventParams.getProperty("tId");
        if (tId != null) {
            templateId = Long.parseLong(tId);
        }
        usageEvent = new UsageEventVO(EventTypes.EVENT_VM_START, event.getAccountId(), zoneId, vmId, vmEventParams.getProperty("vmName"), soId, templateId, "");
    } else if (EventTypes.EVENT_VM_STOP.equals(event.getType())) {
        usageEvent = new UsageEventVO(EventTypes.EVENT_VM_STOP, event.getAccountId(), zoneId, vmId, vmEventParams.getProperty("vmName"));
    } else if (EventTypes.EVENT_VM_CREATE.equals(event.getType())) {
        Long templateId = null;
        String tId = vmEventParams.getProperty("tId");
        if (tId != null) {
            templateId = new Long(Long.parseLong(tId));
        }
        usageEvent = new UsageEventVO(EventTypes.EVENT_VM_CREATE, event.getAccountId(), zoneId, vmId, vmEventParams.getProperty("vmName"), soId, templateId, "");
    } else if (EventTypes.EVENT_VM_DESTROY.equals(event.getType())) {
        usageEvent = new UsageEventVO(EventTypes.EVENT_VM_DESTROY, event.getAccountId(), zoneId, vmId, vmEventParams.getProperty("vmName"));
    }
    return usageEvent;
}
Also used : StringReader(java.io.StringReader) UsageEventVO(com.cloud.event.UsageEventVO) Properties(java.util.Properties)

Example 3 with UsageEventVO

use of com.cloud.event.UsageEventVO in project cloudstack by apache.

the class Upgrade218to22 method convertVolumeEvent.

private UsageEventVO convertVolumeEvent(EventVO event, Connection conn) throws IOException, SQLException {
    Properties volEventParams = new Properties();
    long volId = -1L;
    Long doId = -1L;
    long zoneId = -1L;
    Long templateId = -1L;
    long size = -1L;
    UsageEventVO usageEvent = null;
    volEventParams.load(new StringReader(event.getParameters()));
    volId = Long.parseLong(volEventParams.getProperty("id"));
    if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType())) {
        doId = Long.parseLong(volEventParams.getProperty("doId"));
        zoneId = Long.parseLong(volEventParams.getProperty("dcId"));
        templateId = Long.parseLong(volEventParams.getProperty("tId"));
        size = Long.parseLong(volEventParams.getProperty("size"));
        size = (size * 1048576);
        if (doId == -1) {
            doId = null;
        }
        if (templateId == -1) {
            templateId = null;
        }
    }
    // Get volume name information
    String volumeName = "";
    try (PreparedStatement pstmt = conn.prepareStatement("SELECT name, data_center_id from volumes where id=?")) {
        pstmt.setLong(1, volId);
        try (ResultSet rs = pstmt.executeQuery()) {
            if (rs.next()) {
                volumeName = rs.getString(1);
                zoneId = rs.getLong(2);
            }
            if (EventTypes.EVENT_VOLUME_CREATE.equals(event.getType())) {
                usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_CREATE, event.getAccountId(), zoneId, volId, volumeName, doId, templateId, size);
            } else if (EventTypes.EVENT_VOLUME_DELETE.equals(event.getType())) {
                usageEvent = new UsageEventVO(EventTypes.EVENT_VOLUME_DELETE, event.getAccountId(), zoneId, volId, volumeName);
            }
        }
    }
    return usageEvent;
}
Also used : StringReader(java.io.StringReader) ResultSet(java.sql.ResultSet) UsageEventVO(com.cloud.event.UsageEventVO) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties)

Example 4 with UsageEventVO

use of com.cloud.event.UsageEventVO in project cloudstack by apache.

the class Upgrade218to22 method convertISOEvent.

private UsageEventVO convertISOEvent(EventVO event) throws IOException {
    Properties isoEventParams = new Properties();
    long isoId = -1L;
    long isoSize = -1L;
    long zoneId = -1L;
    UsageEventVO usageEvent = null;
    isoEventParams.load(new StringReader(event.getParameters()));
    isoId = Long.parseLong(isoEventParams.getProperty("id"));
    if (isoEventParams.getProperty("dcId") != null) {
        zoneId = Long.parseLong(isoEventParams.getProperty("dcId"));
    }
    if (EventTypes.EVENT_ISO_CREATE.equals(event.getType()) || EventTypes.EVENT_ISO_COPY.equals(event.getType())) {
        isoSize = Long.parseLong(isoEventParams.getProperty("size"));
        usageEvent = new UsageEventVO(event.getType(), event.getAccountId(), zoneId, isoId, "", null, null, isoSize);
    } else if (EventTypes.EVENT_ISO_DELETE.equals(event.getType())) {
        usageEvent = new UsageEventVO(event.getType(), event.getAccountId(), zoneId, isoId, null);
    }
    return usageEvent;
}
Also used : StringReader(java.io.StringReader) UsageEventVO(com.cloud.event.UsageEventVO) Properties(java.util.Properties)

Example 5 with UsageEventVO

use of com.cloud.event.UsageEventVO in project cloudstack by apache.

the class UsageEventDaoImpl method listLatestEvents.

@Override
public List<UsageEventVO> listLatestEvents(Date endDate) {
    Filter filter = new Filter(UsageEventVO.class, "createDate", Boolean.TRUE, null, null);
    SearchCriteria<UsageEventVO> sc = latestEventsSearch.create();
    sc.setParameters("processed", false);
    sc.setParameters("enddate", endDate);
    return listBy(sc, filter);
}
Also used : Filter(com.cloud.utils.db.Filter) UsageEventVO(com.cloud.event.UsageEventVO)

Aggregations

UsageEventVO (com.cloud.event.UsageEventVO)17 StringReader (java.io.StringReader)6 Properties (java.util.Properties)6 Filter (com.cloud.utils.db.Filter)4 PreparedStatement (java.sql.PreparedStatement)4 VMTemplateVO (com.cloud.storage.VMTemplateVO)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 ResultSet (java.sql.ResultSet)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 ConfigurationException (javax.naming.ConfigurationException)3 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)3 VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)2 VolumeVO (com.cloud.storage.VolumeVO)2 Account (com.cloud.user.Account)2 AccountVO (com.cloud.user.AccountVO)2 UserStatisticsVO (com.cloud.user.UserStatisticsVO)2 DB (com.cloud.utils.db.DB)2 SQLException (java.sql.SQLException)2 Calendar (java.util.Calendar)2