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.List;
24
25 import org.apache.helix.HelixException;
26 import org.apache.helix.ZNRecord;
27 import org.apache.helix.manager.zk.ZkClient;
28 import org.apache.helix.model.IdealState.IdealStateModeProperty;
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 ResourceGroupsResource extends Resource
46 {
47 private final static Logger LOG = Logger.getLogger(ResourceGroupsResource.class);
48
49 public ResourceGroupsResource(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 false;
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 presentation = getHostedEntitiesRepresentation(clusterName);
88 }
89
90 catch (Exception e)
91 {
92 String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
93 presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
94
95 LOG.error("", e);
96 }
97 return presentation;
98 }
99
100 StringRepresentation getHostedEntitiesRepresentation(String clusterName) throws JsonGenerationException,
101 JsonMappingException,
102 IOException
103 {
104 ZkClient zkClient =
105 (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
106 ;
107 ClusterSetup setupTool = new ClusterSetup(zkClient);
108 List<String> hostedEntities =
109 setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
110
111 ZNRecord hostedEntitiesRecord = new ZNRecord("ResourceGroups");
112 hostedEntitiesRecord.setListField("ResourceGroups", hostedEntities);
113
114 StringRepresentation representation =
115 new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord),
116 MediaType.APPLICATION_JSON);
117
118 return representation;
119 }
120
121 @Override
122 public void acceptRepresentation(Representation entity)
123 {
124 try
125 {
126 String clusterName = (String) getRequest().getAttributes().get("clusterName");
127
128 JsonParameters jsonParameters = new JsonParameters(entity);
129 String command = jsonParameters.getCommand();
130
131 if (command.equalsIgnoreCase(ClusterSetup.addResource)
132 || JsonParameters.CLUSTERSETUP_COMMAND_ALIASES.get(ClusterSetup.addResource)
133 .contains(command))
134 {
135 jsonParameters.verifyCommand(ClusterSetup.addResource);
136
137 String entityName =
138 jsonParameters.getParameter(JsonParameters.RESOURCE_GROUP_NAME);
139 String stateModelDefRef =
140 jsonParameters.getParameter(JsonParameters.STATE_MODEL_DEF_REF);
141 int partitions =
142 Integer.parseInt(jsonParameters.getParameter(JsonParameters.PARTITIONS));
143 String mode = IdealStateModeProperty.AUTO.toString();
144 if (jsonParameters.getParameter(JsonParameters.IDEAL_STATE_MODE) != null)
145 {
146 mode = jsonParameters.getParameter(JsonParameters.IDEAL_STATE_MODE);
147 }
148
149 int bucketSize = 0;
150 if (jsonParameters.getParameter(JsonParameters.BUCKET_SIZE) != null)
151 {
152 try
153 {
154 bucketSize = Integer.parseInt(jsonParameters.getParameter(JsonParameters.BUCKET_SIZE));
155 }
156 catch(Exception e)
157 {
158
159 }
160 }
161
162 int maxPartitionsPerNode = -1;
163 if (jsonParameters.getParameter(JsonParameters.MAX_PARTITIONS_PER_NODE) != null)
164 {
165 try
166 {
167 maxPartitionsPerNode = Integer.parseInt(jsonParameters.getParameter(JsonParameters.MAX_PARTITIONS_PER_NODE));
168 }
169 catch(Exception e)
170 {
171
172 }
173 }
174
175 ZkClient zkClient =
176 (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
177 ;
178 ClusterSetup setupTool = new ClusterSetup(zkClient);
179 setupTool.addResourceToCluster(clusterName,
180 entityName,
181 partitions,
182 stateModelDefRef,
183 mode,
184 bucketSize,
185 maxPartitionsPerNode
186 );
187 }
188 else
189 {
190 throw new HelixException("Unsupported command: " + command
191 + ". Should be one of [" + ClusterSetup.addResource + "]");
192
193 }
194
195 getResponse().setEntity(getHostedEntitiesRepresentation(clusterName));
196 getResponse().setStatus(Status.SUCCESS_OK);
197 }
198
199 catch (Exception e)
200 {
201 getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
202 MediaType.APPLICATION_JSON);
203 getResponse().setStatus(Status.SUCCESS_OK);
204 LOG.error("Error in posting " + entity, e);
205 }
206 }
207 }