1 package fr.in2p3.jsaga.impl.resource.manager;
2
3 import fr.in2p3.jsaga.adaptor.resource.ResourceAdaptor;
4 import fr.in2p3.jsaga.adaptor.resource.compute.ComputeResourceAdaptor;
5 import fr.in2p3.jsaga.adaptor.resource.network.NetworkResourceAdaptor;
6 import fr.in2p3.jsaga.adaptor.resource.storage.StorageResourceAdaptor;
7 import fr.in2p3.jsaga.helpers.SAGAId;
8 import fr.in2p3.jsaga.impl.AbstractSagaObjectImpl;
9 import fr.in2p3.jsaga.impl.resource.description.ComputeDescriptionImpl;
10 import fr.in2p3.jsaga.impl.resource.description.NetworkDescriptionImpl;
11 import fr.in2p3.jsaga.impl.resource.description.StorageDescriptionImpl;
12 import fr.in2p3.jsaga.impl.resource.instance.ComputeImpl;
13 import fr.in2p3.jsaga.impl.resource.instance.NetworkImpl;
14 import fr.in2p3.jsaga.impl.resource.instance.StorageImpl;
15 import fr.in2p3.jsaga.impl.resource.task.IndividualResourceStatusPoller;
16 import fr.in2p3.jsaga.impl.resource.task.ResourceMonitorCallback;
17 import fr.in2p3.jsaga.impl.resource.task.StateListener;
18 import fr.in2p3.jsaga.sync.resource.SyncResourceManager;
19 import org.ogf.saga.SagaObject;
20 import org.ogf.saga.error.*;
21 import org.ogf.saga.resource.Type;
22 import org.ogf.saga.resource.description.ComputeDescription;
23 import org.ogf.saga.resource.description.NetworkDescription;
24 import org.ogf.saga.resource.description.ResourceDescription;
25 import org.ogf.saga.resource.description.StorageDescription;
26 import org.ogf.saga.resource.instance.Compute;
27 import org.ogf.saga.resource.instance.Network;
28 import org.ogf.saga.resource.instance.Resource;
29 import org.ogf.saga.resource.instance.Storage;
30 import org.ogf.saga.session.Session;
31 import org.ogf.saga.url.URL;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Properties;
36
37
38
39
40
41 public abstract class AbstractSyncResourceManagerImpl extends AbstractSagaObjectImpl implements SyncResourceManager, StateListener {
42 protected URL m_url;
43 protected ResourceAdaptor m_adaptor;
44 private IndividualResourceStatusPoller m_poller;
45
46 public AbstractSyncResourceManagerImpl(Session session, URL rm, ResourceAdaptor adaptor) {
47 super(session);
48 m_url = rm;
49 m_adaptor = adaptor;
50 m_poller = new IndividualResourceStatusPoller(m_adaptor);
51 }
52
53 public URL getURL() {
54 return this.m_url;
55 }
56
57
58 public SagaObject clone() throws CloneNotSupportedException {
59 AbstractSyncResourceManagerImpl clone = (AbstractSyncResourceManagerImpl) super.clone();
60 clone.m_url = m_url;
61 clone.m_adaptor = m_adaptor;
62 return clone;
63 }
64
65
66
67 public List<String> listResourcesSync(Type type) throws NotImplementedException,
68 AuthenticationFailedException, AuthorizationFailedException, TimeoutException,
69 NoSuccessException {
70 if (Type.COMPUTE.equals(type) && ! (m_adaptor instanceof ComputeResourceAdaptor)) {
71 throw new NotImplementedException("This adaptor does not handle compute resources");
72 }
73 if (Type.STORAGE.equals(type) && ! (m_adaptor instanceof StorageResourceAdaptor)) {
74 throw new NotImplementedException("This adaptor does not handle storage resources");
75 }
76 if (Type.NETWORK.equals(type) && ! (m_adaptor instanceof NetworkResourceAdaptor)) {
77 throw new NotImplementedException("This adaptor does not handle network resources");
78 }
79 List<String> list = new ArrayList<String>();
80 if ((m_adaptor instanceof ComputeResourceAdaptor && type == null) || Type.COMPUTE.equals(type)) {
81 list = addArrayOfSagaIdsToList(list, ((ComputeResourceAdaptor)m_adaptor).listComputeResources());
82 }
83 if ((m_adaptor instanceof StorageResourceAdaptor && type == null) || Type.STORAGE.equals(type)) {
84 list = addArrayOfSagaIdsToList(list, ((StorageResourceAdaptor)m_adaptor).listStorageResources());
85 }
86 if ((m_adaptor instanceof NetworkResourceAdaptor && type == null) || Type.NETWORK.equals(type)) {
87 list = addArrayOfSagaIdsToList(list, ((NetworkResourceAdaptor)m_adaptor).listNetworkResources());
88 }
89 return list;
90 }
91
92 public List<String> listTemplatesSync(Type type) throws NotImplementedException,
93 TimeoutException, NoSuccessException {
94 if (Type.COMPUTE.equals(type) && ! (m_adaptor instanceof ComputeResourceAdaptor)) {
95 throw new NotImplementedException("This adaptor does not handle compute resources");
96 }
97 if (Type.STORAGE.equals(type) && ! (m_adaptor instanceof StorageResourceAdaptor)) {
98 throw new NotImplementedException("This adaptor does not handle storage resources");
99 }
100 if (Type.NETWORK.equals(type) && ! (m_adaptor instanceof NetworkResourceAdaptor)) {
101 throw new NotImplementedException("This adaptor does not handle network resources");
102 }
103 List<String> list = new ArrayList<String>();
104 if ((m_adaptor instanceof ComputeResourceAdaptor && type == null) || Type.COMPUTE.equals(type)) {
105 list = addArrayOfSagaIdsToList(list, ((ComputeResourceAdaptor)m_adaptor).listComputeTemplates());
106 }
107 if ((m_adaptor instanceof StorageResourceAdaptor && type == null) || Type.STORAGE.equals(type)) {
108 list = addArrayOfSagaIdsToList(list, ((StorageResourceAdaptor)m_adaptor).listStorageTemplates());
109 }
110 if ((m_adaptor instanceof NetworkResourceAdaptor && type == null) || Type.NETWORK.equals(type)) {
111 list = addArrayOfSagaIdsToList(list, ((NetworkResourceAdaptor)m_adaptor).listNetworkTemplates());
112 }
113 return list;
114 }
115
116 private List<String> addArrayOfSagaIdsToList(List<String> list, String[] array) {
117 for (int i=0; array!=null && i<array.length; i++) {
118 list.add(SAGAId.idToSagaId(m_url, array[i]));
119 }
120 return list;
121 }
122
123 public ResourceDescription getTemplateSync(String id) throws NotImplementedException,
124 BadParameterException, DoesNotExistException, TimeoutException, NoSuccessException {
125
126 Properties properties = m_adaptor.getTemplate(SAGAId.idFromSagaId(id));
127 String typeString = properties.getProperty(Resource.RESOURCE_TYPE);
128 if (typeString != null) {
129 Type type = Type.valueOf(typeString);
130 switch (type) {
131 case COMPUTE:
132 return new ComputeDescriptionImpl(properties);
133 case NETWORK:
134 return new NetworkDescriptionImpl(properties);
135 case STORAGE:
136 return new StorageDescriptionImpl(properties);
137 default:
138 throw new BadParameterException("Unexpected resource type: "+type.name());
139 }
140 } else {
141 throw new BadParameterException("Template is missing required property: "+Resource.RESOURCE_TYPE);
142 }
143 }
144
145
146
147 public Compute acquireComputeSync(ComputeDescription description) throws NotImplementedException,
148 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
149 TimeoutException, NoSuccessException {
150 try {
151 return new ComputeImpl(m_session, (ResourceManagerImpl) this, m_adaptor, description);
152 } catch (PermissionDeniedException e) {
153 throw new AuthorizationFailedException(e);
154 } catch (IncorrectStateException e) {
155 throw new NoSuccessException(e);
156 } catch (DoesNotExistException e) {
157 throw new BadParameterException(e);
158 }
159 }
160 public Compute acquireComputeSync(String id) throws NotImplementedException,
161 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
162 DoesNotExistException, TimeoutException, NoSuccessException {
163 return new ComputeImpl(m_session, (ResourceManagerImpl) this, m_adaptor, id);
164 }
165 public void releaseComputeSync(String id) throws NotImplementedException,
166 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
167 DoesNotExistException, TimeoutException, NoSuccessException, IncorrectStateException {
168 m_adaptor.release(SAGAId.idFromSagaId(id));
169 }
170 public void releaseComputeSync(String id, boolean drain) throws NotImplementedException,
171 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
172 DoesNotExistException, TimeoutException, NoSuccessException, IncorrectStateException {
173 ((ComputeResourceAdaptor)m_adaptor).release(SAGAId.idFromSagaId(id), drain);
174 }
175
176
177
178 public Network acquireNetworkSync(NetworkDescription description) throws NotImplementedException,
179 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
180 TimeoutException, NoSuccessException {
181 try {
182 return new NetworkImpl(m_session, (ResourceManagerImpl) this, m_adaptor, description);
183 } catch (PermissionDeniedException e) {
184 throw new AuthorizationFailedException(e);
185 } catch (IncorrectStateException e) {
186 throw new NoSuccessException(e);
187 } catch (DoesNotExistException e) {
188 throw new BadParameterException(e);
189 }
190 }
191 public Network acquireNetworkSync(String id) throws NotImplementedException,
192 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
193 DoesNotExistException, TimeoutException, NoSuccessException {
194 return new NetworkImpl(m_session, (ResourceManagerImpl) this, m_adaptor, id);
195 }
196 public void releaseNetworkSync(String id) throws NotImplementedException,
197 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
198 DoesNotExistException, TimeoutException, NoSuccessException, IncorrectStateException {
199 m_adaptor.release(SAGAId.idFromSagaId(id));
200 }
201
202
203
204 public Storage acquireStorageSync(StorageDescription description) throws NotImplementedException,
205 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
206 TimeoutException, NoSuccessException {
207 try {
208 return new StorageImpl(m_session, (ResourceManagerImpl) this, m_adaptor, description);
209 } catch (PermissionDeniedException e) {
210 throw new AuthorizationFailedException(e);
211 } catch (IncorrectStateException e) {
212 throw new NoSuccessException(e);
213 } catch (DoesNotExistException e) {
214 throw new BadParameterException(e);
215 }
216 }
217 public Storage acquireStorageSync(String id) throws NotImplementedException,
218 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
219 DoesNotExistException, TimeoutException, NoSuccessException {
220 return new StorageImpl(m_session, (ResourceManagerImpl) this, m_adaptor, id);
221 }
222 public void releaseStorageSync(String id) throws NotImplementedException,
223 AuthenticationFailedException, AuthorizationFailedException, BadParameterException,
224 DoesNotExistException, TimeoutException, NoSuccessException, IncorrectStateException {
225 m_adaptor.release(SAGAId.idFromSagaId(id));
226 }
227
228
229
230
231 @Override
232 public void startListening(String nativeResourceId, ResourceMonitorCallback callback) {
233 m_poller.subscribeResource(nativeResourceId, callback);
234 }
235
236 @Override
237
238 public void stopListening(String nativeResourceId) {
239 m_poller.unsubscribeResource(nativeResourceId);
240 }
241
242 }