~kirkland/eucalyptus/label-metadata

« back to all changes in this revision

Viewing changes to clc/modules/cluster-manager/src/edu/ucsb/eucalyptus/cloud/cluster/Reservation.java

  • Committer: graziano obertelli
  • Date: 2009-01-07 03:32:35 UTC
  • Revision ID: graziano@cs.ucsb.edu-20090107033235-oxhuexp18v8hg0pw
Tags: 1.4
from CVS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Software License Agreement (BSD License)
 
3
 *
 
4
 * Copyright (c) 2008, Regents of the University of California
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use of this software in source and binary forms, with or
 
8
 * without modification, are permitted provided that the following conditions
 
9
 * are met:
 
10
 *
 
11
 * * Redistributions of source code must retain the above
 
12
 *   copyright notice, this list of conditions and the
 
13
 *   following disclaimer.
 
14
 *
 
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.
 
19
 *
 
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.
 
31
 *
 
32
 * Author: Chris Grzegorczyk grze@cs.ucsb.edu
 
33
 */
 
34
 
 
35
package edu.ucsb.eucalyptus.cloud.cluster;
 
36
 
 
37
import edu.ucsb.eucalyptus.constants.HasName;
 
38
import edu.ucsb.eucalyptus.msgs.ReservationInfoType;
 
39
import org.apache.log4j.Logger;
 
40
 
 
41
import java.util.*;
 
42
import java.util.zip.Adler32;
 
43
 
 
44
public class Reservation implements HasName {
 
45
  private static Logger LOG = Logger.getLogger( Reservation.class );
 
46
 
 
47
  private String ownerId;
 
48
  private String reservationId;
 
49
  private Long rsvId;
 
50
  private int minCount;
 
51
  private int maxCount;
 
52
  private int launchIndexBase = 0;
 
53
  private List<String> vmInstances;
 
54
 
 
55
  public Reservation( String ownerId, String reservationId)
 
56
  {
 
57
    this(ownerId,0l,0,0,0);
 
58
    this.reservationId = reservationId;
 
59
  }
 
60
 
 
61
  public Reservation( String ownerId, Long reservationId, int minCount, int maxCount, int baseMac )
 
62
  {
 
63
    this.ownerId = ownerId;
 
64
    this.rsvId = reservationId;
 
65
 
 
66
    //:: DOCUMENT: reservation ID creation:://
 
67
    Adler32 hash = new Adler32();
 
68
    hash.reset();
 
69
    hash.update( ( ownerId + Long.toHexString( reservationId ) ).getBytes() );
 
70
    this.reservationId = String.format( "r-%08X", hash.getValue() );
 
71
 
 
72
    this.minCount = minCount;
 
73
    this.maxCount = maxCount;
 
74
    this.vmInstances = new ArrayList<String>();
 
75
  }
 
76
 
 
77
  public String getName()
 
78
  {
 
79
    return this.getReservationId();
 
80
  }
 
81
 
 
82
  public boolean isEmpty()
 
83
  {
 
84
    return this.vmInstances.isEmpty();
 
85
  }
 
86
 
 
87
  public boolean contains( String instanceId )
 
88
  {
 
89
    return this.vmInstances.contains( instanceId );
 
90
  }
 
91
 
 
92
  public boolean removeInstance( String instanceId )
 
93
  {
 
94
    return this.vmInstances.remove( instanceId );
 
95
  }
 
96
 
 
97
  public int addInstance( String vmId )
 
98
  {
 
99
    this.vmInstances.add( vmId );
 
100
    return this.launchIndexBase++;
 
101
  }
 
102
 
 
103
  public int getMaxCount()
 
104
  {
 
105
    return maxCount;
 
106
  }
 
107
 
 
108
  public int getMinCount()
 
109
  {
 
110
    return minCount;
 
111
  }
 
112
 
 
113
  public String getOwnerId()
 
114
  {
 
115
    return ownerId;
 
116
  }
 
117
 
 
118
  public String getReservationId()
 
119
  {
 
120
    return reservationId;
 
121
  }
 
122
 
 
123
  public Long getRsvId()
 
124
  {
 
125
    return rsvId;
 
126
  }
 
127
 
 
128
  public ReservationInfoType getAsReservationInfoType()
 
129
  {
 
130
    ReservationInfoType rsvInfo = new ReservationInfoType();
 
131
    for( String vmId : this.vmInstances )
 
132
    {
 
133
      VmInstance vm = VmInstances.getInstance().lookup( vmId );
 
134
      rsvInfo.getInstancesSet().add( vm.getAsRunningInstanceItemType() );
 
135
    }
 
136
    rsvInfo.setOwnerId( this.getOwnerId() );
 
137
    rsvInfo.setReservationId( this.getReservationId() );
 
138
    return rsvInfo;
 
139
  }
 
140
 
 
141
  public int compareTo( final Object o )
 
142
  {
 
143
    Reservation that = (Reservation) o;
 
144
    return this.getRsvId().compareTo( that.getRsvId() );
 
145
  }
 
146
 
 
147
 
 
148
}