Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / 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.controller;
24
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;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.ws.rs.WebApplicationException;
48 import java.util.Collection;
49 import java.util.Collections;
50
51 import static org.onap.vid.utils.Logging.getMethodName;
52 import static org.springframework.http.HttpStatus.*;
53
54 /**
55  * Controller to handle ChangeManagement feature requests.
56  */
57 @RestController
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;
69
70
71     @Autowired
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;
77     }
78
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);
83     }
84
85     @RequestMapping(value = {"/mso"}, method = RequestMethod.GET)
86     public ResponseEntity<Collection<Request>> getMSOChangeManagements() {
87
88         Collection<Request> result = this.changeManagementService.getMSOChangeManagements();
89         return new ResponseEntity<>(result, OK);
90     }
91
92     @RequestMapping(value = "/workflow/{vnfName}", method = RequestMethod.POST)
93     public ResponseEntity<String> changeManagement(HttpServletRequest request,
94                                                    @PathVariable("vnfName") String vnfName,
95                                                    @RequestBody ChangeManagementRequest changeManagmentRequest)
96             throws Exception {
97         return this.changeManagementService.doChangeManagement(changeManagmentRequest, vnfName);
98     }
99
100     @RequestMapping(value = "/uploadConfigUpdateFile", method = RequestMethod.POST)
101     public @ResponseBody ResponseEntity uploadConfigUpdateFile(@RequestPart("file") MultipartFile file)
102         throws Exception {
103         try {
104             String jsonString = this.changeManagementService.uploadConfigUpdateFile(file);
105             return new ResponseEntity<>(jsonString, HttpStatus.OK);
106         }
107         catch(WebApplicationException e){
108             return new ResponseEntity<>(handleException(e),  HttpStatus.valueOf(e.getResponse().getStatus()));
109         }
110         catch (Exception e) {
111             return new ResponseEntity<>(handleException(e), INTERNAL_SERVER_ERROR);
112         }
113     }
114
115
116     @RequestMapping(value = {"/scheduler"}, method = RequestMethod.GET)
117     public ResponseEntity<ArrayNode> getSchedulerChangeManagements() {
118         ArrayNode result = this.changeManagementService.getSchedulerChangeManagements();
119         return new ResponseEntity<>(result, OK);
120     }
121
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();
126     }
127
128     
129     @RequestMapping(value = {GET_VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
130     public ResponseEntity getWorkflows(@RequestBody GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
131         try {
132             GetWorkflowsResponse response = new GetWorkflowsResponse(changeManagementService.getWorkflowsForVnf(getVnfWorkflowRelationRequest));
133             return ResponseEntity.status(OK).body(response);
134         }
135         catch (NotFoundException exception) {
136             LOGGER.error(exception.getMessage(), exception);
137             return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(exception.getMessage())),HttpStatus.NOT_FOUND);
138         }
139         catch (Exception exception) {
140             return handleException(exception, "Failed to get workflows for vnf");
141         }
142     }
143     
144     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
145     public ResponseEntity createWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
146         VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
147         try {
148             vnfWorkflowRelationResponse = changeManagementService.addVnfWorkflowRelation(vnfWorkflowRelationRequest);
149         }
150         catch (Exception exception) {
151             return handleException(exception, "Failed to add vnf to workflow relation");
152         }
153         
154         return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
155     }
156
157     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.GET)
158     public ResponseEntity getAllWorkflowRelation() {
159
160         try {
161             VnfWorkflowRelationAllResponse vnfWorkflowRelationAllResponse = changeManagementService.getAllVnfWorkflowRelations();
162             return new ResponseEntity<>(vnfWorkflowRelationAllResponse, OK);
163         }
164         catch (Exception exception) {
165             return handleException(exception, "Failed to get all vnf to workflow relations");
166         }
167     }
168     
169     @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.DELETE)
170     public ResponseEntity deleteWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
171         VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
172         try {
173                 vnfWorkflowRelationResponse = changeManagementService.deleteVnfWorkflowRelation(vnfWorkflowRelationRequest);
174         }
175         catch (Exception exception) {
176             return handleException(exception, "Failed to delete vnf from workflow relation");
177         }
178
179         return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
180     }
181
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);
185     }
186
187
188     private ExceptionResponse handleException(Exception e) {
189         return ControllersUtils.handleException(e, LOGGER);
190     }
191
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);
196     }
197
198     @ExceptionHandler({
199             javax.ws.rs.BadRequestException.class,
200     })
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);
205     }
206
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;
211     }
212
213 }