@@ -1113,10 +1113,6 @@ public UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationE
11131113
11141114 _accountMgr .incrementResourceCount (account .getId (), ResourceType .volume , new Long (volumes .size ()));
11151115
1116- UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_VM_CREATE , vm .getAccountId (), vm .getDataCenterIdToDeployIn (), vm .getId (), vm .getHostName (), vm .getServiceOfferingId (), vm .getTemplateId (), vm
1117- .getHypervisorType ().toString ());
1118- _usageEventDao .persist (usageEvent );
1119-
11201116 _accountMgr .incrementResourceCount (account .getId (), ResourceType .user_vm );
11211117
11221118 txn .commit ();
@@ -1153,6 +1149,8 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
11531149 _executor = Executors .newScheduledThreadPool (wrks , new NamedThreadFactory ("UserVm-Scavenger" ));
11541150
11551151 _itMgr .registerGuru (VirtualMachine .Type .User , this );
1152+
1153+ VirtualMachine .State .getStateMachine ().registerListener (new UserVmStateListener (_usageEventDao , _networkDao , _nicDao ));
11561154
11571155 s_logger .info ("User VM Manager is configured." );
11581156
@@ -1659,8 +1657,6 @@ private void updateVmStateForFailedVmCreation(Long vmId) {
16591657 s_logger .warn ("Unable to delete volume:" + volume .getId () + " for vm:" + vmId + " whilst transitioning to error state" );
16601658 }
16611659 }
1662- UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_VM_DESTROY , vm .getAccountId (), vm .getDataCenterIdToDeployIn (), vm .getId (), vm .getHostName ());
1663- _usageEventDao .persist (usageEvent );
16641660 String msg = "Failed to deploy Vm with Id: " + vmId ;
16651661 _alertMgr .sendAlert (AlertManager .ALERT_TYPE_USERVM , vm .getDataCenterIdToDeployIn (), vm .getPodIdToDeployIn (), msg , msg );
16661662
@@ -2526,9 +2522,10 @@ protected UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOf
25262522 s_logger .debug ("Successfully allocated DB entry for " + vm );
25272523 }
25282524 UserContext .current ().setEventDetails ("Vm Id: " + vm .getId ());
2525+
25292526 UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (), hypervisorType .toString ());
25302527 _usageEventDao .persist (usageEvent );
2531-
2528+
25322529 _accountMgr .incrementResourceCount (accountId , ResourceType .user_vm );
25332530
25342531 // Assign instance to the group
@@ -2715,9 +2712,6 @@ public boolean finalizeCommandsOnStart(Commands cmds, VirtualMachineProfile<User
27152712 @ Override
27162713 public boolean finalizeStart (VirtualMachineProfile <UserVmVO > profile , long hostId , Commands cmds , ReservationContext context ) {
27172714 UserVmVO vm = profile .getVirtualMachine ();
2718- UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_VM_START , vm .getAccountId (), vm .getDataCenterIdToDeployIn (), vm .getId (), vm .getHostName (), vm .getServiceOfferingId (), vm .getTemplateId (), vm
2719- .getHypervisorType ().toString ());
2720- _usageEventDao .persist (usageEvent );
27212715 Answer startAnswer = cmds .getAnswer (StartAnswer .class );
27222716 String returnedIp = null ;
27232717 String originalIp = null ;
@@ -2736,7 +2730,7 @@ public boolean finalizeStart(VirtualMachineProfile<UserVmVO> profile, long hostI
27362730 for (NicVO nic : nics ) {
27372731 NetworkVO network = _networkDao .findById (nic .getNetworkId ());
27382732 long isDefault = (nic .isDefaultNic ()) ? 1 : 0 ;
2739- usageEvent = new UsageEventVO (EventTypes .EVENT_NETWORK_OFFERING_ASSIGN , vm .getAccountId (), vm .getDataCenterIdToDeployIn (), vm .getId (), vm .getHostName (), network .getNetworkOfferingId (), null , isDefault );
2733+ UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_NETWORK_OFFERING_ASSIGN , vm .getAccountId (), vm .getDataCenterIdToDeployIn (), vm .getId (), vm .getHostName (), network .getNetworkOfferingId (), null , isDefault );
27402734 _usageEventDao .persist (usageEvent );
27412735 if (network .getTrafficType () == TrafficType .Guest ) {
27422736 originalIp = nic .getIp4Address ();
@@ -2827,16 +2821,6 @@ public UserVm stopVirtualMachine(long vmId, boolean forced) throws ConcurrentOpe
28272821
28282822 @ Override
28292823 public void finalizeStop (VirtualMachineProfile <UserVmVO > profile , StopAnswer answer ) {
2830- VMInstanceVO vm = profile .getVirtualMachine ();
2831- UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_VM_STOP , vm .getAccountId (), vm .getDataCenterIdToDeployIn (), vm .getId (), vm .getHostName ());
2832- _usageEventDao .persist (usageEvent );
2833-
2834- List <NicVO > nics = _nicDao .listByVmId (vm .getId ());
2835- for (NicVO nic : nics ) {
2836- NetworkVO network = _networkDao .findById (nic .getNetworkId ());
2837- usageEvent = new UsageEventVO (EventTypes .EVENT_NETWORK_OFFERING_REMOVE , vm .getAccountId (), vm .getDataCenterIdToDeployIn (), vm .getId (), null , network .getNetworkOfferingId (), null , 0L );
2838- _usageEventDao .persist (usageEvent );
2839- }
28402824 }
28412825
28422826 public String generateRandomPassword () {
@@ -2912,8 +2896,6 @@ public UserVm destroyVm(long vmId) throws ResourceUnavailableException, Concurre
29122896 _usageEventDao .persist (usageEvent );
29132897 }
29142898 }
2915- UsageEventVO usageEvent = new UsageEventVO (EventTypes .EVENT_VM_DESTROY , vm .getAccountId (), vm .getDataCenterIdToDeployIn (), vm .getId (), vm .getHostName ());
2916- _usageEventDao .persist (usageEvent );
29172899
29182900 if (vmState != State .Error ) {
29192901 _accountMgr .decrementResourceCount (vm .getAccountId (), ResourceType .user_vm );
0 commit comments