OW2 Consortium elastic-grid

Rev

Rev 343 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 343 Rev 455
1
/**
1
/**
2
 * Elastic Grid
2
 * Elastic Grid
3
 * Copyright (C) 2008-2009 Elastic Grid, LLC.
3
 * Copyright (C) 2008-2009 Elastic Grid, LLC.
4
 * 
4
 * 
5
 * This program is free software: you can redistribute it and/or modify
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as
6
 * it under the terms of the GNU Affero General Public License as
7
 * published by the Free Software Foundation, either version 3 of the
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (at your option) any later version.
8
 * License, or (at your option) any later version.
9
 * 
9
 * 
10
 * This program is distributed in the hope that it will be useful,
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
13
 * GNU Affero General Public License for more details.
14
 * 
14
 * 
15
 * You should have received a copy of the GNU Affero General Public License
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
17
 */
18

18

19
package com.elasticgrid.rest;
19
package com.elasticgrid.rest;
20

20

21
import com.elasticgrid.cluster.ClusterManager;
21
import com.elasticgrid.cluster.ClusterManager;
22
import com.elasticgrid.model.Cluster;
22
import com.elasticgrid.model.Cluster;
23
import com.elasticgrid.model.ClusterProvisioning;
23
import com.elasticgrid.model.ClusterProvisioning;
24
import com.elasticgrid.model.NodeProfileInfo;
-
 
25
import com.elasticgrid.model.NodeProfile;
24
import com.elasticgrid.model.NodeProfile;
-
 
25
import com.elasticgrid.model.NodeProfileInfo;
26
import com.elasticgrid.model.ec2.EC2NodeType;
26
import com.elasticgrid.model.ec2.EC2NodeType;
27
import org.jibx.runtime.JiBXException;
27
import org.jibx.runtime.JiBXException;
28
import org.restlet.Context;
28
import org.restlet.Context;
29
import org.restlet.data.Form;
29
import org.restlet.data.Form;
30
import org.restlet.data.MediaType;
30
import org.restlet.data.MediaType;
31
import org.restlet.data.Request;
31
import org.restlet.data.Request;
32
import org.restlet.data.Response;
32
import org.restlet.data.Response;
33
import org.restlet.data.Status;
33
import org.restlet.data.Status;
34
import org.restlet.ext.jibx.JibxRepresentation;
34
import org.restlet.ext.jibx.JibxRepresentation;
35
import org.restlet.ext.wadl.DocumentationInfo;
35
import org.restlet.ext.wadl.DocumentationInfo;
36
import org.restlet.ext.wadl.MethodInfo;
36
import org.restlet.ext.wadl.MethodInfo;
37
import org.restlet.ext.wadl.ParameterInfo;
37
import org.restlet.ext.wadl.ParameterInfo;
38
import org.restlet.ext.wadl.ParameterStyle;
38
import org.restlet.ext.wadl.ParameterStyle;
39
import org.restlet.ext.wadl.RepresentationInfo;
39
import org.restlet.ext.wadl.RepresentationInfo;
40
import org.restlet.ext.wadl.WadlResource;
40
import org.restlet.ext.wadl.WadlResource;
41
import org.restlet.resource.Representation;
41
import org.restlet.resource.Representation;
42
import org.restlet.resource.ResourceException;
42
import org.restlet.resource.ResourceException;
43
import org.restlet.resource.Variant;
43
import org.restlet.resource.Variant;
44
import org.springframework.beans.factory.annotation.Autowired;
-
 
45
import java.io.IOException;
44
import java.io.IOException;
46
import java.util.Arrays;
45
import java.util.Arrays;
47
import java.util.concurrent.TimeoutException;
46
import java.util.concurrent.TimeoutException;
48

47

