1
/*******************************************************************************
2
* Copyright (c) 2009 Eucalyptus Systems, Inc.
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, only version 3 of the License.
9
* This file is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU General Public License along
15
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Please contact Eucalyptus Systems, Inc., 130 Castilian
18
* Dr., Goleta, CA 93101 USA or visit <http://www.eucalyptus.com/licenses/>
19
* if you need additional information or have any questions.
21
* This file may incorporate work covered under the following copyright and
24
* Software License Agreement (BSD License)
26
* Copyright (c) 2008, Regents of the University of California
27
* All rights reserved.
29
* Redistribution and use of this software in source and binary forms, with
30
* or without modification, are permitted provided that the following
33
* Redistributions of source code must retain the above copyright notice,
34
* this list of conditions and the following disclaimer.
36
* Redistributions in binary form must reproduce the above copyright
37
* notice, this list of conditions and the following disclaimer in the
38
* documentation and/or other materials provided with the distribution.
40
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
41
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
42
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
43
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
44
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
46
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
47
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
49
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. USERS OF
51
* THIS SOFTWARE ACKNOWLEDGE THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE
52
* LICENSED MATERIAL, COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS
53
* SOFTWARE, AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
54
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA, SANTA
55
* BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY, WHICH IN
56
* THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION, REPLACEMENT
57
* OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO IDENTIFIED, OR
58
* WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT NEEDED TO COMPLY WITH
59
* ANY SUCH LICENSES OR RIGHTS.
60
*******************************************************************************
61
* @author chris grzegorczyk <grze@eucalyptus.com>
64
package com.eucalyptus.network;
66
import javax.persistence.Column;
67
import javax.persistence.FetchType;
68
import javax.persistence.JoinColumn;
69
import javax.persistence.ManyToOne;
70
import javax.persistence.OneToOne;
71
import javax.persistence.PersistenceContext;
72
import javax.persistence.Table;
73
import javax.persistence.Transient;
74
import org.hibernate.annotations.Cache;
75
import org.hibernate.annotations.CacheConcurrencyStrategy;
76
import org.hibernate.annotations.Entity;
77
import org.hibernate.annotations.NotFound;
78
import org.hibernate.annotations.NotFoundAction;
79
import com.eucalyptus.cloud.AccountMetadata;
80
import com.eucalyptus.cloud.util.PersistentReference;
81
import com.eucalyptus.cloud.util.Reference;
82
import com.eucalyptus.cloud.util.ResourceAllocationException;
83
import com.eucalyptus.component.ComponentIds;
84
import com.eucalyptus.component.id.Eucalyptus;
85
import com.eucalyptus.util.FullName;
86
import com.eucalyptus.vm.VmInstance;
89
@javax.persistence.Entity
90
@PersistenceContext( name = "eucalyptus_cloud" )
91
@Table( name = "metadata_network_indices" )
92
@Cache( usage = CacheConcurrencyStrategy.TRANSACTIONAL )
93
public class PrivateNetworkIndex extends PersistentReference<PrivateNetworkIndex, VmInstance> {
95
private static final PrivateNetworkIndex bogusIndex = new PrivateNetworkIndex( -1, -1l );
96
@Column( name = "metadata_network_index" )
97
private final Long index;
98
@Column( name = "metadata_network_index_bogus_id", unique = true )
99
private final String bogusId;
101
@JoinColumn( name = "metadata_network_index_extant_network_fk" )
102
@Cache( usage = CacheConcurrencyStrategy.TRANSACTIONAL )
103
private ExtantNetwork extantNetwork;
104
@NotFound( action = NotFoundAction.IGNORE )
105
@OneToOne( mappedBy = "networkIndex", fetch = FetchType.LAZY, optional = true )
106
private VmInstance instance;
108
private PrivateNetworkIndex( ) {
111
this.extantNetwork = null;
115
private PrivateNetworkIndex( ExtantNetwork network ) {
117
this.extantNetwork = network;
118
this.setState( Reference.State.FREE );
123
private PrivateNetworkIndex( ExtantNetwork network, Long index ) {
124
super( network.getOwner( ), network.getTag( ) + ":" + index );
125
this.extantNetwork = network;
126
this.setState( Reference.State.FREE );
127
this.bogusId = network.getTag( ) + ":" + index;
131
private PrivateNetworkIndex( Integer tag, Long index ) {
133
this.bogusId = tag + ":" + index;
134
this.extantNetwork = null;
138
public static PrivateNetworkIndex named( Integer vlan, Long networkIndex ) {
139
return new PrivateNetworkIndex( vlan, networkIndex );
142
public static PrivateNetworkIndex named( ExtantNetwork exNet, Long index ) {
143
return new PrivateNetworkIndex( exNet.getTag( ), index );
146
public static PrivateNetworkIndex create( ExtantNetwork exNet, Long index ) {//TODO:GRZE: fix for sanity.
147
return new PrivateNetworkIndex( exNet, index );
150
public static PrivateNetworkIndex bogus( ) {
154
public Long getIndex( ) {
158
public void setExtantNetwork( ExtantNetwork exNet ) {
159
this.extantNetwork = exNet;
162
public ExtantNetwork getExtantNetwork( ) {
163
return this.extantNetwork;
166
private String getBogusId( ) {
171
protected void setReference( VmInstance referer ) {
172
this.setInstance( referer );
173
if ( referer != null ) {
174
referer.setNetworkIndex( this );
179
protected VmInstance getReference( ) {
180
return this.getInstance( );
184
public int hashCode( ) {
185
final int prime = 31;
186
int result = super.hashCode( );
187
result = prime * result + ( ( this.index == null )
189
: this.index.hashCode( ) );
194
public boolean equals( Object obj ) {
198
if ( !super.equals( obj ) ) {
201
if ( getClass( ) != obj.getClass( ) ) {
204
PrivateNetworkIndex other = ( PrivateNetworkIndex ) obj;
205
if ( this.index == null ) {
206
if ( other.index != null ) {
209
} else if ( !this.index.equals( other.index ) ) {
216
public int compareTo( AccountMetadata that ) {
217
if ( that instanceof PrivateNetworkIndex ) {
218
PrivateNetworkIndex o = ( PrivateNetworkIndex ) that;
219
if ( this.getExtantNetwork( ) != null ) {
220
return ( this.getExtantNetwork( ).getTag( ).equals( o.getExtantNetwork( ).getTag( ) )
221
? this.getIndex( ).compareTo( o.getIndex( ) )
222
: this.getExtantNetwork( ).compareTo( o.getExtantNetwork( ) ) );
224
return ( o.getExtantNetwork( ) == null
229
return super.compareTo( that );
234
public String toString( ) {
235
StringBuilder builder = new StringBuilder( );
236
builder.append( "PrivateNetworkIndex:" );
237
if ( this.extantNetwork != null ) builder.append( this.extantNetwork.getName( ) ).append( " tag=" ).append( this.extantNetwork.getTag( ) );
238
if ( this.index != null ) builder.append( " idx=" ).append( this.index );
239
return builder.toString( );
242
private VmInstance getInstance( ) {
243
return this.instance;
246
private void setInstance( VmInstance instance ) {
247
this.instance = instance;
251
* @see com.eucalyptus.util.HasFullName#getPartition()
254
public String getPartition( ) {
255
return Eucalyptus.INSTANCE.getName( );
259
* @see com.eucalyptus.util.HasFullName#getFullName()
262
public FullName getFullName( ) {
263
return FullName.create.vendor( "euca" )
264
.region( ComponentIds.lookup( Eucalyptus.class ).name( ) )
265
.namespace( this.getOwnerAccountNumber( ) )
266
.relativeId( "security-group", this.getExtantNetwork( ).getNetworkGroup( ).getDisplayName( ),
267
"tag", this.getExtantNetwork( ).getTag( ).toString( ),
268
"index", this.getIndex( ).toString( ) );
272
public PrivateNetworkIndex release( ) throws ResourceAllocationException {
273
this.extantNetwork = null;
274
return super.release( );