Skip to content

Commit 9d263cd

Browse files
authored
Network Usage event model adjustments (#10755)
1 parent 07f4fc2 commit 9d263cd

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.annotation.PostConstruct;
2626
import javax.inject.Inject;
2727

28+
import com.cloud.network.Network;
2829
import org.apache.commons.collections.MapUtils;
2930
import org.apache.log4j.Logger;
3031
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@@ -246,4 +247,22 @@ private static void publishUsageEvent(String usageEventType, Long accountId, Lon
246247

247248
static final String Name = "management-server";
248249

250+
public static void publishNetworkCreation(Network network) {
251+
publishUsageEvent(EventTypes.EVENT_NETWORK_CREATE, network.getAccountId(), network.getDataCenterId(),
252+
network.getId(), network.getName(), network.getNetworkOfferingId(), null, null, null, network.getState().name(),
253+
network.getUuid());
254+
}
255+
256+
public static void publishNetworkUpdate(Network network) {
257+
publishUsageEvent(EventTypes.EVENT_NETWORK_UPDATE, network.getAccountId(), network.getDataCenterId(),
258+
network.getId(), network.getName(), network.getNetworkOfferingId(), null, network.getState().name(),
259+
Network.class.getName(), network.getUuid(), true);
260+
}
261+
262+
public static void publishNetworkDeletion(Network network) {
263+
publishUsageEvent(EventTypes.EVENT_NETWORK_DELETE, network.getAccountId(), network.getDataCenterId(),
264+
network.getId(), network.getName(), network.getNetworkOfferingId(), null, null, null,
265+
Network.class.getName(), network.getUuid());
266+
}
267+
249268
}

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestrator.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,8 +1461,6 @@ public Pair<NetworkGuru, NetworkVO> implementNetwork(final long networkId, final
14611461
if (isNetworkImplemented(network)) {
14621462
s_logger.debug("Network id=" + networkId + " is already implemented");
14631463
implemented.set(guru, network);
1464-
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_UPDATE, network.getAccountId(), network.getDataCenterId(), network.getId(),
1465-
network.getName(), network.getNetworkOfferingId(), null, network.getState().name(), Network.class.getName(), network.getUuid(), true);
14661464
return implemented;
14671465
}
14681466

@@ -1522,9 +1520,8 @@ public Pair<NetworkGuru, NetworkVO> implementNetwork(final long networkId, final
15221520

15231521
network.setRestartRequired(false);
15241522
_networksDao.update(network.getId(), network);
1523+
UsageEventUtils.publishNetworkUpdate(network);
15251524
implemented.set(guru, network);
1526-
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_CREATE, network.getAccountId(), network.getDataCenterId(), network.getId(),
1527-
network.getName(), network.getNetworkOfferingId(), null, null, null, network.getState().name(), network.getUuid());
15281525
return implemented;
15291526
} catch (final NoTransitionException e) {
15301527
s_logger.error(e.getMessage());
@@ -3005,6 +3002,7 @@ public Network doInTransaction(final TransactionStatus status) {
30053002
if (updateResourceCount) {
30063003
_resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.network, isDisplayNetworkEnabled);
30073004
}
3005+
UsageEventUtils.publishNetworkCreation(network);
30083006

30093007
return network;
30103008
}
@@ -3089,12 +3087,13 @@ public boolean shutdownNetwork(final long networkId, final ReservationContext co
30893087
s_logger.debug("Lock is acquired for network " + network + " as a part of network shutdown");
30903088
}
30913089

3092-
if (network.getState() == Network.State.Allocated) {
3090+
final Network.State initialState = network.getState();
3091+
if (initialState == Network.State.Allocated) {
30933092
s_logger.debug(String.format("Network [%s] is in Allocated state, no need to shutdown.", network));
30943093
return true;
30953094
}
30963095

3097-
if (network.getState() != Network.State.Implemented && network.getState() != Network.State.Shutdown) {
3096+
if (initialState != Network.State.Implemented && initialState != Network.State.Shutdown) {
30983097
s_logger.debug("Network is not implemented: " + network);
30993098
return false;
31003099
}
@@ -3141,6 +3140,9 @@ public Boolean doInTransaction(final TransactionStatus status) {
31413140
}
31423141
_networksDao.update(networkFinal.getId(), networkFinal);
31433142
_networksDao.clearCheckForGc(networkId);
3143+
if (initialState == Network.State.Implemented) {
3144+
UsageEventUtils.publishNetworkUpdate(networkFinal);
3145+
}
31443146
result = true;
31453147
} else {
31463148
try {
@@ -3393,8 +3395,7 @@ public List<VlanVO> doInTransaction(TransactionStatus status) {
33933395
final Pair<Class<?>, Long> networkMsg = new Pair<Class<?>, Long>(Network.class, networkFinal.getId());
33943396
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, networkMsg);
33953397
}
3396-
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_DELETE, network.getAccountId(), network.getDataCenterId(), network.getId(),
3397-
network.getName(), network.getNetworkOfferingId(), null, null, null, Network.class.getName(), network.getUuid());
3398+
UsageEventUtils.publishNetworkDeletion(network);
33983399
return true;
33993400
} catch (final CloudRuntimeException e) {
34003401
s_logger.error("Failed to delete network", e);

engine/orchestration/src/test/java/org/apache/cloudstack/engine/orchestration/NetworkOrchestratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ public void testShutdownNetworkInImplementingState() {
894894
boolean shutdownNetworkStatus = testOrchestrator.shutdownNetwork(networkId, reservationContext, false);
895895
Assert.assertFalse(shutdownNetworkStatus);
896896

897-
verify(network, times(3)).getState();
897+
verify(network).getState();
898898
verify(testOrchestrator._networksDao, times(1)).acquireInLockTable(networkId, NetworkLockTimeout.value());
899899
verify(testOrchestrator._networksDao, times(1)).releaseFromLockTable(networkId);
900900
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,12 +2006,7 @@ private Network implementedNetworkInCreation(final Account caller, final DataCen
20062006
if (implementedNetwork == null || implementedNetwork.first() == null) {
20072007
s_logger.warn("Failed to provision the network " + network);
20082008
}
2009-
Network implemented = implementedNetwork.second();
2010-
if (implemented != null) {
2011-
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_CREATE, implemented.getAccountId(), implemented.getDataCenterId(), implemented.getId(),
2012-
implemented.getName(), implemented.getNetworkOfferingId(), null, null, null, Network.class.getName(), implemented.getUuid());
2013-
}
2014-
return implemented;
2009+
return implementedNetwork.second();
20152010
} catch (ResourceUnavailableException ex) {
20162011
s_logger.warn("Failed to implement persistent guest network " + network + "due to ", ex);
20172012
CloudRuntimeException e = new CloudRuntimeException("Failed to implement persistent guest network");
@@ -3407,8 +3402,7 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
34073402
}
34083403
}
34093404
Network updatedNetwork = getNetwork(network.getId());
3410-
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_UPDATE, updatedNetwork.getAccountId(), updatedNetwork.getDataCenterId(), updatedNetwork.getId(),
3411-
updatedNetwork.getName(), updatedNetwork.getNetworkOfferingId(), null, updatedNetwork.getState().name(), Network.class.getName(), updatedNetwork.getUuid(), true);
3405+
UsageEventUtils.publishNetworkUpdate(updatedNetwork);
34123406
return updatedNetwork;
34133407
}
34143408

0 commit comments

Comments
 (0)