Merge "MS Model Input Validation"
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / util / PolicyValidation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.rest.util;
21
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import javax.json.Json;
33 import javax.json.JsonException;
34 import javax.json.JsonObject;
35 import javax.json.JsonReader;
36 import javax.json.JsonValue;
37
38 import org.apache.commons.lang.StringUtils;
39 import org.json.JSONObject;
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
42 import org.onap.policy.rest.adapter.ClosedLoopFaultBody;
43 import org.onap.policy.rest.adapter.ClosedLoopPMBody;
44 import org.onap.policy.rest.adapter.PolicyRestAdapter;
45 import org.onap.policy.rest.dao.CommonClassDao;
46 import org.onap.policy.rest.jpa.MicroServiceModels;
47 import org.onap.policy.rest.jpa.SafePolicyWarning;
48 import org.onap.policy.utils.PolicyUtils;
49 import org.onap.policy.xacml.api.XACMLErrorConstants;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.stereotype.Service;
52
53 import com.fasterxml.jackson.databind.JsonNode;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import com.google.common.base.Splitter;
56 import com.google.common.base.Strings;
57
58 @Service
59 public class PolicyValidation {
60         
61         private static final Logger LOGGER      = FlexLogger.getLogger(PolicyValidation.class);
62         
63         public static final String CONFIG_POLICY = "Config";
64         public static final String ACTION_POLICY = "Action";
65         public static final String DECISION_POLICY = "Decision";
66         public static final String CLOSEDLOOP_POLICY = "ClosedLoop_Fault";
67         public static final String CLOSEDLOOP_PM = "ClosedLoop_PM";
68         public static final String ENFORCER_CONFIG_POLICY= "Enforcer Config";
69         public static final String MICROSERVICES="Micro Service";
70         public static final String FIREWALL="Firewall Config";
71         public static final String HTML_ITALICS_LNBREAK = "</i><br>";
72         public static final String SUCCESS = "success";
73         public static final String EMPTY_COMPONENT_ATTR = "Component Attributes: One or more Fields in Component Attributes is Empty.";
74         
75         private static Map<String, String> mapAttribute = new HashMap<>();
76         private static Map<String, String> jsonRequestMap = new HashMap<>();
77         private static List<String> modelRequiredFieldsList = new ArrayList<>();
78         
79         private static CommonClassDao commonClassDao;
80         
81         @Autowired
82         public PolicyValidation(CommonClassDao commonClassDao){
83                 PolicyValidation.commonClassDao = commonClassDao;
84         }
85         
86         /*
87          * This is an empty constructor
88          */
89         public PolicyValidation(){
90                 // Empty constructor
91         }
92         
93         
94         public StringBuilder validatePolicy(PolicyRestAdapter policyData) throws IOException{
95                 try{
96                         boolean valid = true;
97                         StringBuilder responseString = new StringBuilder();
98                         ObjectMapper mapper = new ObjectMapper();
99                         
100                         if(policyData.getPolicyName() != null){
101                                 String policyNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getPolicyName());
102                                 if(!policyNameValidate.contains(SUCCESS)){
103                                         responseString.append("<b>PolicyName</b>:<i>" +  policyNameValidate + HTML_ITALICS_LNBREAK);
104                                         valid = false;
105                                 }
106                         }else{
107                                 responseString.append( "<b>PolicyName</b>: PolicyName Should not be empty" + HTML_ITALICS_LNBREAK);
108                                 valid = false;
109                         }
110                         if(policyData.getPolicyDescription() != null){
111                                 String descriptionValidate = PolicyUtils.descriptionValidator(policyData.getPolicyDescription());
112                                 if(!descriptionValidate.contains(SUCCESS)){
113                                         responseString.append("<b>Description</b>:<i>" +  descriptionValidate + HTML_ITALICS_LNBREAK);
114                                         valid = false;
115                                 }       
116                         }
117
118                         if(!"API".equals(policyData.getApiflag()) && policyData.getAttributes() != null && !policyData.getAttributes().isEmpty()){
119                 for(Object attribute : policyData.getAttributes()){
120                     if(attribute instanceof LinkedHashMap<?, ?>){
121                         String value = null;
122                         String key = null;
123                         if(((LinkedHashMap<?, ?>) attribute).get("key") != null){
124                             key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
125                             if(!PolicyUtils.policySpecialCharValidator(key).contains("success")){
126                                 responseString.append("<b>Attributes or Component Attributes</b>:<i>" +  value + " : value has spaces or invalid characters</i><br>");
127                                 valid = false;
128                             }
129                         }else{
130                             if(CONFIG_POLICY.equals(policyData.getPolicyType())){
131                                 if("Base".equals(policyData.getConfigPolicyType())){
132                                     responseString.append("<b>Attributes</b>:<i> has one missing Attribute key</i><br>");
133                                 }
134                                 if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){
135                                     responseString.append("<b>Rule Attributes</b>:<i> has one missing Attribute key</i><br>");
136                                 }
137                             }else{
138                                 responseString.append("<b>Component Attributes</b>:<i> has one missing Component Attribute key</i><br>");
139                             }
140                             valid = false;
141                         }
142                         if(((LinkedHashMap<?, ?>) attribute).get("value") != null){
143                             value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
144                             if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){
145                                 if(CONFIG_POLICY.equals(policyData.getPolicyType())){
146                                     if("Base".equals(policyData.getConfigPolicyType())){
147                                         responseString.append("<b>Attributes</b>:<i>" +  value + " : value has spaces or invalid characters</i><br>");
148                                     }
149                                     if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){
150                                         responseString.append("<b>Rule Attributes</b>:<i>" +  value + " : value has spaces or invalid characters</i><br>");
151                                     }
152                                 }else{
153                                     responseString.append("<b>Component Attributes</b>:<i>" +  value + " : value has spaces or invalid characters</i><br>");
154                                 }
155                                 valid = false;
156                             }
157                         }else{
158                             if(CONFIG_POLICY.equals(policyData.getPolicyType())){
159                                 if("Base".equals(policyData.getConfigPolicyType())){
160                                     responseString.append("<b>Attributes</b>:<i> has one missing Attribute value</i><br>");
161                                 }
162                                 if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){
163                                     responseString.append("<b>Rule Attributes</b>:<i> has one missing Attribute value</i><br>");
164                                 }
165                             }else{
166                                 responseString.append("<b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>");
167                             }
168                             valid = false;
169                         }
170                     }
171                 }
172             }
173                         
174             //Decision Policy Attributes Validation
175             if(!"API".equals(policyData.getApiflag()) && policyData.getSettings() != null && !policyData.getSettings().isEmpty()){
176                 for(Object attribute : policyData.getAttributes()){
177                     if(attribute instanceof LinkedHashMap<?, ?>){
178                         String value = null;
179                         if(((LinkedHashMap<?, ?>) attribute).get("key") == null){
180                             responseString.append("<b>Settings Attributes</b>:<i> has one missing Attribute key</i><br>");
181                             valid = false;
182                         }
183                         if(((LinkedHashMap<?, ?>) attribute).get("value") != null){
184                             value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
185                             if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){
186                                 responseString.append("<b>Settings Attributes</b>:<i>" +  value + " : value has spaces or invalid characters</i><br>");
187                                 valid = false;
188                             }
189                         }else{
190                             responseString.append("<b>Settings Attributes</b>:<i> has one missing Attribute Value</i><br>");
191                             valid = false;
192                         }
193                     }
194                 }
195             }
196             
197             if(!"API".equals(policyData.getApiflag()) && policyData.getRuleAlgorithmschoices() != null &&  !policyData.getRuleAlgorithmschoices().isEmpty()){
198                 for(Object attribute : policyData.getRuleAlgorithmschoices()){
199                     if(attribute instanceof LinkedHashMap<?, ?>){
200                         String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString();
201                         if(((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField1") == null){
202                             responseString.append("<b>Rule Algorithms</b>:<i>" +  label + " : Field 1 value is not selected</i><br>");
203                             valid = false;
204                         }
205                         if(((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmCombo") == null){
206                             responseString.append("<b>Rule Algorithms</b>:<i>" +  label + " : Field 2 value is not selected</i><br>");
207                             valid = false;
208                         }
209                         if(((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2") != null){
210                             String value = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2").toString();
211                             if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){
212                                 responseString.append("<b>Rule Algorithms</b>:<i>" +  label + " : Field 3 value has special characters</i><br>");
213                                 valid = false;
214                             }
215                         }else{
216                             responseString.append("<b>Rule Algorithms</b>:<i>" +  label + " : Field 3 value is empty</i><br>");
217                             valid = false;
218                         }
219                     }
220                 }
221             }
222             
223                         if(CONFIG_POLICY.equalsIgnoreCase(policyData.getPolicyType())){
224                                 if ("Base".equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_POLICY.equals(policyData.getConfigPolicyType())
225                                                 ||  CLOSEDLOOP_PM.equals(policyData.getConfigPolicyType()) || ENFORCER_CONFIG_POLICY.equals(policyData.getConfigPolicyType()) 
226                                                 || MICROSERVICES.equals(policyData.getConfigPolicyType())) {
227                                         
228                                         if(!Strings.isNullOrEmpty(policyData.getOnapName())) {
229                                                 String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName());
230                                                 if(!onapNameValidate.contains(SUCCESS)){
231                                                         responseString.append("<b>OnapName</b>:<i>" +  onapNameValidate + HTML_ITALICS_LNBREAK);
232                                                         valid = false;
233                                                 }
234                                         }else{
235                                                 responseString.append("<b>Onap Name</b>: Onap Name Should not be empty" + HTML_ITALICS_LNBREAK);
236                                                 valid = false;
237                                         }
238                                 }
239
240                                 if(!Strings.isNullOrEmpty(policyData.getRiskType())) {
241                                         String riskTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getRiskType());
242                                         if(!riskTypeValidate.contains(SUCCESS)){
243                                                 responseString.append("<b>RiskType</b>:<i>" +  riskTypeValidate + HTML_ITALICS_LNBREAK);
244                                                 valid = false;
245                                         }
246                                 }else {
247                                         responseString.append("<b>RiskType</b>: Risk Type Should not be Empty" + HTML_ITALICS_LNBREAK);
248                                         valid = false;
249                                 }
250
251                                 if(!Strings.isNullOrEmpty(policyData.getRiskLevel())) {
252                                         String validateRiskLevel = PolicyUtils.policySpecialCharValidator(policyData.getRiskLevel());
253                                         if(!validateRiskLevel.contains(SUCCESS)){
254                                                 responseString.append("<b>RiskLevel</b>:<i>" +  validateRiskLevel + HTML_ITALICS_LNBREAK);
255                                                 valid = false;
256                                         }
257                                 }else {
258                                         responseString.append("<b>RiskLevel</b>: Risk Level Should not be Empty" + HTML_ITALICS_LNBREAK);
259                                         valid = false;
260                                 }
261
262                                 if(!Strings.isNullOrEmpty(policyData.getGuard())) {
263                                         String validateGuard = PolicyUtils.policySpecialCharValidator(policyData.getGuard());
264                                         if(!validateGuard.contains(SUCCESS)){
265                                                 responseString.append("<b>Guard</b>:<i>" +  validateGuard + HTML_ITALICS_LNBREAK);
266                                                 valid = false;
267                                         }
268                                 }else {
269                                         responseString.append("<b>Guard</b>: Guard Value Should not be Empty" + HTML_ITALICS_LNBREAK);
270                                         valid = false;
271                                 }
272
273                                 if("Base".equalsIgnoreCase(policyData.getConfigPolicyType())){
274                                         if(!Strings.isNullOrEmpty(policyData.getConfigName())) {
275                                                 String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName());
276                                                 if(!configNameValidate.contains(SUCCESS)){
277                                                         responseString.append("ConfigName:" +  configNameValidate + HTML_ITALICS_LNBREAK);
278                                                         valid = false;
279                                                 }
280                                         }else{
281                                                 responseString.append("Config Name: Config Name Should not be Empty" + HTML_ITALICS_LNBREAK);
282                                                 valid = false;
283                                         }
284                                         if(!Strings.isNullOrEmpty(policyData.getConfigType())) {
285                                                 String configTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigType());
286                                                 if(!configTypeValidate.contains(SUCCESS)){
287                                                         responseString.append("ConfigType:" +  configTypeValidate + HTML_ITALICS_LNBREAK);
288                                                         valid = false;
289                                                 }
290                                         }else{
291                                                 responseString.append("Config Type: Config Type Should not be Empty" + HTML_ITALICS_LNBREAK);
292                                                 valid = false;
293                                         }
294                                         if(!Strings.isNullOrEmpty(policyData.getConfigBodyData())) {
295                                                 String configBodyData = policyData.getConfigBodyData();
296                                                 String configType = policyData.getConfigType();
297                                                 if (configType != null) {
298                                                         if ("JSON".equals(configType)) {
299                                                                 if (!PolicyUtils.isJSONValid(configBodyData)) {
300                                                                         responseString.append("Config Body: JSON Content is not valid" + HTML_ITALICS_LNBREAK);
301                                                                         valid = false;
302                                                                 }
303                                                         } else if ("XML".equals(configType)) {
304                                                                 if (!PolicyUtils.isXMLValid(configBodyData)) {
305                                                                         responseString.append("Config Body: XML Content data is not valid" + HTML_ITALICS_LNBREAK);
306                                                                         valid = false;
307                                                                 }
308                                                         } else if ("PROPERTIES".equals(configType)) {
309                                                                 if (!PolicyUtils.isPropValid(configBodyData) || "".equals(configBodyData)) {
310                                                                         responseString.append("Config Body: Property data is not valid" + HTML_ITALICS_LNBREAK);
311                                                                         valid = false;
312                                                                 } 
313                                                         } else if ("OTHER".equals(configType) && ("".equals(configBodyData))) {
314                                                                 responseString.append("Config Body: Config Body Should not be Empty" + HTML_ITALICS_LNBREAK);
315                                                                 valid = false;
316                                                         }
317                                                 }
318                                         }else{
319                                                 responseString.append("Config Body: Config Body Should not be Empty" + HTML_ITALICS_LNBREAK);
320                                                 valid = false;
321                                         }
322                                 }
323
324                                 if(FIREWALL.equalsIgnoreCase(policyData.getConfigPolicyType())){
325                                         if(policyData.getConfigName() != null && !policyData.getConfigName().isEmpty()){
326                                                 String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName());
327                                                 if(!configNameValidate.contains(SUCCESS)){
328                                                         responseString.append("<b>ConfigName</b>:<i>" +  configNameValidate + HTML_ITALICS_LNBREAK);
329                                                         valid = false;
330                                                 }
331                                         }else{
332                                                 responseString.append("<b>Config Name</b>:<i> Config Name is required" + HTML_ITALICS_LNBREAK);
333                                                 valid = false;
334                                         }
335                                         if(policyData.getSecurityZone() == null || policyData.getSecurityZone().isEmpty()){
336                                                 responseString.append("<b>Security Zone</b>:<i> Security Zone is required" + HTML_ITALICS_LNBREAK);
337                                                 valid = false;
338                                         }
339                                 }
340                                 if("BRMS_Param".equalsIgnoreCase(policyData.getConfigPolicyType()) && Strings.isNullOrEmpty(policyData.getRuleName())){
341                                         responseString.append("<b>BRMS Template</b>:<i>BRMS Template is required" + HTML_ITALICS_LNBREAK);
342                                         valid = false;
343                                 }
344                                 if("BRMS_Raw".equalsIgnoreCase(policyData.getConfigPolicyType())){
345                                         if(policyData.getConfigBodyData() != null && !policyData.getConfigBodyData().isEmpty()){
346                                                 String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData());
347                                                 
348                                                 // If there are any error other than Annotations then this is not Valid
349                                                 if(message.contains("[ERR")){
350                                                         responseString.append("<b>Raw Rule Validate</b>:<i>Raw Rule has error"+ message + HTML_ITALICS_LNBREAK);
351                                                         valid = false;
352                                                 }
353                                         }else{
354                                                 responseString.append("<b>Raw Rule</b>:<i>Raw Rule is required" + HTML_ITALICS_LNBREAK);
355                                                 valid = false;
356                                         }
357                                 }
358                                 if(CLOSEDLOOP_PM.equalsIgnoreCase(policyData.getConfigPolicyType())){
359                                         try{
360                                                 if(Strings.isNullOrEmpty(policyData.getServiceTypePolicyName().get("serviceTypePolicyName").toString())){
361                                                         responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required" + HTML_ITALICS_LNBREAK);
362                                                         valid = false; 
363                                                 }
364                                                 
365                                         }catch(Exception e){
366                                             LOGGER.error("ERROR in ClosedLoop_PM PolicyName" , e);
367                                                 responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required" + HTML_ITALICS_LNBREAK);
368                                                 valid = false;
369                                         }
370
371                                         if(policyData.getJsonBody() != null){
372                                                 
373                                                 ClosedLoopPMBody pmBody = mapper.readValue(policyData.getJsonBody(), ClosedLoopPMBody.class);
374                                                 if(pmBody.getEmailAddress() != null){
375                                                         String result = emailValidation(pmBody.getEmailAddress(), responseString.toString());
376                                                         if(result != SUCCESS){
377                                                                 responseString.append(result + HTML_ITALICS_LNBREAK);
378                                                                 valid = false;
379                                                         }
380                                                 }
381                                                 if((pmBody.isGamma() || pmBody.isMcr() || pmBody.isTrinity() || pmBody.isvDNS() || pmBody.isvUSP()) != true){
382                                                         responseString.append("<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services" + HTML_ITALICS_LNBREAK);
383                                                         valid = false; 
384                                                 }
385                                                 if(pmBody.getGeoLink() != null && !pmBody.getGeoLink().isEmpty()){
386                                                         String result = PolicyUtils.policySpecialCharValidator(pmBody.getGeoLink());
387                                                         if(!result.contains(SUCCESS)){
388                                                                 responseString.append("<b>GeoLink</b>:<i>" +  result + HTML_ITALICS_LNBREAK);
389                                                                 valid = false;
390                                                         }
391                                                 }
392                                                 if(pmBody.getAttributes() != null && !pmBody.getAttributes().isEmpty()){
393                                                         for(Entry<String, String> entry : pmBody.getAttributes().entrySet()){
394                                                                 String key = entry.getKey();
395                                                                 String value = entry.getValue();
396                                                                 if(!key.contains("Message")){
397                                                                         String attributeValidate = PolicyUtils.policySpecialCharValidator(value);
398                                                                         if(!attributeValidate.contains(SUCCESS)){
399                                                                                 responseString.append("<b>Attributes</b>:<i>" +  key + " : value has spaces or invalid characters" + HTML_ITALICS_LNBREAK);
400                                                                                 valid = false;
401                                                                         }
402                                                                 }
403                                                         }       
404                                                 }
405                                         }else{
406                                                 responseString.append("<b>D2/Virtualized Services</b>:<i>Select atleast one D2/Virtualized Services" + HTML_ITALICS_LNBREAK);
407                                                 valid = false;
408                                         }
409                                 }
410                                 if(CLOSEDLOOP_POLICY.equalsIgnoreCase(policyData.getConfigPolicyType())){
411                                         if(policyData.getJsonBody() != null){
412
413                                                 // For API we need to get the conditions key from the Json request and check it before deserializing to POJO due to the enum
414                                                 if("API".equals(policyData.getApiflag())){
415                                                         JSONObject json = new JSONObject(policyData.getJsonBody());
416                                                         if(!json.isNull("conditions")){
417                                                                 String apiCondition = (String) json.get("conditions");
418                                                                 if(Strings.isNullOrEmpty(apiCondition)){
419                                                                         responseString.append("<b>Conditions</b>: <i>Select At least one Condition" + HTML_ITALICS_LNBREAK);
420                                                                         return responseString;
421                                                                 }
422                                                         } else {
423                                                                 responseString.append("<b>Conditions</b>: <i>There were no conditions provided in configBody json" + HTML_ITALICS_LNBREAK);
424                                                                 return responseString;
425                                                         }
426                                                 }else{
427                                                         if(policyData.getTrapDatas().getTrap1() != null){
428                                                                 if(policyData.getClearTimeOut() == null){
429                                                                         responseString.append("<b>Trigger Clear TimeOut</b>: <i>Trigger Clear TimeOut is required when atleast One Trigger Signature is enabled</i><br>");
430                                                                         valid = false;
431                                                                 }
432                                                                 if(policyData.getTrapMaxAge() == null){
433                                                                         responseString.append("<b>Trap Max Age</b>: <i>Trap Max Age is required when atleast One Trigger Signature is enabled</i><br>");
434                                                                         valid = false;
435                                                                 }
436                                                         }
437                                                         if(policyData.getFaultDatas().getTrap1() != null && policyData.getVerificationclearTimeOut() == null){
438                                                                 responseString.append("<b>Fault Clear TimeOut</b>: <i>Fault Clear TimeOut is required when atleast One Fault Signature is enabled</i><br>");
439                                                                 valid = false;
440                                                         }
441                                                 }
442
443                                                 ClosedLoopFaultBody faultBody = mapper.readValue(policyData.getJsonBody(), ClosedLoopFaultBody.class);
444                                                 if(faultBody.getEmailAddress() != null && !faultBody.getEmailAddress().isEmpty()){
445                                                         String result = emailValidation(faultBody.getEmailAddress(), responseString.toString());
446                                                         if(!SUCCESS.equals(result)){
447                                                                 responseString.append(result+ HTML_ITALICS_LNBREAK);
448                                                                 valid = false;
449                                                         }
450                                                 }
451                                                 if((faultBody.isGamma() || faultBody.isMcr() || faultBody.isTrinity() || faultBody.isvDNS() || faultBody.isvUSP()) != true){
452                                                         responseString.append("<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services" + HTML_ITALICS_LNBREAK);
453                                                         valid = false; 
454                                                 }
455                                                 if(faultBody.getActions() == null || faultBody.getActions().isEmpty()){
456                                                         responseString.append("<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK);
457                                                         valid = false;
458                                                 }
459                                                 if(faultBody.getClosedLoopPolicyStatus() == null || faultBody.getClosedLoopPolicyStatus().isEmpty()){
460                                                         responseString.append("<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK);
461                                                         valid = false;
462                                                 }
463                                                 if(faultBody.getConditions() == null){
464                                                         responseString.append("<b>Conditions</b>: <i>Select At least one Condition" + HTML_ITALICS_LNBREAK);
465                                                         valid = false;
466                                                 }
467                                                 if(faultBody.getGeoLink() != null && !faultBody.getGeoLink().isEmpty()){
468                                                         String result = PolicyUtils.policySpecialCharWithSpaceValidator(faultBody.getGeoLink());
469                                                         if(!result.contains(SUCCESS)){
470                                                                 responseString.append("<b>GeoLink</b>:<i>" +  result + HTML_ITALICS_LNBREAK);
471                                                                 valid = false;
472                                                         }
473                                                 }
474                                                 if(faultBody.getAgingWindow() == 0){
475                                                         responseString.append("<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK);
476                                                         valid = false;
477                                                 }
478                                                 if(faultBody.getTimeInterval() == 0){
479                                                         responseString.append("<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK);
480                                                         valid = false;
481                                                 }
482                                                 if(faultBody.getRetrys() == 0){
483                                                         responseString.append("<b>Number of Retries</b>: <i>Number of Retries is required" + HTML_ITALICS_LNBREAK);
484                                                         valid = false;
485                                                 }
486                                                 if(faultBody.getTimeOutvPRO() == 0){
487                                                         responseString.append("<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK);
488                                                         valid = false;
489                                                 }
490                                                 if(faultBody.getTimeOutRuby() == 0){
491                                                         responseString.append("<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK);
492                                                         valid = false;
493                                                 }
494                                                 if(faultBody.getVnfType() == null || faultBody.getVnfType().isEmpty()){
495                                                         responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK);
496                                                         valid = false;
497                                                 }
498                                         }else{
499                                                 responseString.append("<b>D2/Virtualized Services</b>: <i>Select atleast one D2/Virtualized Services" + HTML_ITALICS_LNBREAK);
500                                                 responseString.append("<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK);
501                                                 responseString.append("<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK);
502                                                 responseString.append("<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK);
503                                                 responseString.append("<b>Conditions</b>: <i>Select Atleast one Condition" + HTML_ITALICS_LNBREAK);
504                                                 responseString.append("<b>PEP Name</b>: <i>PEP Name is required" + HTML_ITALICS_LNBREAK);
505                                                 responseString.append("<b>PEP Action</b>: <i>PEP Action is required" + HTML_ITALICS_LNBREAK);
506                                                 responseString.append("<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK);
507                                                 responseString.append("<b>Number of Retries</b>: <i>Number of Retries is required" + HTML_ITALICS_LNBREAK);
508                                                 responseString.append("<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK);
509                                                 responseString.append("<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK);
510                                                 responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK);
511                                                 valid = false; 
512                                         }
513                                 }
514
515                                 if (MICROSERVICES.equals(policyData.getConfigPolicyType())){
516                                         if(!Strings.isNullOrEmpty(policyData.getServiceType())){
517                                                 
518                                                 modelRequiredFieldsList.clear();
519                                                 pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON());
520
521                                                 String service;
522                                                 String version;
523                                                 if (policyData.getServiceType().contains("-v")){
524                                                         service = policyData.getServiceType().split("-v")[0];
525                                                         version = policyData.getServiceType().split("-v")[1];
526                                                 }else {
527                                                         service = policyData.getServiceType();
528                                                         version = policyData.getVersion();
529                                                 }
530                                                 
531                                                 if(!Strings.isNullOrEmpty(version)) {
532                                                         MicroServiceModels returnModel = getAttributeObject(service, version);
533                                                         
534                                                         if(returnModel != null) {
535                                                                 
536                                                                 String annotation = returnModel.getAnnotation();
537                                                                 String refAttributes = returnModel.getRef_attributes();
538                                                                 String subAttributes = returnModel.getSub_attributes();
539                                                                 String modelAttributes = returnModel.getAttributes();
540                                                                 
541                                                                 if (!Strings.isNullOrEmpty(annotation)){ 
542                                                                         Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annotation);
543                                                                         for (Entry<String, String> rMap : rangeMap.entrySet()){
544                                                                                 if (rMap.getValue().contains("range::")){
545                                                                                         String value = mapAttribute.get(rMap.getKey().trim());
546                                                                                         String[] tempString = rMap.getValue().split("::")[1].split("-");
547                                                                                         int startNum = Integer.parseInt(tempString[0]);
548                                                                                         int endNum = Integer.parseInt(tempString[1]);
549                                                                                         String returnString = "InvalidreturnModel Range:" + rMap.getKey() + " must be between " 
550                                                                                                         + startNum + " - "  + endNum + ",";
551                                                                                         
552                                                                                         if(value != null) {
553                                                                                                 if (PolicyUtils.isInteger(value.replace("\"", ""))){
554                                                                                                         int result = Integer.parseInt(value.replace("\"", ""));
555                                                                                                         if (result < startNum || result > endNum){
556                                                                                                                 responseString.append(returnString);                                                                    
557                                                                                                                 valid = false;
558                                                                                                         }
559                                                                                                 }else {
560                                                                                                         responseString.append(returnString);
561                                                                                                         valid = false;
562                                                                                                 }
563                                                                                         } else {
564                                                                                                 responseString.append("<b>"+rMap.getKey()+"</b>:<i>" + rMap.getKey() 
565                                                                                                 + " is required for the MicroService model " + service + HTML_ITALICS_LNBREAK);
566                                                                                                 valid = false;
567                                                                                         }
568
569                                                                                 }
570                                                                         }
571                                                                 } else {
572                                                                         // Validate for configName, location, uuid, and policyScope if no annotations exist for this model
573                                                                         if(Strings.isNullOrEmpty(policyData.getLocation())){
574                                                                                 responseString.append("<b>Micro Service Model</b>:<i> location is required for this model" + HTML_ITALICS_LNBREAK);
575                                                                                 valid = false;
576                                                                         }
577                                                                         
578                                                                         if(Strings.isNullOrEmpty(policyData.getConfigName())){
579                                                                                 responseString.append("<b>Micro Service Model</b>:<i> configName is required for this model" + HTML_ITALICS_LNBREAK);
580                                                                                 valid = false;
581                                                                         }       
582                                                                         
583                                                                         if(Strings.isNullOrEmpty(policyData.getUuid())){
584                                                                                 responseString.append("<b>Micro Service Model</b>:<i> uuid is required for this model" + HTML_ITALICS_LNBREAK);
585                                                                                 valid = false;
586                                                                         }       
587                                                                         
588                                                                         if(Strings.isNullOrEmpty(policyData.getPolicyScope())){
589                                                                                 responseString.append("<b>Micro Service Model</b>:<i> policyScope is required for this model" + HTML_ITALICS_LNBREAK);
590                                                                                 valid = false;
591                                                                         }       
592                                                                 }
593                                                                 
594                                                                 // If request comes from the API we need to validate required fields in the Micro Service Model 
595                                                                 // GUI request are already validated from the SDK-APP
596                                                                 if("API".equals(policyData.getApiflag())){
597                                                                         // get list of required fields from the sub_Attributes of the Model
598                                                                         if(!Strings.isNullOrEmpty(subAttributes)) {
599                                                                                 JsonObject subAttributesJson = stringToJsonObject(subAttributes);
600                                                                                 findRequiredFields(subAttributesJson);
601                                                                         }
602                                                                         
603                                                                         // get list of required fields from the attributes of the Model
604                                                                         if (!Strings.isNullOrEmpty(modelAttributes)) {
605                                                                                 Map<String, String> modelAttributesMap = null;
606                                                                                 if (",".equals(modelAttributes.substring(modelAttributes.length()-1))) {
607                                                                                         String attributeString = modelAttributes.substring(0, modelAttributes.length()-1);
608                                                                                         modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributeString);
609                                                                                 } else {
610                                                                                         modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes);
611                                                                                 }
612                                                                                 String json = new ObjectMapper().writeValueAsString(modelAttributesMap);
613                                                                                 findRequiredFields(stringToJsonObject(json));
614                                                                         }
615                                                                         
616                                                                         // get list of required fields from the ref_Attributes of the Model
617                                                                         if (!Strings.isNullOrEmpty(refAttributes)) {
618                                                                                 Map<String, String> refAttributesMap = null;
619                                                                                 if (",".equals(refAttributes.substring(refAttributes.length()-1))) {
620                                                                                         String attributesString = refAttributes.substring(0, refAttributes.length()-1);
621                                                                                         refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributesString);
622                                                                                 } else {
623                                                                                         refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes);
624                                                                                 }
625                                                                                 String json = new ObjectMapper().writeValueAsString(refAttributesMap);
626                                                                                 findRequiredFields(stringToJsonObject(json));
627                                                                         }
628                                                                         
629                                                                         if (modelRequiredFieldsList!=null || !modelRequiredFieldsList.isEmpty()) {
630                                                                                 // create jsonRequestMap with all json keys and values from request
631                                                                                 JsonNode rootNode = (JsonNode) policyData.getPolicyJSON();
632                                                                                 jsonRequestMap.clear();
633                                                                                 pullModelJsonKeyPairs(rootNode);
634                                                                                 
635                                                                                 // validate if the requiredFields are in the request
636                                                                                 for(String requiredField : modelRequiredFieldsList) {
637                                                                                         if (jsonRequestMap.containsKey(requiredField)) {
638                                                                                                 String value = jsonRequestMap.get(requiredField);
639                                                                                                 if(Strings.isNullOrEmpty(jsonRequestMap.get(requiredField)) || 
640                                                                                                                 "\"\"".equals(value) || 
641                                                                                                                 "".equals(jsonRequestMap.get(requiredField))){
642                                                                                                         responseString.append("<b>Micro Service Model</b>:<i> " + requiredField + " is required" + HTML_ITALICS_LNBREAK);
643                                                                                                         valid = false; 
644                                                                                                 }
645                                                                                         } else {
646                                                                                                 responseString.append("<b>Micro Service Model</b>:<i> " + requiredField + " is required" + HTML_ITALICS_LNBREAK);
647                                                                                                 valid = false; 
648                                                                                         }
649                                                                                 }
650                                                                         }
651                                                                 }                                                               
652                                                         } else {
653                                                                 responseString.append("<b>Micro Service Model</b>:<i> Invalid Model. The model name, " + service + 
654                                                                                 " of version, " + version + " was not found in the dictionary" + HTML_ITALICS_LNBREAK);
655                                                                 valid = false;
656                                                         }
657                                                 } else {
658                                                         responseString.append("<b>Micro Service Version</b>:<i> Micro Service Version is required" + HTML_ITALICS_LNBREAK);
659                                                         valid = false;
660                                                 }
661                                         } else {
662                                                 responseString.append("<b>Micro Service</b>:<i> Micro Service Model is required" + HTML_ITALICS_LNBREAK);
663                                                 valid = false;
664                                         }
665
666                                         if(Strings.isNullOrEmpty(policyData.getPriority())){
667                                                 responseString.append("<b>Priority</b>:<i> Priority is required" + HTML_ITALICS_LNBREAK);
668                                                 valid = false;
669                                         }
670                                 }       
671                         }
672                         if (DECISION_POLICY.equalsIgnoreCase(policyData.getPolicyType())){
673                                 if(!Strings.isNullOrEmpty(policyData.getOnapName())){
674                                         String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName());
675                                         if(!onapNameValidate.contains(SUCCESS)){
676                                                 responseString.append("OnapName:" +  onapNameValidate + HTML_ITALICS_LNBREAK);
677                                                 valid = false;
678                                         }
679                                 }else{
680                                         responseString.append("Onap Name: Onap Name Should not be empty" + HTML_ITALICS_LNBREAK);
681                                         valid = false;
682                                 }
683
684                                 if("Rainy_Day".equals(policyData.getRuleProvider())){
685                                         if(policyData.getRainyday()==null){
686                                                 responseString.append("<b> Rainy Day Parameters are Required </b><br>");
687                                                 valid = false;
688                                         }else{
689                                                 if(Strings.isNullOrEmpty(policyData.getRainyday().getServiceType())){
690                                                         responseString.append("Rainy Day <b>Service Type</b> is Required<br>");
691                                                         valid = false;
692                                                 }
693                                                 if(Strings.isNullOrEmpty(policyData.getRainyday().getVnfType())){
694                                                         responseString.append("Rainy Day <b>VNF Type</b> is Required<br>");
695                                                         valid = false;
696                                                 }                                               
697                                                 if(Strings.isNullOrEmpty(policyData.getRainyday().getBbid())){
698                                                         responseString.append("Rainy Day <b>Building Block ID</b> is Required<br>");
699                                                         valid = false;
700                                                 }
701                                                 if(Strings.isNullOrEmpty(policyData.getRainyday().getWorkstep())){
702                                                         responseString.append("Rainy Day <b>Work Step</b> is Required<br>");
703                                                         valid = false;
704                                                 }
705                                                 if(!policyData.getRainyday().getTreatmentTableChoices().isEmpty() &&
706                                                                 policyData.getRainyday().getTreatmentTableChoices() != null){
707                                                         
708                                                         for(Object treatmentMap: policyData.getRainyday().getTreatmentTableChoices()){
709                                                                 String errorCode = null;
710                                                                 String treatment = null;
711                                                                 if(treatmentMap instanceof LinkedHashMap<?, ?>){
712                                                                         
713                                                                         if(((LinkedHashMap<?, ?>) treatmentMap).containsKey("errorcode")){
714                                                                                 errorCode = ((LinkedHashMap<?, ?>) treatmentMap).get("errorcode").toString();
715                                                                         }
716                                                                         if(((LinkedHashMap<?, ?>) treatmentMap).containsKey("treatment")){
717                                                                                 treatment = ((LinkedHashMap<?, ?>) treatmentMap).get("treatment").toString();
718                                                                         }
719                                                                         
720                                                                 }
721                                                                 if(Strings.isNullOrEmpty(errorCode) && Strings.isNullOrEmpty(treatment)){
722                                                                         responseString.append("Rainy Day <b>Error Code</b> and <b>Desired Treatment</b> cannot be empty<br>");
723                                                                         valid = false;
724                                                                         break;
725                                                                 }
726                                                                 if(Strings.isNullOrEmpty(errorCode)){
727                                                                         responseString.append("Rainy Day <b>Error Code</b> is Required for each Desired Treatment<br>");
728                                                                         valid = false;
729                                                                         break;
730                                                                 }
731                                                                 if(Strings.isNullOrEmpty(treatment)){
732                                                                         responseString.append("Rainy Day <b>Desired Treatment</b> is Required for each Error Code<br>");
733                                                                         valid = false;
734                                                                         break;
735                                                                 }
736                                                         }
737                                                         
738                                                 } else {
739                                                         responseString.append("Rainy Day <b>Desired Automated Treatments</b> are Required<br>");
740                                                         valid = false;
741                                                 }
742                                         }
743                                 }
744                                 
745                                 if("GUARD_YAML".equals(policyData.getRuleProvider()) || "GUARD_BL_YAML".equals(policyData.getRuleProvider())){
746                                         if(policyData.getYamlparams()==null){
747                                                 responseString.append("<b> Guard Params are Required </b>" + HTML_ITALICS_LNBREAK);
748                                                 valid = false;
749                                         }else{
750                                                 if(Strings.isNullOrEmpty(policyData.getYamlparams().getActor())){
751                                                         responseString.append("Guard Params <b>Actor</b> is Required " + HTML_ITALICS_LNBREAK);
752                                                         valid = false;
753                                                 }
754                                                 if(Strings.isNullOrEmpty(policyData.getYamlparams().getRecipe())){
755                                                         responseString.append("Guard Params <b>Recipe</b> is Required " + HTML_ITALICS_LNBREAK);
756                                                         valid = false;
757                                                 }
758                                                 if(Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveStart())){
759                                                         responseString.append("Guard Params <b>Guard Active Start</b> is Required " + HTML_ITALICS_LNBREAK);
760                                                         valid = false;
761                                                 }
762                                                 if(Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveEnd())){
763                                                         responseString.append("Guard Params <b>Guard Active End</b> is Required " + HTML_ITALICS_LNBREAK);
764                                                         valid = false;
765                                                 }
766                                                 if("GUARD_YAML".equals(policyData.getRuleProvider())){
767                                                         if(Strings.isNullOrEmpty(policyData.getYamlparams().getLimit())){
768                                                                 responseString.append(" Guard Params <b>Limit</b> is Required " + HTML_ITALICS_LNBREAK);
769                                                                 valid = false;
770                                                         }else if(!PolicyUtils.isInteger(policyData.getYamlparams().getLimit())){
771                                                                 responseString.append(" Guard Params <b>Limit</b> Should be Integer " + HTML_ITALICS_LNBREAK);
772                                                                 valid = false;
773                                                         }
774                                                         if(Strings.isNullOrEmpty(policyData.getYamlparams().getTimeWindow())){
775                                                                 responseString.append("Guard Params <b>Time Window</b> is Required" + HTML_ITALICS_LNBREAK);
776                                                                 valid = false;
777                                                         }else if(!PolicyUtils.isInteger(policyData.getYamlparams().getTimeWindow())){
778                                                                 responseString.append(" Guard Params <b>Time Window</b> Should be Integer " + HTML_ITALICS_LNBREAK);
779                                                                 valid = false;
780                                                         }
781                                                         if(Strings.isNullOrEmpty(policyData.getYamlparams().getTimeUnits())){
782                                                                 responseString.append("Guard Params <b>Time Units</b> is Required" + HTML_ITALICS_LNBREAK);
783                                                                 valid = false;
784                                                         }
785                                                 }else if("GUARD_BL_YAML".equals(policyData.getRuleProvider())){
786                                                         if(policyData.getYamlparams().getBlackList()==null || policyData.getYamlparams().getBlackList().isEmpty()){
787                                                                 responseString.append(" Guard Params <b>BlackList</b> is Required " + HTML_ITALICS_LNBREAK);
788                                                                 valid = false;
789                                                         }else{
790                                                                 for(String blackList: policyData.getYamlparams().getBlackList()){
791                                                                         if(blackList==null || !(SUCCESS.equals(PolicyUtils.policySpecialCharValidator(blackList)))){
792                                                                                 responseString.append(" Guard Params <b>BlackList</b> Should be valid String" + HTML_ITALICS_LNBREAK);
793                                                                                 valid = false;
794                                                                                 break;
795                                                                         }
796                                                                 }
797                                                         }
798                                                 }
799                                         }
800                                 }
801                         }
802
803                         if(ACTION_POLICY.equalsIgnoreCase(policyData.getPolicyType())){
804                                 if(!Strings.isNullOrEmpty(policyData.getActionPerformer())){
805                                         String actionPerformer = PolicyUtils.policySpecialCharValidator(policyData.getActionPerformer());
806                                         if(!actionPerformer.contains(SUCCESS)){
807                                                 responseString.append("<b>ActionPerformer</b>:<i>" +  actionPerformer + HTML_ITALICS_LNBREAK);
808                                                 valid = false;
809                                         }
810                                 }else{
811                                         responseString.append("<b>ActionPerformer</b>:<i> ActionPerformer Should not be empty" + HTML_ITALICS_LNBREAK);
812                                         valid = false;
813                                 }
814         
815                                 if(!Strings.isNullOrEmpty(policyData.getActionAttributeValue())){
816                                         String actionAttribute = PolicyUtils.policySpecialCharValidator(policyData.getActionAttributeValue());
817                                         if(!actionAttribute.contains(SUCCESS)){
818                                                 responseString.append("<b>ActionAttribute</b>:<i>" +  actionAttribute + HTML_ITALICS_LNBREAK);
819                                                 valid = false;
820                                         }
821                                 }else{
822                                         responseString.append("<b>ActionAttribute</b>:<i> ActionAttribute Should not be empty" + HTML_ITALICS_LNBREAK);
823                                         valid = false;
824                                 }
825                         }
826
827                         if(CONFIG_POLICY.equals(policyData.getPolicyType())){
828                                 String value = "";
829                                 if(valid){
830                                         if(commonClassDao!=null){
831                                                 List<Object> spData = commonClassDao.getDataById(SafePolicyWarning.class, "riskType", policyData.getRiskType());
832                                                 if (!spData.isEmpty()){
833                                                         SafePolicyWarning safePolicyWarningData  = (SafePolicyWarning) spData.get(0);
834                                                         value = "<b>Message</b>:<i>" +  safePolicyWarningData.getMessage() +"</i>";
835                                                 }
836                                         }
837                                         responseString.append(SUCCESS + "@#"+ value);
838                                 }
839                         }else{
840                                 if(valid){
841                                         responseString.append(SUCCESS);
842                                 }
843                         }
844
845                         return responseString;
846                 }
847                 catch (Exception e){
848                         LOGGER.error("Exception Occured during Policy Validation" +e);
849                         return null;
850                 }
851         }
852
853         protected String emailValidation(String email, String response){
854                 String res = response;
855                 if(email != null){
856                         String validateEmail = PolicyUtils.validateEmailAddress(email.replace("\"", ""));
857                         if(!validateEmail.contains(SUCCESS)){
858                                 res  += "<b>Email</b>:<i>" +  validateEmail + HTML_ITALICS_LNBREAK;
859                         }
860                         else {
861                                 return SUCCESS;
862                         }
863                 }
864                 return res;
865         }
866
867         private MicroServiceModels getAttributeObject(String name, String version) {    
868                 MicroServiceModels workingModel = null;
869                 try{
870                         List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", name+":"+version);
871                         if(microServiceModelsData != null){
872                                 workingModel = (MicroServiceModels) microServiceModelsData.get(0);
873                         }
874                 }catch(Exception e){
875                         String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Template.  The template name, " 
876                     + name + " was not found in the dictionary: ";
877                         LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message + e);
878                         return null;
879                 }
880
881                 return workingModel;
882         }
883
884         private void pullJsonKeyPairs(JsonNode rootNode) {
885                 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
886
887                 while (fieldsIterator.hasNext()) {
888                         Map.Entry<String, JsonNode> field = fieldsIterator.next();
889                         final String key = field.getKey();
890                         final JsonNode value = field.getValue();
891                         if (value.isContainerNode() && !value.isArray()) {
892                                 pullJsonKeyPairs(value); // RECURSIVE CALL
893                         } else {
894                                 if (value.isArray()){
895                                         String newValue = StringUtils.replaceEach(value.toString(), new String[]{"[", "]", "\""}, new String[]{"", "", ""});
896                                         mapAttribute.put(key, newValue);
897                                 }else {
898                                         mapAttribute.put(key, value.toString().trim());
899                                 }
900                         }
901                 }
902         }
903         
904         private void pullModelJsonKeyPairs(JsonNode rootNode) {
905                 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
906                 
907                 while (fieldsIterator.hasNext()) {
908                         Map.Entry<String, JsonNode> field = fieldsIterator.next();
909                         final String key = field.getKey();
910                         final JsonNode value = field.getValue();
911                         
912                         if (value.isContainerNode() && !value.isArray()) {
913                                 pullModelJsonKeyPairs(value); // RECURSIVE CALL
914                         } else if (value.isArray()) {
915                                 try {
916                                         String stringValue = StringUtils.replaceEach(value.toString(), new String[]{"[", "]"}, new String[]{"",""});
917                                         ObjectMapper mapper = new ObjectMapper();
918                                         JsonNode newValue = mapper.readTree(stringValue);
919                                         pullModelJsonKeyPairs(newValue);
920                                 } catch (IOException e) {
921                                         LOGGER.info("PolicyValidation: Exception occurred while mapping string to JsonNode " + e);
922                                 }
923                         } else {
924                                 jsonRequestMap.put(key, value.toString().trim());
925                         }
926                 }
927                                 
928         }
929         
930     private JsonObject stringToJsonObject(String value) {
931         try(JsonReader jsonReader = Json.createReader(new StringReader(value))){
932             return jsonReader.readObject();
933         } catch(JsonException| IllegalStateException e){
934             LOGGER.info(XACMLErrorConstants.ERROR_DATA_ISSUE+ "Improper JSON format... may or may not cause issues in validating the policy: " + value, e);
935             return null;
936         }
937     }
938     
939     private void findRequiredFields(JsonObject json) {
940
941         for(Entry<String, JsonValue> keyMap : json.entrySet()){
942                 Object obj = keyMap.getValue();
943                 if(obj instanceof JsonObject){
944                         JsonObject jsonObj = (JsonObject)obj;
945                         for(Entry<String, JsonValue> jsonMap : jsonObj.entrySet()){
946                                 if(jsonMap.getValue().toString().contains("required-true")){
947                                         modelRequiredFieldsList.add(jsonMap.getKey());
948                                 }
949                         }
950                 } else {
951                         if(keyMap.getValue().toString().contains("required-true")){
952                                 modelRequiredFieldsList.add(keyMap.getKey());
953                         }
954                 }
955         }
956                 
957     }
958
959 }