[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / FirewallPolicyService.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 javax.json.JsonException;
23 import javax.json.JsonObject;
24
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.xacml.api.XACMLErrorConstants;
31 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
32
33 /**
34  * Firewall Policy Implementation. 
35  * 
36  * @version 0.1
37  */
38 public class FirewallPolicyService {
39         private static final Logger LOGGER = FlexLogger.getLogger(FirewallPolicyService.class.getName());
40         
41         private  PAPServices papServices = null;
42         private PolicyParameters policyParameters = null;
43         private String message = null;
44         private String policyName = null;
45         private String policyScope = null;
46         private String date = null;
47         private JsonObject firewallJson = null;
48         
49         public FirewallPolicyService(String policyName, String policyScope,
50                         PolicyParameters policyParameters, String date) {
51                 this.policyParameters = policyParameters;
52                 this.policyName = policyName;
53                 this.policyScope = policyScope;
54                 this.date = date;
55                 papServices = new PAPServices();
56         }
57
58         public Boolean getValidation() {
59                 if(policyParameters.getConfigBody()==null){
60                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ "No Config Body given.";
61                         return false;
62                 }
63                 try{
64                         firewallJson = PolicyApiUtils.stringToJsonObject(policyParameters.getConfigBody());
65                 } catch(JsonException| IllegalStateException e){
66                         message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper JSON object : " + policyParameters.getConfigBody();
67                         LOGGER.error("Error while parsing JSON body for creating Firewall Policy " , e);
68                         return false;
69                 }
70                 if(firewallJson==null|| firewallJson.isEmpty()){
71                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Config-Body given.";
72                         return false;
73                 }
74                 boolean levelCheck = false;
75                 levelCheck = PolicyApiUtils.isNumeric(policyParameters.getRiskLevel());
76                 if (!levelCheck){
77                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Incorrect Risk Level given.";
78                         return false;
79                 }
80                 return true;
81         }
82
83         public String getMessage() {
84                 return message;
85         }
86
87         public String getResult(boolean updateFlag) throws PolicyException {
88                 String response = null;
89                 String operation = null;
90                 if (updateFlag){
91                         operation = "update";
92                 } else {
93                         operation = "create";
94                 }
95                 //set values for basic policy information
96                 if(!firewallJson.containsKey("configName")){
97                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No configName given in firwall JSON.";
98                         LOGGER.error(message);
99                         return message;
100                 }
101                 String configName = firewallJson.get("configName").toString();
102                 String configDescription = "";
103                 String json = firewallJson.toString();
104                 // Create Policy. 
105                 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Firewall Config", policyName, configDescription, configName, updateFlag, policyScope, json, 0, 
106                                 policyParameters.getRiskLevel(),policyParameters.getRiskType(), String.valueOf(policyParameters.getGuard()), date);
107                 // Send Json to PAP. 
108                 response = (String) papServices.callPAP(newPAPPolicy, new String[] {"operation="+operation, "apiflag=api", "policyType=Config"}, policyParameters.getRequestID(), "ConfigFirewall");
109                 LOGGER.info(response);
110                 return response;
111         }
112
113 }