|
27 | 27 | import javax.inject.Inject; |
28 | 28 | import javax.naming.ConfigurationException; |
29 | 29 |
|
| 30 | +import com.cloud.host.HostVO; |
| 31 | +import com.cloud.host.dao.HostDao; |
| 32 | +import com.cloud.network.VpnUserVO; |
| 33 | +import com.cloud.network.dao.LoadBalancerVO; |
| 34 | +import com.cloud.network.dao.IPAddressVO; |
| 35 | +import com.cloud.network.dao.IPAddressDao; |
| 36 | +import com.cloud.network.dao.LoadBalancerDao; |
| 37 | +import com.cloud.network.dao.VpnUserDao; |
| 38 | +import com.cloud.network.rules.PortForwardingRuleVO; |
| 39 | +import com.cloud.network.rules.dao.PortForwardingRulesDao; |
| 40 | +import com.cloud.network.security.SecurityGroupVO; |
| 41 | +import com.cloud.network.security.dao.SecurityGroupDao; |
| 42 | +import com.cloud.storage.SnapshotVO; |
| 43 | +import com.cloud.storage.VMTemplateVO; |
| 44 | +import com.cloud.storage.VolumeVO; |
| 45 | +import com.cloud.storage.dao.SnapshotDao; |
| 46 | +import com.cloud.storage.dao.VMTemplateDao; |
| 47 | +import com.cloud.storage.dao.VolumeDao; |
| 48 | +import com.cloud.vm.VMInstanceVO; |
| 49 | +import com.cloud.vm.dao.VMInstanceDao; |
30 | 50 | import org.apache.log4j.Logger; |
31 | 51 | import org.springframework.stereotype.Component; |
32 | 52 |
|
@@ -81,6 +101,26 @@ public class UsageServiceImpl extends ManagerBase implements UsageService, Manag |
81 | 101 | private TimeZone _usageTimezone; |
82 | 102 | @Inject |
83 | 103 | private AccountService _accountService; |
| 104 | + @Inject |
| 105 | + private VMInstanceDao _vmDao; |
| 106 | + @Inject |
| 107 | + private SnapshotDao _snapshotDao; |
| 108 | + @Inject |
| 109 | + private SecurityGroupDao _sgDao; |
| 110 | + @Inject |
| 111 | + private VpnUserDao _vpnUserDao; |
| 112 | + @Inject |
| 113 | + private PortForwardingRulesDao _pfDao; |
| 114 | + @Inject |
| 115 | + private LoadBalancerDao _lbDao; |
| 116 | + @Inject |
| 117 | + private VMTemplateDao _vmTemplateDao; |
| 118 | + @Inject |
| 119 | + private VolumeDao _volumeDao; |
| 120 | + @Inject |
| 121 | + private IPAddressDao _ipDao; |
| 122 | + @Inject |
| 123 | + private HostDao _hostDao; |
84 | 124 |
|
85 | 125 | public UsageServiceImpl() { |
86 | 126 | } |
@@ -131,6 +171,7 @@ public Pair<List<? extends Usage>, Integer> getUsageRecords(GetUsageRecordsCmd c |
131 | 171 | Account caller = CallContext.current().getCallingAccount(); |
132 | 172 | Long usageType = cmd.getUsageType(); |
133 | 173 | Long projectId = cmd.getProjectId(); |
| 174 | + String usageId = cmd.getUsageId(); |
134 | 175 |
|
135 | 176 | if (projectId != null) { |
136 | 177 | if (accountId != null) { |
@@ -217,6 +258,96 @@ public Pair<List<? extends Usage>, Integer> getUsageRecords(GetUsageRecordsCmd c |
217 | 258 | sc.addAnd("usageType", SearchCriteria.Op.EQ, usageType); |
218 | 259 | } |
219 | 260 |
|
| 261 | + if (usageId != null) { |
| 262 | + if (usageType == null) { |
| 263 | + throw new InvalidParameterValueException("Usageid must be specified together with usageType"); |
| 264 | + } |
| 265 | + |
| 266 | + Long usageDbId = null; |
| 267 | + |
| 268 | + switch (usageType.intValue()) { |
| 269 | + case UsageTypes.NETWORK_BYTES_RECEIVED: |
| 270 | + case UsageTypes.NETWORK_BYTES_SENT: |
| 271 | + case UsageTypes.RUNNING_VM: |
| 272 | + case UsageTypes.ALLOCATED_VM: |
| 273 | + case UsageTypes.VM_SNAPSHOT: |
| 274 | + VMInstanceVO vm = _vmDao.findByUuidIncludingRemoved(usageId); |
| 275 | + if (vm != null) { |
| 276 | + usageDbId = vm.getId(); |
| 277 | + } |
| 278 | + |
| 279 | + if (vm == null && (usageType == UsageTypes.NETWORK_BYTES_RECEIVED || usageType == UsageTypes.NETWORK_BYTES_SENT)) { |
| 280 | + HostVO host = _hostDao.findByUuidIncludingRemoved(usageId); |
| 281 | + if (host != null) { |
| 282 | + usageDbId = host.getId(); |
| 283 | + } |
| 284 | + } |
| 285 | + break; |
| 286 | + case UsageTypes.SNAPSHOT: |
| 287 | + SnapshotVO snap = _snapshotDao.findByUuidIncludingRemoved(usageId); |
| 288 | + if (snap != null) { |
| 289 | + usageDbId = snap.getId(); |
| 290 | + } |
| 291 | + break; |
| 292 | + case UsageTypes.TEMPLATE: |
| 293 | + case UsageTypes.ISO: |
| 294 | + VMTemplateVO tmpl = _vmTemplateDao.findByUuidIncludingRemoved(usageId); |
| 295 | + if (tmpl != null) { |
| 296 | + usageDbId = tmpl.getId(); |
| 297 | + } |
| 298 | + break; |
| 299 | + case UsageTypes.LOAD_BALANCER_POLICY: |
| 300 | + LoadBalancerVO lb = _lbDao.findByUuidIncludingRemoved(usageId); |
| 301 | + if (lb != null) { |
| 302 | + usageDbId = lb.getId(); |
| 303 | + } |
| 304 | + break; |
| 305 | + case UsageTypes.PORT_FORWARDING_RULE: |
| 306 | + PortForwardingRuleVO pf = _pfDao.findByUuidIncludingRemoved(usageId); |
| 307 | + if (pf != null) { |
| 308 | + usageDbId = pf.getId(); |
| 309 | + } |
| 310 | + break; |
| 311 | + case UsageTypes.VOLUME: |
| 312 | + case UsageTypes.VM_DISK_IO_READ: |
| 313 | + case UsageTypes.VM_DISK_IO_WRITE: |
| 314 | + case UsageTypes.VM_DISK_BYTES_READ: |
| 315 | + case UsageTypes.VM_DISK_BYTES_WRITE: |
| 316 | + VolumeVO volume = _volumeDao.findByUuidIncludingRemoved(usageId); |
| 317 | + if (volume != null) { |
| 318 | + usageDbId = volume.getId(); |
| 319 | + } |
| 320 | + break; |
| 321 | + case UsageTypes.VPN_USERS: |
| 322 | + VpnUserVO vpnUser = _vpnUserDao.findByUuidIncludingRemoved(usageId); |
| 323 | + if (vpnUser != null) { |
| 324 | + usageDbId = vpnUser.getId(); |
| 325 | + } |
| 326 | + break; |
| 327 | + case UsageTypes.SECURITY_GROUP: |
| 328 | + SecurityGroupVO sg = _sgDao.findByUuidIncludingRemoved(usageId); |
| 329 | + if (sg != null) { |
| 330 | + usageDbId = sg.getId(); |
| 331 | + } |
| 332 | + break; |
| 333 | + case UsageTypes.IP_ADDRESS: |
| 334 | + IPAddressVO ip = _ipDao.findByUuidIncludingRemoved(usageId); |
| 335 | + if (ip != null) { |
| 336 | + usageDbId = ip.getId(); |
| 337 | + } |
| 338 | + break; |
| 339 | + default: |
| 340 | + break; |
| 341 | + } |
| 342 | + |
| 343 | + if (usageDbId != null) { |
| 344 | + sc.addAnd("usageId", SearchCriteria.Op.EQ, usageDbId); |
| 345 | + } else { |
| 346 | + // return an empty list if usageId was not found |
| 347 | + return new Pair<List<? extends Usage>, Integer>(new ArrayList<Usage>(), new Integer(0)); |
| 348 | + } |
| 349 | + } |
| 350 | + |
220 | 351 | if ((adjustedStartDate != null) && (adjustedEndDate != null) && adjustedStartDate.before(adjustedEndDate)) { |
221 | 352 | sc.addAnd("startDate", SearchCriteria.Op.BETWEEN, adjustedStartDate, adjustedEndDate); |
222 | 353 | sc.addAnd("endDate", SearchCriteria.Op.BETWEEN, adjustedStartDate, adjustedEndDate); |
|
0 commit comments