49
public class ClusterResource extends WadlResource {
48
public class ClusterResource extends WadlResource {
50
    private String clusterName;
49
    private String clusterName;
51
    @Autowired
-
 
52
    private ClusterManager clusterManager;
50
    private ClusterManager clusterManager;
53

51

54
    @Override
52
    @Override
55
    public void init(Context context, Request request, Response response) {
53
    public void init(Context context, Request request, Response response) {
56
        super.init(context, request, response);
54
        super.init(context, request, response);
-
 
55
        clusterManager = RestJSB.getClusterManager();
57
        // Allow modifications of this resource via POST requests
56
        // Allow modifications of this resource via POST requests
58
        setModifiable(true);
57
        setModifiable(true);
59
        setAutoDescribed(true);
58
        setAutoDescribed(true);
60
        // Declare the kind of representations supported by this resource
59
        // Declare the kind of representations supported by this resource
61
        getVariants().add(new Variant(MediaType.APPLICATION_XML));
60
        getVariants().add(new Variant(MediaType.APPLICATION_XML));
62
        // Extract URI variables
61
        // Extract URI variables
63
        clusterName = (String) request.getAttributes().get("clusterName");
62
        clusterName = (String) request.getAttributes().get("clusterName");
64
    }
63
    }
65

64

66
    /**
65
    /**
67
     * Handle GET requests: describe cluster.
66
     * Handle GET requests: describe cluster.
68
     */
67
     */
69
    @Override
68
    @Override
70
    public Representation represent(Variant variant) throws ResourceException {
69
    public Representation represent(Variant variant) throws ResourceException {
71
        try {
70
        try {
72
            Cluster cluster = clusterManager.cluster(clusterName);
71
            Cluster cluster = clusterManager.cluster(clusterName);
73
            return new JibxRepresentation<Cluster>(MediaType.APPLICATION_XML, cluster, "ElasticGridREST");
72
            return new JibxRepresentation<Cluster>(MediaType.APPLICATION_XML, cluster, "ElasticGridREST");
74
        } catch (Exception e) {
73
        } catch (Exception e) {
75
            e.printStackTrace();
74
            e.printStackTrace();
76
            throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE, e);
75
            throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE, e);
77
        }
76
        }
78
    }
77
    }
79

78

80
    /**
79
    /**
81
     * Handle POST requests: update cluster.
80
     * Handle POST requests: update cluster.
82
     */
81
     */
83
    @Override
82
    @Override
