Skip to content

Commit 5f8289f

Browse files
Re-enable IP address usage hiding (#4327)
1 parent 63fbbe7 commit 5f8289f

File tree

12 files changed

+99
-26
lines changed

12 files changed

+99
-26
lines changed

api/src/main/java/org/apache/cloudstack/usage/Usage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ public interface Usage {
6363
public Date getEndDate();
6464

6565
public Long getVirtualSize();
66+
67+
public boolean isHidden();
6668
}

engine/components-api/src/main/java/com/cloud/event/UsageEventUtils.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public static void publishUsageEvent(String usageType, long accountId, long zone
116116
}
117117

118118
public static void publishUsageEvent(String usageType, long accountId, long zoneId, long ipAddressId, String ipAddress, boolean isSourceNat, String guestType,
119-
boolean isSystem, String entityType, String entityUUID) {
120-
saveUsageEvent(usageType, accountId, zoneId, ipAddressId, ipAddress, isSourceNat, guestType, isSystem);
119+
boolean isSystem, boolean usageHidden, String entityType, String entityUUID) {
120+
saveUsageEvent(usageType, accountId, zoneId, ipAddressId, ipAddress, isSourceNat, guestType, isSystem, usageHidden);
121121
publishUsageEvent(usageType, accountId, zoneId, entityType, entityUUID);
122122
}
123123

@@ -182,8 +182,12 @@ public static void saveUsageEvent(String usageType, long accountId, long zoneId,
182182
}
183183

184184
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long ipAddressId, String ipAddress, boolean isSourceNat, String guestType,
185-
boolean isSystem) {
186-
s_usageEventDao.persist(new UsageEventVO(usageType, accountId, zoneId, ipAddressId, ipAddress, isSourceNat, guestType, isSystem));
185+
boolean isSystem, boolean usageHidden) {
186+
final UsageEventVO usageEventVO = new UsageEventVO(usageType, accountId, zoneId, ipAddressId, ipAddress, isSourceNat, guestType, isSystem);
187+
s_usageEventDao.persist(usageEventVO);
188+
if (usageHidden) {
189+
s_usageEventDao.saveDetails(usageEventVO.getId(), Map.of("hidden", "true"));
190+
}
187191
}
188192

