/nodecollector/src/main/java/com/abiquo/nodecollector/resource/NodeResource.java

https://github.com/yangbenf/abiquo · Java · 191 lines · 113 code · 23 blank · 55 comment · 0 complexity · 92ac426982cc1c27b844777bed3d2977 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.nodecollector.resource;
  22. import javax.validation.constraints.NotNull;
  23. import javax.ws.rs.DefaultValue;
  24. import javax.ws.rs.GET;
  25. import javax.ws.rs.Path;
  26. import javax.ws.rs.PathParam;
  27. import javax.ws.rs.QueryParam;
  28. import org.apache.wink.common.annotations.Workspace;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Controller;
  31. import com.abiquo.model.enumerator.HypervisorType;
  32. import com.abiquo.model.validation.Ip;
  33. import com.abiquo.model.validation.Port;
  34. import com.abiquo.nodecollector.constants.MessageValues;
  35. import com.abiquo.nodecollector.exception.BadRequestException;
  36. import com.abiquo.nodecollector.exception.CollectorException;
  37. import com.abiquo.nodecollector.exception.ConnectionException;
  38. import com.abiquo.nodecollector.exception.LoginException;
  39. import com.abiquo.nodecollector.exception.NodecollectorException;
  40. import com.abiquo.nodecollector.service.HostService;
  41. import com.abiquo.nodecollector.service.HypervisorService;
  42. import com.abiquo.nodecollector.service.VirtualSystemService;
  43. import com.abiquo.server.core.infrastructure.nodecollector.HostDto;
  44. import com.abiquo.server.core.infrastructure.nodecollector.HypervisorEnumTypeDto;
  45. import com.abiquo.server.core.infrastructure.nodecollector.VirtualSystemCollectionDto;
  46. import com.abiquo.server.core.infrastructure.nodecollector.VirtualSystemDto;
  47. /**
  48. * The resource is responsible of route the functionality to retrieve the physical capabilities of
  49. * the host.
  50. *
  51. * @author jdevesa@abiquo.com
  52. */
  53. @Path("{ip}")
  54. @Controller
  55. @Workspace(collectionTitle = "Remote standalone hypervisors", workspaceTitle = "Nodes")
  56. public class NodeResource
  57. {
  58. public static final String HOST_IP = "ip";
  59. public static final String AIMPORT = "aimport";
  60. public static final String HYPERVISOR = "hypervisor";
  61. public static final String HOST = "host";
  62. public static final String VIRTUAL_SYSTEM = "virtualsystem";
  63. @Autowired
  64. private HypervisorService hypervisorService;
  65. @Autowired
  66. private HostService hostService;
  67. @Autowired
  68. private VirtualSystemService virtualSystemService;
  69. /**
  70. * Returns the current Hypervisor running in the Host.
  71. *
  72. * @param ip IP address of the Host.
  73. * @return a {@link HypervisorEnumTypeDto} object containing the Hypervisor serialized.
  74. * @throws NoHypervisorException
  75. * @throws CollectorException
  76. */
  77. @GET
  78. @Path(HYPERVISOR)
  79. public String getHypervisorType(@PathParam(HOST_IP) @NotNull @Ip final String ip,
  80. @QueryParam(AIMPORT) @DefaultValue("8889") @Port final Integer aimport)
  81. throws NodecollectorException
  82. {
  83. return hypervisorService.discoverHypervisor(ip, aimport).getValue();
  84. }
  85. /**
  86. * Returns the physical information of a remote node.
  87. *
  88. * @return a {@link HostDto} instance
  89. * @throws NoHypervisorException
  90. * @throws CollectorException
  91. * @throws LoginException
  92. * @throws ConnectionException
  93. */
  94. @GET
  95. @Path(HOST)
  96. public HostDto getHostInfo(@PathParam("ip") @NotNull @Ip final String ip,
  97. @QueryParam("hyp") @NotNull final String hypervisorType,
  98. @QueryParam("user") @NotNull final String user,
  99. @QueryParam("passwd") @NotNull final String password,
  100. @QueryParam(AIMPORT) @DefaultValue("8889") @Port final Integer aimport)
  101. throws NodecollectorException
  102. {
  103. HypervisorType hypType;
  104. try
  105. {
  106. hypType = HypervisorType.fromValue(hypervisorType);
  107. }
  108. catch (IllegalArgumentException e)
  109. {
  110. throw new BadRequestException(MessageValues.UNKNOWN_HYPERVISOR);
  111. }
  112. return hostService.getHostInfo(ip, hypType, user, password, aimport);
  113. }
  114. /**
  115. * Routes the rest of the URI to {@link VirtualSystemResource}.
  116. *
  117. * @param ipAddress IP address of the host
  118. * @return the {@link VirtualSystemResource} object
  119. */
  120. @GET
  121. @Path(VIRTUAL_SYSTEM)
  122. public VirtualSystemCollectionDto getVirtualSystemCollectionInfo(
  123. @PathParam("ip") @NotNull @Ip final String ip,
  124. @QueryParam("hyp") @NotNull final String hypervisorType,
  125. @QueryParam("user") @NotNull final String user,
  126. @QueryParam("passwd") @NotNull final String password,
  127. @QueryParam(AIMPORT) @DefaultValue("8889") @Port final Integer aimport)
  128. throws NodecollectorException
  129. {
  130. HypervisorType hypType;
  131. try
  132. {
  133. hypType = HypervisorType.fromValue(hypervisorType);
  134. }
  135. catch (IllegalArgumentException e)
  136. {
  137. throw new BadRequestException(MessageValues.UNKNOWN_HYPERVISOR);
  138. }
  139. return virtualSystemService.getVirtualSystemList(ip, hypType, user, password, aimport);
  140. }
  141. /**
  142. * Routes the rest of the URI to {@link VirtualSystemResource}.
  143. *
  144. * @param ipAddress IP address of the host
  145. * @return the {@link VirtualSystemResource} object
  146. */
  147. @GET
  148. @Path(VIRTUAL_SYSTEM + "/{uuid}")
  149. public VirtualSystemDto getVirtualSystem(@PathParam("ip") @NotNull @Ip final String ip,
  150. @PathParam("uuid") @NotNull final String uuid,
  151. @QueryParam("hyp") @NotNull final String hypervisorType,
  152. @QueryParam("user") @NotNull final String user,
  153. @QueryParam("passwd") @NotNull final String password,
  154. @QueryParam(AIMPORT) @DefaultValue("8889") @Port final Integer aimport)
  155. throws NodecollectorException
  156. {
  157. HypervisorType hypType;
  158. try
  159. {
  160. hypType = HypervisorType.fromValue(hypervisorType);
  161. }
  162. catch (IllegalArgumentException e)
  163. {
  164. throw new BadRequestException(MessageValues.UNKNOWN_HYPERVISOR);
  165. }
  166. return virtualSystemService.getVirtualSystem(ip, hypType, user, password, aimport, uuid);
  167. }
  168. }