1 package org.onap.vid.controller;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import org.apache.commons.lang3.exception.ExceptionUtils;
5 import org.apache.commons.lang3.tuple.Pair;
6 import org.json.simple.JSONArray;
7 import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController;
8 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
9 import org.onap.vid.changeManagement.*;
10 import org.onap.vid.exceptions.NotFoundException;
11 import org.onap.vid.model.ExceptionResponse;
12 import org.onap.vid.model.MsoExceptionResponse;
13 import org.onap.vid.mso.MsoResponseWrapper2;
14 import org.onap.vid.mso.MsoResponseWrapperInterface;
15 import org.onap.vid.mso.rest.Request;
16 import org.onap.vid.services.ChangeManagementService;
17 import org.onap.vid.services.WorkflowService;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.http.HttpStatus;
20 import org.springframework.http.ResponseEntity;
21 import org.springframework.web.bind.annotation.*;
22 import org.springframework.web.multipart.MultipartFile;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.WebApplicationException;
26 import java.io.IOException;
27 import java.util.Collection;
28 import java.util.Collections;
30 import static org.onap.vid.utils.Logging.getMethodName;
31 import static org.springframework.http.HttpStatus.*;
34 * Controller to handle ChangeManagement feature requests.
37 @RequestMapping(ChangeManagementController.CHANGE_MANAGEMENT)
38 public class ChangeManagementController extends UnRestrictedBaseController {
39 private static final String GetWorkflowsResponse = null;
40 public static final String VNF_WORKFLOW_RELATION = "vnf_workflow_relation";
41 public static final String CHANGE_MANAGEMENT = "change-management";
42 public static final String GET_VNF_WORKFLOW_RELATION = "get_vnf_workflow_relation";
43 public static final String SCHEDULER_BY_SCHEDULE_ID = "/scheduler/schedules/{scheduleId}";
44 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ChangeManagementController.class);
45 private String fromAppId;
46 private final WorkflowService workflowService;
47 private final ChangeManagementService changeManagementService;
48 private final ObjectMapper objectMapper;
52 public ChangeManagementController(WorkflowService workflowService, ChangeManagementService changeManagementService, ObjectMapper objectMapper) {
53 this.fromAppId = "VidChangeManagementController";
54 this.workflowService = workflowService;
55 this.changeManagementService = changeManagementService;
56 this.objectMapper = objectMapper;
59 @RequestMapping(value = {"/workflow"}, method = RequestMethod.GET)
60 public ResponseEntity<Collection<String>> getWorkflow(@RequestParam("vnfs") Collection<String> vnfs) throws IOException, InterruptedException {
61 Collection<String> result = this.workflowService.getWorkflowsForVNFs(vnfs);
62 return new ResponseEntity<>(result, OK);
65 @RequestMapping(value = {"/mso"}, method = RequestMethod.GET)
66 public ResponseEntity<Collection<Request>> getMSOChangeManagements() throws Exception, IOException, InterruptedException {
68 Collection<Request> result = this.changeManagementService.getMSOChangeManagements();
69 return new ResponseEntity<>(result, OK);
72 @RequestMapping(value = "/workflow/{vnfName}", method = RequestMethod.POST)
73 public ResponseEntity<String> changeManagement(@PathVariable("vnfName") String vnfName,
74 HttpServletRequest request,
75 @RequestBody ChangeManagementRequest changeManagmentRequest)
77 return this.changeManagementService.doChangeManagement(changeManagmentRequest, vnfName);
80 @RequestMapping(value = "/uploadConfigUpdateFile", method = RequestMethod.POST)
81 public @ResponseBody ResponseEntity uploadConfigUpdateFile(@RequestPart("file") MultipartFile file)
84 String jsonString = this.changeManagementService.uploadConfigUpdateFile(file);
85 return new ResponseEntity<>(jsonString, HttpStatus.OK);
87 catch(WebApplicationException e){
88 return new ResponseEntity<>(handleException(e), HttpStatus.valueOf(e.getResponse().getStatus()));
91 return new ResponseEntity<>(handleException(e), INTERNAL_SERVER_ERROR);
96 @RequestMapping(value = {"/scheduler"}, method = RequestMethod.GET)
97 public ResponseEntity<JSONArray> getSchedulerChangeManagements() throws IOException, InterruptedException {
98 JSONArray result = this.changeManagementService.getSchedulerChangeManagements();
99 return new ResponseEntity<>(result, OK);
102 @RequestMapping(value = {SCHEDULER_BY_SCHEDULE_ID}, method = RequestMethod.DELETE)
103 public ResponseEntity deleteSchedule(@PathVariable("scheduleId") String scheduleId) throws IOException, InterruptedException {
104 Pair<String, Integer> result = this.changeManagementService.deleteSchedule(scheduleId);
105 return ResponseEntity.status(result.getRight()).build();
109 @RequestMapping(value = {GET_VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
110 public ResponseEntity getWorkflows(@RequestBody GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) throws IOException, InterruptedException {
112 GetWorkflowsResponse response = new GetWorkflowsResponse(changeManagementService.getWorkflowsForVnf(getVnfWorkflowRelationRequest));
113 return ResponseEntity.status(OK).body(response);
115 catch (NotFoundException exception) {
116 LOGGER.error(exception.getMessage(), exception);
117 return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(exception.getMessage())),HttpStatus.NOT_FOUND);
119 catch (Exception exception) {
120 return handleException(exception, "Failed to get workflows for vnf");
124 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
125 public ResponseEntity createWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) throws IOException, InterruptedException {
126 VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
128 vnfWorkflowRelationResponse = changeManagementService.addVnfWorkflowRelation(vnfWorkflowRelationRequest);
130 catch (Exception exception) {
131 return handleException(exception, "Failed to add vnf to workflow relation");
134 return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
137 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.GET)
138 public ResponseEntity getAllWorkflowRelation() throws IOException, InterruptedException {
141 VnfWorkflowRelationAllResponse vnfWorkflowRelationAllResponse = changeManagementService.getAllVnfWorkflowRelations();
142 return new ResponseEntity<>(vnfWorkflowRelationAllResponse, OK);
144 catch (Exception exception) {
145 return handleException(exception, "Failed to get all vnf to workflow relations");
149 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.DELETE)
150 public ResponseEntity deleteWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) throws IOException, InterruptedException {
151 VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
153 vnfWorkflowRelationResponse = changeManagementService.deleteVnfWorkflowRelation(vnfWorkflowRelationRequest);
155 catch (Exception exception) {
156 return handleException(exception, "Failed to delete vnf from workflow relation");
159 return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
162 private ResponseEntity handleException(Exception exception, String msg) {
163 LOGGER.error(msg, exception);
164 return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(msg)), HttpStatus.INTERNAL_SERVER_ERROR);
168 private ExceptionResponse handleException(Exception e) {
169 return ControllersUtils.handleException(e, LOGGER);
172 @ExceptionHandler(Exception.class)
173 @ResponseStatus(value=OK) //return 200 for Backwards compatibility with the previous responses to scheduler
174 private MsoResponseWrapperInterface exceptionHandler(Exception e) {
175 return exceptionHandler(e, INTERNAL_SERVER_ERROR);
179 javax.ws.rs.BadRequestException.class,
181 @ResponseStatus(value = OK) //return 200 for Backwards compatibility with the previous responses to scheduler
182 public MsoResponseWrapperInterface clientDerivedExceptionAsBadRequest(Exception e) {
183 // same handler, different HTTP Code
184 return exceptionHandler(e, BAD_REQUEST);
187 private MsoResponseWrapperInterface exceptionHandler(Exception e, HttpStatus httpStatus) {
188 LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodName(), ExceptionUtils.getMessage(e), e);
189 MsoResponseWrapper2<MsoExceptionResponse> responseWrapper2 = new MsoResponseWrapper2<>(httpStatus.value(), new MsoExceptionResponse(e));
190 return responseWrapper2;