summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/resources/v1_0/TasksResource.java
blob: e147a7a46085876d2026fd91af6646384d356d51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Gateway.
 *
 * Gluster Management Gateway is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Gluster Management Gateway is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.gateway.resources.v1_0;

import static org.gluster.storage.management.core.constants.RESTConstants.FORM_PARAM_OPERATION;
import static org.gluster.storage.management.core.constants.RESTConstants.PATH_PARAM_CLUSTER_NAME;
import static org.gluster.storage.management.core.constants.RESTConstants.PATH_PARAM_TASK_ID;
import static org.gluster.storage.management.core.constants.RESTConstants.RESOURCE_PATH_CLUSTERS;
import static org.gluster.storage.management.core.constants.RESTConstants.RESOURCE_TASKS;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.gluster.storage.management.core.constants.RESTConstants;
import org.gluster.storage.management.core.exceptions.GlusterRuntimeException;
import org.gluster.storage.management.core.exceptions.GlusterValidationException;
import org.gluster.storage.management.core.model.Status;
import org.gluster.storage.management.core.model.TaskInfo;
import org.gluster.storage.management.core.response.TaskInfoListResponse;
import org.gluster.storage.management.gateway.tasks.Task;
import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;

@Path(RESOURCE_PATH_CLUSTERS + "/{" + PATH_PARAM_CLUSTER_NAME + "}/" + RESOURCE_TASKS)
@Singleton
@Component

public class TasksResource extends AbstractResource {
	private Map<String, Map<String, Task>> tasksMap = new HashMap<String, Map<String, Task>>();

	public TasksResource() {
	}

	@SuppressWarnings({"unchecked", "rawtypes"})
	public void addTask(String clusterName, Task task) {
		Map clusterTaskMap = tasksMap.get(clusterName);
		if(clusterTaskMap == null) {
			clusterTaskMap = new HashMap<String, Task>();
			tasksMap.put(clusterName, clusterTaskMap);
		}
		// Remove the task if already exist
		if (clusterTaskMap.containsKey(task.getId())) {
			 removeTask(clusterName, task);
		}
		clusterTaskMap.put(task.getId(), task);
	}

	@SuppressWarnings("rawtypes")
	public void removeTask(String clusterName, Task task) {
		Map clusterTaskMap = tasksMap.get(clusterName);
		if (clusterTaskMap != null) {
			clusterTaskMap.remove(task.getId());
		}
	}

	public List<TaskInfo> getAllTasksInfo(String clusterName) {
		Map<String, Task> clusterTasksMap = tasksMap.get(clusterName);
		List<TaskInfo> allTasksInfo = new ArrayList<TaskInfo>();
		if ( clusterTasksMap == null) {
			return allTasksInfo;
		}
		for (Map.Entry<String, Task> entry : clusterTasksMap.entrySet()) {
			checkTaskStatus(clusterName, entry.getKey());
			allTasksInfo.add(entry.getValue().getTaskInfo()); // TaskInfo with latest status 
		}
		return allTasksInfo;
	}

	public Task getTask(String clusterName, String taskId) {
		Map<String, Task> clusterTasksMap = tasksMap.get(clusterName);
		for (Map.Entry<String, Task> entry : clusterTasksMap.entrySet()) {
			if (entry.getValue().getId().equals(taskId)) {
				return entry.getValue();
			}
		}
		return null;
	}
	
	public List<Task> getAllTasks(String clusterName) {
		Map<String, Task> clusterTasksMap = tasksMap.get(clusterName);
		List<Task> tasks = new ArrayList<Task>();
		for (Map.Entry<String, Task> entry : clusterTasksMap.entrySet()) {
			checkTaskStatus(clusterName, entry.getKey());
			tasks.add( (Task) entry.getValue());
		}
		return tasks;
	}
	
	@GET
	@Produces(MediaType.APPLICATION_XML)
	public Response getTasks(@PathParam(PATH_PARAM_CLUSTER_NAME) String clusterName) {
		try {
			return okResponse(new TaskInfoListResponse(getAllTasksInfo(clusterName)), MediaType.APPLICATION_XML);
		} catch (GlusterRuntimeException e) {
			return errorResponse(e.getMessage());
		}
	}
	
