472d3aa95471a4686773b69bd4e0246d7c6c7d12
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / utils / PolicyApiUtils.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.utils;
21
22 import java.io.StringReader;
23
24 import javax.json.Json;
25 import javax.json.JsonArray;
26 import javax.json.JsonException;
27 import javax.json.JsonObject;
28 import javax.json.JsonReader;
29 import javax.json.stream.JsonParsingException;
30
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.utils.PolicyUtils;
34 import org.onap.policy.xacml.api.XACMLErrorConstants;
35
36 import com.google.common.base.CharMatcher;
37
38 public class PolicyApiUtils {
39     private static Logger LOGGER = FlexLogger.getLogger(PolicyApiUtils.class
40             .getName());
41     private static final String SUCCESS = "success";
42
43                 
44     public static Boolean validateNONASCIICharactersAndAllowSpaces(
45             String jsonString) {
46         Boolean isValidForm = false;
47         if (jsonString.isEmpty()) {
48             LOGGER.error("The Value is empty.");
49             return false;
50         } else {
51             if (CharMatcher.ASCII.matchesAllOf((CharSequence) jsonString)) {
52                 LOGGER.info("The Value does not contain ASCII Characters");
53                 isValidForm = true;
54             } else {
55                 LOGGER.error("The Value Contains Non ASCII Characters");
56                 isValidForm = false;
57             }
58         }
59         return isValidForm;
60     }
61     
62     public static String formatResponse(StringBuilder responseString){
63         
64         LOGGER.info("Formatting response message from Policy Validator");
65                 String response = null;
66         response = responseString.toString().replace("<br>", " | ");            
67                 response = response.replaceAll("(<b>|<\\/b>|<br>|<i>|<\\/i>|@#)", "");
68                                 
69         return response;
70     }
71     
72     public static boolean isNumeric(String str) {
73         for (char c : str.toCharArray()) {
74             if (!Character.isDigit(c))
75                 return false;
76         }
77         return true;
78     }
79
80     public static JsonObject stringToJsonObject(String value)
81             throws JsonException, JsonParsingException, IllegalStateException {
82         JsonReader jsonReader = Json.createReader(new StringReader(value));
83         JsonObject object = jsonReader.readObject();
84         jsonReader.close();
85         return object;
86     }
87     
88     public static String validateDictionaryJsonFields(JsonObject json, String dictionary) {
89         
90         LOGGER.info("Validating DictionaryJsonField values");
91         String message;
92         
93         if("Action".equals(dictionary.trim())){
94                 if(json.containsKey("attributeName")){
95                         if(json.getString("attributeName")==null || json.getString("attributeName").trim().isEmpty()){
96                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Attribute Name provided.";
97                                 return message;
98                         }
99                         if(!SUCCESS.equals(PolicyUtils.policySpecialCharValidator(json.getString("attributeName").trim()))){
100                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Attribute Name value.";
101                                 return message;
102                         }
103                 }else{
104                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing attributeName key in the dictionaryJson parameter.";
105                         return message;
106                 }
107                 if(json.containsKey("type")){
108                         if(json.getString("type")==null || json.getString("type").trim().isEmpty()){
109                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Type provided.";
110                                 return message;
111                         }
112                         if(!"REST".equals(json.getString("type").trim())){
113                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Type value.";
114                                 return message;
115                         }
116                 }else{
117                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing type key in the dictionaryJson parameter.";
118                         return message;
119                 }
120                 if(json.containsKey("method")){
121                         if(json.getString("method")==null || json.getString("method").trim().isEmpty()){
122                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Method provided.";
123                                 return message;
124                         }
125                         if("GET".equals(json.getString("method").trim()) 
126                                         || "PUT".equals(json.getString("method").trim()) 
127                                         || "POST".equals(json.getString("method").trim())){
128                                 
129                                 message = SUCCESS;
130                                 
131                         }else{
132                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Method value.";
133                                 return message; 
134                         }
135                 }else{
136                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing method key in the dictionaryJson parameter.";
137                         return message;
138                 }
139                 if(json.containsKey("url")){
140                         if(json.getString("url")==null || json.getString("url").trim().isEmpty()){
141                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No URL provided.";
142                                 return message;
143                         }
144                 }else{
145                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing url key in the dictionaryJson parameter.";
146                         return message;
147                 }
148                 if(json.containsKey("body")){
149                         if(json.getString("body")==null || json.getString("body").trim().isEmpty()){
150                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Body provided.";
151                                 return message;
152                         }
153                 }else{
154                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing body key in the dictionaryJson parameter.";
155                         return message;
156                 }
157                 if(json.containsKey("headers")){
158                         JsonArray array = json.getJsonArray("headers");
159                         
160                         for (int i = 0;i<array.size(); i++) { 
161                                 JsonObject jsonObj = array.getJsonObject(i);
162                                 if(jsonObj.containsKey("option")){
163                                         if(jsonObj.getString("option")==null || jsonObj.getString("option").trim().isEmpty()){
164                                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing required Option value";
165                                         return message;
166                                         }
167                                 }else{
168                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing option key in the headers list of the dictionaryJson parameter.";
169                                 return message;
170                                 }
171
172                                 if(jsonObj.containsKey("number")){
173                                         if(jsonObj.getString("number")==null || jsonObj.getString("number").trim().isEmpty()){
174                                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing required Number value";
175                                         return message;
176                                         }
177                                 }else{
178                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing number key in the headers list of the dictionaryJson parameter.";
179                                 return message;
180                                 }
181                         }
182                 }
183
184         }
185
186         return SUCCESS;
187     }
188 }