Update license headers
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / AsyncInstantiationController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.controller;
22
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
26 import org.onap.vid.exceptions.OperationNotAllowedException;
27 import org.onap.vid.model.ExceptionResponse;
28 import org.onap.vid.model.JobAuditStatus;
29 import org.onap.vid.model.ServiceInfo;
30 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
31 import org.onap.vid.mso.MsoResponseWrapper2;
32 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
33 import org.onap.vid.services.AuditService;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.web.bind.annotation.*;
36
37 import javax.servlet.http.HttpServletRequest;
38 import java.util.List;
39 import java.util.UUID;
40
41 import static org.springframework.http.HttpStatus.METHOD_NOT_ALLOWED;
42
43
44 @RestController
45 @RequestMapping(AsyncInstantiationController.ASYNC_INSTANTIATION)
46 public class AsyncInstantiationController extends VidRestrictedBaseController {
47
48     public static final String ASYNC_INSTANTIATION = "asyncInstantiation";
49
50     protected final AsyncInstantiationBusinessLogic asyncInstantiationBL;
51
52     protected ObjectMapper objectMapper = new ObjectMapper();
53
54     @Autowired
55     protected AuditService auditService;
56
57     @Autowired
58     public AsyncInstantiationController(AsyncInstantiationBusinessLogic asyncInstantiationBL) {
59         this.asyncInstantiationBL = asyncInstantiationBL;
60     }
61
62     @ExceptionHandler(OperationNotAllowedException.class)
63     @ResponseStatus(value=METHOD_NOT_ALLOWED)
64     public ExceptionResponse illegalStateExceptionHandler(Exception e) {
65         return ControllersUtils.handleException(e, LOGGER);
66     }
67
68     /**
69      * Gets the new services status.
70      * @param request the request
71      * @return the services list
72      */
73     @RequestMapping(method = RequestMethod.GET)
74     public List<ServiceInfo> getServicesInfo(HttpServletRequest request) {
75         return asyncInstantiationBL.getAllServicesInfo();
76     }
77
78     @RequestMapping(value = "bulk", method = RequestMethod.POST)
79     public MsoResponseWrapper2<List<String>> createBulkOfServices(@RequestBody ServiceInstantiation request, HttpServletRequest httpServletRequest) {
80         //Push to DB according the model
81         try {
82             LOGGER.debug(EELFLoggerDelegate.debugLogger, "incoming ServiceInstantiation request: "+ objectMapper.writeValueAsString(request));
83         }
84         catch (Exception e) {
85             LOGGER.error(EELFLoggerDelegate.errorLogger, "failed to log incoming ServiceInstantiation request ", e);
86         }
87         String userId = ControllersUtils.extractUserId(httpServletRequest);
88         List<UUID> uuids =  asyncInstantiationBL.pushBulkJob(request, userId);
89
90         return new MsoResponseWrapper2(200, uuids);
91     }
92
93     @RequestMapping(value = "job/{jobId}", method = RequestMethod.DELETE)
94     public void deleteServiceInfo(@PathVariable("jobId") UUID jobId) {
95         asyncInstantiationBL.deleteJob(jobId);
96     }
97
98     @RequestMapping(value = "hide/{jobId}", method = RequestMethod.POST)
99     public void hideServiceInfo(@PathVariable("jobId") UUID jobId) {
100         asyncInstantiationBL.hideServiceInfo(jobId);
101     }
102
103     @RequestMapping(value = "auditStatus/{jobId}", method = RequestMethod.GET)
104     public List<JobAuditStatus> getJobAuditStatus(HttpServletRequest request, @PathVariable(value="jobId") UUID jobId, @RequestParam(value="source") JobAuditStatus.SourceStatus source){
105         return asyncInstantiationBL.getAuditStatuses(jobId, source);
106     }
107
108     @RequestMapping(value = "auditStatus/{jobId}/mso", method = RequestMethod.GET)
109     public List<JobAuditStatus> getJobMsoAuditStatusForAlaCarte(HttpServletRequest request,
110                                                                 @PathVariable(value="jobId") UUID jobId,
111                                                                 @RequestParam(value="requestId", required = false) UUID requestId,
112                                                                 @RequestParam(value="serviceInstanceId", required = false) UUID serviceInstanceId){
113         if (serviceInstanceId != null) {
114             return auditService.getAuditStatusFromMsoByServiceInstanceId(jobId, serviceInstanceId);
115         }
116         if (requestId != null){
117             return auditService.getAuditStatusFromMsoByRequestId(jobId, requestId);
118         }
119         return auditService.getAuditStatusFromMsoByJobId(jobId);
120
121     }
122
123
124 }