	@GET
	@Path("/{" + PATH_PARAM_TASK_ID + "}")
	@Produces(MediaType.APPLICATION_XML)
	public Response getTaskStatus(@PathParam(PATH_PARAM_CLUSTER_NAME) String clusterName,
			@PathParam(PATH_PARAM_TASK_ID) String taskId) {
		try {
			Task task = checkTaskStatus(clusterName, taskId);
			return okResponse(task.getTaskInfo(), MediaType.APPLICATION_XML);
		} catch (GlusterRuntimeException e) {
			return errorResponse(e.getMessage());
		}
	}

	private Task checkTaskStatus(String clusterName, String taskId) {
		Task task = getTask(clusterName, taskId);
		// No status check required if the task already complete or failure
		if (task.getTaskInfo().getStatus().getCode() == Status.STATUS_CODE_FAILURE
				|| task.getTaskInfo().getStatus().getCode() == Status.STATUS_CODE_SUCCESS) {
			return task;
		}
		task.getTaskInfo().setStatus(task.checkStatus());
		return task;
	}

	@PUT
	@Path("/{" + PATH_PARAM_TASK_ID + "}")
	@Produces(MediaType.APPLICATION_XML)
	public Response performTask(@PathParam(PATH_PARAM_CLUSTER_NAME) String clusterName,
			@PathParam(PATH_PARAM_TASK_ID) String taskId, @FormParam(FORM_PARAM_OPERATION) String taskOperation) {
		Task task = getTask(clusterName, taskId);
		
		try {
			if (taskOperation.equals(RESTConstants.TASK_RESUME)) {
				task.resume();
			} else if (taskOperation.equals(RESTConstants.TASK_PAUSE)) {
				task.pause();
			} else if (taskOperation.equals(RESTConstants.TASK_STOP)) {
				// task.stop();
				clearTask(clusterName, taskId, taskOperation); // Stop and remove from the task list
			} else if (taskOperation.equals(RESTConstants.TASK_COMMIT)) {
				task.commit();
			}
			return (Response) noContentResponse();
		} catch(GlusterValidationException ve) {
			return badRequestResponse(ve.getMessage());
		} catch (GlusterRuntimeException e) {
			return errorResponse(e.getMessage());
		}
	}

	@DELETE
	@Path("/{" + PATH_PARAM_TASK_ID + "}")
	@Produces(MediaType.APPLICATION_XML)
	public Response clearTask(@PathParam(PATH_PARAM_CLUSTER_NAME) String clusterName,
			@PathParam(PATH_PARAM_TASK_ID) String taskId, @QueryParam(FORM_PARAM_OPERATION) String taskOperation) {
		Task task = getTask(clusterName, taskId);
		if (task == null) {
			return notFoundResponse("Requested task not found!");
		}
		
		if(clusterName == null || clusterName.isEmpty()) {
			return badRequestResponse("Parameter [" + PATH_PARAM_CLUSTER_NAME + "] is missing in request!");
		}
		
		if(taskOperation == null || taskOperation.isEmpty()) {
			int taskStatus = task.getTaskInfo().getStatus().getCode();
			if (taskStatus == Status.STATUS_CODE_SUCCESS || taskStatus == Status.STATUS_CODE_FAILURE
					|| taskStatus == Status.STATUS_CODE_ERROR) {
				taskOperation = RESTConstants.TASK_DELETE;
			} else {
				taskOperation = RESTConstants.TASK_STOP;
			}
//			return badRequestResponse("Parameter [" + FORM_PARAM_OPERATION + "] is missing in request!");
		}
		
		if(!taskOperation.equals(RESTConstants.TASK_STOP) && !taskOperation.equals(RESTConstants.TASK_DELETE)) {
			return badRequestResponse("Invalid value [" + taskOperation + "] for parameter [" + FORM_PARAM_OPERATION
					+ "]");
		}
		
		try {
			if (taskOperation.equals(RESTConstants.TASK_STOP)) {
				task.stop();
				// On successful, intentionally stopped task can be removed from task list also
				taskOperation = RESTConstants.TASK_DELETE;
			}

			if (taskOperation.equals(RESTConstants.TASK_DELETE)) {
				removeTask(clusterName, task);
			}
			
			return noContentResponse();
		} catch (Exception e) {
			return errorResponse(e.getMessage());
		}
	}
}