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