aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/controllers/ChangeManagementController.java
blob: 88875b510e8a8712c265bd87eb771213ecf0587d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright 2018 Nokia
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.controllers;

import static org.onap.vid.utils.Logging.getMethodName;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Collection;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.WebApplicationException;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.json.simple.JSONArray;
import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.vid.changeManagement.ChangeManagementRequest;
import org.onap.vid.changeManagement.GetVnfWorkflowRelationRequest;
import org.onap.vid.changeManagement.GetWorkflowsResponse;
import org.onap.vid.changeManagement.VnfWorkflowRelationAllResponse;
import org.onap.vid.changeManagement.VnfWorkflowRelationRequest;
import org.onap.vid.changeManagement.VnfWorkflowRelationResponse;
import org.onap.vid.exceptions.NotFoundException;
import org.onap.vid.model.ExceptionResponse;
import org.onap.vid.model.MsoExceptionResponse;
import org.onap.vid.mso.MsoResponseWrapper2;
import org.onap.vid.mso.MsoResponseWrapperInterface;
import org.onap.vid.mso.rest.Request;
import org.onap.vid.services.ChangeManagementService;
import org.onap.vid.services.WorkflowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Controller to handle ChangeManagement feature requests.
 */
@RestController
@RequestMapping(ChangeManagementController.CHANGE_MANAGEMENT)
public class ChangeManagementController extends UnRestrictedBaseController {
    public static final String VNF_WORKFLOW_RELATION = "vnf_workflow_relation";
    public static final String CHANGE_MANAGEMENT = "change-management";
    public static final String GET_VNF_WORKFLOW_RELATION = "get_vnf_workflow_relation";
    public static final String SCHEDULER_BY_SCHEDULE_ID = "/scheduler/schedules/{scheduleId}";
    private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(ChangeManagementController.class);
    private String fromAppId;
    private final WorkflowService workflowService;
    private final ChangeManagementService changeManagementService;
    private final ObjectMapper objectMapper;


    @Autowired
    public ChangeManagementController(WorkflowService workflowService, ChangeManagementService changeManagementService, ObjectMapper objectMapper) {
        this.fromAppId = "VidChangeManagementController";
        this.workflowService = workflowService;
        this.changeManagementService = changeManagementService;
        this.objectMapper = objectMapper;
    }

    @RequestMapping(value = {"/workflow"}, method = RequestMethod.GET)
    public ResponseEntity<Collection<String>> getWorkflow(@RequestParam("vnfs") Collection<String> vnfs) {
        Collection<String> result = this.workflowService.getWorkflowsForVNFs(vnfs);
        return new ResponseEntity<>(result, OK);
    }

    @RequestMapping(value = {"/mso"}, method = RequestMethod.GET)
    public ResponseEntity<Collection<Request>> getMSOChangeManagements() {

        Collection<Request> result = this.changeManagementService.getMSOChangeManagements();
        return new ResponseEntity<>(result, OK);
    }

    @RequestMapping(value = "/workflow/{vnfName}", method = RequestMethod.POST)
    public ResponseEntity<String> changeManagement(HttpServletRequest request, @PathVariable("vnfName") String vnfName,
                                                   @RequestBody ChangeManagementRequest changeManagmentRequest)
            throws Exception {
        return this.changeManagementService.doChangeManagement(changeManagmentRequest, vnfName);
    }

    @RequestMapping(value = "/uploadConfigUpdateFile", method = RequestMethod.POST)
    public @ResponseBody ResponseEntity uploadConfigUpdateFile(@RequestPart("file") MultipartFile file)
        throws Exception {
        try {
            String jsonString = this.changeManagementService.uploadConfigUpdateFile(file);
            return new ResponseEntity<>(jsonString, HttpStatus.OK);
        }
        catch(WebApplicationException e){
            return new ResponseEntity<>(handleException(e),  HttpStatus.valueOf(e.getResponse().getStatus()));
        }
        catch (Exception e) {
            return new ResponseEntity<>(handleException(e), INTERNAL_SERVER_ERROR);
        }
    }


