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