Modified the code to fix various pushPolicy issues
[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 boolean isNumeric(String str) {
63         for (char c : str.toCharArray()) {
64             if (!Character.isDigit(c))
65                 return false;
66         }
67         return true;
68     }
69
70     public static JsonObject stringToJsonObject(String value)
71             throws JsonException, JsonParsingException, IllegalStateException {
72         JsonReader jsonReader = Json.createReader(new StringReader(value));
73         JsonObject object = jsonReader.readObject();
74         jsonReader.close();
75         return object;
76     }
77     
78     public static String validateDictionaryJsonFields(JsonObject json, String dictionary) {
79         
80         LOGGER.info("Validating DictionaryJsonField values");
81         String message;
82         
83         if("Action".equals(dictionary.trim())){
84                 if(json.containsKey("attributeName")){
85                         if(json.getString("attributeName")==null || json.getString("attributeName").trim().isEmpty()){
86                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Attribute Name provided.";
87                                 return message;
88                         }
89                         if(!SUCCESS.equals(PolicyUtils.policySpecialCharValidator(json.getString("attributeName").trim()))){
90                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Attribute Name value.";
91                                 return message;
92                         }
93                 }else{
94                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing attributeName key in the dictionaryJson parameter.";
95                         return message;
96                 }
97                 if(json.containsKey("type")){
98                         if(json.getString("type")==null || json.getString("type").trim().isEmpty()){
99                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Type provided.";
100                                 return message;
101                         }
102                         if(!"REST".equals(json.getString("type").trim())){
103                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Type value.";
104                                 return message;
105                         }
106                 }else{
107                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing type key in the dictionaryJson parameter.";
108                         return message;
109                 }
110                 if(json.containsKey("method")){
111                         if(json.getString("method")==null || json.getString("method").trim().isEmpty()){
112                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Method provided.";
113                                 return message;
114                         }
115                         if("GET".equals(json.getString("method").trim()) 
116                                         || "PUT".equals(json.getString("method").trim()) 
117                                         || "POST".equals(json.getString("method").trim())){
118                                 
119                                 message = SUCCESS;
120                                 
121                         }else{
122                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Method value.";
123                                 return message; 
124                         }
125                 }else{
126                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing method key in the dictionaryJson parameter.";
127                         return message;
128                 }
129                 if(json.containsKey("url")){
130                         if(json.getString("url")==null || json.getString("url").trim().isEmpty()){
131                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No URL provided.";
132                                 return message;
133                         }
134                 }else{
135                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing url key in the dictionaryJson parameter.";
136                         return message;
137                 }
138                 if(json.containsKey("body")){
139                         if(json.getString("body")==null || json.getString("body").trim().isEmpty()){
140                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Body provided.";
141                                 return message;
142                         }
143                 }else{
144                         message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing body key in the dictionaryJson parameter.";
145                         return message;
146                 }
147                 if(json.containsKey("headers")){
148                         JsonArray array = json.getJsonArray("headers");
149                         
150                         for (int i = 0;i<array.size(); i++) { 
151                                 JsonObject jsonObj = array.getJsonObject(i);
152                                 if(jsonObj.containsKey("option")){
153                                         if(jsonObj.getString("option")==null || jsonObj.getString("option").trim().isEmpty()){
154                                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing required Option value";
155                                         return message;
156                                         }
157                                 }else{
158                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing option key in the headers list of the dictionaryJson parameter.";
159                                 return message;
160                                 }
161
162                                 if(jsonObj.containsKey("number")){
163                                         if(jsonObj.getString("number")==null || jsonObj.getString("number").trim().isEmpty()){
164                                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing required Number value";
165                                         return message;
166                                         }
167                                 }else{
168                                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing number key in the headers list of the dictionaryJson parameter.";
169                                 return message;
170                                 }
171                         }
172                 }
173
174         }
175
176         return SUCCESS;
177     }
178 }