    @RequestMapping(value = {"/scheduler"}, method = RequestMethod.GET)
    public ResponseEntity<JSONArray> getSchedulerChangeManagements() {
        JSONArray result = this.changeManagementService.getSchedulerChangeManagements();
        return new ResponseEntity<>(result, OK);
    }

    @RequestMapping(value = {SCHEDULER_BY_SCHEDULE_ID}, method = RequestMethod.DELETE)
    public ResponseEntity deleteSchedule(@PathVariable("scheduleId") String scheduleId) {
        Pair<String, Integer> result = this.changeManagementService.deleteSchedule(scheduleId);
        return ResponseEntity.status(result.getRight()).build();
    }

    
    @RequestMapping(value = {GET_VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
    public ResponseEntity getWorkflows(@RequestBody GetVnfWorkflowRelationRequest getVnfWorkflowRelationRequest) {
        try {
            GetWorkflowsResponse response = new GetWorkflowsResponse(changeManagementService.getWorkflowsForVnf(getVnfWorkflowRelationRequest));
            return ResponseEntity.status(OK).body(response);
        }
        catch (NotFoundException exception) {
            LOGGER.error(exception.getMessage(), exception);
            return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(exception.getMessage())),HttpStatus.NOT_FOUND);
        }
        catch (Exception exception) {
            return handleException(exception, "Failed to get workflows for vnf");
        }
    }
    
    @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.POST)
    public ResponseEntity createWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
    	VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
        try {
            vnfWorkflowRelationResponse = changeManagementService.addVnfWorkflowRelation(vnfWorkflowRelationRequest);
        }
        catch (Exception exception) {
            return handleException(exception, "Failed to add vnf to workflow relation");
        }
        
        return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
    }

    @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.GET)
    public ResponseEntity getAllWorkflowRelation() {

        try {
            VnfWorkflowRelationAllResponse vnfWorkflowRelationAllResponse = changeManagementService.getAllVnfWorkflowRelations();
            return new ResponseEntity<>(vnfWorkflowRelationAllResponse, OK);
        }
        catch (Exception exception) {
            return handleException(exception, "Failed to get all vnf to workflow relations");
        }
    }
    
    @RequestMapping(value = {VNF_WORKFLOW_RELATION}, method = RequestMethod.DELETE)
    public ResponseEntity deleteWorkflowRelation(@RequestBody VnfWorkflowRelationRequest vnfWorkflowRelationRequest) {
    	VnfWorkflowRelationResponse vnfWorkflowRelationResponse;
    	try {
    		vnfWorkflowRelationResponse = changeManagementService.deleteVnfWorkflowRelation(vnfWorkflowRelationRequest);
        }
        catch (Exception exception) {
            return handleException(exception, "Failed to delete vnf from workflow relation");
        }

        return new ResponseEntity<>(vnfWorkflowRelationResponse, OK);
    }

    private ResponseEntity handleException(Exception exception, String msg) {
        LOGGER.error(msg, exception);
        return new ResponseEntity<>(new VnfWorkflowRelationResponse(Collections.singletonList(msg)), HttpStatus.INTERNAL_SERVER_ERROR);
    }


    private ExceptionResponse handleException(Exception e) {
        return ControllersUtils.handleException(e, LOGGER);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(value=OK) //return 200 for Backwards compatibility with the previous responses to scheduler
    private MsoResponseWrapperInterface exceptionHandler(Exception e) {
        return exceptionHandler(e, INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler({
            javax.ws.rs.BadRequestException.class,
    })
    @ResponseStatus(value = OK) //return 200 for Backwards compatibility with the previous responses to scheduler
    public MsoResponseWrapperInterface clientDerivedExceptionAsBadRequest(Exception e) {
        // same handler, different HTTP Code
        return exceptionHandler(e, BAD_REQUEST);
    }

    private MsoResponseWrapperInterface exceptionHandler(Exception e, HttpStatus httpStatus) {
        LOGGER.error(EELFLoggerDelegate.errorLogger, "{}: {}", getMethodName(), ExceptionUtils.getMessage(e), e);
        MsoResponseWrapper2<MsoExceptionResponse> responseWrapper2 = new MsoResponseWrapper2<>(httpStatus.value(), new MsoExceptionResponse(e));
        return responseWrapper2;
    }

}