/api/src/main/java/com/abiquo/api/util/EventingSupport.java

https://github.com/yangbenf/abiquo · Java · 374 lines · 155 code · 28 blank · 191 comment · 10 complexity · 2fd99c3ad8d4324871fdd5388ec77885 MD5 · raw file

  1. /**
  2. * Abiquo community edition
  3. * cloud management application for hybrid clouds
  4. * Copyright (C) 2008-2010 - Abiquo Holdings S.L.
  5. *
  6. * This application is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU LESSER GENERAL PUBLIC
  8. * LICENSE as published by the Free Software Foundation under
  9. * version 3 of the License
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * LESSER GENERAL PUBLIC LICENSE v.3 for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. package com.abiquo.api.util;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import com.abiquo.api.exceptions.EventingException;
  25. import com.abiquo.model.enumerator.HypervisorType;
  26. import com.abiquo.model.enumerator.VirtualMachineState;
  27. import com.abiquo.server.core.cloud.Hypervisor;
  28. import com.abiquo.server.core.cloud.NodeVirtualImage;
  29. import com.abiquo.server.core.cloud.VirtualAppliance;
  30. import com.abiquo.server.core.cloud.VirtualMachine;
  31. import com.abiquo.vsm.client.VSMClient;
  32. /**
  33. * Eventing support utility class for sending subscribe messages to be monitored by the abicloud
  34. * virtual system monitor. By default the notifications will be sent to the eventSink servlet
  35. *
  36. * @author pnavarro
  37. */
  38. public final class EventingSupport
  39. {
  40. /** The Constant logger. */
  41. private final static Logger logger = LoggerFactory.getLogger(EventingSupport.class);
  42. /**
  43. * Instantiates a new eventing support.
  44. */
  45. private EventingSupport()
  46. {
  47. }
  48. /**
  49. * It sends a subscription message to the abicloud virtualSystemMonitor.
  50. *
  51. * @param virtualSystemAddress the virtualSystemAddress of the hypervisor
  52. * @param virtualSystemID the UUID of the virtual system
  53. * @param virtualSystemMonitorAddress the abicloud virtual system monitor address
  54. * @throws EventingException the eventing exception
  55. */
  56. public static void subscribe(final String virtualSystemAddress,
  57. final HypervisorType hypervisorType, final String virtualSystemID,
  58. final String virtualSystemMonitorAddress)
  59. {
  60. try
  61. {
  62. VSMClient vsmClient = new VSMClient(virtualSystemMonitorAddress);
  63. vsmClient.subscribe(virtualSystemAddress, hypervisorType.name(), virtualSystemID);
  64. }
  65. catch (Exception e)
  66. {
  67. throw new EventingException(e);
  68. }
  69. }
  70. /**
  71. * It sends a unsubscription message to the abicloud virtualSystemMonitor.
  72. *
  73. * @param virtualSystemID the UUID of the virtual system
  74. * @param virtualSystemMonitorAddress the abicloud virtual system monitor address
  75. * @throws EventingException the eventing exception
  76. */
  77. public static void unsubscribe(final String virtualSystemID,
  78. final String virtualSystemMonitorAddress) throws EventingException
  79. {
  80. try
  81. {
  82. VSMClient vsmClient = new VSMClient(virtualSystemMonitorAddress);
  83. vsmClient.unsubscribe(virtualSystemID);
  84. }
  85. catch (Exception e)
  86. {
  87. throw new EventingException(e);
  88. }
  89. }
  90. /**
  91. * Helper to unsubscribe to a specific event of a virtual machine.
  92. *
  93. * @param virtualMachine the virtual machine
  94. * @param virtualSystemMonitorAddress the virtual system monitor address
  95. * @throws EventingException
  96. */
  97. public static void unsubscribeEvent(final VirtualMachine virtualMachine,
  98. final String virtualSystemMonitorAddress) throws EventingException
  99. {
  100. unsubscribe(virtualMachine.getName(), virtualSystemMonitorAddress);
  101. }
  102. /**
  103. * Helper to subscribe to a virtual machine state.
  104. *
  105. * @param virtualMachine the virtual machine to subscribe
  106. * @param virtualSystemMonitorAddress the virtual system monitor address
  107. * @throws EventingException
  108. */
  109. public static void subscribeEvent(final VirtualMachine virtualMachine,
  110. final String virtualSystemMonitorAddress) throws EventingException
  111. {
  112. Hypervisor hypervisor = virtualMachine.getHypervisor();
  113. if (hypervisor != null)
  114. {
  115. String virtualSystemAddress =
  116. "http://" + hypervisor.getIp() + ":" + hypervisor.getPort() + "/";
  117. subscribe(virtualSystemAddress, hypervisor.getType(), virtualMachine.getName(),
  118. virtualSystemMonitorAddress);
  119. }
  120. else
  121. {
  122. logger.error("Error while subscribing to the virtual machine with id: "
  123. + virtualMachine.getId() + ". The hypervisor is not setted.");
  124. }
  125. }
  126. /**
  127. * Subscribes to all events in the VirtualAppliance.
  128. *
  129. * @param virtualAppliance the virtual appliance
  130. * @throws EventingException
  131. */
  132. public static void subscribeToAllVA(final VirtualAppliance virtualAppliance,
  133. final String virtualSystemMonitorAddress) throws EventingException
  134. {
  135. for (NodeVirtualImage node : virtualAppliance.getNodes())
  136. {
  137. VirtualMachine virtualMachine = node.getVirtualMachine();
  138. if (virtualMachine != null)
  139. {
  140. subscribeEvent(virtualMachine, virtualSystemMonitorAddress);
  141. }
  142. else
  143. {
  144. logger.error("Error. The virtual machine id is NULL for the node with id: "
  145. + node.getId());
  146. }
  147. }
  148. }
  149. /**
  150. * Updates the subscription of the VirtualAppliance
  151. *
  152. * @param virtualAppliance the virtual appliance
  153. * @throws EventingException
  154. */
  155. public static void updateSubscription(final VirtualAppliance virtualAppliance,
  156. final String virtualSystemMonitorAddress) throws EventingException
  157. {
  158. for (NodeVirtualImage node : virtualAppliance.getNodes())
  159. {
  160. VirtualMachine vm = node.getVirtualMachine();
  161. if (vm.getState() == VirtualMachineState.NOT_DEPLOYED)
  162. {
  163. subscribeEvent(vm, virtualSystemMonitorAddress);
  164. }
  165. }
  166. }
  167. /**
  168. * Unsubscribes to all events in the VirtualAppliance.
  169. *
  170. * @param virtualAppliance the virtual appliance
  171. * @throws EventingException
  172. */
  173. // public static void unsubscribeToAllVA(final VirtualAppliance virtualAppliance)
  174. // {
  175. // String virtualSystemMonitorAddress = null;
  176. //
  177. // try
  178. // {
  179. // virtualSystemMonitorAddress =
  180. // RemoteServiceUtils.getVirtualSystemMonitorFromVA(virtualAppliance);
  181. // }
  182. // catch (Exception e)
  183. // {
  184. // logger.trace("An error occured while finding the VirtualSystemMonitor", e
  185. // .getStackTrace()[0]);
  186. //
  187. // // Do not unsubscribe
  188. // return;
  189. // }
  190. //
  191. // for (Node< ? > node : virtualAppliance.getNodes())
  192. // {
  193. // if (node.isNodeTypeVirtualImage())
  194. // {
  195. // NodeVirtualImage nodeVI = (NodeVirtualImage) node;
  196. //
  197. // try
  198. // {
  199. // unsubscribeEvent(nodeVI.getVirtualMachine(), virtualSystemMonitorAddress);
  200. // }
  201. // catch (EventingException e)
  202. // {
  203. // // This exception is thrown because we are not following the WS-eventing
  204. // // standard in the unsubscription process
  205. // logger.trace("As in the unsubscription message we are not following the"
  206. // + " WS-eventing standard this exception can be thrown: {}", e
  207. // .getStackTrace()[0]);
  208. // }
  209. // }
  210. // }
  211. //
  212. // }
  213. /**
  214. * Subscribes to events pulling to all events in the VirtualAppliance to an specific event.
  215. *
  216. * @param virtualAppliance the virtual appliance
  217. * @throws EventingException
  218. */
  219. // public static void subscribePullToAllVA(final VirtualAppliance virtualAppliance)
  220. // throws EventingException
  221. // {
  222. // String virtualSystemMonitorAddress;
  223. // try
  224. // {
  225. // virtualSystemMonitorAddress =
  226. // RemoteServiceUtils.getVirtualSystemMonitorFromVA(virtualAppliance);
  227. //
  228. // for (Node node : virtualAppliance.getNodes())
  229. // {
  230. // if (node.isNodeTypeVirtualImage())
  231. // {
  232. // // Convert to virtualImage node
  233. // NodeVirtualImage nodeVI = (NodeVirtualImage) node;
  234. // subscribePullEvent(nodeVI.getVirtualMachine(), virtualSystemMonitorAddress);
  235. // }
  236. // }
  237. //
  238. // }
  239. // catch (PersistenceException e)
  240. // {
  241. // logger.trace("Exists a problem finding the VirtualSystemMonitor", e.getStackTrace()[0]);
  242. // }
  243. // catch (RemoteServiceException e)
  244. // {
  245. // logger.trace("Exists a problem finding the VirtualSystemMonitor", e.getStackTrace()[0]);
  246. // }
  247. //
  248. // }
  249. public static void subscribePullEventToVM(final VirtualMachine virtualMachine,
  250. final String virtualSystemMonitorAddress) throws EventingException
  251. {
  252. subscribePullEvent(virtualMachine, virtualSystemMonitorAddress);
  253. }
  254. /**
  255. * Helper to subscribe to pulling events to a virtual machine state.
  256. *
  257. * @param virtualMachine the virtual machine to subscribe
  258. * @param eventType the eventType to subscribe
  259. * @throws EventingException
  260. */
  261. public static void subscribePullEvent(final VirtualMachine virtualMachine,
  262. final String virtualSystemMonitorAddress) throws EventingException
  263. {
  264. Hypervisor hypervisor = virtualMachine.getHypervisor();
  265. if (hypervisor != null)
  266. {
  267. String virtualSystemAddress =
  268. "http://" + hypervisor.getIp() + ":" + hypervisor.getPort() + "/";
  269. subscribePull(virtualSystemAddress, virtualMachine.getName(),
  270. virtualSystemMonitorAddress);
  271. }
  272. }
  273. /**
  274. * It sends a subscription message to the abicloud virtualSystemMonitor with the PULL develiry
  275. * mode.
  276. *
  277. * @param virtualSystemAddress the virtualSystemAddress of the hypervisor
  278. * @param virtualSystemID the UUID of the virtual system
  279. * @param virtualSystemMonitorAddress the abicloud virtual system monitor address
  280. * @param eventType the event type to monitor
  281. * @throws EventingException the eventing exception
  282. */
  283. public static void subscribePull(final String virtualSystemAddress,
  284. final String virtualSystemID, final String virtualSystemMonitorAddress)
  285. throws EventingException
  286. {
  287. try
  288. {
  289. VSMClient vsmClient = new VSMClient(virtualSystemMonitorAddress);
  290. vsmClient.publishState(virtualSystemAddress, virtualSystemID);
  291. }
  292. catch (Exception e)
  293. {
  294. throw new EventingException(e);
  295. }
  296. }
  297. /**
  298. * Starts the monitoring in the VSM of a physical machine
  299. *
  300. * @param virtualSystemAddress the physical machine addres to monitor
  301. * @param virtualSystemType the hypervisor type
  302. * @param virtualSystemMonitorAddress the URI of the VSM service
  303. * @param user the admin hypervisor user
  304. * @param password the admin hypervisor password
  305. * @throws EventingException
  306. */
  307. public static void monitorPhysicalMachine(final String virtualSystemAddress,
  308. final HypervisorType hypervisorType, final String virtualSystemMonitorAddress,
  309. final String user, final String password) throws EventingException
  310. {
  311. try
  312. {
  313. VSMClient vsmClient = new VSMClient(virtualSystemMonitorAddress);
  314. vsmClient.monitor(virtualSystemAddress, hypervisorType.name(), user, password);
  315. }
  316. catch (Exception e)
  317. {
  318. throw new EventingException(e);
  319. }
  320. }
  321. /**
  322. * Stops the monitoring in the VSM of a physical machine
  323. *
  324. * @param virtualSystemAddress TODO
  325. * @param virtualSystemType the hypervisor type
  326. * @param virtualSystemMonitorAddress the URI of the VSM service
  327. * @param user the admin hypervisor user
  328. * @param password the admin hypervisor password
  329. * @param virtualSystemAddress the physical machine addres to monitor
  330. * @throws EventingException
  331. */
  332. public static void unMonitorPhysicalMachine(final String virtualSystemAddress,
  333. final String virtualSystemMonitorAddress, final String user, final String password)
  334. throws EventingException
  335. {
  336. try
  337. {
  338. VSMClient vsmClient = new VSMClient(virtualSystemMonitorAddress);
  339. vsmClient.shutdown(virtualSystemAddress);
  340. }
  341. catch (Exception e)
  342. {
  343. throw new EventingException(e);
  344. }
  345. }
  346. }