189193
public static void saveUsageEvent(String usageType, long accountId, long zoneId, long resourceId, String resourceName, Long offeringId, Long templateId,

engine/components-api/src/main/java/com/cloud/network/IpAddressManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,7 @@ void allocateNicValues(NicProfile nic, DataCenter dc, VirtualMachineProfile vm,
206206
public boolean isIpEqualsGatewayOrNetworkOfferingsEmpty(Network network, String requestedIp);
207207

208208
void releasePodIp(Long id) throws CloudRuntimeException;
209+
210+
boolean isUsageHidden(IPAddressVO address);
209211
}
210212

engine/schema/src/main/java/com/cloud/usage/UsageIPAddressVO.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ public class UsageIPAddressVO implements InternalIdentity {
5858
@Temporal(value = TemporalType.TIMESTAMP)
5959
private Date released = null;
6060

61+
@Column(name = "is_hidden")
62+
private boolean isHidden = false;
63+
6164
protected UsageIPAddressVO() {
6265
}
6366

64-
public UsageIPAddressVO(long id, long accountId, long domainId, long zoneId, String address, boolean isSourceNat, boolean isSystem, Date assigned, Date released) {
67+
public UsageIPAddressVO(long id, long accountId, long domainId, long zoneId, String address, boolean isSourceNat, boolean isSystem, Date assigned, Date released, boolean isHidden) {
6568
this.id = id;
6669
this.accountId = accountId;
6770
this.domainId = domainId;
@@ -71,6 +74,7 @@ public UsageIPAddressVO(long id, long accountId, long domainId, long zoneId, Str
7174
this.isSystem = isSystem;
7275
this.assigned = assigned;
7376
this.released = released;
77+
this.isHidden = isHidden;
7478
}
7579

7680
public UsageIPAddressVO(long accountId, String address, Date assigned, Date released) {
@@ -120,4 +124,8 @@ public Date getReleased() {
120124
public void setReleased(Date released) {
121125
this.released = released;
122126
}
127+
128+
public boolean isHidden() {
129+
return isHidden;
130+
}
123131
}

engine/schema/src/main/java/com/cloud/usage/UsageVO.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public class UsageVO implements Usage, InternalIdentity {
106106
@Column(name = "quota_calculated")
107107
private Integer quotaCalculated = 0;
108108

109+
@Column(name = "is_hidden")
110+
private boolean isHidden = false;
111+
109112
public Integer getQuotaCalculated() {
110113
return quotaCalculated;
111114
}
@@ -215,7 +218,7 @@ public UsageVO(Long zoneId, Long accountId, Long domainId, String description, S
215218

216219
//IPAddress Usage
217220
public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay, int usageType, Double rawUsage, Long usageId, long size,
218-
String type, Date startDate, Date endDate) {
221+
String type, Date startDate, Date endDate, boolean isHidden) {
219222
this.zoneId = zoneId;
220223
this.accountId = accountId;
221224
this.domainId = domainId;
@@ -228,6 +231,7 @@ public UsageVO(Long zoneId, Long accountId, Long domainId, String description, S
228231
this.type = type;
229232
this.startDate = startDate == null ? null : new Date(startDate.getTime());
230233
this.endDate = endDate == null ? null : new Date(endDate.getTime());
234+
this.isHidden = isHidden;
231235
}
232236

233237
@Override
@@ -340,6 +344,11 @@ public Date getEndDate() {
340344
return endDate == null ? null : new Date(endDate.getTime());
341345
}
342346

347+
@Override
348+
public boolean isHidden() {
349+
return isHidden;
350+
}
351+
343352
public void setId(Long id) {
344353
this.id = id;
345354
}
@@ -383,4 +392,8 @@ public void setSize(Long size) {
383392
public void setVirtualSize(Long virtualSize) {
384393
this.virtualSize = virtualSize;
385394
}
395+
396+
public void setHidden(boolean hidden) {
397+
this.isHidden = hidden;
398+
}
386399
}

engine/schema/src/main/java/com/cloud/usage/dao/UsageIPAddressDaoImpl.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,20 @@ public class UsageIPAddressDaoImpl extends GenericDaoBase<UsageIPAddressVO, Long
4040

4141
protected static final String UPDATE_RELEASED = "UPDATE usage_ip_address SET released = ? WHERE account_id = ? AND public_ip_address = ? and released IS NULL";
4242
protected static final String GET_USAGE_RECORDS_BY_ACCOUNT =
43-
"SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_system, assigned, released " + "FROM usage_ip_address "
43+
"SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_system, assigned, released, is_hidden "
44+
+ "FROM usage_ip_address "
4445
+ "WHERE account_id = ? AND ((released IS NULL AND assigned <= ?) OR (assigned BETWEEN ? AND ?) OR "
4546
+ " (released BETWEEN ? AND ?) OR ((assigned <= ?) AND (released >= ?)))";
4647
protected static final String GET_USAGE_RECORDS_BY_DOMAIN =
47-
"SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_system, assigned, released " + "FROM usage_ip_address "
48+
"SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_system, assigned, released, is_hidden "
49+
+ "FROM usage_ip_address "
4850
+ "WHERE domain_id = ? AND ((released IS NULL AND assigned <= ?) OR (assigned BETWEEN ? AND ?) OR "
4951
+ " (released BETWEEN ? AND ?) OR ((assigned <= ?) AND (released >= ?)))";
50-
protected static final String GET_ALL_USAGE_RECORDS = "SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_system, assigned, released "
51-
+ "FROM usage_ip_address " + "WHERE (released IS NULL AND assigned <= ?) OR (assigned BETWEEN ? AND ?) OR "
52-
+ " (released BETWEEN ? AND ?) OR ((assigned <= ?) AND (released >= ?))";
52+
protected static final String GET_ALL_USAGE_RECORDS =
53+
"SELECT id, account_id, domain_id, zone_id, public_ip_address, is_source_nat, is_system, assigned, released, is_hidden "
54+
+ "FROM usage_ip_address "
55+
+ "WHERE (released IS NULL AND assigned <= ?) OR (assigned BETWEEN ? AND ?) OR "
56+
+ " (released BETWEEN ? AND ?) OR ((assigned <= ?) AND (released >= ?))";
5357

5458
public UsageIPAddressDaoImpl() {
5559
}
@@ -128,6 +132,7 @@ public List<UsageIPAddressVO> getUsageRecords(Long accountId, Long domainId, Dat
128132
Date releasedDate = null;
129133
String assignedTS = rs.getString(8);
130134
String releasedTS = rs.getString(9);
135+
Boolean isHidden = Boolean.valueOf(rs.getBoolean(10));
131136

132137
if (assignedTS != null) {
133138
assignedDate = DateUtil.parseDateString(s_gmtTimeZone, assignedTS);
@@ -136,7 +141,7 @@ public List<UsageIPAddressVO> getUsageRecords(Long accountId, Long domainId, Dat
136141
releasedDate = DateUtil.parseDateString(s_gmtTimeZone, releasedTS);
137142
}
138143

139-
usageRecords.add(new UsageIPAddressVO(id, acctId, dId, zId, addr, isSourceNat, isSystem, assignedDate, releasedDate));
144+
usageRecords.add(new UsageIPAddressVO(id, acctId, dId, zId, addr, isSourceNat, isSystem, assignedDate, releasedDate, isHidden));
140145
}
141146
} catch (Exception e) {
142147
txn.rollback();

engine/schema/src/main/resources/META-INF/db/schema-41400to41500.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,3 +833,7 @@ INSERT INTO `cloud`.`guest_os_hypervisor` (uuid,hypervisor_type, hypervisor_vers
833833

834834
-- Fix OS category for Guest OS 'Other PV Virtio-SCSI (64-bit)'
835835
UPDATE `cloud`.`guest_os` SET category_id = 7 WHERE id = 275 AND display_name = 'Other PV Virtio-SCSI (64-bit)';
836+
837+
-- Add flag 'hidden' in tables usage_ip_address and cloud_usage
838+
ALTER TABLE `cloud_usage`.`usage_ip_address` ADD COLUMN `is_hidden` smallint(1) NOT NULL DEFAULT '0' COMMENT 'is usage hidden';
839+
ALTER TABLE `cloud_usage`.`cloud_usage` ADD COLUMN `is_hidden` smallint(1) NOT NULL DEFAULT '0' COMMENT 'is usage hidden';

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4123,8 +4123,9 @@ public VlanVO doInTransaction(final TransactionStatus status) {
41234123
// range
41244124
final List<IPAddressVO> ips = _publicIpAddressDao.listByVlanId(vlan.getId());
41254125
for (final IPAddressVO ip : ips) {
4126+
final boolean usageHidden = _ipAddrMgr.isUsageHidden(ip);
41264127
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, vlanOwner.getId(), ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(),
4127-
ip.isSourceNat(), vlan.getVlanType().toString(), ip.getSystem(), ip.getClass().getName(), ip.getUuid());
4128+
ip.isSourceNat(), vlan.getVlanType().toString(), ip.getSystem(), usageHidden, ip.getClass().getName(), ip.getUuid());
41284129
}
41294130
// increment resource count for dedicated public ip's
41304131
_resourceLimitMgr.incrementResourceCount(vlanOwner.getId(), ResourceType.public_ip, new Long(ips.size()));
@@ -4204,8 +4205,9 @@ public boolean deleteVlanAndPublicIpRange(final long userId, final long vlanDbId
42044205
s_logger.warn("Some ip addresses failed to be released as a part of vlan " + vlanDbId + " removal");
42054206
} else {
42064207
resourceCountToBeDecrement++;
4208+
final boolean usageHidden = _ipAddrMgr.isUsageHidden(ip);
42074209
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getAccountId(), ip.getDataCenterId(), ip.getId(),
4208-
ip.getAddress().toString(), ip.isSourceNat(), vlanRange.getVlanType().toString(), ip.getSystem(), ip.getClass().getName(), ip.getUuid());
4210+
ip.getAddress().toString(), ip.isSourceNat(), vlanRange.getVlanType().toString(), ip.getSystem(), usageHidden, ip.getClass().getName(), ip.getUuid());
42094211
}
42104212
}
42114213
} finally {
@@ -4347,8 +4349,9 @@ public Vlan dedicatePublicIpRange(final DedicatePublicIpRangeCmd cmd) throws Res
43474349

43484350
// generate usage event for dedication of every ip address in the range
43494351
for (final IPAddressVO ip : ips) {
4352+
final boolean usageHidden = _ipAddrMgr.isUsageHidden(ip);
43504353
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, vlanOwner.getId(), ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(), ip.isSourceNat(),
4351-
vlan.getVlanType().toString(), ip.getSystem(), ip.getClass().getName(), ip.getUuid());
4354+
vlan.getVlanType().toString(), ip.getSystem(), usageHidden, ip.getClass().getName(), ip.getUuid());
43524355
}
43534356
} else if (domain != null && !forSystemVms) {
43544357
// Create an DomainVlanMapVO entry
@@ -4441,8 +4444,9 @@ public boolean releasePublicIpRange(final long vlanDbId, final long userId, fina
44414444
// generate usage events to remove dedication for every ip in the range that has been disassociated
44424445
for (final IPAddressVO ip : ips) {
44434446
if (!ipsInUse.contains(ip)) {
4447+
final boolean usageHidden = _ipAddrMgr.isUsageHidden(ip);
44444448
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_RELEASE, acctVln.get(0).getAccountId(), ip.getDataCenterId(), ip.getId(), ip.getAddress().toString(),
4445-
ip.isSourceNat(), vlan.getVlanType().toString(), ip.getSystem(), ip.getClass().getName(), ip.getUuid());
4449+
ip.isSourceNat(), vlan.getVlanType().toString(), ip.getSystem(), usageHidden, ip.getClass().getName(), ip.getUuid());
44464450
}
44474451
}
44484452
// decrement resource count for dedicated public ip's

server/src/main/java/com/cloud/network/IpAddressManagerImpl.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
import com.cloud.network.dao.LoadBalancerDao;
102102
import com.cloud.network.dao.NetworkAccountDao;
103103
import com.cloud.network.dao.NetworkDao;
104+
import com.cloud.network.dao.NetworkDetailsDao;
105+
import com.cloud.network.dao.NetworkDetailVO;
104106
import com.cloud.network.dao.NetworkDomainDao;
105107
import com.cloud.network.dao.NetworkServiceMapDao;
106108
import com.cloud.network.dao.PhysicalNetworkDao;
@@ -212,6 +214,8 @@ public class IpAddressManagerImpl extends ManagerBase implements IpAddressManage
212214
@Inject
213215
NetworkDao _networksDao;
214216
@Inject
217+
NetworkDetailsDao _networkDetailsDao;
218+
@Inject
215219
NicDao _nicDao;
216220
@Inject
217221
RulesManager _rulesMgr;
@@ -924,9 +928,10 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
924928
VlanVO vlan = _vlanDao.findById(addr.getVlanId());
925929
String guestType = vlan.getVlanType().toString();
926930
if (!isIpDedicated(addr)) {
931+
final boolean usageHidden = isUsageHidden(addr);
927932
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, owner.getId(), addr.getDataCenterId(), addr.getId(),
928-
addr.getAddress().toString(),
929-
addr.isSourceNat(), guestType, addr.getSystem(), addr.getClass().getName(), addr.getUuid());
933+
addr.getAddress().toString(), addr.isSourceNat(), guestType, addr.getSystem(), usageHidden,
934+
addr.getClass().getName(), addr.getUuid());
930935
}
931936
if (updateIpResourceCount(addr)) {
932937
_resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.public_ip);
@@ -1277,9 +1282,10 @@ public IPAddressVO doInTransaction(TransactionStatus status) throws Insufficient
12771282
ipaddr.setAllocatedInDomainId(ipOwner.getDomainId());
12781283
ipaddr.setAllocatedToAccountId(ipOwner.getId());
12791284
ipaddr = _ipAddressDao.persist(ipaddr);
1285+
final boolean usageHidden = isUsageHidden(ipaddr);
12801286

