1 package org.apache.helix.webapp.resources;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.Arrays;
24
25 import org.apache.helix.HelixException;
26 import org.apache.helix.PropertyKey;
27 import org.apache.helix.PropertyKey.Builder;
28 import org.apache.helix.manager.zk.ZkClient;
29 import org.apache.helix.tools.ClusterSetup;
30 import org.apache.helix.webapp.RestAdminApplication;
31 import org.apache.log4j.Logger;
32 import org.codehaus.jackson.JsonGenerationException;
33 import org.codehaus.jackson.map.JsonMappingException;
34 import org.restlet.Context;
35 import org.restlet.data.MediaType;
36 import org.restlet.data.Request;
37 import org.restlet.data.Response;
38 import org.restlet.data.Status;
39 import org.restlet.resource.Representation;
40 import org.restlet.resource.Resource;
41 import org.restlet.resource.StringRepresentation;
42 import org.restlet.resource.Variant;
43
44
45 public class ResourceGroupResource extends Resource
46 {
47 private final static Logger LOG = Logger.getLogger(ResourceGroupResource.class);
48
49 public ResourceGroupResource(Context context, Request request, Response response)
50 {
51 super(context, request, response);
52 getVariants().add(new Variant(MediaType.TEXT_PLAIN));
53 getVariants().add(new Variant(MediaType.APPLICATION_JSON));
54 }
55
56 @Override
57 public boolean allowGet()
58 {
59 return true;
60 }
61
62 @Override
63 public boolean allowPost()
64 {
65 return true;
66 }
67
68 @Override
69 public boolean allowPut()
70 {
71 return false;
72 }
73
74 @Override
75 public boolean allowDelete()
76 {
77 return true;
78 }
79
80 @Override
81 public Representation represent(Variant variant)
82 {
83 StringRepresentation presentation = null;
84 try
85 {
86 String clusterName = (String) getRequest().getAttributes().get("clusterName");
87 String resourceName = (String) getRequest().getAttributes().get("resourceName");
88 presentation = getIdealStateRepresentation(clusterName, resourceName);
89 }
90
91 catch (Exception e)
92 {
93 String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
94 presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
95
96 LOG.error("", e);
97 }
98 return presentation;
99 }
100
101 StringRepresentation getIdealStateRepresentation(String clusterName, String resourceName) throws JsonGenerationException,
102 JsonMappingException,
103 IOException
104 {
105 Builder keyBuilder = new PropertyKey.Builder(clusterName);
106 ZkClient zkClient =
107 (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
108
109 String message =
110 ClusterRepresentationUtil.getClusterPropertyAsString(zkClient,
111 clusterName,
112 keyBuilder.idealStates(resourceName),
113 MediaType.APPLICATION_JSON);
114
115 StringRepresentation representation =
116 new StringRepresentation(message, MediaType.APPLICATION_JSON);
117
118 return representation;
119 }
120
121 @Override
122 public void removeRepresentations()
123 {
124 try
125 {
126 String clusterName = (String) getRequest().getAttributes().get("clusterName");
127 String resourceGroupName =
128 (String) getRequest().getAttributes().get("resourceName");
129 ZkClient zkClient =
130 (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
131
132 ClusterSetup setupTool = new ClusterSetup(zkClient);
133 setupTool.dropResourceFromCluster(clusterName, resourceGroupName);
134 getResponse().setStatus(Status.SUCCESS_OK);
135 }
136 catch (Exception e)
137 {
138 getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
139 MediaType.APPLICATION_JSON);
140 getResponse().setStatus(Status.SUCCESS_OK);
141 LOG.error("", e);
142 }
143 }
144
145 @Override
146 public void acceptRepresentation(Representation entity)
147 {
148 try
149 {
150 String clusterName = (String) getRequest().getAttributes().get("clusterName");
151 String resourceName = (String) getRequest().getAttributes().get("resourceName");
152
153 JsonParameters jsonParameters = new JsonParameters(entity);
154 String command = jsonParameters.getCommand();
155 if (command.equalsIgnoreCase(ClusterSetup.resetResource))
156 {
157 ZkClient zkClient =
158 (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
159 ClusterSetup setupTool = new ClusterSetup(zkClient);
160 setupTool.getClusterManagementTool().resetResource(clusterName, Arrays.asList(resourceName));
161 }
162 else
163 {
164 throw new HelixException("Unsupported command: " + command
165 + ". Should be one of [" + ClusterSetup.resetResource + "]");
166 }
167 }
168 catch (Exception e)
169 {
170 getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
171 MediaType.APPLICATION_JSON);
172 getResponse().setStatus(Status.SUCCESS_OK);
173 LOG.error("", e);
174 }
175 }
176
177 }