-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Usage] Create network billing #7236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
10b04ad
Add network billing
BryanMLima 82342af
Address Daniel and Joao reviews
BryanMLima 38575a3
Fix update paths
BryanMLima 21385bc
Fix end of files
4d2b465
Change logger to log4j2 apis
BryanMLima 9a39da0
Add logs
BryanMLima 5d38079
Remove leftover schema upgrade command
BryanMLima 1067adc
Add new line at EOF
BryanMLima 2d9f0c4
Fix logs
BryanMLima 5054528
Address reviews
BryanMLima bd05aa5
Merge branch '4.19' of github.com:apache/cloudstack into usage-networ…
BryanMLima d2e2c0c
Address Daan review
BryanMLima 83c1b1a
Merge branch '4.19' of github.com:apache/cloudstack into usage-networ…
BryanMLima cbcf711
Add missing constraints
BryanMLima 9c2669e
Add primary key on schema
BryanMLima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
engine/schema/src/main/java/com/cloud/usage/UsageNetworksVO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.usage; | ||
|
|
||
| import javax.persistence.Column; | ||
| import javax.persistence.Entity; | ||
| import javax.persistence.GeneratedValue; | ||
| import javax.persistence.GenerationType; | ||
| import javax.persistence.Id; | ||
| import javax.persistence.Table; | ||
| import javax.persistence.Temporal; | ||
| import javax.persistence.TemporalType; | ||
| import org.apache.cloudstack.api.InternalIdentity; | ||
|
|
||
| import java.util.Date; | ||
|
|
||
| @Entity | ||
| @Table(name = "usage_networks") | ||
| public class UsageNetworksVO implements InternalIdentity { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "id") | ||
| private long id; | ||
|
|
||
| @Column(name = "network_id") | ||
| private long networkId; | ||
|
|
||
| @Column(name = "network_offering_id") | ||
| private long networkOfferingId; | ||
|
|
||
| @Column(name = "zone_id") | ||
| private long zoneId; | ||
|
|
||
| @Column(name = "account_id") | ||
| private long accountId; | ||
|
|
||
| @Column(name = "domain_id") | ||
| private long domainId; | ||
|
|
||
| @Column(name = "state") | ||
| private String state; | ||
|
|
||
| @Column(name = "created") | ||
| @Temporal(value = TemporalType.TIMESTAMP) | ||
| private Date created = null; | ||
|
|
||
| @Column(name = "removed") | ||
| @Temporal(value = TemporalType.TIMESTAMP) | ||
| private Date removed = null; | ||
|
|
||
| protected UsageNetworksVO() { | ||
| } | ||
|
|
||
| public UsageNetworksVO(long id, long networkId, long networkOfferingId, long zoneId, long accountId, long domainId, String state, Date created, Date removed) { | ||
| this.id = id; | ||
| this.networkId = networkId; | ||
| this.networkOfferingId = networkOfferingId; | ||
| this.zoneId = zoneId; | ||
| this.domainId = domainId; | ||
| this.accountId = accountId; | ||
| this.state = state; | ||
| this.created = created; | ||
| this.removed = removed; | ||
| } | ||
|
|
||
| public UsageNetworksVO(long networkId, long networkOfferingId, long zoneId, long accountId, long domainId, String state, Date created, Date removed) { | ||
| this.networkId = networkId; | ||
| this.networkOfferingId = networkOfferingId; | ||
| this.zoneId = zoneId; | ||
| this.domainId = domainId; | ||
| this.accountId = accountId; | ||
| this.state = state; | ||
| this.created = created; | ||
| this.removed = removed; | ||
| } | ||
|
|
||
| @Override | ||
| public long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public long getZoneId() { | ||
| return zoneId; | ||
| } | ||
|
|
||
| public long getAccountId() { | ||
| return accountId; | ||
| } | ||
|
|
||
| public long getDomainId() { | ||
| return domainId; | ||
| } | ||
|
|
||
| public long getNetworkId() { | ||
| return networkId; | ||
| } | ||
|
|
||
| public void setNetworkId(long networkId) { | ||
| this.networkId = networkId; | ||
| } | ||
|
|
||
| public long getNetworkOfferingId() { | ||
| return networkOfferingId; | ||
| } | ||
|
|
||
| public void setNetworkOfferingId(long networkOfferingId) { | ||
| this.networkOfferingId = networkOfferingId; | ||
| } | ||
|
|
||
| public String getState() { | ||
| return state; | ||
| } | ||
|
|
||
| public void setState(String state) { | ||
| this.state = state; | ||
| } | ||
|
|
||
| public Date getCreated() { | ||
| return created; | ||
| } | ||
|
|
||
| public Date getRemoved() { | ||
| return removed; | ||
| } | ||
|
|
||
| public void setRemoved(Date removed) { | ||
| this.removed = removed; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
engine/schema/src/main/java/com/cloud/usage/dao/UsageNetworksDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.usage.dao; | ||
|
|
||
| import com.cloud.usage.UsageNetworksVO; | ||
| import com.cloud.utils.db.GenericDao; | ||
|
|
||
| import java.util.Date; | ||
| import java.util.List; | ||
|
|
||
| public interface UsageNetworksDao extends GenericDao<UsageNetworksVO, Long> { | ||
| void update(long networkId, long newNetworkOffering, String state); | ||
|
|
||
| void remove(long networkId, Date removed); | ||
|
|
||
| List<UsageNetworksVO> getUsageRecords(Long accountId, Date startDate, Date endDate); | ||
| } |
134 changes: 134 additions & 0 deletions
134
engine/schema/src/main/java/com/cloud/usage/dao/UsageNetworksDaoImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.usage.dao; | ||
|
|
||
| import com.cloud.network.Network; | ||
| import com.cloud.usage.UsageNetworksVO; | ||
| import com.cloud.utils.DateUtil; | ||
| import com.cloud.utils.db.GenericDaoBase; | ||
| import com.cloud.utils.db.SearchCriteria; | ||
| import com.cloud.utils.db.TransactionLegacy; | ||
| import org.apache.log4j.Logger; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
| import java.util.List; | ||
| import java.util.TimeZone; | ||
|
|
||
| @Component | ||
| public class UsageNetworksDaoImpl extends GenericDaoBase<UsageNetworksVO, Long> implements UsageNetworksDao { | ||
| private static final Logger LOGGER = Logger.getLogger(UsageNetworksDaoImpl.class); | ||
| protected static final String GET_USAGE_RECORDS_BY_ACCOUNT = "SELECT id, network_id, network_offering_id, zone_id, account_id, domain_id, state, created, removed FROM usage_networks WHERE " + | ||
| " account_id = ? AND ((removed IS NULL AND created <= ?) OR (created BETWEEN ? AND ?) OR (removed BETWEEN ? AND ?) " + | ||
| " OR ((created <= ?) AND (removed >= ?)))"; | ||
|
|
||
|
|
||
| @Override | ||
| public void update(long networkId, long newNetworkOffering, String state) { | ||
| TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); | ||
| try { | ||
| SearchCriteria<UsageNetworksVO> sc = this.createSearchCriteria(); | ||
| sc.addAnd("networkId", SearchCriteria.Op.EQ, networkId); | ||
| sc.addAnd("removed", SearchCriteria.Op.NULL); | ||
| UsageNetworksVO vo = findOneBy(sc); | ||
| if (vo != null) { | ||
| vo.setNetworkOfferingId(newNetworkOffering); | ||
| vo.setState(state); | ||
| update(vo.getId(), vo); | ||
| } | ||
| } catch (final Exception e) { | ||
| txn.rollback(); | ||
| LOGGER.error(String.format("Error updating usage of network due to [%s].", e.getMessage()), e); | ||
| } finally { | ||
| txn.close(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void remove(long networkId, Date removed) { | ||
| TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); | ||
| try { | ||
| SearchCriteria<UsageNetworksVO> sc = this.createSearchCriteria(); | ||
| sc.addAnd("networkId", SearchCriteria.Op.EQ, networkId); | ||
| sc.addAnd("removed", SearchCriteria.Op.NULL); | ||
| UsageNetworksVO vo = findOneBy(sc); | ||
| if (vo != null) { | ||
| vo.setRemoved(removed); | ||
| vo.setState(Network.State.Destroy.name()); | ||
| update(vo.getId(), vo); | ||
| } | ||
| } catch (final Exception e) { | ||
| txn.rollback(); | ||
| LOGGER.error(String.format("Error updating usage of network due to [%s].", e.getMessage()), e); | ||
| } finally { | ||
| txn.close(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<UsageNetworksVO> getUsageRecords(Long accountId, Date startDate, Date endDate) { | ||
| List<UsageNetworksVO> usageRecords = new ArrayList<>(); | ||
| TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.USAGE_DB); | ||
| PreparedStatement pstmt; | ||
| try { | ||
| int i = 1; | ||
| pstmt = txn.prepareAutoCloseStatement(GET_USAGE_RECORDS_BY_ACCOUNT); | ||
| pstmt.setLong(i++, accountId); | ||
|
|
||
| pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate)); | ||
| pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate)); | ||
| pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate)); | ||
| pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate)); | ||
| pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate)); | ||
| pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), startDate)); | ||
| pstmt.setString(i++, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), endDate)); | ||
|
|
||
| ResultSet rs = pstmt.executeQuery(); | ||
| while (rs.next()) { | ||
| long id = rs.getLong(1); | ||
| long networkId = rs.getLong(2); | ||
| long networkOfferingId = rs.getLong(3); | ||
| long zoneId = rs.getLong(4); | ||
| long acctId = rs.getLong(5); | ||
| long domId = rs.getLong(6); | ||
| String stateTS = rs.getString(7); | ||
| Date createdDate = null; | ||
| Date removedDate = null; | ||
| String createdTS = rs.getString(8); | ||
| String removedTS = rs.getString(9); | ||
|
|
||
| if (createdTS != null) { | ||
| createdDate = DateUtil.parseDateString(s_gmtTimeZone, createdTS); | ||
| } | ||
| if (removedTS != null) { | ||
| removedDate = DateUtil.parseDateString(s_gmtTimeZone, removedTS); | ||
| } | ||
| usageRecords.add(new UsageNetworksVO(id, networkId, networkOfferingId, zoneId, acctId, domId, stateTS, createdDate, removedDate)); | ||
| } | ||
| } catch (Exception e) { | ||
| txn.rollback(); | ||
| LOGGER.warn("Error getting networks usage records", e); | ||
| } finally { | ||
| txn.close(); | ||
| } | ||
|
|
||
| return usageRecords; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.