84
    public void acceptRepresentation(Representation entity) throws ResourceException {
83
    public void acceptRepresentation(Representation entity) throws ResourceException {
85
        String clusterName = null;
84
        String clusterName = null;
86
        NodeProfileInfo monitorsInfo = null;
85
        NodeProfileInfo monitorsInfo = null;
87
        NodeProfileInfo monitorsAndAgentsInfo = null;
86
        NodeProfileInfo monitorsAndAgentsInfo = null;
88
        NodeProfileInfo agentsInfo = null;
87
        NodeProfileInfo agentsInfo = null;
89
        if (MediaType.APPLICATION_XML.equals(entity.getMediaType())) {
88
        if (MediaType.APPLICATION_XML.equals(entity.getMediaType())) {
90
            try {
89
            try {
91
                JibxRepresentation<ClusterProvisioning> representation =
90
                JibxRepresentation<ClusterProvisioning> representation =
92
                        new JibxRepresentation<ClusterProvisioning>(entity, ClusterProvisioning.class, "ElasticGridREST");
91
                        new JibxRepresentation<ClusterProvisioning>(entity, ClusterProvisioning.class, "ElasticGridREST");
93
                ClusterProvisioning clusterProvisioning = representation.getObject();
92
                ClusterProvisioning clusterProvisioning = representation.getObject();
94
                clusterName = clusterProvisioning.getClusterName();
93
                clusterName = clusterProvisioning.getClusterName();
95
                monitorsInfo = clusterProvisioning.getMonitorsInfo();
94
                monitorsInfo = clusterProvisioning.getMonitorsInfo();
96
                monitorsAndAgentsInfo = clusterProvisioning.getMonitorsAndAgentsInfo();
95
                monitorsAndAgentsInfo = clusterProvisioning.getMonitorsAndAgentsInfo();
97
                agentsInfo = clusterProvisioning.getAgentsInfo();
96
                agentsInfo = clusterProvisioning.getAgentsInfo();
98
            } catch (JiBXException e) {
97
            } catch (JiBXException e) {
99
                e.printStackTrace();
98
                e.printStackTrace();
100
                throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
99
                throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
101
            } catch (IOException e) {
100
            } catch (IOException e) {
102
                e.printStackTrace();
101
                e.printStackTrace();
103
                throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
102
                throw new ResourceException(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY, e);
104
            }
103
            }
105
        } else if (MediaType.APPLICATION_WWW_FORM.equals(entity.getMediaType())) {
104
        } else if (MediaType.APPLICATION_WWW_FORM.equals(entity.getMediaType())) {
106
            Form form = new Form(entity);
105
            Form form = new Form(entity);
107
            clusterName = form.getFirstValue("clusterName");
106
            clusterName = form.getFirstValue("clusterName");
108
            monitorsInfo = new NodeProfileInfo(NodeProfile.MONITOR,
107
            monitorsInfo = new NodeProfileInfo(NodeProfile.MONITOR,
109
                    EC2NodeType.valueOf(form.getFirstValue("monitorNodeType")),
108
                    EC2NodeType.valueOf(form.getFirstValue("monitorNodeType")),
110
                    Integer.parseInt(form.getFirstValue("numberOfMonitors")));
109
                    Integer.parseInt(form.getFirstValue("numberOfMonitors")));
111
            monitorsAndAgentsInfo = new NodeProfileInfo(NodeProfile.MONITOR_AND_AGENT,
110
            monitorsAndAgentsInfo = new NodeProfileInfo(NodeProfile.MONITOR_AND_AGENT,
112
                    EC2NodeType.valueOf(form.getFirstValue("monitorAndAgentNodeType")),
111
                    EC2NodeType.valueOf(form.getFirstValue("monitorAndAgentNodeType")),
113
                    Integer.parseInt(form.getFirstValue("numberOfMonitorsAndAgents")));
112
                    Integer.parseInt(form.getFirstValue("numberOfMonitorsAndAgents")));
114
            agentsInfo = new NodeProfileInfo(NodeProfile.AGENT,
113
            agentsInfo = new NodeProfileInfo(NodeProfile.AGENT,
115
                    EC2NodeType.valueOf(form.getFirstValue("agentNodeType")),
114
                    EC2NodeType.valueOf(form.getFirstValue("agentNodeType")),
116
                    Integer.parseInt(form.getFirstValue("numberOfAgents")));
115
                    Integer.parseInt(form.getFirstValue("numberOfAgents")));
117
        }
116
        }
118
        try {
117
        try {
119
            clusterManager.resizeCluster(clusterName, Arrays.asList(monitorsInfo, monitorsAndAgentsInfo, agentsInfo));
118
            clusterManager.resizeCluster(clusterName, Arrays.asList(monitorsInfo, monitorsAndAgentsInfo, agentsInfo));
120
        } catch (TimeoutException e) {
119
        } catch (TimeoutException e) {
121
            e.printStackTrace();
120
            e.printStackTrace();
122
            throw new ResourceException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, e);
121
            throw new ResourceException(Status.SERVER_ERROR_GATEWAY_TIMEOUT, e);
123
        } catch (Exception e) {
122
        } catch (Exception e) {
124
            e.printStackTrace();
123
            e.printStackTrace();
125
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
124
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
126
        }
125
        }
127
    }
126
    }
128

127

129
    /**
128
    /**
130
     * Handle DELETE requests: stop the cluster.
129
     * Handle DELETE requests: stop the cluster.
131
     */
130
     */
132
    @Override
131
    @Override
133
    public void removeRepresentations() throws ResourceException {
132
    public void removeRepresentations() throws ResourceException {
134
        try {
133
        try {
135
            clusterManager.stopCluster(clusterName);
134
            clusterManager.stopCluster(clusterName);
136
        } catch (Exception e) {
135
        } catch (Exception e) {
137
            e.printStackTrace();
136
            e.printStackTrace();
138
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Could not stop cluster " + clusterName, e);
137
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Could not stop cluster " + clusterName, e);
139
        }
138
        }
140
    }
139
    }
141

140

142
    @Override
141
    @Override
