Merge 1806 code of vid-common
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / ChangeManagementController.java
1 package org.onap.vid.controllers;
2
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;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.WebApplicationException;
26 import java.util.Collection;
27 import java.util.Collections;
28
29 import static org.onap.vid.utils.Logging.getMethodName;
30 import static org.springframework.http.HttpStatus.*;
31
32 /**
33  * Controller to handle ChangeManagement feature requests.
34  */
35 @RestController
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;
47
48
49     @Autowired
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;
55     }
56
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);
61     }
62
63     @RequestMapping(value = {"/mso"}, method = RequestMethod.GET)
64     public ResponseEntity<Collection<Request>> getMSOChangeManagements() {
65
66         Collection<Request> result = this.changeManagementService.getMSOChangeManagements();
67         return new ResponseEntity<>(result, OK);
68     }
69
70     @RequestMapping(value = "/workflow/{vnfName}", method = RequestMethod.POST)
71     public ResponseEntity<String> changeManagement(@PathVariable("vnfName") String vnfName,
72                                                    HttpServletRequest request,
73                                                    @RequestBody ChangeManagementRequest changeManagmentRequest)
74             throws Exception {
75         return this.changeManagementService.doChangeManagement(changeManagmentRequest, vnfName);
76     }
77
78     @RequestMapping(value = "/uploadConfigUpdateFile", method = RequestMethod.POST)
79     public @ResponseBody ResponseEntity uploadConfigUpdateFile(@RequestPart("file") MultipartFile file)
80         throws Exception {
81         try {
82             String jsonString = this.changeManagementService.uploadConfigUpdateFile(file);
83             return new ResponseEntity<>(jsonString, HttpStatus.OK);
84         }
85         catch(WebApplicationException e){
86             return new ResponseEntity<>(handleException(e),  HttpStatus.valueOf(e.getResponse().getStatus()));
87         }
88         catch (Exception e) {
89             return new ResponseEntity<>(handleException(e), INTERNAL_SERVER_ERROR);
90         }
91     }
92
93
94     @RequestMapping(value = {"/scheduler"}, method = RequestMethod.GET)
95     public ResponseEntity<JSONArray> getSchedulerChangeManagements() {
96         JSONArray result = this.changeManagementService.getSchedulerChangeManagements();
97         return new ResponseEntity<>(result, OK);
98     }
99
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();
104     }
105
106     
107     @RequestMapping(value = {GET_VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
108     public ResponseEntity getWorkflows(@RequestBody GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
109         try {
110             GetWorkflowsResponse response = new GetWorkflowsResponse(changeManagementService.getWorkflowsForVnf(getVnfWorkflowRelationRequest));
111             return ResponseEntity.status(OK).body(response);
112         }
113         catch (NotFoundException exception) {
114             LOGGER.error(exception.getMessage(), exception);
115             return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(exception.getMessage())),HttpStatus.NOT_FOUND);
116         }
117         catch (Exception exception) {
118             return handleException(exception, "Failed to get workflows for vnf");
119         }
120     }
121     
122     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
123     public ResponseEntity createWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
124         VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
125         try {
126             vnfWorkflowRelationResponse = changeManagementService.addVnfWorkflowRelation(vnfWorkflowRelationRequest);
127         }
128         catch (Exception exception) {
129             return handleException(exception, "Failed to add vnf to workflow relation");
130         }
131         
132         return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
133     }
134
135     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.GET)
136     public ResponseEntity getAllWorkflowRelation() {
137
138         try {
139             VnfWorkflowRelationAllResponse vnfWorkflowRelationAllResponse = changeManagementService.getAllVnfWorkflowRelations();
140             return new ResponseEntity<>(vnfWorkflowRelationAllResponse, OK);
141         }
142         catch (Exception exception) {
143             return handleException(exception, "Failed to get all vnf to workflow relations");
144         }
145     }
146     
147     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.DELETE)
148     public ResponseEntity deleteWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
149         VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
150         try {
151                 vnfWorkflowRelationResponse = changeManagementService.deleteVnfWorkflowRelation(vnfWorkflowRelationRequest);
152         }
153         catch (Exception exception) {
154             return handleException(exception, "Failed to delete vnf from workflow relation");
155         }
156
157         return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
158     }
159
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);
163     }
164
165
166     private ExceptionResponse handleException(Exception e) {
167         return ControllersUtils.handleException(e, LOGGER);
168     }
169
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);
174     }
175
176     @ExceptionHandler({
177             javax.ws.rs.BadRequestException.class,
178     })
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);
183     }
184
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;
189     }
190
191 }