Remove the illegalStateExceptionHandler, as it's already in parent class
[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 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
24
25 import java.util.List;
26 import java.util.UUID;
27 import javax.servlet.http.HttpServletRequest;
28 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.onap.vid.exceptions.AccessDeniedException;
30 import org.onap.vid.model.JobAuditStatus;
31 import org.onap.vid.model.ServiceInfo;
32 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
33 import org.onap.vid.mso.MsoResponseWrapper2;
34 import org.onap.vid.properties.Features;
35 import org.onap.vid.roles.RoleProvider;
36 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
37 import org.onap.vid.services.AuditService;
38 import org.onap.vid.utils.SystemPropertiesWrapper;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.web.bind.annotation.PathVariable;
41 import org.springframework.web.bind.annotation.RequestBody;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestMethod;
44 import org.springframework.web.bind.annotation.RequestParam;
45 import org.springframework.web.bind.annotation.RestController;
46 import org.togglz.core.manager.FeatureManager;
47
48
49 @RestController
50 @RequestMapping(AsyncInstantiationController.ASYNC_INSTANTIATION)
51 public class AsyncInstantiationController extends VidRestrictedBaseController {
52
53     public static final String ASYNC_INSTANTIATION = "asyncInstantiation";
54
55     protected final AsyncInstantiationBusinessLogic asyncInstantiationBL;
56     private final SystemPropertiesWrapper systemPropertiesWrapper;
57
58     private final RoleProvider roleProvider;
59
60     private final FeatureManager featureManager;
61
62     @Autowired
63     protected AuditService auditService;
64
65     @Autowired
66     public AsyncInstantiationController(AsyncInstantiationBusinessLogic asyncInstantiationBL, RoleProvider roleProvider, FeatureManager featureManager, SystemPropertiesWrapper systemPropertiesWrapper) {
67         this.asyncInstantiationBL = asyncInstantiationBL;
68         this.roleProvider = roleProvider;
69         this.featureManager = featureManager;
70         this.systemPropertiesWrapper = systemPropertiesWrapper;
71     }
72
73     /**
74      * Gets the new services status.
75      * @param request the request
76      * @return the services list
77      */
78     @RequestMapping(method = RequestMethod.GET)
79     public List<ServiceInfo> getServicesInfo(HttpServletRequest request) {
80         return asyncInstantiationBL.getAllServicesInfo();
81     }
82
83     @RequestMapping(value = "bulk", method = RequestMethod.POST)
84     public MsoResponseWrapper2<List<String>> createBulkOfServices(@RequestBody ServiceInstantiation request, HttpServletRequest httpServletRequest) {
85         //Push to DB according the model
86         try {
87             LOGGER.debug(EELFLoggerDelegate.debugLogger, "incoming ServiceInstantiation request: "+ JACKSON_OBJECT_MAPPER.writeValueAsString(request));
88         }
89         catch (Exception e) {
90             LOGGER.error(EELFLoggerDelegate.errorLogger, "failed to log incoming ServiceInstantiation request ", e);
91         }
92         String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(httpServletRequest);
93
94         throwExceptionIfAccessDenied(request, httpServletRequest, userId);
95         List<UUID> uuids = asyncInstantiationBL.pushBulkJob(request, userId);
96         return new MsoResponseWrapper2(200, uuids);
97     }
98
99
100
101     @RequestMapping(value = "retryJobWithChangedData/{jobId}", method = RequestMethod.POST)
102     public MsoResponseWrapper2<List<String>> retryJobWithChangedData(@RequestBody ServiceInstantiation request, @PathVariable(value="jobId") UUID jobId, HttpServletRequest httpServletRequest) {
103
104         String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(httpServletRequest);
105         List<UUID> uuids =  asyncInstantiationBL.retryJob(request, jobId, userId);
106         return new MsoResponseWrapper2(200, uuids);
107     }
108
109     @RequestMapping(value = "job/{jobId}", method = RequestMethod.DELETE)
110     public void deleteServiceInfo(@PathVariable("jobId") UUID jobId) {
111         asyncInstantiationBL.deleteJob(jobId);
112     }
113
114     @RequestMapping(value = "hide/{jobId}", method = RequestMethod.POST)
115     public void hideServiceInfo(@PathVariable("jobId") UUID jobId) {
116         asyncInstantiationBL.hideServiceInfo(jobId);
117     }
118
119     @RequestMapping(value = "auditStatus/{jobId}", method = RequestMethod.GET)
120     public List<JobAuditStatus> getJobAuditStatus(HttpServletRequest request, @PathVariable(value="jobId") UUID jobId, @RequestParam(value="source") JobAuditStatus.SourceStatus source){
121         return auditService.getAuditStatuses(jobId, source);
122     }
123
124     @RequestMapping(value = "auditStatus/{jobId}/mso", method = RequestMethod.GET)
125     public List<JobAuditStatus> getJobMsoAuditStatusForAlaCarte(HttpServletRequest request,
126                                                                 @PathVariable(value="jobId") UUID jobId,
127                                                                 @RequestParam(value="requestId", required = false) UUID requestId,
128                                                                 @RequestParam(value="serviceInstanceId", required = false) UUID serviceInstanceId){
129         if (serviceInstanceId != null) {
130             return auditService.getAuditStatusFromMsoByInstanceId(JobAuditStatus.ResourceTypeFilter.SERVICE, serviceInstanceId, jobId);
131         }
132         if (requestId != null){
133             return auditService.getAuditStatusFromMsoByRequestId(jobId, requestId);
134         }
135         return auditService.getAuditStatusFromMsoByJobId(jobId);
136
137     }
138
139     @RequestMapping(value = "auditStatus/{type}/{instanceId}/mso", method = RequestMethod.GET)
140     public List<JobAuditStatus> getAuditStatusFromMsoByInstanceId(HttpServletRequest request,
141                                                                   @PathVariable(value="type") JobAuditStatus.ResourceTypeFilter resourceTypeFilter,
142                                                                   @PathVariable(value="instanceId") UUID instanceId) {
143         return auditService.getAuditStatusFromMsoByInstanceId(resourceTypeFilter, instanceId, null);
144     }
145
146     @RequestMapping(value = "/bulkForRetry/{jobId}", method = RequestMethod.GET)
147     public ServiceInstantiation getBulkForRetry(HttpServletRequest request, @PathVariable(value="jobId") UUID jobId) {
148         return asyncInstantiationBL.getBulkForRetry(jobId);
149     }
150
151     @RequestMapping(value = "retry/{jobId}", method = RequestMethod.POST)
152     public MsoResponseWrapper2<List<UUID>> retryJobRequest(HttpServletRequest httpServletRequest,
153                                                            @PathVariable(value="jobId") UUID jobId) {
154
155         String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(httpServletRequest);
156         List<UUID> uuids =  asyncInstantiationBL.retryJob(jobId, userId);
157
158         return new MsoResponseWrapper2(200, uuids);
159     }
160
161     @RequestMapping(value = "/auditStatusForRetry/{trackById}", method = RequestMethod.GET)
162     public JobAuditStatus getResourceAuditStatus(HttpServletRequest request, @PathVariable(value="trackById") String trackById) {
163         return auditService.getResourceAuditStatus(trackById);
164     }
165
166     private void throwExceptionIfAccessDenied(ServiceInstantiation request, HttpServletRequest httpServletRequest, String userId) {
167         if (featureManager.isActive(Features.FLAG_1906_INSTANTIATION_API_USER_VALIDATION) && !roleProvider.getUserRolesValidator(httpServletRequest).isServicePermitted(request.getGlobalSubscriberId(), request.getSubscriptionServiceType())) {
168             throw new AccessDeniedException(String.format("User %s is not allowed to make this request", userId));
169         }
170     }
171 }