12811287
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_PORTABLE_IP_ASSIGN, ipaddr.getId(), ipaddr.getDataCenterId(), ipaddr.getId(),
1282-
ipaddr.getAddress().toString(), ipaddr.isSourceNat(), null, ipaddr.getSystem(), ipaddr.getClass().getName(), ipaddr.getUuid());
1288+
ipaddr.getAddress().toString(), ipaddr.isSourceNat(), null, ipaddr.getSystem(), usageHidden, ipaddr.getClass().getName(), ipaddr.getUuid());
12831289

12841290
return ipaddr;
12851291
}
@@ -1829,8 +1835,9 @@ public IPAddressVO doInTransaction(TransactionStatus status) {
18291835
String guestType = vlan.getVlanType().toString();
18301836
if (!isIpDedicated(ip)) {
18311837
String eventType = ip.isPortable() ? EventTypes.EVENT_PORTABLE_IP_RELEASE : EventTypes.EVENT_NET_IP_RELEASE;
1838+
final boolean usageHidden = isUsageHidden(ip);
18321839
UsageEventUtils.publishUsageEvent(eventType, ip.getAllocatedToAccountId(), ip.getDataCenterId(), addrId, ip.getAddress().addr(), ip.isSourceNat(),
1833-
guestType, ip.getSystem(), ip.getClass().getName(), ip.getUuid());
1840+
guestType, ip.getSystem(), usageHidden, ip.getClass().getName(), ip.getUuid());
18341841
}
18351842
}
18361843

