1 package org.onap.vid.controllers;
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.onap.portalsdk.core.controller.UnRestrictedBaseController;
8 import org.onap.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.util.Collection;
27 import java.util.Collections;
29 import static org.onap.vid.utils.Logging.getMethodName;
30 import static org.springframework.http.HttpStatus.*;
33 * Controller to handle ChangeManagement feature requests.
36 @RequestMapping(ChangeManagementController.CHANGE_MANAGEMENT)
37 public class ChangeManagementController extends UnRestrictedBaseController {
38 public static final String VNF_WORKFLOW_RELATION = "vnf_workflow_relation";
39 public static final String CHANGE_MANAGEMENT = "change-management";
40 public static final String GET_VNF_WORKFLOW_RELATION = "get_vnf_workflow_relation";
41 public static final String SCHEDULER_BY_SCHEDULE_ID = "/scheduler/schedules/{scheduleId}";
42 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ChangeManagementController.class);
43 private String fromAppId;
44 private final WorkflowService workflowService;
45 private final ChangeManagementService changeManagementService;
46 private final ObjectMapper objectMapper;
50 public ChangeManagementController(WorkflowService workflowService, ChangeManagementService changeManagementService, ObjectMapper objectMapper) {
51 this.fromAppId = "VidChangeManagementController";
52 this.workflowService = workflowService;
53 this.changeManagementService = changeManagementService;
54 this.objectMapper = objectMapper;
57 @RequestMapping(value = {"/workflow"}, method = RequestMethod.GET)
58 public ResponseEntity<Collection<String>> getWorkflow(@RequestParam("vnfs") Collection<String> vnfs) {
59 Collection<String> result = this.workflowService.getWorkflowsForVNFs(vnfs);
60 return new ResponseEntity<>(result, OK);
63 @RequestMapping(value = {"/mso"}, method = RequestMethod.GET)
64 public ResponseEntity<Collection<Request>> getMSOChangeManagements() {
66 Collection<Request> result = this.changeManagementService.getMSOChangeManagements();
67 return new ResponseEntity<>(result, OK);
70 @RequestMapping(value = "/workflow/{vnfName}", method = RequestMethod.POST)
71 public ResponseEntity<String> changeManagement(@PathVariable("vnfName") String vnfName,
72 HttpServletRequest request,
73 @RequestBody ChangeManagementRequest changeManagmentRequest)
75 return this.changeManagementService.doChangeManagement(changeManagmentRequest, vnfName);
78 @RequestMapping(value = "/uploadConfigUpdateFile", method = RequestMethod.POST)
79 public @ResponseBody ResponseEntity uploadConfigUpdateFile(@RequestPart("file") MultipartFile file)
82 String jsonString = this.changeManagementService.uploadConfigUpdateFile(file);
83 return new ResponseEntity<>(jsonString, HttpStatus.OK);
85 catch(WebApplicationException e){
86 return new ResponseEntity<>(handleException(e), HttpStatus.valueOf(e.getResponse().getStatus()));
89 return new ResponseEntity<>(handleException(e), INTERNAL_SERVER_ERROR);
94 @RequestMapping(value = {"/scheduler"}, method = RequestMethod.GET)
95 public ResponseEntity<JSONArray> getSchedulerChangeManagements() {
96 JSONArray result = this.changeManagementService.getSchedulerChangeManagements();
97 return new ResponseEntity<>(result, OK);
100 @RequestMapping(value = {SCHEDULER_BY_SCHEDULE_ID}, method = RequestMethod.DELETE)
101 public ResponseEntity deleteSchedule(@PathVariable("scheduleId") String scheduleId) {
102 Pair<String, Integer> result = this.changeManagementService.deleteSchedule(scheduleId);
103 return ResponseEntity.status(result.getRight()).build();
107 @RequestMapping(value = {GET_VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
108 public ResponseEntity getWorkflows(@RequestBody GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
110 GetWorkflowsResponse response = new GetWorkflowsResponse(changeManagementService.getWorkflowsForVnf(getVnfWorkflowRelationRequest));
111 return ResponseEntity.status(OK).body(response);
113 catch (NotFoundException exception) {
114 LOGGER.error(exception.getMessage(), exception);
115 return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(exception.getMessage())),HttpStatus.NOT_FOUND);
117 catch (Exception exception) {
118 return handleException(exception, "Failed to get workflows for vnf");
122 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
123 public ResponseEntity createWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
124 VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
126 vnfWorkflowRelationResponse = changeManagementService.addVnfWorkflowRelation(vnfWorkflowRelationRequest);
128 catch (Exception exception) {
129 return handleException(exception, "Failed to add vnf to workflow relation");
132 return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
135 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.GET)
136 public ResponseEntity getAllWorkflowRelation() {
139 VnfWorkflowRelationAllResponse vnfWorkflowRelationAllResponse = changeManagementService.getAllVnfWorkflowRelations();
140 return new ResponseEntity<>(vnfWorkflowRelationAllResponse, OK);
142 catch (Exception exception) {
143 return handleException(exception, "Failed to get all vnf to workflow relations");
147 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.DELETE)
148 public ResponseEntity deleteWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
149 VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
151 vnfWorkflowRelationResponse = changeManagementService.deleteVnfWorkflowRelation(vnfWorkflowRelationRequest);
153 catch (Exception exception) {
154 return handleException(exception, "Failed to delete vnf from workflow relation");
157 return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
160 private ResponseEntity handleException(Exception exception, String msg) {
161 LOGGER.error(msg, exception);
162 return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(msg)), HttpStatus.INTERNAL_SERVER_ERROR);
166 private ExceptionResponse handleException(Exception e) {
167 return ControllersUtils.handleException(e, LOGGER);
170 @ExceptionHandler(Exception.class)
171 @ResponseStatus(value=OK) //return 200 for Backwards compatibility with the previous responses to scheduler
172 private MsoResponseWrapperInterface exceptionHandler(Exception e) {
173 return exceptionHandler(e, INTERNAL_SERVER_ERROR);
177 javax.ws.rs.BadRequestException.class,
179 @ResponseStatus(value = OK) //return 200 for Backwards compatibility with the previous responses to scheduler
180 public MsoResponseWrapperInterface clientDerivedExceptionAsBadRequest(Exception e) {
181 // same handler, different HTTP Code
182 return exceptionHandler(e, BAD_REQUEST);
185 private MsoResponseWrapperInterface exceptionHandler(Exception e, HttpStatus httpStatus) {
186 LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodName(), ExceptionUtils.getMessage(e), e);
187 MsoResponseWrapper2<MsoExceptionResponse> responseWrapper2 = new MsoResponseWrapper2<>(httpStatus.value(), new MsoExceptionResponse(e));
188 return responseWrapper2;