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