aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdatePolicyServiceImpl.java
blob: 62b85037098b8b21abd60fe49efff5785cd2edcc (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*-
 * ============LICENSE_START=======================================================
 * ONAP-PDP-REST
 * ================================================================================
 * Copyright (C) 2017-2018 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.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.onap.policy.api.PolicyException;
import org.onap.policy.api.PolicyParameters;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
import org.onap.policy.rest.util.PolicyValidation;
import org.onap.policy.rest.util.PolicyValidationRequestWrapper;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.springframework.http.HttpStatus;

import com.google.common.base.Strings;

public class CreateUpdatePolicyServiceImpl implements CreateUpdatePolicyService {
	private static final Logger LOGGER = FlexLogger.getLogger(CreateUpdatePolicyServiceImpl.class.getName());
    
    private String policyResult = null;
    private HttpStatus status = HttpStatus.BAD_REQUEST;
    private Boolean updateFlag = false;
    private String message = null;
    private PolicyParameters policyParameters = new PolicyParameters();
    private String policyName = null;
    private String policyScope = null;
    private String date = null;
    
	public CreateUpdatePolicyServiceImpl(PolicyParameters policyParameters,
			String requestID, boolean updateFlag) {
		this.updateFlag = updateFlag;
        this.policyParameters = policyParameters;
        if(policyParameters.getRequestID()==null){
            UUID requestUUID = null;
            if (requestID != null && !requestID.isEmpty()) {
                try {
                    requestUUID = UUID.fromString(requestID);
                } 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.policyParameters.setRequestID(requestUUID);
        }
        try{
            run();
            specialCheck();
        }catch(PolicyException e){
            policyResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
            status = HttpStatus.BAD_REQUEST;
        }
    }

    public void run() throws PolicyException{
        // Check Validation. 
        if(!getValidation()){
            LOGGER.error(message);
            throw new PolicyException(message);
        }
        // Get Result. 
        try{
            status = HttpStatus.OK;
            policyResult = processResult();
        }catch (Exception e){
            LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
            status = HttpStatus.BAD_REQUEST;
            throw new PolicyException(e);
        }
    }
    
    @SuppressWarnings("incomplete-switch")
    public String processResult() throws PolicyException{
        String response = null;
        if(policyParameters.getPolicyConfigType()!=null){
            // This is a Config Type Policy. 
            switch(policyParameters.getPolicyConfigType()){
            case BRMS_PARAM:
                BRMSParamPolicyService bRMSParamPolicyService = new BRMSParamPolicyService(policyName, policyScope, policyParameters, date);
                // Check Validation. 
                if(!bRMSParamPolicyService.getValidation()){
                    LOGGER.error(bRMSParamPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return bRMSParamPolicyService.getMessage();
                }
                // Get Result. 
                response = bRMSParamPolicyService.getResult(updateFlag);
                break;
            case BRMS_RAW:
                BRMSRawPolicyService bRMSRawPolicyService = new BRMSRawPolicyService(policyName, policyScope, policyParameters, date);
                // Check Validation. 
                if(!bRMSRawPolicyService.getValidation()){
                    LOGGER.error(bRMSRawPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return bRMSRawPolicyService.getMessage();
                }
                // Get Result. 
                response = bRMSRawPolicyService.getResult(updateFlag);
                break;
            case Base:
                ConfigPolicyService configPolicyService = new ConfigPolicyService(policyName, policyScope, policyParameters, date);
                // Check Validation. 
                if(!configPolicyService.getValidation()){
                    LOGGER.error(configPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return configPolicyService.getMessage();
                }
                // Get Result. 
                response = configPolicyService.getResult(updateFlag);
                break;
            case ClosedLoop_Fault:
                ClosedLoopFaultPolicyService closedLoopFaultPolicyService = new ClosedLoopFaultPolicyService(policyName, policyScope, policyParameters, date);
                // Check Validation. 
                if(!closedLoopFaultPolicyService.getValidation()){
                    LOGGER.error(closedLoopFaultPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return closedLoopFaultPolicyService.getMessage();
                }
                // Get Result. 
                response = closedLoopFaultPolicyService.getResult(updateFlag);
                break;
            case ClosedLoop_PM:
                ClosedLoopPMPolicyService closedLoopPMPolicyService = new ClosedLoopPMPolicyService(policyName, policyScope, policyParameters, date);
                // Check Validation. 
                if(!closedLoopPMPolicyService.getValidation()){
                    LOGGER.error(closedLoopPMPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return closedLoopPMPolicyService.getMessage();
                }
                // Get Result. 
                response = closedLoopPMPolicyService.getResult(updateFlag);
                break;
            case Firewall:
                FirewallPolicyService firewallPolicyService = new FirewallPolicyService(policyName, policyScope, policyParameters, date);
                // Check Validation. 
                if(!firewallPolicyService.getValidation()){
                    LOGGER.error(firewallPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return firewallPolicyService.getMessage();
                }
                // Get Result. 
                response = firewallPolicyService.getResult(updateFlag);
                break;
            case MicroService:
                MicroServicesPolicyService microServicesPolicyService = new MicroServicesPolicyService(policyName, policyScope, policyParameters, date);
                // Check Validation. 
                if(!microServicesPolicyService.getValidation()){
                    LOGGER.error(microServicesPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return microServicesPolicyService.getMessage();
                }
                // Get Result. 
                response = microServicesPolicyService.getResult(updateFlag);
                break;
            default:
                message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " Invalid Config Type Present";
                LOGGER.error(message);
                status = HttpStatus.BAD_REQUEST;
                return message;
            }
        }else if (policyParameters.getPolicyClass()!=null){
            switch (policyParameters.getPolicyClass()){
            case Action:
                ActionPolicyService actionPolicyService = new ActionPolicyService(policyScope, policyName, policyParameters);
                // Check Validation. 
                if(!actionPolicyService.getValidation()){
                    LOGGER.error(actionPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return actionPolicyService.getMessage();
                }
                // Get Result. 
                response = actionPolicyService.getResult(updateFlag);
                break;
            case Decision:
                DecisionPolicyService decisionPolicyService = new DecisionPolicyService(policyScope, policyName, policyParameters);
                // Check Validation. 
                if(!decisionPolicyService.getValidation()){
                    LOGGER.error(decisionPolicyService.getMessage());
                    status = HttpStatus.BAD_REQUEST;
                    return decisionPolicyService.getMessage();
                }
                // Get Result. 
                response = decisionPolicyService.getResult(updateFlag);
                break;
            }
        }else {
            message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Class found.";
            LOGGER.error(message);
            status = HttpStatus.BAD_REQUEST;
            response = message;
        }
        return response;
    }

    protected boolean getValidation() {
    	
    	PolicyValidation validation = new PolicyValidation();
    	
		StringBuilder responseString;
		
    	if (policyParameters != null) {
    		
    		if (!Strings.isNullOrEmpty(policyParameters.getPolicyName())){
                if (policyParameters.getPolicyName().contains(".")) {
                    policyName = policyParameters.getPolicyName().substring(policyParameters.getPolicyName().lastIndexOf('.') + 1,
                            policyParameters.getPolicyName().length());
                    policyScope = policyParameters.getPolicyName().substring(0,policyParameters.getPolicyName().lastIndexOf('.'));
                    policyParameters.setPolicyName(policyName);
                    LOGGER.info("Name is " + policyName + "   scope is " + policyScope);
                } else {
                    message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy Scope: No Policy Scope given";
            		LOGGER.error("Common validation did not return success:  " + message);
                    return false;
                }
    		} else {
    			message = XACMLErrorConstants.ERROR_DATA_ISSUE + "PolicyName: PolicyName Should not be empty";
        		LOGGER.error("Common validation did not return success:  " + message);
    			return false;
    		}
    		
    		if(policyParameters.getPolicyClass() != null && "Config".equals(policyParameters.getPolicyClass().toString())){
    			String policyConfigType = policyParameters.getPolicyConfigType().toString();
    			if(!"BRMS_Param".equalsIgnoreCase(policyConfigType) && Strings.isNullOrEmpty(policyParameters.getConfigBody())){
    				message = XACMLErrorConstants.ERROR_DATA_ISSUE + "ConfigBody: No Config Body given";
            		LOGGER.error("Common validation did not return success:  " + message);
                    return false;
    			}
    		}

    		try {
    			PolicyValidationRequestWrapper wrapper = new PolicyValidationRequestWrapper();    			
    			PolicyRestAdapter policyData = wrapper.populateRequestParameters(policyParameters);
    			if(policyData!=null) {
    				responseString = validation.validatePolicy(policyData);
    			} else {
    				message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody();
    				return false;
    			}
			} catch (Exception e) {
				LOGGER.error("Exception Occured during Policy Validation" +e);
				if(e.getMessage()!=null){
					if("Action".equals(policyParameters.getPolicyClass().toString()) && e.getMessage().contains("Index:")){
						message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Rule Algorithms: One or more Fields in Rule Algorithms is Empty.";
					} else {
						message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Exception Occured During Policy Validation: " + e;
					}
				}
				return false;
			}
    	} else {
    		message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy parameters given. ";
    		return false;
    	}

        // Set some default Values. 
        if (policyParameters.getTtlDate()!=null){
            date = convertDate(policyParameters.getTtlDate());
        }
        
        if (responseString!=null){
    		String response = responseString.toString().substring(0, 7);
        	if("success".equals(response)) {
        		return true;
        	} else {
    			message = XACMLErrorConstants.ERROR_DATA_ISSUE + PolicyApiUtils.formatResponse(responseString);
        		LOGGER.error("Common validation did not return success:  " + message);
        		return false;
        	}
        } else {
			message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Unknown Error Occured During Policy Validation";
			LOGGER.error(message);
			return false;
        }

    }
    
    protected String convertDate(Date date) {
        String strDate = null;
        if (date!=null) {
            SimpleDateFormat dateformatJava = new SimpleDateFormat("dd-MM-yyyy");
            strDate = dateformatJava.format(date);
        }
        return (strDate==null) ? "NA": strDate;
    }

    protected void specialCheck() {
        if(policyResult== null || policyResult.contains("BAD REQUEST")||policyResult.contains("PE300")){
            status = HttpStatus.BAD_REQUEST;
        } else if (policyResult.contains("Policy Exist Error")) {
            status = HttpStatus.CONFLICT;
        } else if (policyResult.contains("PE200")||policyResult.contains("PE900")){
            status = HttpStatus.INTERNAL_SERVER_ERROR;
        }
    }

    public String getResult() {
        return policyResult;
    }

    public HttpStatus getResponseCode() {
        return status;
    }

}