081e3c640986dbf61f08b395c78bf4315354529c
[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.onap.vid.utils.SystemPropertiesWrapper;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.web.bind.annotation.*;
37
38 import javax.servlet.http.HttpServletRequest;
39 import java.util.List;
40 import java.util.UUID;
41
42 import static org.springframework.http.HttpStatus.METHOD_NOT_ALLOWED;
43
44
45 @RestController
46 @RequestMapping(AsyncInstantiationController.ASYNC_INSTANTIATION)
47 public class AsyncInstantiationController extends VidRestrictedBaseController {
48
49     public static final String ASYNC_INSTANTIATION = "asyncInstantiation";
50
51     protected final AsyncInstantiationBusinessLogic asyncInstantiationBL;
52     private final SystemPropertiesWrapper systemPropertiesWrapper;
53
54     protected ObjectMapper objectMapper = new ObjectMapper();
55
56     @Autowired
57     protected AuditService auditService;
58
59     @Autowired
60     public AsyncInstantiationController(AsyncInstantiationBusinessLogic asyncInstantiationBL, SystemPropertiesWrapper systemPropertiesWrapper) {
61         this.asyncInstantiationBL = asyncInstantiationBL;
62         this.systemPropertiesWrapper = systemPropertiesWrapper;
63     }
64
65     @ExceptionHandler(OperationNotAllowedException.class)
66     @ResponseStatus(value=METHOD_NOT_ALLOWED)
67     public ExceptionResponse illegalStateExceptionHandler(Exception e) {
68         return ControllersUtils.handleException(e, LOGGER);
69     }
70
71     /**
72      * Gets the new services status.
73      * @param request the request
74      * @return the services list
75      */
76     @RequestMapping(method = RequestMethod.GET)
77     public List<ServiceInfo> getServicesInfo(HttpServletRequest request) {
78         return asyncInstantiationBL.getAllServicesInfo();
79     }
80
81     @RequestMapping(value = "bulk", method = RequestMethod.POST)
82     public MsoResponseWrapper2<List<String>> createBulkOfServices(@RequestBody ServiceInstantiation request, HttpServletRequest httpServletRequest) {
83         //Push to DB according the model
84         try {
85             LOGGER.debug(EELFLoggerDelegate.debugLogger, "incoming ServiceInstantiation request: "+ objectMapper.writeValueAsString(request));
86         }
87         catch (Exception e) {
88             LOGGER.error(EELFLoggerDelegate.errorLogger, "failed to log incoming ServiceInstantiation request ", e);
89         }
90         String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(httpServletRequest);
91         List<UUID> uuids =  asyncInstantiationBL.pushBulkJob(request, userId);
92
93         return new MsoResponseWrapper2(200, uuids);
94     }
95
96     @RequestMapping(value = "job/{jobId}", method = RequestMethod.DELETE)
97     public void deleteServiceInfo(@PathVariable("jobId") UUID jobId) {
98         asyncInstantiationBL.deleteJob(jobId);
99     }
100
101     @RequestMapping(value = "hide/{jobId}", method = RequestMethod.POST)
102     public void hideServiceInfo(@PathVariable("jobId") UUID jobId) {
103         asyncInstantiationBL.hideServiceInfo(jobId);
104     }
105
106     @RequestMapping(value = "auditStatus/{jobId}", method = RequestMethod.GET)
107     public List<JobAuditStatus> getJobAuditStatus(HttpServletRequest request, @PathVariable(value="jobId") UUID jobId, @RequestParam(value="source") JobAuditStatus.SourceStatus source){
108         return asyncInstantiationBL.getAuditStatuses(jobId, source);
109     }
110
111     @RequestMapping(value = "auditStatus/{jobId}/mso", method = RequestMethod.GET)
112     public List<JobAuditStatus> getJobMsoAuditStatusForAlaCarte(HttpServletRequest request,
113                                                                 @PathVariable(value="jobId") UUID jobId,
114                                                                 @RequestParam(value="requestId", required = false) UUID requestId,
115                                                                 @RequestParam(value="serviceInstanceId", required = false) UUID serviceInstanceId){
116         if (serviceInstanceId != null) {
117             return auditService.getAuditStatusFromMsoByServiceInstanceId(jobId, serviceInstanceId);
118         }
119         if (requestId != null){
120             return auditService.getAuditStatusFromMsoByRequestId(jobId, requestId);
121         }
122         return auditService.getAuditStatusFromMsoByJobId(jobId);
123
124     }
125
126
127 }