1 package org.ogf.saga.resource;
2
3 import java.util.List;
4
5 import org.apache.log4j.Logger;
6 import org.junit.After;
7 import org.junit.Before;
8 import org.junit.Test;
9 import static org.junit.Assume.assumeTrue;
10 import org.ogf.saga.JSAGABaseTest;
11 import org.ogf.saga.context.Context;
12 import org.ogf.saga.error.AuthenticationFailedException;
13 import org.ogf.saga.error.AuthorizationFailedException;
14 import org.ogf.saga.error.BadParameterException;
15 import org.ogf.saga.error.DoesNotExistException;
16 import org.ogf.saga.error.IncorrectURLException;
17 import org.ogf.saga.error.NoSuccessException;
18 import org.ogf.saga.error.NotImplementedException;
19 import org.ogf.saga.error.TimeoutException;
20 import org.ogf.saga.monitoring.Metric;
21 import org.ogf.saga.resource.description.ComputeDescription;
22 import org.ogf.saga.resource.description.NetworkDescription;
23 import org.ogf.saga.resource.description.ResourceDescription;
24 import org.ogf.saga.resource.description.StorageDescription;
25 import org.ogf.saga.resource.instance.Resource;
26 import org.ogf.saga.resource.manager.ResourceManager;
27 import org.ogf.saga.resource.task.ResourceTask;
28 import org.ogf.saga.resource.task.State;
29 import org.ogf.saga.session.Session;
30 import org.ogf.saga.session.SessionFactory;
31 import org.ogf.saga.url.URL;
32 import org.ogf.saga.url.URLFactory;
33
34
35
36
37
38
39
40
41
42
43
44
45 public abstract class ResourceBaseTest extends JSAGABaseTest {
46
47 private Logger m_logger = Logger.getLogger(this.getClass());
48
49 private static final String DEFAULT_DELAY_BEFORE_USE = "0";
50
51
52
53
54 protected String DELAY_BEFORE_USE = "delayBeforeUse";
55 protected String ACQUIRE_TEMPLATE = "acquireTemplate";
56 protected String RECONFIGURE_TEMPLATE = "reconfigureTemplate";
57
58 protected List<String> m_templatesForAcquire;
59 protected List<String> m_templatesForReconfigure;
60 protected int m_delayBeforeUseInSeconds;
61
62 protected URL m_resourcemanager;
63 protected Session m_session;
64 protected ResourceManager m_rm;
65 protected Type m_type;
66
67 protected Resource m_currentResource;
68
69 protected ResourceBaseTest(String resourceprotocol, Type type) throws Exception {
70 super();
71
72 m_resourcemanager = URLFactory.createURL(getRequiredProperty(resourceprotocol, CONFIG_RM_URL));
73 m_session = SessionFactory.createSession(true);
74 m_type = type;
75 String prefix = resourceprotocol + "." + m_type.name();
76 m_delayBeforeUseInSeconds = Integer.parseInt(
77 super.getOptionalProperty(prefix, DELAY_BEFORE_USE, DEFAULT_DELAY_BEFORE_USE));
78 m_templatesForAcquire = super.getOptionalProperties(prefix, ACQUIRE_TEMPLATE);
79 m_templatesForReconfigure = super.getOptionalProperties(prefix, RECONFIGURE_TEMPLATE);
80 }
81
82 @Before
83 public void setUp() throws Exception {
84 m_rm = ResourceFactory.createResourceManager(m_session, m_resourcemanager);
85 m_currentResource = null;
86 }
87
88
89 @After
90 public void cleanUp() throws Exception {
91 if (this.m_currentResource != null) {
92 try {
93 this.m_currentResource.release();
94 } catch (Exception e) {
95
96 }
97 }
98 }
99 protected abstract Resource acquire(ResourceDescription rd) throws NotImplementedException, AuthenticationFailedException, AuthorizationFailedException, BadParameterException, TimeoutException, NoSuccessException;
100
101
102
103
104 @Test
105 public void listTemplates() throws Exception {
106 List<String> templates = m_rm.listTemplates(m_type);
107 assertNotNull(templates);
108 if (templates.size()>0) {
109 System.out.println(templates.get(0));
110
111 ResourceDescription rd = m_rm.getTemplate(templates.get(0));
112 if (Type.COMPUTE.equals(m_type)) {
113 assertTrue(rd instanceof ComputeDescription);
114 } else if (Type.STORAGE.equals(m_type)) {
115 assertTrue(rd instanceof StorageDescription);
116 } else if (Type.NETWORK.equals(m_type)) {
117 assertTrue(rd instanceof NetworkDescription);
118 } else {
119 fail("Unknown type:" + m_type.name());
120 }
121 this.dumpDescription(rd);
122 }
123 }
124
125
126
127
128 @Test
129 public void getTemplate() throws NotImplementedException, BadParameterException,
130 IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException,
131 TimeoutException, NoSuccessException, DoesNotExistException {
132 assumeTrue(m_templatesForAcquire.size() > 0);
133 m_rm.getTemplate(m_templatesForAcquire.get(0));
134 }
135
136 @Test(expected = DoesNotExistException.class)
137 public void unknownTemplate() throws NotImplementedException, BadParameterException,
138 IncorrectURLException, AuthenticationFailedException, AuthorizationFailedException,
139 TimeoutException, NoSuccessException, DoesNotExistException {
140 assumeTrue(m_templatesForAcquire.size() > 0);
141 String templateToTest = "thisTemplateDoesNotExists";
142
143 templateToTest = m_templatesForAcquire.get(0).replaceAll("]$", templateToTest + "]");
144 m_rm.getTemplate(templateToTest);
145 }
146
147
148
149
150
151 @Test
152 public void listResources() throws Exception {
153 List<String> resources = m_rm.listResources(m_type);
154 int count = 1;
155 for (String resourceId: resources) {
156 Resource resource;
157 if (Type.NETWORK.equals(m_type)) {
158 resource = m_rm.acquireNetwork(resourceId);
159 } else if (Type.STORAGE.equals(m_type)) {
160 resource = m_rm.acquireStorage(resourceId);
161 } else if (Type.COMPUTE.equals(m_type)) {
162 resource = m_rm.acquireCompute(resourceId);
163 } else {
164 throw new Exception("Type not supported: " + m_type.name());
165 }
166 this.dumpResource(resource);
167 if (count++ == 10) {
168 return;
169 }
170 }
171
172 }
173
174
175
176
177 @Test
178 public void acquireNewAndRelease() throws Exception {
179 m_currentResource = this.acquireResourceReadyNotForUse();
180 this.dumpResource(m_currentResource);
181 for (Context c: m_session.listContexts()) {
182 m_logger.info("** Context :" + c.getAttribute(Context.TYPE));
183 m_logger.debug(c.toString());
184 }
185 }
186
187 @Test
188 public void acquire2ResourcesInTheSameSessionAndRelease() throws Exception {
189 m_currentResource = this.acquireResourceReadyNotForUse();
190 Resource res2 = null;
191 try {
192 res2 = this.acquireResourceReadyNotForUse();
193 for (Context c: m_session.listContexts()) {
194 m_logger.info("** Context :" + c.getAttribute(Context.TYPE));
195 m_logger.debug(c.toString());
196 }
197 } catch (Exception e) {
198 throw e;
199 } finally {
200 if (res2 != null) {
201 try {res2.release();} catch (Exception e) {}
202 }
203 }
204 }
205
206 @Test
207 public void acquireAndReconfigureAndRelease() throws Exception {
208 assumeTrue(m_templatesForReconfigure.size()>0);
209 m_currentResource = this.acquireResourceReadyNotForUse();
210 m_currentResource = this.reconfResourceReady(0);
211 }
212
213
214
215
216
217
218
219 protected final Resource acquireResourceReadyForUse() throws Exception {
220 return this.acquireResourceReady(m_delayBeforeUseInSeconds);
221 }
222
223
224
225 protected final Resource acquireResourceReadyNotForUse() throws Exception {
226 return this.acquireResourceReady(0);
227 }
228
229 private final Resource acquireResourceReady(int delay) throws Exception {
230 return this.getResourceFromTemplatesReady(delay, m_templatesForAcquire);
231 }
232
233 private final Resource reconfResourceReady(int delay) throws Exception {
234 return this.getResourceFromTemplatesReady(delay, m_templatesForReconfigure);
235 }
236
237 private final Resource getResourceFromTemplatesReady(int delay, List<String> templateList) throws Exception {
238 ResourceDescription nd = ResourceFactory.createResourceDescription(m_type);
239 if (templateList.size() > 0) {
240 String[] templates = new String[templateList.size()];
241 nd.setVectorAttribute(ResourceDescription.TEMPLATE, templateList.toArray(templates));
242 }
243 return this.acquireResourceFromDescReady(delay, nd);
244 }
245
246 protected final Resource acquireResourceFromDescReadyForUse(ResourceDescription nd) throws Exception {
247 return this.acquireResourceFromDescReady(m_delayBeforeUseInSeconds, nd);
248 }
249
250 private final Resource acquireResourceFromDescReady(int delay, ResourceDescription nd) throws Exception {
251 Resource res = this.acquire(nd);
252 res.waitFor(120, State.ACTIVE);
253 assertEquals(State.ACTIVE, res.getState());
254 if (delay > 0) {
255 Thread.sleep(delay*1000);
256 }
257 return res;
258 }
259
260
261 protected void dumpDescription(ResourceDescription rd) throws Exception {
262 for (String a: rd.listAttributes()) {
263 if (rd.isVectorAttribute(a)) {
264 System.out.println(" * " + a);
265 for (String v: rd.getVectorAttribute(a)) {
266 System.out.println(" * " + v);
267 }
268 } else {
269 System.out.println(" * " + a + "=" + rd.getAttribute(a));
270 }
271 }
272 }
273
274 protected void dumpResource(Resource resource) throws Exception {
275 ResourceDescription rd = (ResourceDescription) resource.getDescription();
276 assertNotNull(rd);
277 System.out.println(resource.getId());
278 this.dumpDescription(rd);
279
280 System.out.println(" * status=" + resource.getState().name() + " // "
281 + resource.getMetric(ResourceTask.RESOURCE_STATEDETAIL).getAttribute(Metric.VALUE));
282 for (String access: resource.getAccess()) {
283 System.out.println(" => " + access);
284 }
285 }
286 }