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