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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License")
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.so.apihandlerinfra;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.apache.http.HttpStatus;
import org.json.JSONObject;
import org.onap.so.apihandler.common.ErrorNumbers;
import org.onap.so.apihandler.common.ResponseBuilder;
import org.onap.so.apihandlerinfra.exceptions.ApiException;
import org.onap.so.db.request.beans.OrchestrationTask;
import org.onap.so.db.request.client.RequestsDbClient;
import org.onap.logging.filter.base.ErrorCode;
import org.onap.so.logger.LoggingAnchor;
import org.onap.so.logger.MessageEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.transaction.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.onap.so.apihandlerinfra.Constants.MSO_PROP_APIHANDLER_INFRA;
@Path("/onap/so/infra/orchestrationTasks")
@OpenAPIDefinition(
info = @Info(title = "onap/so/infra/orchestrationTasks", description = "API Requests for Orchestration Task"))
@Component
public class OrchestrationTasks {
private static Logger logger = LoggerFactory.getLogger(OrchestrationTasks.class);
@Autowired
private MsoRequest msoRequest;
@Autowired
private CamundaRequestHandler camundaRequestHandler;
@Autowired
private RequestsDbClient requestsDbClient;
@Autowired
private ResponseBuilder builder;
private ObjectMapper mapper = new ObjectMapper();
@GET
@Path("/{version:[vV][4-7]}/")
@Operation(description = "Find All Orchestrated Task", responses = @ApiResponse(
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response getAllOrchestrationTasks(@QueryParam("status") String status,
@PathParam("version") String version) {
List<OrchestrationTask> orchestrationTaskList = requestsDbClient.getAllOrchestrationTasks();
if (status != null && !status.isEmpty()) {
for (Iterator<OrchestrationTask> it = orchestrationTaskList.iterator(); it.hasNext();) {
OrchestrationTask task = it.next();
if (!status.equals(task.getStatus())) {
it.remove();
}
}
}
return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTaskList, version);
}
@GET
@Path("/{version:[vV][4-7]}/{taskId}")
@Operation(description = "Find Orchestrated Task for a given TaskId", responses = @ApiResponse(
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response getOrchestrationTask(@PathParam("taskId") String taskId, @PathParam("version") String version)
throws ApiException {
try {
OrchestrationTask orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Lookup", e);
Response response =
msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
return response;
}
}
@POST
@Path("/{version:[vV][4-7]}/")
@Operation(description = "Create an Orchestrated Task", responses = @ApiResponse(
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response CreateOrchestrationTask(String requestJson, @PathParam("version") String version) {
try {
OrchestrationTask orchestrationTask = mapper.readValue(requestJson, OrchestrationTask.class);
requestsDbClient.createOrchestrationTask(orchestrationTask);
return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Create", e);
Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
MsoException.ServiceException, e.getMessage(), ErrorNumbers.COULD_NOT_WRITE_TO_REQUESTS_DB, null,
version);
return response;
}
}
@PUT
@Path("/{version:[vV][4-7]}/{taskId}")
@Operation(description = "Update an Orchestrated Task", responses = @ApiResponse(
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response UpdateOrchestrationTask(@PathParam("taskId") String taskId, String requestJson,
@PathParam("version") String version) {
try {
OrchestrationTask orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Update", e);
Response response =
msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
return response;
}
try {
OrchestrationTask orchestrationTask = mapper.readValue(requestJson, OrchestrationTask.class);
requestsDbClient.updateOrchestrationTask(taskId, orchestrationTask);
return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Update", e);
Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
MsoException.ServiceException, e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null,
version);
return response;
}
}
@DELETE
@Path("/{version:[vV][4-7]}/{taskId}")
@Operation(description = "Delete an Orchestrated Task", responses = @ApiResponse(
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response DeleteOrchestrationTask(@PathParam("taskId") String taskId, @PathParam("version") String version) {
try {
OrchestrationTask orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Delete", e);
Response response =
msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
return response;
}
try {
requestsDbClient.deleteOrchestrationTask(taskId);
return builder.buildResponse(HttpStatus.SC_OK, null, null, version);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Delete", e);
Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
MsoException.ServiceException, e.getMessage(), ErrorNumbers.COULD_NOT_WRITE_TO_REQUESTS_DB, null,
version);
return response;
}
}
@POST
@Path("/{version:[vV][4-7]}/{taskId}/commit")
@Operation(description = "Commit an Orchestrated Task", responses = @ApiResponse(
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response CommitOrchestrationTask(@PathParam("taskId") String taskId, @PathParam("version") String version) {
OrchestrationTask orchestrationTask;
try {
orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Commit", e);
Response response =
msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
return response;
}
try {
String taskName = orchestrationTask.getName();
Map<String, String> commitVar = new HashMap<>();
commitVar.put("taskAction", "commit");
JSONObject msgJson = createMessageBody(taskId, taskName, commitVar);
camundaRequestHandler.sendCamundaMessages(msgJson);
return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Delete", e);
Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
MsoException.ServiceException, e.getMessage(), ErrorNumbers.ERROR_FROM_BPEL, null, version);
return response;
}
}
@POST
@Path("/{version:[vV][4-7]}/{taskId}/abort")
@Operation(description = "Commit an Orchestrated Task", responses = @ApiResponse(
content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response AbortOrchestrationTask(@PathParam("taskId") String taskId, @PathParam("version") String version) {
OrchestrationTask orchestrationTask;
try {
orchestrationTask = requestsDbClient.getOrchestrationTask(taskId);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Commit", e);
Response response =
msoRequest.buildServiceErrorResponse(HttpStatus.SC_NOT_FOUND, MsoException.ServiceException,
e.getMessage(), ErrorNumbers.NO_COMMUNICATION_TO_REQUESTS_DB, null, version);
return response;
}
try {
String taskName = orchestrationTask.getName();
Map<String, String> commitVar = new HashMap<>();
commitVar.put("taskAction", "abort");
JSONObject msgJson = createMessageBody(taskId, taskName, commitVar);
camundaRequestHandler.sendCamundaMessages(msgJson);
return builder.buildResponse(HttpStatus.SC_OK, null, orchestrationTask, version);
} catch (Exception e) {
logger.error(LoggingAnchor.FOUR, MessageEnum.APIH_DB_ACCESS_EXC.toString(), MSO_PROP_APIHANDLER_INFRA,
ErrorCode.AvailabilityError.getValue(),
"Exception while communciate with Request DB - Orchestration Task Delete", e);
Response response = msoRequest.buildServiceErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
MsoException.ServiceException, e.getMessage(), ErrorNumbers.ERROR_FROM_BPEL, null, version);
return response;
}
}
private JSONObject createMessageBody(String taskId, String taskName, Map<String, ?> variables) {
JSONObject msgJson = new JSONObject();
msgJson.put("messageName", taskName);
msgJson.put("businessKey", taskId);
JSONObject processVariables = new JSONObject();
for (Map.Entry<String, ?> entry : variables.entrySet()) {
JSONObject valueInfo = new JSONObject();
String key = entry.getKey();
Object value = entry.getValue();
valueInfo.put("value", value.toString());
valueInfo.put("type", value.getClass().getSimpleName());
processVariables.put(key, valueInfo);
}
msgJson.put("processVariables", processVariables);
return msgJson;
}
}
|