View Javadoc

1   package org.apache.helix.webapp.resources;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 InstanceResource extends Resource
46  {
47    private final static Logger LOG = Logger.getLogger(InstanceResource.class);
48  
49    public InstanceResource(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        presentation = getInstanceRepresentation();
87      }
88      catch (Exception e)
89      {
90        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
91        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
92  
93        LOG.error("", e);
94      }
95      return presentation;
96    }
97  
98    StringRepresentation getInstanceRepresentation() throws JsonGenerationException,
99        JsonMappingException,
100       IOException
101   {
102     String clusterName = (String) getRequest().getAttributes().get("clusterName");
103     String instanceName = (String) getRequest().getAttributes().get("instanceName");
104     Builder keyBuilder = new PropertyKey.Builder(clusterName);
105     ZkClient zkClient =
106         (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
107 
108     String message =
109         ClusterRepresentationUtil.getClusterPropertyAsString(zkClient,
110                                                              clusterName,
111                                                              MediaType.APPLICATION_JSON,
112                                                              keyBuilder.instanceConfig(instanceName));
113 
114     StringRepresentation representation =
115         new StringRepresentation(message, MediaType.APPLICATION_JSON);
116 
117     return representation;
118   }
119 
120   @Override
121   public void acceptRepresentation(Representation entity)
122   {
123     try
124     {
125       String clusterName = (String) getRequest().getAttributes().get("clusterName");
126       String instanceName = (String) getRequest().getAttributes().get("instanceName");
127 
128       JsonParameters jsonParameters = new JsonParameters(entity);
129       String command = jsonParameters.getCommand();
130       if (command.equalsIgnoreCase(ClusterSetup.enableInstance))
131       {
132         jsonParameters.verifyCommand(ClusterSetup.enableInstance);
133 
134         boolean enabled =
135             Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
136 
137         ZkClient zkClient =
138             (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
139         ClusterSetup setupTool = new ClusterSetup(zkClient);
140         setupTool.getClusterManagementTool().enableInstance(clusterName,
141                                                             instanceName,
142                                                             enabled);
143       }
144       else if (command.equalsIgnoreCase(ClusterSetup.enablePartition))
145       {
146         jsonParameters.verifyCommand(ClusterSetup.enablePartition);
147  
148         boolean enabled =
149              Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
150 
151         String[] partitions = 
152             jsonParameters.getParameter(JsonParameters.PARTITION).split(";");
153         String resource = 
154             jsonParameters.getParameter(JsonParameters.RESOURCE);
155 
156         ZkClient zkClient =
157             (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
158         ClusterSetup setupTool = new ClusterSetup(zkClient);
159         setupTool.getClusterManagementTool().enablePartition(enabled,
160                                                              clusterName,
161                                                              instanceName,
162                                                              resource,
163                                                              Arrays.asList(partitions));
164       }
165       else if (command.equalsIgnoreCase(ClusterSetup.resetPartition))
166       {
167         jsonParameters.verifyCommand(ClusterSetup.resetPartition);
168 
169         String resource = 
170             jsonParameters.getParameter(JsonParameters.RESOURCE);
171 
172         ZkClient zkClient =
173             (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
174         ClusterSetup setupTool = new ClusterSetup(zkClient);
175         String[] partitionNames = 
176             jsonParameters.getParameter(JsonParameters.PARTITION).split("\\s+");
177         setupTool.getClusterManagementTool()
178                  .resetPartition(clusterName,
179                                  instanceName,
180                                  resource,
181                                  Arrays.asList(partitionNames));
182       }
183       else if (command.equalsIgnoreCase(ClusterSetup.resetInstance))
184       {
185         jsonParameters.verifyCommand(ClusterSetup.resetInstance);
186 
187         ZkClient zkClient =
188             (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
189         ClusterSetup setupTool = new ClusterSetup(zkClient);
190         setupTool.getClusterManagementTool().resetInstance(clusterName,
191                                                            Arrays.asList(instanceName));
192       }
193       else if (command.equalsIgnoreCase(ClusterSetup.addInstanceTag))
194       {
195         jsonParameters.verifyCommand(ClusterSetup.addInstanceTag);
196         String tag = 
197             jsonParameters.getParameter(ClusterSetup.instanceGroupTag);
198         ZkClient zkClient =
199             (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
200         ClusterSetup setupTool = new ClusterSetup(zkClient);
201         setupTool.getClusterManagementTool().addInstanceTag(clusterName, instanceName, tag);
202       }
203       else if (command.equalsIgnoreCase(ClusterSetup.removeInstanceTag))
204       {
205         jsonParameters.verifyCommand(ClusterSetup.removeInstanceTag);
206         String tag = 
207             jsonParameters.getParameter(ClusterSetup.instanceGroupTag);
208         ZkClient zkClient =
209             (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
210         ClusterSetup setupTool = new ClusterSetup(zkClient);
211         setupTool.getClusterManagementTool().removeInstanceTag(clusterName, instanceName, tag);
212       }
213       else
214       {
215         throw new HelixException("Unsupported command: " + command
216             + ". Should be one of [" + ClusterSetup.enableInstance + ", "
217             + ClusterSetup.enablePartition + ", " + ClusterSetup.resetInstance + "]");
218       }
219 
220       getResponse().setEntity(getInstanceRepresentation());
221       getResponse().setStatus(Status.SUCCESS_OK);
222     }
223     catch (Exception e)
224     {
225       getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
226                               MediaType.APPLICATION_JSON);
227       getResponse().setStatus(Status.SUCCESS_OK);
228       LOG.error("", e);
229     }
230   }
231 
232   @Override
233   public void removeRepresentations()
234   {
235     try
236     {
237       String clusterName = (String) getRequest().getAttributes().get("clusterName");
238       String instanceName = (String) getRequest().getAttributes().get("instanceName");
239       ZkClient zkClient =
240           (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
241 
242       ClusterSetup setupTool = new ClusterSetup(zkClient);
243       setupTool.dropInstanceFromCluster(clusterName, instanceName);
244       getResponse().setStatus(Status.SUCCESS_OK);
245     }
246     catch (Exception e)
247     {
248       getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
249                               MediaType.APPLICATION_JSON);
250       getResponse().setStatus(Status.SUCCESS_OK);
251       LOG.error("Error in remove", e);
252     }
253   }
254 }