143
    protected void describeGet(MethodInfo info) {
142
    protected void describeGet(MethodInfo info) {
144
        super.describeGet(info);
143
        super.describeGet(info);
145
        info.setDocumentation("Describe cluster {clusterName}.");
144
        info.setDocumentation("Describe cluster {clusterName}.");
146
        info.getResponse().setDocumentation("The cluster.");
145
        info.getResponse().setDocumentation("The cluster.");
147
        RepresentationInfo representation = new RepresentationInfo();
146
        RepresentationInfo representation = new RepresentationInfo();
148
        representation.setDocumentation("This resource exposes cluster {clusterName}.");
147
        representation.setDocumentation("This resource exposes cluster {clusterName}.");
149
        representation.getDocumentations().get(0).setTitle("cluster");
148
        representation.getDocumentations().get(0).setTitle("cluster");
150
        representation.setMediaType(MediaType.APPLICATION_XML);
149
        representation.setMediaType(MediaType.APPLICATION_XML);
151
        representation.getDocumentations().addAll(Arrays.asList(
150
        representation.getDocumentations().addAll(Arrays.asList(
152
                new DocumentationInfo("Example of output:<pre><![CDATA[" +
151
                new DocumentationInfo("Example of output:<pre><![CDATA[" +
153
                        "<cluster name=\"cluster1\" xmlns=\"urn:elastic-grid:eg\">\n" +
152
                        "<cluster name=\"cluster1\" xmlns=\"urn:elastic-grid:eg\">\n" +
154
                        "  <node profile=\"monitor\">ec2-75...</node>\n" +
153
                        "  <node profile=\"monitor\">ec2-75...</node>\n" +
155
                        "  <node profile=\"monitor\">ec2-77...</node>\n" +
154
                        "  <node profile=\"monitor\">ec2-77...</node>\n" +
156
                        "  <node profile=\"agent\">ec2-37...</node>\n" +
155
                        "  <node profile=\"agent\">ec2-37...</node>\n" +
157
                        "  <node profile=\"agent\">ec2-33...</node>\n" +
156
                        "  <node profile=\"agent\">ec2-33...</node>\n" +
158
                        "  <node profile=\"agent\">ec2-32...</node>\n" +
157
                        "  <node profile=\"agent\">ec2-32...</node>\n" +
159
                        "</cluster>" +
158
                        "</cluster>" +
160
                        "]]></pre>")
159
                        "]]></pre>")
161
        ));
160
        ));
162
        representation.setXmlElement("eg:cluster");
161
        representation.setXmlElement("eg:cluster");
163
        info.getResponse().setRepresentations(Arrays.asList(representation));
162
        info.getResponse().setRepresentations(Arrays.asList(representation));
164
    }
163
    }
165

164

166
    @Override
165
    @Override
