2
* Software License Agreement (BSD License)
4
* Copyright (c) 2008, Regents of the University of California
7
* Redistribution and use of this software in source and binary forms, with or
8
* without modification, are permitted provided that the following conditions
11
* * Redistributions of source code must retain the above
12
* copyright notice, this list of conditions and the
13
* following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the
17
* following disclaimer in the documentation and/or other
18
* materials provided with the distribution.
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
* POSSIBILITY OF SUCH DAMAGE.
32
* Author: Chris Grzegorczyk grze@cs.ucsb.edu
35
package edu.ucsb.eucalyptus.cloud.entities;
37
import edu.ucsb.eucalyptus.cloud.EucalyptusCloudException;
38
import edu.ucsb.eucalyptus.msgs.CacheImageType;
39
import edu.ucsb.eucalyptus.msgs.CheckImageType;
40
import edu.ucsb.eucalyptus.msgs.FlushCachedImageType;
41
import edu.ucsb.eucalyptus.msgs.ImageDetails;
42
import edu.ucsb.eucalyptus.util.Messaging;
43
import edu.ucsb.eucalyptus.util.WalrusProperties;
44
import org.hibernate.annotations.Cache;
45
import org.hibernate.annotations.CacheConcurrencyStrategy;
47
import javax.persistence.*;
48
import java.util.ArrayList;
49
import java.util.List;
52
@Table( name = "Images" )
53
@Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
54
public class ImageInfo {
58
@Column( name = "image_id" )
59
private Long id = -1l;
60
@Column( name = "image_name" )
61
private String imageId;
62
@Column( name = "image_path" )
63
private String imageLocation;
64
@Column( name = "image_availability" )
65
private String imageState;
66
@Column( name = "image_owner_id" )
67
private String imageOwnerId;
68
@Column( name = "image_arch" )
69
private String architecture;
70
@Column( name = "image_type" )
71
private String imageType;
72
@Column( name = "image_kernel_id" )
73
private String kernelId;
74
@Column( name = "image_ramdisk_id" )
75
private String ramdiskId;
76
@Column( name = "image_is_public" )
77
private Boolean isPublic;
79
@Column( name = "image_signature" )
80
private String signature;
81
@ManyToMany( cascade = CascadeType.PERSIST )
83
name = "image_has_groups",
84
joinColumns = { @JoinColumn( name = "image_id" ) },
85
inverseJoinColumns = @JoinColumn( name = "user_group_id" )
87
@Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
88
private List<UserGroupInfo> userGroups = new ArrayList<UserGroupInfo>();
91
name = "image_has_perms",
92
joinColumns = { @JoinColumn( name = "image_id" ) },
93
inverseJoinColumns = @JoinColumn( name = "user_id" )
95
@Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
96
private List<UserInfo> permissions = new ArrayList<UserInfo>();
99
name = "image_has_product_codes",
100
joinColumns = { @JoinColumn( name = "image_id" ) },
101
inverseJoinColumns = @JoinColumn( name = "image_product_code_id" )
103
@Cache( usage = CacheConcurrencyStrategy.READ_WRITE )
104
private List<ProductCode> productCodes = new ArrayList<ProductCode>();
106
public static ImageInfo deregistered() {
107
ImageInfo img = new ImageInfo();
108
img.setImageState( "deregistered" );
112
public static ImageInfo byOwnerId( String ownerId ) {
113
ImageInfo img = new ImageInfo();
114
img.setImageOwnerId( ownerId );
118
public ImageInfo() {}
120
public ImageInfo( final String imageId ) {
121
this.imageId = imageId;
124
public ImageInfo( final String imageLocation, final String imageOwnerId, final String imageState, final Boolean aPublic ) {
125
this.imageLocation = imageLocation;
126
this.imageOwnerId = imageOwnerId;
127
this.imageState = imageState;
128
this.isPublic = aPublic;
131
public ImageInfo( String architecture, String imageId, String imageLocation, String imageOwnerId, String imageState, String imageType, Boolean aPublic, String kernelId, String ramdiskId ) {
132
this.architecture = architecture;
133
this.imageId = imageId;
134
this.imageLocation = imageLocation;
135
this.imageOwnerId = imageOwnerId;
136
this.imageState = imageState;
137
this.imageType = imageType;
138
this.isPublic = aPublic;
139
this.kernelId = kernelId;
140
this.ramdiskId = ramdiskId;
143
public Long getId() {
147
public String getArchitecture() {
151
public void setArchitecture( String architecture ) {
152
this.architecture = architecture;
155
public String getImageId() {
159
public void setImageId( String imageId ) {
160
this.imageId = imageId;
163
public String getImageLocation() {
164
return imageLocation;
167
public void setImageLocation( String imageLocation ) {
168
this.imageLocation = imageLocation;
171
public String getImageOwnerId() {
175
public void setImageOwnerId( String imageOwnerId ) {
176
this.imageOwnerId = imageOwnerId;
179
public String getImageState() {
183
public void setImageState( String imageState ) {
184
this.imageState = imageState;
187
public String getImageType() {
191
public void setImageType( String imageType ) {
192
this.imageType = imageType;
195
public Boolean getPublic() {
199
public void setPublic( Boolean aPublic ) {
203
public String getKernelId() {
207
public void setKernelId( String kernelId ) {
208
this.kernelId = kernelId;
211
public String getRamdiskId() {
215
public void setRamdiskId( String ramdiskId ) {
216
this.ramdiskId = ramdiskId;
219
public String getSignature() {
223
public void setSignature( final String signature ) {
224
this.signature = signature;
227
public List<UserGroupInfo> getUserGroups() {
231
public void setUserGroups( final List<UserGroupInfo> userGroups ) {
232
this.userGroups = userGroups;
235
public List<UserInfo> getPermissions() {
239
public void setPermissions( final List<UserInfo> permissions ) {
240
this.permissions = permissions;
243
public ImageDetails getAsImageDetails() {
244
ImageDetails i = new ImageDetails();
245
i.setArchitecture( this.getArchitecture() );
246
i.setImageId( this.getImageId() );
247
i.setImageLocation( this.getImageLocation() );
248
i.setImageOwnerId( this.getImageOwnerId() );
249
i.setImageState( this.getImageState() );
250
i.setImageType( this.getImageType() );
251
i.setIsPublic( this.getPublic() );
252
i.setKernelId( this.getKernelId() );
253
i.setRamdiskId( this.getRamdiskId() );
257
public List<ProductCode> getProductCodes() {
261
public void setProductCodes( final List<ProductCode> productCodes ) {
262
this.productCodes = productCodes;
265
public void checkValid() {
266
String[] parts = this.getImageLocation().split( "/" );
267
CheckImageType check = new CheckImageType();
268
check.setUserId( imageOwnerId );
269
check.setBucket( parts[ 0 ] );
270
check.setKey( parts[ 1 ] );
271
Messaging.dispatch( WalrusProperties.WALRUS_REF, check );
274
public void triggerCaching() {
275
String[] parts = this.getImageLocation().split( "/" );
276
CacheImageType cache = new CacheImageType();
277
cache.setUserId( imageOwnerId );
278
cache.setBucket( parts[ 0 ] );
279
cache.setKey( parts[ 1 ] );
280
Messaging.dispatch( WalrusProperties.WALRUS_REF, cache );
283
public void invalidate() {
284
String[] parts = this.getImageLocation().split( "/" );
285
this.setImageState( "deregistered" );
287
Messaging.dispatch( WalrusProperties.WALRUS_REF, new FlushCachedImageType( parts[ 0 ], parts[ 1 ] ) );
288
} catch ( Exception e ) {}
292
public boolean equals( final Object o ) {
293
if ( this == o ) return true;
294
if ( o == null || getClass() != o.getClass() ) return false;
296
ImageInfo imageInfo = ( ImageInfo ) o;
298
if ( !imageId.equals( imageInfo.imageId ) ) return false;
304
public int hashCode() {
305
return imageId.hashCode();
308
public boolean isAllowed( UserInfo user ) {
309
if ( user.isAdministrator() || user.getUserName().equals( this.getImageOwnerId() ) )
311
for ( UserGroupInfo g : this.getUserGroups() )
312
if ( "all".equals( g.getName() ) )
314
return this.getPermissions().contains( user );
317
public static ImageInfo named( String imageId ) throws EucalyptusCloudException {
318
EntityWrapper<ImageInfo> db = new EntityWrapper<ImageInfo>();
319
ImageInfo image = null;
321
image = db.getUnique( new ImageInfo( imageId ) );