aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/controller/AsyncInstantiationController.java
blob: ce8bbb50c9996866574a701fc8b56b5320a5a824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.controller;

import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;

import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.vid.dal.AsyncInstantiationRepository;
import org.onap.vid.exceptions.AccessDeniedException;
import org.onap.vid.model.JobAuditStatus;
import org.onap.vid.model.ServiceInfo;
import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
import org.onap.vid.mso.MsoResponseWrapper2;
import org.onap.vid.properties.Features;
import org.onap.vid.roles.AllPermissionProperties;
import org.onap.vid.roles.RoleProvider;
import org.onap.vid.roles.RoleValidator;
import org.onap.vid.services.AsyncInstantiationBusinessLogic;
import org.onap.vid.services.AuditService;
import org.onap.vid.utils.SystemPropertiesWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.togglz.core.manager.FeatureManager;


@RestController
@RequestMapping(AsyncInstantiationController.ASYNC_INSTANTIATION)
public class AsyncInstantiationController extends VidRestrictedBaseController {

    public static final String ASYNC_INSTANTIATION = "asyncInstantiation";

    protected final AsyncInstantiationBusinessLogic asyncInstantiationBL;
    protected final AsyncInstantiationRepository asyncInstantiationRepository;
    private final SystemPropertiesWrapper systemPropertiesWrapper;

    private final RoleProvider roleProvider;

    private final FeatureManager featureManager;

    protected final AuditService auditService;

    @Autowired
    public AsyncInstantiationController(AsyncInstantiationBusinessLogic asyncInstantiationBL,
        AsyncInstantiationRepository asyncInstantiationRepository, RoleProvider roleProvider,
        FeatureManager featureManager, SystemPropertiesWrapper systemPropertiesWrapper,
        AuditService auditService) {
        this.asyncInstantiationBL = asyncInstantiationBL;
        this.asyncInstantiationRepository = asyncInstantiationRepository;
        this.roleProvider = roleProvider;
        this.featureManager = featureManager;
        this.systemPropertiesWrapper = systemPropertiesWrapper;
        this.auditService = auditService;
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<ServiceInfo> getServicesInfo(HttpServletRequest request) {
        return asyncInstantiationBL.getAllServicesInfo();
    }

    @RequestMapping(value = "bulk", method = RequestMethod.POST)
    public MsoResponseWrapper2<List<String>> createBulkOfServices(@RequestBody ServiceInstantiation request, HttpServletRequest httpServletRequest) {
        //Push to DB according the model
        try {
            LOGGER.debug(EELFLoggerDelegate.debugLogger, "incoming ServiceInstantiation request: "+ JACKSON_OBJECT_MAPPER.writeValueAsString(request));
        }
        catch (Exception e) {
            LOGGER.error(EELFLoggerDelegate.errorLogger, "failed to log incoming ServiceInstantiation request ", e);
        }
        String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(httpServletRequest);

        throwExceptionIfAccessDenied(request, httpServletRequest, userId);
        List<UUID> uuids = asyncInstantiationBL.pushBulkJob(request, userId);
        return new MsoResponseWrapper2(200, uuids);
    }



    @RequestMapping(value = "retryJobWithChangedData/{jobId}", method = RequestMethod.POST)
    public MsoResponseWrapper2<List<String>> retryJobWithChangedData(@RequestBody ServiceInstantiation request, @PathVariable(value="jobId") UUID jobId, HttpServletRequest httpServletRequest) {

        String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(httpServletRequest);
        List<UUID> uuids =  asyncInstantiationBL.retryJob(request, jobId, userId);
        return new MsoResponseWrapper2(200, uuids);
    }

    @RequestMapping(value = "job/{jobId}", method = RequestMethod.DELETE)
    public void deleteServiceInfo(@PathVariable("jobId") UUID jobId) {
        asyncInstantiationBL.deleteJob(jobId);
    }

    @RequestMapping(value = "hide/{jobId}", method = RequestMethod.POST)
    public void hideServiceInfo(@PathVariable("jobId") UUID jobId) {
        asyncInstantiationBL.hideServiceInfo(jobId);
    }

    @RequestMapping(value = "auditStatus/{jobId}", method = RequestMethod.GET)
    public List<JobAuditStatus> getJobAuditStatus(HttpServletRequest request, @PathVariable(value="jobId") UUID jobId, @RequestParam(value="source") JobAuditStatus.SourceStatus source){
        return auditService.getAuditStatuses(jobId, source);
    }

    @RequestMapping(value = "auditStatus/{jobId}/mso", method = RequestMethod.GET)
    public List<JobAuditStatus> getJobMsoAuditStatusForAlaCarte(HttpServletRequest request,
                                                                @PathVariable(value="jobId") UUID jobId,
                                                                @RequestParam(value="requestId", required = false) UUID requestId,
                                                                @RequestParam(value="serviceInstanceId", required = false) UUID serviceInstanceId){
        if (serviceInstanceId != null) {
            return auditService.getAuditStatusFromMsoByInstanceId(JobAuditStatus.ResourceTypeFilter.SERVICE, serviceInstanceId, jobId);
        }
        if (requestId != null){
            return auditService.getAuditStatusFromMsoByRequestId(jobId, requestId);
        }
        return auditService.getAuditStatusFromMsoByJobId(jobId);

    }

    @RequestMapping(value = "auditStatus/{type}/{instanceId}/mso", method = RequestMethod.GET)
    public List<JobAuditStatus> getAuditStatusFromMsoByInstanceId(HttpServletRequest request,
                                                                  @PathVariable(value="type") JobAuditStatus.ResourceTypeFilter resourceTypeFilter,
                                                                  @PathVariable(value="instanceId") UUID instanceId) {
        return auditService.getAuditStatusFromMsoByInstanceId(resourceTypeFilter, instanceId, null);
    }

    @RequestMapping(value = "/bulkForRetry/{jobId}", method = RequestMethod.GET)
    public ServiceInstantiation getBulkForRetry(HttpServletRequest request, @PathVariable(value="jobId") UUID jobId) {
        return asyncInstantiationBL.getBulkForRetry(jobId);
    }

    @RequestMapping(value = "retry/{jobId}", method = RequestMethod.POST)
    public MsoResponseWrapper2<List<UUID>> retryJobRequest(HttpServletRequest httpServletRequest,
                                                           @PathVariable(value="jobId") UUID jobId) {

        String userId = new ControllersUtils(systemPropertiesWrapper).extractUserId(httpServletRequest);
        List<UUID> uuids =  asyncInstantiationBL.retryJob(jobId, userId);

        return new MsoResponseWrapper2(200, uuids);
    }

    @RequestMapping(value = "/auditStatusForRetry/{trackById}", method = RequestMethod.GET)
    public JobAuditStatus getResourceAuditStatus(HttpServletRequest request, @PathVariable(value="trackById") String trackById) {
        return auditService.getResourceAuditStatus(trackById);
    }

    private void throwExceptionIfAccessDenied(ServiceInstantiation request, HttpServletRequest httpServletRequest, String userId) {
        if (featureManager.isActive(Features.FLAG_1906_INSTANTIATION_API_USER_VALIDATION)) {
            RoleValidator roleValidator = roleProvider.getUserRolesValidator(httpServletRequest);
            if (!roleValidator.isServicePermitted(new AllPermissionProperties(
                request.getGlobalSubscriberId(),
                request.getSubscriptionServiceType(),
                request.getOwningEntityId()))
            ) {
                throw new AccessDeniedException(String.format("User %s is not allowed to make this request", userId));
            }
        }
    }
}