88875b510e8a8712c265bd87eb771213ecf0587d
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controllers / ChangeManagementController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright 2018 Nokia
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.vid.controllers;
24
25 import static org.onap.vid.utils.Logging.getMethodName;
26 import static org.springframework.http.HttpStatus.BAD_REQUEST;
27 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
28 import static org.springframework.http.HttpStatus.OK;
29
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import java.util.Collection;
32 import java.util.Collections;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.WebApplicationException;
35 import org.apache.commons.lang3.exception.ExceptionUtils;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.json.simple.JSONArray;
38 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
39 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
40 import org.onap.vid.changeManagement.ChangeManagementRequest;
41 import org.onap.vid.changeManagement.GetVnfWorkflowRelationRequest;
42 import org.onap.vid.changeManagement.GetWorkflowsResponse;
43 import org.onap.vid.changeManagement.VnfWorkflowRelationAllResponse;
44 import org.onap.vid.changeManagement.VnfWorkflowRelationRequest;
45 import org.onap.vid.changeManagement.VnfWorkflowRelationResponse;
46 import org.onap.vid.exceptions.NotFoundException;
47 import org.onap.vid.model.ExceptionResponse;
48 import org.onap.vid.model.MsoExceptionResponse;
49 import org.onap.vid.mso.MsoResponseWrapper2;
50 import org.onap.vid.mso.MsoResponseWrapperInterface;
51 import org.onap.vid.mso.rest.Request;
52 import org.onap.vid.services.ChangeManagementService;
53 import org.onap.vid.services.WorkflowService;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.web.bind.annotation.ExceptionHandler;
58 import org.springframework.web.bind.annotation.PathVariable;
59 import org.springframework.web.bind.annotation.RequestBody;
60 import org.springframework.web.bind.annotation.RequestMapping;
61 import org.springframework.web.bind.annotation.RequestMethod;
62 import org.springframework.web.bind.annotation.RequestParam;
63 import org.springframework.web.bind.annotation.RequestPart;
64 import org.springframework.web.bind.annotation.ResponseBody;
65 import org.springframework.web.bind.annotation.ResponseStatus;
66 import org.springframework.web.bind.annotation.RestController;
67 import org.springframework.web.multipart.MultipartFile;
68
69 /**
70  * Controller to handle ChangeManagement feature requests.
71  */
72 @RestController
73 @RequestMapping(ChangeManagementController.CHANGE_MANAGEMENT)
74 public class ChangeManagementController extends UnRestrictedBaseController {
75     public static final String VNF_WORKFLOW_RELATION = "vnf_workflow_relation";
76     public static final String CHANGE_MANAGEMENT = "change-management";
77     public static final String GET_VNF_WORKFLOW_RELATION = "get_vnf_workflow_relation";
78     public static final String SCHEDULER_BY_SCHEDULE_ID = "/scheduler/schedules/{scheduleId}";
79     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ChangeManagementController.class);
80     private String fromAppId;
81     private final WorkflowService workflowService;
82     private final ChangeManagementService changeManagementService;
83     private final ObjectMapper objectMapper;
84
85
86     @Autowired
87     public ChangeManagementController(WorkflowService workflowService, ChangeManagementService changeManagementService, ObjectMapper objectMapper) {
88         this.fromAppId = "VidChangeManagementController";
89         this.workflowService = workflowService;
90         this.changeManagementService = changeManagementService;
91         this.objectMapper = objectMapper;
92     }
93
94     @RequestMapping(value = {"/workflow"}, method = RequestMethod.GET)
95     public ResponseEntity<Collection<String>> getWorkflow(@RequestParam("vnfs") Collection<String> vnfs) {
96         Collection<String> result = this.workflowService.getWorkflowsForVNFs(vnfs);
97         return new ResponseEntity<>(result, OK);
98     }
99
100     @RequestMapping(value = {"/mso"}, method = RequestMethod.GET)
101     public ResponseEntity<Collection<Request>> getMSOChangeManagements() {
102
103         Collection<Request> result = this.changeManagementService.getMSOChangeManagements();
104         return new ResponseEntity<>(result, OK);
105     }
106
107     @RequestMapping(value = "/workflow/{vnfName}", method = RequestMethod.POST)
108     public ResponseEntity<String> changeManagement(HttpServletRequest request, @PathVariable("vnfName") String vnfName,
109                                                    @RequestBody ChangeManagementRequest changeManagmentRequest)
110             throws Exception {
111         return this.changeManagementService.doChangeManagement(changeManagmentRequest, vnfName);
112     }
113
114     @RequestMapping(value = "/uploadConfigUpdateFile", method = RequestMethod.POST)
115     public @ResponseBody ResponseEntity uploadConfigUpdateFile(@RequestPart("file") MultipartFile file)
116         throws Exception {
117         try {
118             String jsonString = this.changeManagementService.uploadConfigUpdateFile(file);
119             return new ResponseEntity<>(jsonString, HttpStatus.OK);
120         }
121         catch(WebApplicationException e){
122             return new ResponseEntity<>(handleException(e),  HttpStatus.valueOf(e.getResponse().getStatus()));
123         }
124         catch (Exception e) {
125             return new ResponseEntity<>(handleException(e), INTERNAL_SERVER_ERROR);
126         }
127     }
128
129
130     @RequestMapping(value = {"/scheduler"}, method = RequestMethod.GET)
131     public ResponseEntity<JSONArray> getSchedulerChangeManagements() {
132         JSONArray result = this.changeManagementService.getSchedulerChangeManagements();
133         return new ResponseEntity<>(result, OK);
134     }
135
136     @RequestMapping(value = {SCHEDULER_BY_SCHEDULE_ID}, method = RequestMethod.DELETE)
137     public ResponseEntity deleteSchedule(@PathVariable("scheduleId") String scheduleId) {
138         Pair<String, Integer> result = this.changeManagementService.deleteSchedule(scheduleId);
139         return ResponseEntity.status(result.getRight()).build();
140     }
141
142     
143     @RequestMapping(value = {GET_VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
144     public ResponseEntity getWorkflows(@RequestBody GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
145         try {
146             GetWorkflowsResponse response = new GetWorkflowsResponse(changeManagementService.getWorkflowsForVnf(getVnfWorkflowRelationRequest));
147             return ResponseEntity.status(OK).body(response);
148         }
149         catch (NotFoundException exception) {
150             LOGGER.error(exception.getMessage(), exception);
151             return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(exception.getMessage())),HttpStatus.NOT_FOUND);
152         }
153         catch (Exception exception) {
154             return handleException(exception, "Failed to get workflows for vnf");
155         }
156     }
157     
158     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
159     public ResponseEntity createWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
160         VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
161         try {
162             vnfWorkflowRelationResponse = changeManagementService.addVnfWorkflowRelation(vnfWorkflowRelationRequest);
163         }
164         catch (Exception exception) {
165             return handleException(exception, "Failed to add vnf to workflow relation");
166         }
167         
168         return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
169     }
170
171     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.GET)
172     public ResponseEntity getAllWorkflowRelation() {
173
174         try {
175             VnfWorkflowRelationAllResponse vnfWorkflowRelationAllResponse = changeManagementService.getAllVnfWorkflowRelations();
176             return new ResponseEntity<>(vnfWorkflowRelationAllResponse, OK);
177         }
178         catch (Exception exception) {
179             return handleException(exception, "Failed to get all vnf to workflow relations");
180         }
181     }
182     
183     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.DELETE)
184     public ResponseEntity deleteWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
185         VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
186         try {
187                 vnfWorkflowRelationResponse = changeManagementService.deleteVnfWorkflowRelation(vnfWorkflowRelationRequest);
188         }
189         catch (Exception exception) {
190             return handleException(exception, "Failed to delete vnf from workflow relation");
191         }
192
193         return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
194     }
195
196     private ResponseEntity handleException(Exception exception, String msg) {
197         LOGGER.error(msg, exception);
198         return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(msg)), HttpStatus.INTERNAL_SERVER_ERROR);
199     }
200
201
202     private ExceptionResponse handleException(Exception e) {
203         return ControllersUtils.handleException(e, LOGGER);
204     }
205
206     @ExceptionHandler(Exception.class)
207     @ResponseStatus(value=OK) //return 200 for Backwards compatibility with the previous responses to scheduler
208     private MsoResponseWrapperInterface exceptionHandler(Exception e) {
209         return exceptionHandler(e, INTERNAL_SERVER_ERROR);
210     }
211
212     @ExceptionHandler({
213             javax.ws.rs.BadRequestException.class,
214     })
215     @ResponseStatus(value = OK) //return 200 for Backwards compatibility with the previous responses to scheduler
216     public MsoResponseWrapperInterface clientDerivedExceptionAsBadRequest(Exception e) {
217         // same handler, different HTTP Code
218         return exceptionHandler(e, BAD_REQUEST);
219     }
220
221     private MsoResponseWrapperInterface exceptionHandler(Exception e, HttpStatus httpStatus) {
222         LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodName(), ExceptionUtils.getMessage(e), e);
223         MsoResponseWrapper2<MsoExceptionResponse> responseWrapper2 = new MsoResponseWrapper2<>(httpStatus.value(), new MsoExceptionResponse(e));
224         return responseWrapper2;
225     }
226
227 }