2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.vid.controller;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.node.ArrayNode;
27 import org.apache.commons.lang3.exception.ExceptionUtils;
28 import org.apache.commons.lang3.tuple.Pair;
29 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
30 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import org.onap.vid.changeManagement.*;
32 import org.onap.vid.exceptions.NotFoundException;
33 import org.onap.vid.model.ExceptionResponse;
34 import org.onap.vid.model.MsoExceptionResponse;
35 import org.onap.vid.mso.MsoResponseWrapper2;
36 import org.onap.vid.mso.MsoResponseWrapperInterface;
37 import org.onap.vid.mso.rest.Request;
38 import org.onap.vid.services.ChangeManagementService;
39 import org.onap.vid.services.WorkflowService;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.*;
44 import org.springframework.web.multipart.MultipartFile;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.ws.rs.WebApplicationException;
48 import java.util.Collection;
49 import java.util.Collections;
51 import static org.onap.vid.utils.Logging.getMethodName;
52 import static org.springframework.http.HttpStatus.*;
55 * Controller to handle ChangeManagement feature requests.
58 @RequestMapping(ChangeManagementController.CHANGE_MANAGEMENT)
59 public class ChangeManagementController extends UnRestrictedBaseController {
60 public static final String VNF_WORKFLOW_RELATION = "vnf_workflow_relation";
61 public static final String CHANGE_MANAGEMENT = "change-management";
62 public static final String GET_VNF_WORKFLOW_RELATION = "get_vnf_workflow_relation";
63 public static final String SCHEDULER_BY_SCHEDULE_ID = "/scheduler/schedules/{scheduleId}";
64 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ChangeManagementController.class);
65 private String fromAppId;
66 private final WorkflowService workflowService;
67 private final ChangeManagementService changeManagementService;
68 private final ObjectMapper objectMapper;
72 public ChangeManagementController(WorkflowService workflowService, ChangeManagementService changeManagementService, ObjectMapper objectMapper) {
73 this.fromAppId = "VidChangeManagementController";
74 this.workflowService = workflowService;
75 this.changeManagementService = changeManagementService;
76 this.objectMapper = objectMapper;
79 @RequestMapping(value = {"/workflow"}, method = RequestMethod.GET)
80 public ResponseEntity<Collection<String>> getWorkflow(@RequestParam("vnfs") Collection<String> vnfs) {
81 Collection<String> result = this.workflowService.getWorkflowsForVNFs(vnfs);
82 return new ResponseEntity<>(result, OK);
85 @RequestMapping(value = {"/mso"}, method = RequestMethod.GET)
86 public ResponseEntity<Collection<Request>> getMSOChangeManagements() {
88 Collection<Request> result = this.changeManagementService.getMSOChangeManagements();
89 return new ResponseEntity<>(result, OK);
92 @RequestMapping(value = "/workflow/{vnfName}", method = RequestMethod.POST)
93 public ResponseEntity<String> changeManagement(HttpServletRequest request,
94 @PathVariable("vnfName") String vnfName,
95 @RequestBody ChangeManagementRequest changeManagmentRequest)
97 return this.changeManagementService.doChangeManagement(changeManagmentRequest, vnfName);
100 @RequestMapping(value = "/uploadConfigUpdateFile", method = RequestMethod.POST)
101 public @ResponseBody ResponseEntity uploadConfigUpdateFile(@RequestPart("file") MultipartFile file)
104 String jsonString = this.changeManagementService.uploadConfigUpdateFile(file);
105 return new ResponseEntity<>(jsonString, HttpStatus.OK);
107 catch(WebApplicationException e){
108 return new ResponseEntity<>(handleException(e), HttpStatus.valueOf(e.getResponse().getStatus()));
110 catch (Exception e) {
111 return new ResponseEntity<>(handleException(e), INTERNAL_SERVER_ERROR);
116 @RequestMapping(value = {"/scheduler"}, method = RequestMethod.GET)
117 public ResponseEntity<ArrayNode> getSchedulerChangeManagements() {
118 ArrayNode result = this.changeManagementService.getSchedulerChangeManagements();
119 return new ResponseEntity<>(result, OK);
122 @RequestMapping(value = {SCHEDULER_BY_SCHEDULE_ID}, method = RequestMethod.DELETE)
123 public ResponseEntity deleteSchedule(@PathVariable("scheduleId") String scheduleId) {
124 Pair<String, Integer> result = this.changeManagementService.deleteSchedule(scheduleId);
125 return ResponseEntity.status(result.getRight()).build();
129 @RequestMapping(value = {GET_VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
130 public ResponseEntity getWorkflows(@RequestBody GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
132 GetWorkflowsResponse response = new GetWorkflowsResponse(changeManagementService.getWorkflowsForVnf(getVnfWorkflowRelationRequest));
133 return ResponseEntity.status(OK).body(response);
135 catch (NotFoundException exception) {
136 LOGGER.error(exception.getMessage(), exception);
137 return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(exception.getMessage())),HttpStatus.NOT_FOUND);
139 catch (Exception exception) {
140 return handleException(exception, "Failed to get workflows for vnf");
144 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
145 public ResponseEntity createWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
146 VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
148 vnfWorkflowRelationResponse = changeManagementService.addVnfWorkflowRelation(vnfWorkflowRelationRequest);
150 catch (Exception exception) {
151 return handleException(exception, "Failed to add vnf to workflow relation");
154 return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
157 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.GET)
158 public ResponseEntity getAllWorkflowRelation() {
161 VnfWorkflowRelationAllResponse vnfWorkflowRelationAllResponse = changeManagementService.getAllVnfWorkflowRelations();
162 return new ResponseEntity<>(vnfWorkflowRelationAllResponse, OK);
164 catch (Exception exception) {
165 return handleException(exception, "Failed to get all vnf to workflow relations");
169 @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.DELETE)
170 public ResponseEntity deleteWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
171 VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
173 vnfWorkflowRelationResponse = changeManagementService.deleteVnfWorkflowRelation(vnfWorkflowRelationRequest);
175 catch (Exception exception) {
176 return handleException(exception, "Failed to delete vnf from workflow relation");
179 return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
182 private ResponseEntity handleException(Exception exception, String msg) {
183 LOGGER.error(msg, exception);
184 return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(msg)), HttpStatus.INTERNAL_SERVER_ERROR);
188 private ExceptionResponse handleException(Exception e) {
189 return ControllersUtils.handleException(e, LOGGER);
192 @ExceptionHandler(Exception.class)
193 @ResponseStatus(value=OK) //return 200 for Backwards compatibility with the previous responses to scheduler
194 private MsoResponseWrapperInterface exceptionHandler(Exception e) {
195 return exceptionHandler(e, INTERNAL_SERVER_ERROR);
199 javax.ws.rs.BadRequestException.class,
201 @ResponseStatus(value = OK) //return 200 for Backwards compatibility with the previous responses to scheduler
202 public MsoResponseWrapperInterface clientDerivedExceptionAsBadRequest(Exception e) {
203 // same handler, different HTTP Code
204 return exceptionHandler(e, BAD_REQUEST);
207 private MsoResponseWrapperInterface exceptionHandler(Exception e, HttpStatus httpStatus) {
208 LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodName(), ExceptionUtils.getMessage(e), e);
209 MsoResponseWrapper2<MsoExceptionResponse> responseWrapper2 = new MsoResponseWrapper2<>(httpStatus.value(), new MsoExceptionResponse(e));
210 return responseWrapper2;