[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / ConfigPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.onap.policy.pdp.rest.api.services;
21
22 import java.util.Map;
23
24 import org.onap.policy.api.AttributeType;
25 import org.onap.policy.api.PolicyException;
26 import org.onap.policy.api.PolicyParameters;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.pdp.rest.api.utils.PolicyApiUtils;
30 import org.onap.policy.utils.PolicyUtils;
31 import org.onap.policy.xacml.api.XACMLErrorConstants;
32 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
33
34 /**
35  * Config Base Policy Implementation. 
36  * 
37  * @version 0.1
38  */
39 public class ConfigPolicyService {
40         private static final Logger LOGGER = FlexLogger.getLogger(ConfigPolicyService.class.getName());
41         private PAPServices papServices = null;
42         
43         private PolicyParameters policyParameters = null;
44         private String message = null;
45         private String policyName = null;
46         private String policyScope = null;
47         private String date = null;
48         private String onapName = null;
49         private String configName = null;
50         
51         public ConfigPolicyService(String policyName, String policyScope,
52                         PolicyParameters policyParameters, String date) {
53                 this.policyParameters = policyParameters;
54                 this.policyName = policyName;
55                 this.policyScope = policyScope;
56                 this.date = date;
57                 papServices = new PAPServices();
58         }
59
60         public Boolean getValidation() {
61                 if(policyParameters.getConfigBody()==null || policyParameters.getConfigBody().trim().isEmpty()){
62                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ "No Config Body given.";
63                         return false;
64                 }
65                 if(policyParameters.getConfigBodyType()==null){
66                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ "No Config Body Type given.";
67                         return false;
68                 }
69                 boolean levelCheck = false;
70                 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
71                 if (!levelCheck){
72                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
73                         return false;
74                 }
75                 onapName = policyParameters.getOnapName();
76                 configName = policyParameters.getConfigName();
77                 if(onapName==null || onapName.trim().isEmpty()){
78                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given.";
79                         return false;
80                 }
81                 if(configName==null || configName.trim().isEmpty()){
82                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config Name given.";
83                         return false;
84                 }
85                 message = PolicyUtils.emptyPolicyValidator(onapName);
86         if(!message.contains("success")){
87             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
88             return false;
89         }
90         message = PolicyUtils.emptyPolicyValidator(configName);
91         if(!message.contains("success")){
92             message = XACMLErrorConstants.ERROR_DATA_ISSUE+ message;
93             return false;
94         }
95                 return true;
96         }
97
98         public String getMessage() {
99                 return message;
100         }
101
102         public String getResult(boolean updateFlag) throws PolicyException {
103                 String response = null;
104                 String operation = null;
105                 if (updateFlag){
106                         operation = "update";
107                 } else {
108                         operation = "create";
109                 }
110                 String configType = policyParameters.getConfigBodyType().toString();
111                 String body = policyParameters.getConfigBody();
112                 String configBody = null;
113                 //check body for JSON form and remove single quotes if present
114                 if ("JSON".equalsIgnoreCase(configType)) {
115                         if (body.contains("'")) {
116                                 configBody = body.replace("'", "\"");
117                         } else {
118                                 configBody = body;
119                         }
120                 } else {
121                         configBody = body;
122                 }
123                 Map<String,String> configAttributes = null;
124                 if(policyParameters.getAttributes()!=null){
125                         configAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
126                 }
127                 // create Policy. 
128                 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Base", policyName, policyParameters.getPolicyDescription(), onapName, configName, configAttributes, configType, 
129                                 configBody, updateFlag, policyScope,0, policyParameters.getRiskLevel(),policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date);
130                 // Send Json to PAP. 
131                 response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "Config");
132                 LOGGER.info(response);
133                 return response;
134         }
135
136 }