2727import org .apache .cloudstack .api .command .user .iso .DeleteIsoCmd ;
2828import org .apache .cloudstack .api .command .user .iso .RegisterIsoCmd ;
2929import org .apache .cloudstack .api .command .user .template .DeleteTemplateCmd ;
30- import org .apache .cloudstack .api .command .user .template .ExtractTemplateCmd ;
3130import org .apache .cloudstack .api .command .user .template .RegisterTemplateCmd ;
3231import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
3332import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
34- import org .apache .cloudstack .engine .subsystem .api .storage .EndPoint ;
3533import org .apache .cloudstack .engine .subsystem .api .storage .EndPointSelector ;
34+ import org .apache .cloudstack .engine .subsystem .api .storage .Scope ;
3635import org .apache .cloudstack .engine .subsystem .api .storage .TemplateDataFactory ;
3736import org .apache .cloudstack .engine .subsystem .api .storage .TemplateInfo ;
3837import org .apache .cloudstack .engine .subsystem .api .storage .TemplateService ;
4746import org .apache .log4j .Logger ;
4847
4948import com .cloud .agent .AgentManager ;
50- import com .cloud .agent .api .Answer ;
51- import com .cloud .agent .api .storage .PrepareOVAPackingCommand ;
5249import com .cloud .alert .AlertManager ;
5350import com .cloud .configuration .Resource .ResourceType ;
54- import com .cloud .dc .DataCenterVO ;
5551import com .cloud .event .EventTypes ;
5652import com .cloud .event .UsageEventUtils ;
5753import com .cloud .exception .InvalidParameterValueException ;
5854import com .cloud .exception .ResourceAllocationException ;
59- import com .cloud .hypervisor .Hypervisor .HypervisorType ;
6055import com .cloud .storage .Storage .ImageFormat ;
6156import com .cloud .storage .Storage .TemplateType ;
6257import com .cloud .storage .TemplateProfile ;
6358import com .cloud .storage .VMTemplateStorageResourceAssoc .Status ;
59+ import com .cloud .storage .ScopeType ;
6460import com .cloud .storage .VMTemplateVO ;
6561import com .cloud .storage .VMTemplateZoneVO ;
6662import com .cloud .storage .dao .VMTemplateZoneDao ;
@@ -154,14 +150,14 @@ private void checkFormat(String format, String url) {
154150 || (format .equalsIgnoreCase ("qcow2" ) && (!url .toLowerCase ().endsWith ("qcow2" )
155151 && !url .toLowerCase ().endsWith ("qcow2.zip" ) && !url .toLowerCase ().endsWith ("qcow2.bz2" ) && !url
156152 .toLowerCase ().endsWith ("qcow2.gz" )))
157- || (format .equalsIgnoreCase ("ova" ) && (!url .toLowerCase ().endsWith ("ova" )
158- && !url .toLowerCase ().endsWith ("ova.zip" ) && !url .toLowerCase ().endsWith ("ova.bz2" ) && !url
159- .toLowerCase ().endsWith ("ova.gz" )))
160- || (format .equalsIgnoreCase ("tar" ) && (!url .toLowerCase ().endsWith ("tar" )
161- && !url .toLowerCase ().endsWith ("tar.zip" ) && !url .toLowerCase ().endsWith ("tar.bz2" ) && !url
162- .toLowerCase ().endsWith ("tar.gz" )))
163- || (format .equalsIgnoreCase ("raw" ) && (!url .toLowerCase ().endsWith ("img" ) && !url .toLowerCase ()
164- .endsWith ("raw" )))) {
153+ || (format .equalsIgnoreCase ("ova" ) && (!url .toLowerCase ().endsWith ("ova" )
154+ && !url .toLowerCase ().endsWith ("ova.zip" ) && !url .toLowerCase ().endsWith ("ova.bz2" ) && !url
155+ .toLowerCase ().endsWith ("ova.gz" )))
156+ || (format .equalsIgnoreCase ("tar" ) && (!url .toLowerCase ().endsWith ("tar" )
157+ && !url .toLowerCase ().endsWith ("tar.zip" ) && !url .toLowerCase ().endsWith ("tar.bz2" ) && !url
158+ .toLowerCase ().endsWith ("tar.gz" )))
159+ || (format .equalsIgnoreCase ("raw" ) && (!url .toLowerCase ().endsWith ("img" ) && !url .toLowerCase ()
160+ .endsWith ("raw" )))) {
165161 throw new InvalidParameterValueException ("Please specify a valid URL. URL:" + url
166162 + " is an invalid for the format " + format .toLowerCase ());
167163 }
@@ -197,13 +193,13 @@ public VMTemplateVO create(TemplateProfile profile) {
197193 return template ;
198194 }
199195
200- private class CreateTemplateContext <T > extends AsyncRpcContext <T > {
201- final TemplateInfo template ;
202- public CreateTemplateContext (AsyncCompletionCallback <T > callback , TemplateInfo template ) {
203- super (callback );
204- this .template = template ;
205- }
206- }
196+ private class CreateTemplateContext <T > extends AsyncRpcContext <T > {
197+ final TemplateInfo template ;
198+ public CreateTemplateContext (AsyncCompletionCallback <T > callback , TemplateInfo template ) {
199+ super (callback );
200+ this .template = template ;
201+ }
202+ }
207203
208204 protected Void createTemplateAsyncCallBack (AsyncCallbackDispatcher <HypervisorTemplateAdapter , TemplateApiResult > callback ,
209205 CreateTemplateContext <TemplateApiResult > context ) {
@@ -217,6 +213,31 @@ protected Void createTemplateAsyncCallBack(AsyncCallbackDispatcher<HypervisorTem
217213 VMTemplateVO tmplt = this ._tmpltDao .findById (template .getId ());
218214 long accountId = tmplt .getAccountId ();
219215 if (template .getSize () != null ) {
216+ // publish usage event
217+ // get physical size from template_store_ref table
218+ long physicalSize = 0 ;
219+ DataStore ds = template .getDataStore ();
220+ TemplateDataStoreVO tmpltStore = _tmpltStoreDao .findByStoreTemplate (ds .getId (), template .getId ());
221+ if (tmpltStore != null ) {
222+ physicalSize = tmpltStore .getPhysicalSize ();
223+ } else {
224+ s_logger .warn ("No entry found in template_store_ref for template id: " + template .getId () + " and image store id: " + ds .getId ()
225+ + " at the end of registering template!" );
226+ }
227+ Scope dsScope = ds .getScope ();
228+ if (dsScope .getScopeType () == ScopeType .ZONE ) {
229+ if (dsScope .getScopeId () != null ) {
230+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_TEMPLATE_CREATE , template .getAccountId (), dsScope .getScopeId (), template .getId (), template .getName (), null ,
231+ null , physicalSize , template .getSize (), VirtualMachineTemplate .class .getName (), template .getUuid ());
232+ }
233+ else {
234+ s_logger .warn ("Zone scope image store " + ds .getId () + " has a null scope id" );
235+ }
236+ } else if (dsScope .getScopeType () == ScopeType .REGION ) {
237+ // publish usage event for region-wide image store using a -1 zoneId for 4.2, need to revisit post-4.2
238+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_TEMPLATE_CREATE , template .getAccountId (), -1 , template .getId (), template .getName (), null , null ,
239+ physicalSize , template .getSize (), VirtualMachineTemplate .class .getName (), template .getUuid ());
240+ }
220241 _resourceLimitMgr .incrementResourceCount (accountId , ResourceType .secondary_storage , template .getSize ());
221242 }
222243 }
@@ -275,7 +296,7 @@ public boolean delete(TemplateProfile profile) {
275296 TemplateApiResult result = future .get ();
276297 success = result .isSuccess ();
277298 if (!success ) {
278- s_logger .warn ("Failed to delete the template " + template +
299+ s_logger .warn ("Failed to delete the template " + template +
279300 " from the image store: " + imageStore .getName () + " due to: " + result .getResult ());
280301 break ;
281302 }
0 commit comments