167
    protected void describePost(MethodInfo info) {
166
    protected void describePost(MethodInfo info) {
168
        super.describePut(info);
167
        super.describePut(info);
169
        info.setDocumentation("Update Elastic Grid Cluster {clusterName}.");
168
        info.setDocumentation("Update Elastic Grid Cluster {clusterName}.");
170
        info.getRequest().setDocumentation("The cluster to update.");
169
        info.getRequest().setDocumentation("The cluster to update.");
171
        RepresentationInfo xmlRepresentation = new RepresentationInfo();
170
        RepresentationInfo xmlRepresentation = new RepresentationInfo();
172
        xmlRepresentation.setDocumentation("This representation exposes a provisioning request for updating an Elastic Grid Cluster.");
171
        xmlRepresentation.setDocumentation("This representation exposes a provisioning request for updating an Elastic Grid Cluster.");
173
        xmlRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
172
        xmlRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
174
        xmlRepresentation.setMediaType(MediaType.APPLICATION_XML);
173
        xmlRepresentation.setMediaType(MediaType.APPLICATION_XML);
175
        xmlRepresentation.getDocumentations().addAll(Arrays.asList(
174
        xmlRepresentation.getDocumentations().addAll(Arrays.asList(
176
                new DocumentationInfo("Example of input:<pre><![CDATA[" +
175
                new DocumentationInfo("Example of input:<pre><![CDATA[" +
177
                        "<cluster-provisioning name=\"my-cluster\" xmlns=\"urn:elastic-grid:eg\">\n" +
176
                        "<cluster-provisioning name=\"my-cluster\" xmlns=\"urn:elastic-grid:eg\">\n" +
178
                        "\t<!-- Start 2 monitors -->\n" +
177
                        "\t<!-- Start 2 monitors -->\n" +
179
                        "\t<monitors>2</monitors>\n" +
178
                        "\t<monitors>2</monitors>\n" +
180
                        "\t<!-- Start 4 monitors and agents -->\n" +
179
                        "\t<!-- Start 4 monitors and agents -->\n" +
181
                        "\t<monitors-and-agents>4</monitors-and-agents>\n" +
180
                        "\t<monitors-and-agents>4</monitors-and-agents>\n" +
182
                        "\t<!-- Start 3 agents -->\n" +
181
                        "\t<!-- Start 3 agents -->\n" +
183
                        "\t<agents>3</agents>\n" +
182
                        "\t<agents>3</agents>\n" +
184
                        "</cluster-provisioning>" +
183
                        "</cluster-provisioning>" +
185
                        "]]></pre>")
184
                        "]]></pre>")
186
        ));
185
        ));
187
        xmlRepresentation.setXmlElement("eg:cluster-provisioning");
186
        xmlRepresentation.setXmlElement("eg:cluster-provisioning");
188
        RepresentationInfo formRepresentation = new RepresentationInfo();
187
        RepresentationInfo formRepresentation = new RepresentationInfo();
189
        formRepresentation.setDocumentation("This representation exposes a provisioning request for updating an Elastic Grid Cluster.");
188
        formRepresentation.setDocumentation("This representation exposes a provisioning request for updating an Elastic Grid Cluster.");
190
        formRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
189
        formRepresentation.getDocumentations().get(0).setTitle("cluster-provisioning");
191
        formRepresentation.setMediaType(MediaType.APPLICATION_WWW_FORM);
190
        formRepresentation.setMediaType(MediaType.APPLICATION_WWW_FORM);
192
        formRepresentation.setParameters(Arrays.asList(
191
        formRepresentation.setParameters(Arrays.asList(
193
                new ParameterInfo("clusterName", true, "xs:string", ParameterStyle.PLAIN,
192
                new ParameterInfo("clusterName", true, "xs:string", ParameterStyle.PLAIN,
194
                        "The name of the Elastic Grid Cluster to start."),
193
                        "The name of the Elastic Grid Cluster to start."),
195
                new ParameterInfo("numberOfMonitors", true, "xs:integer", ParameterStyle.PLAIN,
194
                new ParameterInfo("numberOfMonitors", true, "xs:integer", ParameterStyle.PLAIN,
196
                        "The number of monitors to start in the cluster."),
195
                        "The number of monitors to start in the cluster."),
197
                new ParameterInfo("numberOfMonitorsAndAgents", true, "xs:integer", ParameterStyle.PLAIN,
196
                new ParameterInfo("numberOfMonitorsAndAgents", true, "xs:integer", ParameterStyle.PLAIN,
198
                        "The number of 'monitors and agents' to start in the cluster."),
197
                        "The number of 'monitors and agents' to start in the cluster."),
199
                new ParameterInfo("numberOfAgents", true, "xs:integer", ParameterStyle.PLAIN,
198
                new ParameterInfo("numberOfAgents", true, "xs:integer", ParameterStyle.PLAIN,
200
                        "The number of agents to start in the cluster.")
199
                        "The number of agents to start in the cluster.")
201
        ));
200
        ));
202
        info.getRequest().setRepresentations(Arrays.asList(xmlRepresentation, formRepresentation));
201
        info.getRequest().setRepresentations(Arrays.asList(xmlRepresentation, formRepresentation));
203
    }
202
    }
204

203

205
    @Override
204
    @Override
206
    protected void describeDelete(MethodInfo info) {
205
    protected void describeDelete(MethodInfo info) {
207
        super.describeDelete(info);
206
        super.describeDelete(info);
208
        info.setDocumentations(Arrays.asList(
207
        info.setDocumentations(Arrays.asList(
209
                new DocumentationInfo("Stop {clusterName} cluster."),
208
                new DocumentationInfo("Stop {clusterName} cluster."),
210
                new DocumentationInfo("All nodes in the cluster will be shut down.")
209
                new DocumentationInfo("All nodes in the cluster will be shut down.")
211
        ));
210
        ));
212
    }
211
    }
213

212

214
    @Override
213
    @Override
215
    public boolean allowPut() {
214
    public boolean allowPut() {
216
        return false;
215
        return false;
217
    }
216
    }
218

217

219
}
218
}
220

219

221
Generated by GNU enscript 1.6.4.
220
Generated by GNU enscript 1.6.4.
222

221

223

222