xref: /illumos-gate/usr/src/cmd/pools/poold/com/sun/solaris/domain/pools/Move.java (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * ident	"%Z%%M%	%I%	%E% SMI"
27  */
28 
29 package com.sun.solaris.domain.pools;
30 
31 import java.util.*;
32 
33 import com.sun.solaris.service.pools.*;
34 
35 /**
36  * This class represents a move of resources between two resource
37  * sets. It is designed to be extended by classes which implement
38  * different types of moves.
39  */
40 abstract class Move
41 {
42 	/**
43 	 * Source resource set
44 	 */
45 	private Resource from;
46 
47 	/**
48 	 * Destination resource set
49 	 */
50 	private Resource to;
51 
52 	/**
53 	 * Sole constructor.  (For invocation by subclass constructors)
54 	 *
55 	 * @param from The source of the move
56 	 * @param to The destination of the move
57 	 */
Move(Resource from, Resource to)58 	protected Move(Resource from, Resource to)
59 	{
60 		this.from = from;
61 		this.to = to;
62 	}
63 
64 	/**
65 	 * Return the source of this move.
66 	 */
getFrom()67 	Resource getFrom()
68 	{
69 		return (from);
70 	}
71 
72 	/**
73 	 * Return the destination of this move.
74 	 */
getTo()75 	Resource getTo()
76 	{
77 		return (to);
78 	}
79 
80 	/**
81 	 * Apply this move to the resources described.
82 	 *
83 	 * @throws PoolsException If the move fails for any reason.
84 	 */
apply()85 	abstract void apply() throws PoolsException;
86 
87 	/**
88 	 * Return the quantity of moved resource.
89 	 */
getQty()90 	abstract long getQty();
91 }
92 
93 /**
94  * This class represents a move of component resources between two
95  * resource sets. A component of a resource set is a uniquely
96  * identifiable component, such as a processor.
97  */
98 final class ComponentMove extends Move
99 {
100 	/**
101 	 * List of components being moved.
102 	 */
103 	private List compList;
104 
105 	/**
106 	 * Constructor
107 	 *
108 	 * @param from The source of the move
109 	 * @param to The destination of the move
110 	 * @param comp The component which is to be moved
111 	 */
ComponentMove(Resource from, Resource to, Component comp)112 	ComponentMove(Resource from, Resource to, Component comp)
113 	{
114 		super(from, to);
115 		compList = new ArrayList();
116 		compList.add(comp);
117 	}
118 
119 	/**
120 	 * Return a list of the components that comprise this move.
121 	 *
122 	 * The members of the list are guaranteed to be Component
123 	 * objects.
124 	 */
getComponents()125 	List getComponents()
126 	{
127 		return ((List) ((ArrayList) compList).clone());
128 	}
129 
130 	/**
131 	 * Apply the move to the configuration to which the resources
132 	 * belong.
133 	 *
134 	 * @throws PoolsException if the transfer of resource fails.
135 	 */
apply()136 	void apply() throws PoolsException
137 	{
138 		getTo().transfer(getFrom(), compList);
139 	}
140 
141 	/**
142 	 * Return the quantity of resource which is participating in
143 	 * this move.
144 	 */
getQty()145 	long getQty()
146 	{
147 		return (compList.size());
148 	}
149 
150 	/**
151 	 * Converts the <code>ComponentMove</code> to a
152 	 * <code>String</code> of the form:
153 	 */
toString()154 	public String toString()
155 	{
156 		return ("from " + getFrom().toString() + " to " +
157 		    getTo().toString() + " components " + compList);
158 	}
159 }
160 
161 /**
162  * This class represents a move of commodity resources between two
163  * resource sets. Such a resource cannot be uniquely identified in the
164  * resource abstraction and is thus different to a move consisting of
165  * uniquely identifiable resource components.
166  */
167 class QuantityMove extends Move
168 {
169 	/**
170 	 * The resource quantity of the move.
171 	 */
172 	private long qty;
173 
174 	/**
175 	 * Construct a quantity move using the supplied details.
176 	 *
177 	 * @param from The source of the resources.
178 	 * @param to The destination of the resources.
179 	 * @param qty The quantity of the resources.
180 	 *
181 	 * @throws IllegalArgumentException if the qty is negative.
182 	 */
QuantityMove(Resource from, Resource to, long qty)183 	QuantityMove(Resource from, Resource to, long qty)
184 	{
185 		super(from, to);
186 		if (qty < 0)
187 			throw new IllegalArgumentException(
188 			    "The resource quantity supplied (" + qty +
189 			    ") is illegal.");
190 		this.qty = qty;
191 	}
192 
193 	/**
194 	 * Apply the move to the configuration to which the resources
195 	 * belong.
196 	 *
197 	 * @throws PoolsException if the transfer of resource fails.
198 	 */
apply()199 	void apply() throws PoolsException
200 	{
201 		getTo().transfer(getFrom(), qty);
202 	}
203 
204 	/**
205 	 * Return the quantity of resource which is participating in
206 	 * this move.
207 	 */
getQty()208 	long getQty()
209 	{
210 		return (qty);
211 	}
212 
213 	/**
214 	 * Return a string representation of the move.
215 	 */
toString()216 	public String toString()
217 	{
218 		return ("from " + getFrom().toString() + " to " +
219 		    getTo().toString() + " quantity " + qty);
220 	}
221 }
222