~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to clc/modules/cluster-manager/src/main/java/com/eucalyptus/network/PrivateNetworkIndex.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
ImportĀ upstreamĀ versionĀ 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2009  Eucalyptus Systems, Inc.
 
3
 * 
 
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.
 
7
 * 
 
8
 * 
 
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
 
12
 *  for more details.
 
13
 * 
 
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/>.
 
16
 * 
 
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.
 
20
 * 
 
21
 *  This file may incorporate work covered under the following copyright and
 
22
 *  permission notice:
 
23
 * 
 
24
 *    Software License Agreement (BSD License)
 
25
 * 
 
26
 *    Copyright (c) 2008, Regents of the University of California
 
27
 *    All rights reserved.
 
28
 * 
 
29
 *    Redistribution and use of this software in source and binary forms, with
 
30
 *    or without modification, are permitted provided that the following
 
31
 *    conditions are met:
 
32
 * 
 
33
 *      Redistributions of source code must retain the above copyright notice,
 
34
 *      this list of conditions and the following disclaimer.
 
35
 * 
 
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.
 
39
 * 
 
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>
 
62
 */
 
63
 
 
64
package com.eucalyptus.network;
 
65
 
 
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;
 
87
 
 
88
@Entity
 
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> {
 
94
  @Transient
 
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;
 
100
  @ManyToOne
 
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;
 
107
  
 
108
  private PrivateNetworkIndex( ) {
 
109
    super( null, null );
 
110
    this.index = null;
 
111
    this.extantNetwork = null;
 
112
    this.bogusId = null;
 
113
  }
 
114
  
 
115
  private PrivateNetworkIndex( ExtantNetwork network ) {
 
116
    super( null, null );
 
117
    this.extantNetwork = network;
 
118
    this.setState( Reference.State.FREE );
 
119
    this.bogusId = null;
 
120
    this.index = null;
 
121
  }
 
122
  
 
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;
 
128
    this.index = index;
 
129
  }
 
130
  
 
131
  private PrivateNetworkIndex( Integer tag, Long index ) {
 
132
    super( null, null );
 
133
    this.bogusId = tag + ":" + index;
 
134
    this.extantNetwork = null;
 
135
    this.index = index;
 
136
  }
 
137
  
 
138
  public static PrivateNetworkIndex named( Integer vlan, Long networkIndex ) {
 
139
    return new PrivateNetworkIndex( vlan, networkIndex );
 
140
  }
 
141
  
 
142
  public static PrivateNetworkIndex named( ExtantNetwork exNet, Long index ) {
 
143
    return new PrivateNetworkIndex( exNet.getTag( ), index );
 
144
  }
 
145
  
 
146
  public static PrivateNetworkIndex create( ExtantNetwork exNet, Long index ) {//TODO:GRZE: fix for sanity.
 
147
    return new PrivateNetworkIndex( exNet, index );
 
148
  }
 
149
  
 
150
  public static PrivateNetworkIndex bogus( ) {
 
151
    return bogusIndex;
 
152
  }
 
153
  
 
154
  public Long getIndex( ) {
 
155
    return this.index;
 
156
  }
 
157
  
 
158
  public void setExtantNetwork( ExtantNetwork exNet ) {
 
159
    this.extantNetwork = exNet;
 
160
  }
 
161
  
 
162
  public ExtantNetwork getExtantNetwork( ) {
 
163
    return this.extantNetwork;
 
164
  }
 
165
  
 
166
  private String getBogusId( ) {
 
167
    return this.bogusId;
 
168
  }
 
169
  
 
170
  @Override
 
171
  protected void setReference( VmInstance referer ) {
 
172
    this.setInstance( referer );
 
173
    if ( referer != null ) {
 
174
      referer.setNetworkIndex( this );
 
175
    }
 
176
  }
 
177
  
 
178
  @Override
 
179
  protected VmInstance getReference( ) {
 
180
    return this.getInstance( );
 
181
  }
 
182
  
 
183
  @Override
 
184
  public int hashCode( ) {
 
185
    final int prime = 31;
 
186
    int result = super.hashCode( );
 
187
    result = prime * result + ( ( this.index == null )
 
188
      ? 0
 
189
      : this.index.hashCode( ) );
 
190
    return result;
 
191
  }
 
192
  
 
193
  @Override
 
194
  public boolean equals( Object obj ) {
 
195
    if ( this == obj ) {
 
196
      return true;
 
197
    }
 
198
    if ( !super.equals( obj ) ) {
 
199
      return false;
 
200
    }
 
201
    if ( getClass( ) != obj.getClass( ) ) {
 
202
      return false;
 
203
    }
 
204
    PrivateNetworkIndex other = ( PrivateNetworkIndex ) obj;
 
205
    if ( this.index == null ) {
 
206
      if ( other.index != null ) {
 
207
        return false;
 
208
      }
 
209
    } else if ( !this.index.equals( other.index ) ) {
 
210
      return false;
 
211
    }
 
212
    return true;
 
213
  }
 
214
  
 
215
  @Override
 
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( ) ) );
 
223
      } else {
 
224
        return ( o.getExtantNetwork( ) == null
 
225
          ? 0
 
226
          : -1 );
 
227
      }
 
228
    } else {
 
229
      return super.compareTo( that );
 
230
    }
 
231
  }
 
232
  
 
233
  @Override
 
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( );
 
240
  }
 
241
  
 
242
  private VmInstance getInstance( ) {
 
243
    return this.instance;
 
244
  }
 
245
  
 
246
  private void setInstance( VmInstance instance ) {
 
247
    this.instance = instance;
 
248
  }
 
249
  
 
250
  /**
 
251
   * @see com.eucalyptus.util.HasFullName#getPartition()
 
252
   */
 
253
  @Override
 
254
  public String getPartition( ) {
 
255
    return Eucalyptus.INSTANCE.getName( );
 
256
  }
 
257
  
 
258
  /**
 
259
   * @see com.eucalyptus.util.HasFullName#getFullName()
 
260
   */
 
261
  @Override
 
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( ) );
 
269
  }
 
270
  
 
271
  @Override
 
272
  public PrivateNetworkIndex release( ) throws ResourceAllocationException {
 
273
    this.extantNetwork = null;
 
274
    return super.release( );
 
275
  }
 
276
  
 
277
}