@@ -2238,4 +2245,17 @@ public boolean isIpEqualsGatewayOrNetworkOfferingsEmpty(Network network, String
22382245
}
22392246
return false;
22402247
}
2248+
2249+
@Override
2250+
public boolean isUsageHidden(IPAddressVO ip) {
2251+
Long networkId = ip.getAssociatedWithNetworkId();
2252+
if (networkId == null) {
2253+
networkId = ip.getSourceNetworkId();
2254+
}
2255+
if (networkId == null) {
2256+
throw new CloudRuntimeException("No network for IP " + ip.getId());
2257+
}
2258+
NetworkDetailVO networkDetail = _networkDetailsDao.findDetail(networkId, Network.hideIpAddressUsage);
2259+
return networkDetail != null && "true".equals(networkDetail.getValue());
2260+
}
22412261
}

server/src/main/java/com/cloud/usage/UsageServiceImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ public Pair<List<? extends Usage>, Integer> getUsageRecords(ListUsageRecordsCmd
347347
}
348348
}
349349

350+
// Filter out hidden usages
351+
sc.addAnd("isHidden", SearchCriteria.Op.EQ, false);
352+
350353
if ((adjustedStartDate != null) && (adjustedEndDate != null) && adjustedStartDate.before(adjustedEndDate)) {
351354
sc.addAnd("startDate", SearchCriteria.Op.BETWEEN, adjustedStartDate, adjustedEndDate);
352355
sc.addAnd("endDate", SearchCriteria.Op.BETWEEN, adjustedStartDate, adjustedEndDate);

0 commit comments

Comments
 (0)