Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/src/main/java/com/cloud/event/EventTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,10 @@ public class EventTypes {
entityEventDetails.put(EVENT_QUOTA_TARIFF_UPDATE, QuotaTariff.class);
}

public static boolean isNetworkEvent(String eventType) {
return EVENT_NETWORK_CREATE.equals(eventType) || EVENT_NETWORK_DELETE.equals(eventType) ||
EVENT_NETWORK_UPDATE.equals(eventType);
}
public static String getEntityForEvent(String eventName) {
Object entityClass = entityEventDetails.get(eventName);
if (entityClass == null) {
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/java/org/apache/cloudstack/usage/UsageTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class UsageTypes {
public static final int VM_SNAPSHOT_ON_PRIMARY = 27;
public static final int BACKUP = 28;
public static final int BUCKET = 29;
public static final int NETWORK = 30;
public static final int VPC = 31;

public static List<UsageTypeResponse> listUsageTypes() {
Expand Down Expand Up @@ -73,6 +74,7 @@ public static List<UsageTypeResponse> listUsageTypes() {
responseList.add(new UsageTypeResponse(VM_SNAPSHOT_ON_PRIMARY, "VM Snapshot on primary storage usage"));
responseList.add(new UsageTypeResponse(BACKUP, "Backup storage usage"));
responseList.add(new UsageTypeResponse(BUCKET, "Bucket storage usage"));
responseList.add(new UsageTypeResponse(NETWORK, "Network usage"));
responseList.add(new UsageTypeResponse(VPC, "VPC usage"));
return responseList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,8 @@ public Pair<NetworkGuru, NetworkVO> implementNetwork(final long networkId, final
if (isNetworkImplemented(network)) {
s_logger.debug("Network id=" + networkId + " is already implemented");
implemented.set(guru, network);
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_UPDATE, network.getAccountId(), network.getDataCenterId(), network.getId(),
network.getName(), network.getNetworkOfferingId(), null, network.getState().name(), Network.class.getName(), network.getUuid(), true);
return implemented;
}

Expand Down Expand Up @@ -1469,6 +1471,8 @@ public Pair<NetworkGuru, NetworkVO> implementNetwork(final long networkId, final
network.setRestartRequired(false);
_networksDao.update(network.getId(), network);
implemented.set(guru, network);
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_CREATE, network.getAccountId(), network.getDataCenterId(), network.getId(),
network.getName(), network.getNetworkOfferingId(), null, null, null, network.getState().name(), network.getUuid());
return implemented;
} catch (final NoTransitionException e) {
s_logger.error(e.getMessage());
Expand Down Expand Up @@ -3344,6 +3348,8 @@ public void doInTransactionWithoutResult(final TransactionStatus status) {
final Pair<Class<?>, Long> networkMsg = new Pair<Class<?>, Long>(Network.class, networkFinal.getId());
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, networkMsg);
}
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_DELETE, network.getAccountId(), network.getDataCenterId(), network.getId(),
network.getName(), network.getNetworkOfferingId(), null, null, null, Network.class.getName(), network.getUuid());
return true;
} catch (final CloudRuntimeException e) {
s_logger.error("Failed to delete network", e);
Expand Down
143 changes: 143 additions & 0 deletions engine/schema/src/main/java/com/cloud/usage/UsageNetworksVO.java
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;
}
}
11 changes: 11 additions & 0 deletions engine/schema/src/main/java/com/cloud/usage/UsageVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public class UsageVO implements Usage, InternalIdentity {
@Column(name = "is_hidden")
private boolean isHidden = false;

@Column(name = "state")
private String state;

public Integer getQuotaCalculated() {
return quotaCalculated;
}
Expand Down Expand Up @@ -398,6 +401,14 @@ public void setHidden(boolean hidden) {
this.isHidden = hidden;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

@Override
public String toString() {
return ReflectionToStringBuilderUtils.reflectOnlySelectedFields(this, "id", "usageId", "usageType", "startDate", "endDate");
Expand Down
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);
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<bean id="vMInstanceDaoImpl" class="com.cloud.vm.dao.VMInstanceDaoImpl" />
<bean id="vMSnapshotDaoImpl" class="com.cloud.vm.snapshot.dao.VMSnapshotDaoImpl" />
<bean id="VmTemplateDaoImpl" class="org.apache.cloudstack.quota.dao.VmTemplateDaoImpl" />
<bean id="NetworkDaoImpl" class="org.apache.cloudstack.quota.dao.NetworkDaoImpl" />
<bean id="VpcDaoImpl" class="org.apache.cloudstack.quota.dao.VpcDaoImpl" />
<bean id="volumeDaoImpl" class="com.cloud.storage.dao.VolumeDaoImpl" />
<bean id="backupOfferingDaoImpl" class="org.apache.cloudstack.backup.dao.BackupOfferingDaoImpl" />
Expand Down
Loading