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