aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PdpApiService.java
blob: 7ecfb2dbeec663a5f5eefe37f32505cf257f8188 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PDP-REST
 * ================================================================================
 * Copyright (C) 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.policy.pdp.rest.api.services;

import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.onap.policy.api.PolicyNameType;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.springframework.http.HttpStatus;

public abstract class PdpApiService {
    private static final Logger LOGGER = FlexLogger.getLogger(PdpApiService.class.getName());
    protected static final String PRINT_REQUESTID = " - RequestId - ";
    protected String requestId = null;
    protected String filePrefix = null;
    protected String clientScope = null;
    protected String message = null;
    protected HttpStatus status = HttpStatus.BAD_REQUEST;

    protected UUID populateRequestId(UUID paramReqId, String reqId) {
        UUID requestUuid = paramReqId;
        if (paramReqId == null) {
            if (!StringUtils.isBlank(reqId)) {
                try {
                    requestUuid = UUID.fromString(reqId);
                } catch (IllegalArgumentException e) {
                    requestUuid = UUID.randomUUID();
                    LOGGER.info("Generated Random UUID: " + requestUuid.toString(), e);
                }
            } else {
                requestUuid = UUID.randomUUID();
                LOGGER.info("Generated Random UUID: " + requestUuid.toString());
            }
        }
        this.requestId = requestUuid.toString();
        return requestUuid;
    }

    /**
     * Sets the client scope.
     */
    protected void setClientScope(String policyType) {
        if ("Firewall".equalsIgnoreCase(policyType)) {
            clientScope = "ConfigFirewall";
            filePrefix = "Config_FW_";
        } else if ("Action".equalsIgnoreCase(policyType)) {
            clientScope = "Action";
            filePrefix = "Action_";
        } else if ("Decision".equalsIgnoreCase(policyType)) {
            clientScope = "Decision";
            filePrefix = "Decision_";
        } else if ("Base".equalsIgnoreCase(policyType)) {
            clientScope = "Config";
            filePrefix = "Config_";
        } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
            clientScope = "ConfigClosedLoop";
            filePrefix = "Config_Fault_";
        } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
            clientScope = "ConfigClosedLoop";
            filePrefix = "Config_PM_";
        } else if ("MicroService".equalsIgnoreCase(policyType)) {
            clientScope = "ConfigMS";
            filePrefix = "Config_MS_";
        } else if ("Optimization".equalsIgnoreCase(policyType)) {
            clientScope = "ConfigOptimization";
            filePrefix = "Config_OOF_";
        } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
            clientScope = "ConfigBrmsRaw";
            filePrefix = "Config_BRMS_Raw_";
        } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
            clientScope = "ConfigBrmsParam";
            filePrefix = "Config_BRMS_Param_";
        } else {
            extendedClientScope(policyType);
        }
    }

    protected void extendedClientScope(String policyType) {
        clientScope = null;
        message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType + " is not a valid Policy Type.";
    }

    protected boolean validatePolicyNameAndScope(List<String> policyNames, List<String> policyTypes,
            List<PolicyNameType> policyList) {

        String polType = null;

        if (policyTypes.size() == 1) {
            polType = policyTypes.get(0).trim();
        }

        try {
            for (int i = 0; i < policyNames.size(); i++) {
                String polName = policyNames.get(i);
                if (policyTypes.size() > 1) {
                    polType = policyTypes.get(i).trim();
                }

                String policyName = null;
                String policyScope = null;
                if (polName.endsWith("xml")) {
                    policyName = polName;
                    policyScope = policyName.substring(0, StringUtils.lastOrdinalIndexOf(policyName, ".", 3));
                } else if (polName.contains(".")) {
                    policyName = polName.substring(polName.lastIndexOf('.') + 1);
                    policyScope = polName.substring(0, polName.lastIndexOf('.'));
                } else {
                    message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
                    return false;
                }

                if (StringUtils.isBlank(policyName) || StringUtils.isBlank(policyScope)) {
                    message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
                    return false;
                }

                clientScope = null;
                filePrefix = null;
                setClientScope(polType);
                if (StringUtils.isBlank(clientScope) || StringUtils.isBlank(filePrefix)) {
                    message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Policy Name.";
                    return false;
                }
                PolicyNameType policyData = new PolicyNameType(policyName.trim(), policyScope.trim(), polType,
                        filePrefix, clientScope, null);
                policyList.add(policyData);
                LOGGER.info("RequestId - " + requestId + " , Policy - " + policyData);

            }
        } catch (Exception e) {
            message = XACMLErrorConstants.ERROR_DATA_ISSUE
                    + "validatePolicies - Failed Validation. Please check the input parameters.";
            return false;
        }
        return true;
    }


    /**
     * Gets the response code.
     *
     * @return the response code
     */
    public HttpStatus getResponseCode() {
        return status;
    }

}