aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main/rest/PolicyAuditControllerV1.java
blob: d360cb5afed475721849923324e96d1529d074ea (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
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2021-2022 Bell Canada. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * 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.policy.pap.main.rest;

import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PolicyAudit;
import org.onap.policy.pap.main.service.PolicyAuditService;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

/**
 * Class to provide REST end points for PAP component to retrieve the audit information for
 * various operations on policies.
 */
@RestController
@RequiredArgsConstructor
@Profile("default")
public class PolicyAuditControllerV1 extends PapRestControllerV1 implements PolicyAuditControllerV1Api {

    public static final String NO_AUDIT_RECORD_FOUND = "No records found matching the input parameters";

    private final PolicyAuditService policyAuditService;

    /**
     * Queries audit information of all policies.
     *
     * @param requestId request ID used in ONAP logging
     * @param recordCount number of records to fetch
     * @param startTime the starting time for the query in epoch timestamp
     * @param endTime the ending time for the query in epoch timestamp
     * @return a response
     * @throws PfModelException the exception
     */
    @Override
    public ResponseEntity<List<PolicyAudit>> getAllAuditRecords(
            UUID requestId,
            Integer recordCount,
            Long startTime,
            Long endTime) {

        return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(policyAuditService
            .getAuditRecords(recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
    }

    /**
     * Queries audit information of policies in a specific PdpGroup.
     *
     * @param requestId request ID used in ONAP logging
     * @param recordCount number of records to fetch
     * @param startTime the starting time for the query in epoch timestamp
     * @param endTime the ending time for the query in epoch timestamp
     * @param pdpGroupName the pdp group name for the query
     * @return a response
     * @throws PfModelException the exception
     */
    @Override
    public ResponseEntity<Object> getAuditRecordsByGroup(
            String pdpGroupName,
            UUID requestId,
            Integer recordCount,
            Long startTime,
            Long endTime) {

        return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(pdpGroupName, recordCount,
            convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
    }

    /**
     * Queries audit information of a specific version of a policy in a PdpGroup.
     *
     * @param requestId request ID used in ONAP logging
     * @param recordCount number of records to fetch
     * @param startTime the starting time for the query in epoch timestamp
     * @param endTime the ending time for the query in epoch timestamp
     * @param pdpGroupName the pdp group name for the query
     * @param policyName name of the Policy
     * @param policyVersion version of the Policy
     * @return a response
     * @throws PfModelException the exception
     */
    @Override
    public ResponseEntity<Object> getAuditRecordsOfPolicyinPdpGroup(
            String pdpGroupName,
            String policyName,
            String policyVersion,
            UUID requestId,
            Integer recordCount,
            Long startTime,
            Long endTime) {

        return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(pdpGroupName, policyName,
            policyVersion, recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
    }

    /**
     * Queries audit information of a specific version of a policy.
     *
     * @param requestId request ID used in ONAP logging
     * @param recordCount number of records to fetch
     * @param startTime the starting time for the query in epoch timestamp
     * @param endTime the ending time for the query in epoch timestamp
     * @param policyName name of the Policy
     * @param policyVersion version of the Policy
     * @return a response
     * @throws PfModelException the exception
     */
    @Override
    public ResponseEntity<Object> getAuditRecordsOfPolicy(
            String policyName,
            String policyVersion,
            UUID requestId,
            Integer recordCount,
            Long startTime,
            Long endTime) {

        return makeOkOrNotFoundResponse(requestId, policyAuditService.getAuditRecords(policyName, policyVersion,
            recordCount, convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
    }

    private ResponseEntity<Object> makeOkOrNotFoundResponse(UUID requestId, Collection<PolicyAudit> result) {
        if (result.isEmpty()) {
            return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.NOT_FOUND)), requestId)
                .body(NO_AUDIT_RECORD_FOUND);
        } else {
            return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
        }
    }

    private Instant convertEpochtoInstant(Long epochSecond) {
        return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));
    }
}