2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2018 - 2019 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.vid.controller;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.fasterxml.jackson.databind.node.ArrayNode;
26 import org.apache.commons.lang3.exception.ExceptionUtils;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.onap.vid.changeManagement.*;
31 import org.onap.vid.exceptions.NotFoundException;
32 import org.onap.vid.model.ExceptionResponse;
33 import org.onap.vid.model.MsoExceptionResponse;
34 import org.onap.vid.mso.MsoResponseWrapper2;
35 import org.onap.vid.mso.MsoResponseWrapperInterface;
36 import org.onap.vid.mso.rest.Request;
37 import org.onap.vid.services.ChangeManagementService;
38 import org.onap.vid.services.WorkflowService;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.*;
43 import org.springframework.web.multipart.MultipartFile;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.ws.rs.WebApplicationException;
47 import java.util.Collection;
48 import java.util.Collections;
50 import static org.onap.vid.utils.Logging.getMethodName;
51 import static org.springframework.http.HttpStatus.*;
54 * Controller to handle ChangeManagement feature requests.
57 @RequestMapping(ChangeManagementController.CHANGE_MANAGEMENT)
58 public class ChangeManagementController extends UnRestrictedBaseController {
59 public static final String VNF_WORKFLOW_RELATION = "vnf_workflow_relation";
60 public static final String CHANGE_MANAGEMENT = "change-management";
61 public static final String GET_VNF_WORKFLOW_RELATION = "get_vnf_workflow_relation";
62 public static final String SCHEDULER_BY_SCHEDULE_ID = "/scheduler/schedules/{scheduleId}";
63 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ChangeManagementController.class);
64 private String fromAppId;
65 private final WorkflowService workflowService;
66 private final ChangeManagementService changeManagementService;
67 private final ObjectMapper objectMapper;
71 public ChangeManagementController(WorkflowService workflowService, ChangeManagementService changeManagementService, ObjectMapper objectMapper) {
72 this.fromAppId = "VidChangeManagementController";
73 this.workflowService = workflowService;
74 this.changeManagementService = changeManagementService;
75 this.objectMapper = objectMapper;
78 @RequestMapping(value = {"/workflow"}, method = RequestMethod.GET)
79 public ResponseEntity<Collection<String>> getWorkflow(@RequestParam("vnfs") Collection<String> vnfs) {
80 Collection<String> result = this.workflowService.getWorkflowsForVNFs(vnfs);
81 return new ResponseEntity<>(result, OK);
84 @RequestMapping(value = {"/mso"}, method = RequestMethod.GET)
85 public ResponseEntity<Collection<Request>> getMSOChangeManagements() {
87 Collection<Request> result = this.changeManagementService.getMSOChangeManagements();
88 return new ResponseEntity<>(result, OK);
91 @RequestMapping(value = "/workflow/{vnfName}", method = RequestMethod.POST)
92 public ResponseEntity<String> changeManagement(HttpServletRequest request,
93 @PathVariable("vnfName") String vnfName,
94 @RequestBody ChangeManagementRequest changeManagmentRequest)
96 return this.changeManagementService.doChangeManagement(changeManagmentRequest, vnfName);
99 @RequestMapping(value = "/uploadConfigUpdateFile", method = RequestMethod.POST)
100 public @ResponseBody ResponseEntity uploadConfigUpdateFile(@RequestPart("file") MultipartFile file)
103 String jsonString = this.changeManagementService.uploadConfigUpdateFile(file);
104 return new ResponseEntity<>(jsonString, HttpStatus.OK);
106 catch(WebApplicationException e){
107 return new ResponseEntity<>(handleException(e), HttpStatus.valueOf(e.getResponse().getStatus()));
109 catch (Exception e) {
110 return new ResponseEntity<>(handleException(e), INTERNAL_SERVER_ERROR);
115 @RequestMapping(value = {"/scheduler"}, method = RequestMethod.GET)
116 public ResponseEntity<ArrayNode> getSchedulerChangeManagements() {
117 ArrayNode result = this.changeManagementService.getSchedulerChangeManagements();
118 return new ResponseEntity<>(result, OK);
121 @RequestMapping(value = {SCHEDULER_BY_SCHEDULE_ID}, method = RequestMethod.DELETE)
122 public ResponseEntity deleteSchedule(@PathVariable("scheduleId") String scheduleId) {
123 Pair<String, Integer> result = this.changeManagementService.deleteSchedule(scheduleId);
124 return ResponseEntity.status(result.getRight()).build();
128 @RequestMapping(value = {GET_VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
129 public ResponseEntity getWorkflows(@RequestBody GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
131 GetWorkflowsResponse response = new GetWorkflowsResponse(changeManagementService.getWorkflowsForVnf(getVnfWorkflowRelationRequest));
132 return ResponseEntity.status(OK).body(response);
134 catch (NotFoundException exception) {
135 LOGGER.error(exception.getMessage(), exception);
136 return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(exception.getMessage())),HttpStatus.NOT_FOUND);
138 catch (Exception exception) {
139 return handleException(exception, "Failed to get workflows for vnf");
143 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
144 public ResponseEntity createWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
145 VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
147 vnfWorkflowRelationResponse = changeManagementService.addVnfWorkflowRelation(vnfWorkflowRelationRequest);
149 catch (Exception exception) {
150 return handleException(exception, "Failed to add vnf to workflow relation");
153 return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
156 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.GET)
157 public ResponseEntity getAllWorkflowRelation() {
160 VnfWorkflowRelationAllResponse vnfWorkflowRelationAllResponse = changeManagementService.getAllVnfWorkflowRelations();
161 return new ResponseEntity<>(vnfWorkflowRelationAllResponse, OK);
163 catch (Exception exception) {
164 return handleException(exception, "Failed to get all vnf to workflow relations");
168 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.DELETE)
169 public ResponseEntity deleteWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
170 VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
172 vnfWorkflowRelationResponse = changeManagementService.deleteVnfWorkflowRelation(vnfWorkflowRelationRequest);
174 catch (Exception exception) {
175 return handleException(exception, "Failed to delete vnf from workflow relation");
178 return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
181 private ResponseEntity handleException(Exception exception, String msg) {
182 LOGGER.error(msg, exception);
183 return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(msg)), HttpStatus.INTERNAL_SERVER_ERROR);
187 private ExceptionResponse handleException(Exception e) {
188 return ControllersUtils.handleException(e, LOGGER);
191 @ExceptionHandler(Exception.class)
192 @ResponseStatus(value=OK) //return 200 for Backwards compatibility with the previous responses to scheduler
193 private MsoResponseWrapperInterface exceptionHandler(Exception e) {
194 return exceptionHandler(e, INTERNAL_SERVER_ERROR);
198 javax.ws.rs.BadRequestException.class,
200 @ResponseStatus(value = OK) //return 200 for Backwards compatibility with the previous responses to scheduler
201 public MsoResponseWrapperInterface clientDerivedExceptionAsBadRequest(Exception e) {
202 // same handler, different HTTP Code
203 return exceptionHandler(e, BAD_REQUEST);
206 private MsoResponseWrapperInterface exceptionHandler(Exception e, HttpStatus httpStatus) {
207 LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodName(), ExceptionUtils.getMessage(e), e);
208 MsoResponseWrapper2<MsoExceptionResponse> responseWrapper2 = new MsoResponseWrapper2<>(httpStatus.value(), new MsoExceptionResponse(e));
209 return responseWrapper2;