Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / main / java / org / openecomp / policy / pypdp / ConfigFirewallPolicyRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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
21 package org.openecomp.policy.pypdp;
22
23 import java.io.StringReader;
24 import java.util.UUID;
25
26 import javax.json.Json;
27 import javax.json.JsonObject;
28 import javax.json.JsonReader;
29
30 import org.openecomp.policy.api.PolicyConfigException;
31 import org.openecomp.policy.pypdp.model_pojo.PepConfigFirewallPolicyRequest;
32 import org.openecomp.policy.std.StdPolicyEngine;
33
34 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
35
36 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
37
38 public class ConfigFirewallPolicyRequest {
39
40         private StdPolicyEngine pe;
41         public ConfigFirewallPolicyRequest(StdPolicyEngine pe){
42                 this.pe= pe;
43         }
44         
45         public String run(PepConfigFirewallPolicyRequest pep, String requestID, String operation, String userID, String passcode) {
46
47                 String result = null;
48                 
49                 // construct a UUID from the request string
50                 UUID requestUUID = null;
51                 if (requestID != null && !requestID.isEmpty()) {
52                         try {
53                                 requestUUID = UUID.fromString(requestID);
54                         }
55                         catch (IllegalArgumentException e) {
56                                 requestUUID = UUID.randomUUID();
57                                 PolicyLogger.info("Generated Random UUID: " + requestUUID.toString());
58                         }
59                 }
60                 
61                 if (pep.getPolicyName()!= null && !pep.getPolicyName().isEmpty()) {
62                         if (pep.getFirewallJson() != null && !pep.getFirewallJson().isEmpty()) {
63                                 if (pep.getPolicyScope() != null && !pep.getPolicyScope().isEmpty()) {
64                                         try {
65                                                 
66                                                 JsonObject json = stringToJson(pep.getFirewallJson());
67                                                 
68                                                 if(!json.toString().contains("errorMessage")){
69                                                         if (operation.equalsIgnoreCase("create")) {
70                                                                 result = pe.createConfigFirewallPolicy(pep.getPolicyName(), json, pep.getPolicyScope(), requestUUID, userID, passcode,
71                                                                                 pep.getRiskLevel(), pep.getRiskType(), pep.getGuard(), pep.getTtlDate());
72                                                         } else {
73                                                                 result = pe.updateConfigFirewallPolicy(pep.getPolicyName(), json, pep.getPolicyScope(), requestUUID, userID, passcode,
74                                                                                 pep.getRiskLevel(), pep.getRiskType(), pep.getGuard(), pep.getTtlDate());
75                                                         }
76                                                 } else {
77                                                         result = XACMLErrorConstants.ERROR_SCHEMA_INVALID + "BAD REQUEST:  Invalid Json for firewallJson: " + pep.getFirewallJson();
78                                                 }
79                                         } catch (PolicyConfigException e) {
80                                                 result = e.getMessage();
81                                         } catch (Exception e) {
82                                                 // TODO Auto-generated catch block
83                                                 e.printStackTrace();
84                                         }
85                                 } else {
86                                         result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  policyScope was null or empty.";
87                                 }
88                         } else {
89                                 result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  firewallJson was null or empty.";
90                         }
91                 } else {
92                         result = XACMLErrorConstants.ERROR_DATA_ISSUE + "BAD REQUEST:  policyName was null or empty.";
93                 }
94                 
95                 return result;
96                 
97         }
98         
99         private JsonObject stringToJson(String jsonString) {
100                 
101                 JsonObject json = null;
102                 if (jsonString != null) {
103                         
104                         try {
105                                 
106                                 //Read jsonBody to JsonObject
107                                 StringReader in = null;
108                                 
109                                 in = new StringReader(jsonString);
110                                 
111                                 JsonReader jsonReader = Json.createReader(in);
112                                 json = jsonReader.readObject();
113                                 
114                         } catch (Exception e) {
115                                 String jsonError = "{\"errorMessage\": \"" +  e.getMessage() + "\"}";
116                                 StringReader error = null;
117                                 error = new StringReader(jsonError);
118                                 JsonReader jsonReader = Json.createReader(error);
119                                 JsonObject badJson = jsonReader.readObject();
120                                 return badJson;
121                         }
122
123                 }
124                 
125                 return json;
126         }
127         
128 }