da54b05bd03f252124193f297bc553ecd88a0cde
[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
21 package org.onap.policy.rest.util;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.common.base.Splitter;
27 import com.google.common.base.Strings;
28
29 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
30 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
31
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.StringReader;
35 import java.nio.charset.StandardCharsets;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.Iterator;
40 import java.util.LinkedHashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Map.Entry;
44 import java.util.Set;
45 import javax.json.Json;
46 import javax.json.JsonException;
47 import javax.json.JsonObject;
48 import javax.json.JsonReader;
49 import javax.json.JsonValue;
50
51 import org.apache.commons.lang3.StringEscapeUtils;
52 import org.apache.commons.lang3.StringUtils;
53 import org.json.JSONArray;
54 import org.json.JSONObject;
55 import org.onap.policy.common.logging.flexlogger.FlexLogger;
56 import org.onap.policy.common.logging.flexlogger.Logger;
57 import org.onap.policy.rest.adapter.ClosedLoopFaultBody;
58 import org.onap.policy.rest.adapter.ClosedLoopPMBody;
59 import org.onap.policy.rest.adapter.PolicyRestAdapter;
60 import org.onap.policy.rest.dao.CommonClassDao;
61 import org.onap.policy.rest.jpa.MicroServiceModels;
62 import org.onap.policy.rest.jpa.OptimizationModels;
63 import org.onap.policy.rest.jpa.SafePolicyWarning;
64 import org.onap.policy.utils.PolicyUtils;
65 import org.onap.policy.xacml.api.XACMLErrorConstants;
66 import org.onap.policy.xacml.util.XACMLPolicyScanner;
67 import org.springframework.beans.factory.annotation.Autowired;
68 import org.springframework.stereotype.Service;
69
70
71 @Service
72 public class PolicyValidation {
73
74     private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidation.class);
75
76     public static final String CONFIG_POLICY = "Config";
77     public static final String ACTION_POLICY = "Action";
78     public static final String DECISION_POLICY = "Decision";
79     public static final String DECISION_POLICY_MS = "Decision_MS";
80     public static final String CLOSEDLOOP_POLICY = "ClosedLoop_Fault";
81     public static final String CLOSEDLOOP_PM = "ClosedLoop_PM";
82     public static final String ENFORCER_CONFIG_POLICY = "Enforcer Config";
83     public static final String MICROSERVICES = "Micro Service";
84     public static final String FIREWALL = "Firewall Config";
85     public static final String OPTIMIZATION="Optimization";
86     public static final String BRMSPARAM = "BRMS_Param";
87     public static final String BRMSRAW = "BRMS_Raw";
88     public static final String HTML_ITALICS_LNBREAK = "</i><br>";
89     public static final String SUCCESS = "success";
90     public static final String EMPTY_COMPONENT_ATTR =
91             "Component Attributes: One or more Fields in Component Attributes is Empty.";
92     public static final String ISREQUIRED = " is required";
93     public static final String SPACESINVALIDCHARS = " : value has spaces or invalid characters</i><br>";
94     private static final String REQUIRED_ATTRIBUTE = "required-true";
95     private static final String DECISION_MS_MODEL = "MicroService_Model";
96     private static final String RAW = "Raw";
97
98     private static Map<String, String> mapAttribute = new HashMap<>();
99     private static Map<String, String> jsonRequestMap = new HashMap<>();
100     private static List<String> modelRequiredFieldsList = new ArrayList<>();
101     private Set<String> allReqTrueKeys = new HashSet<>();
102     private Set<String> allOptReqTrueKeys = new HashSet<>();
103
104     private static CommonClassDao commonClassDao;
105
106     @Autowired
107     public PolicyValidation(CommonClassDao commonClassDao) {
108         PolicyValidation.commonClassDao = commonClassDao;
109     }
110
111     /*
112      * This is an empty constructor
113      */
114     public PolicyValidation() {
115         // Empty constructor
116     }
117
118
119     /**
120      * Validate policy.
121      *
122      * @param policyData the policy data
123      * @return the string builder
124      * @throws IOException Signals that an I/O exception has occurred.
125      */
126     public StringBuilder validatePolicy(PolicyRestAdapter policyData) throws IOException {
127         try {
128             boolean valid = true;
129             StringBuilder responseString = new StringBuilder();
130             ObjectMapper mapper = new ObjectMapper();
131
132             if (policyData.getPolicyName() != null) {
133                 String policyNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getPolicyName());
134                 if (!policyNameValidate.contains(SUCCESS)) {
135                     responseString.append("<b>PolicyName</b>:<i>" + policyNameValidate + HTML_ITALICS_LNBREAK);
136                     valid = false;
137                 }
138             } else {
139                 responseString.append("<b>PolicyName</b>: PolicyName Should not be empty" + HTML_ITALICS_LNBREAK);
140                 valid = false;
141             }
142             if (policyData.getPolicyDescription() != null) {
143                 String descriptionValidate = PolicyUtils.descriptionValidator(policyData.getPolicyDescription());
144                 if (!descriptionValidate.contains(SUCCESS)) {
145                     responseString.append("<b>Description</b>:<i>" + descriptionValidate + HTML_ITALICS_LNBREAK);
146                     valid = false;
147                 }
148             }
149
150             if (!"API".equals(policyData.getApiflag()) && policyData.getAttributes() != null
151                     && !policyData.getAttributes().isEmpty()) {
152                 for (Object attribute : policyData.getAttributes()) {
153                     if (attribute instanceof LinkedHashMap<?, ?>) {
154                         String value = null;
155                         String key = null;
156                         if (((LinkedHashMap<?, ?>) attribute).get("key") != null) {
157                             key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
158                             if (!PolicyUtils.policySpecialCharWithDashValidator(key).contains(SUCCESS)) {
159                                 responseString.append("<b>Attributes or Component Attributes</b>:<i>" + value
160                                         + SPACESINVALIDCHARS);
161                                 valid = false;
162                             }
163                         } else {
164                             if (CONFIG_POLICY.equals(policyData.getPolicyType())) {
165                                 if ("Base".equals(policyData.getConfigPolicyType())) {
166                                     responseString
167                                     .append("<b>Attributes</b>:<i> has one missing Attribute key</i><br>");
168                                 }
169                                 if (BRMSPARAM.equals(policyData.getConfigPolicyType())
170                                         || BRMSRAW.equals(policyData.getConfigPolicyType())) {
171                                     responseString
172                                     .append("<b>Rule Attributes</b>:<i> has one missing Attribute key</i><br>");
173                                 }
174                             } else {
175                                 responseString.append(
176                                         "<b>Component Attributes</b>:<i> has one missing Component Attribute key</i><br>");
177                             }
178                             valid = false;
179                         }
180                         if (((LinkedHashMap<?, ?>) attribute).get("value") != null) {
181                             value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
182                             if (!PolicyUtils.policySpecialCharWithDashValidator(value).contains(SUCCESS)) {
183                                 if (CONFIG_POLICY.equals(policyData.getPolicyType())) {
184                                     if ("Base".equals(policyData.getConfigPolicyType())) {
185                                         responseString.append("<b>Attributes</b>:<i>" + value
186                                                 + SPACESINVALIDCHARS);
187                                     }
188                                     if (BRMSPARAM.equals(policyData.getConfigPolicyType())
189                                             || BRMSRAW.equals(policyData.getConfigPolicyType())) {
190                                         responseString.append("<b>Rule Attributes</b>:<i>" + value
191                                                 + SPACESINVALIDCHARS);
192                                     }
193                                 } else {
194                                     responseString.append("<b>Component Attributes</b>:<i>" + value
195                                             + SPACESINVALIDCHARS);
196                                 }
197                                 valid = false;
198                             }
199                         } else {
200                             if (CONFIG_POLICY.equals(policyData.getPolicyType())) {
201                                 if ("Base".equals(policyData.getConfigPolicyType())) {
202                                     responseString
203                                     .append("<b>Attributes</b>:<i> has one missing Attribute value</i><br>");
204                                 }
205                                 if (BRMSPARAM.equals(policyData.getConfigPolicyType())
206                                         || BRMSRAW.equals(policyData.getConfigPolicyType())) {
207                                     responseString.append(
208                                             "<b>Rule Attributes</b>:<i> has one missing Attribute value</i><br>");
209                                 }
210                             } else {
211                                 responseString.append(
212                                         "<b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>");
213                             }
214                             valid = false;
215                         }
216                     }
217                 }
218             }
219
220             // Decision Policy Attributes Validation
221             if (!"API".equals(policyData.getApiflag()) && policyData.getSettings() != null
222                     && !policyData.getSettings().isEmpty()) {
223                 for (Object attribute : policyData.getAttributes()) {
224                     if (attribute instanceof LinkedHashMap<?, ?>) {
225                         String value = null;
226                         if (((LinkedHashMap<?, ?>) attribute).get("key") == null) {
227                             responseString
228                             .append("<b>Settings Attributes</b>:<i> has one missing Attribute key</i><br>");
229                             valid = false;
230                         }
231                         if (((LinkedHashMap<?, ?>) attribute).get("value") != null) {
232                             value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
233                             if (!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)) {
234                                 responseString.append("<b>Settings Attributes</b>:<i>" + value
235                                         + SPACESINVALIDCHARS);
236                                 valid = false;
237                             }
238                         } else {
239                             responseString
240                             .append("<b>Settings Attributes</b>:<i> has one missing Attribute Value</i><br>");
241                             valid = false;
242                         }
243                     }
244                 }
245             }
246
247             if (!"API".equals(policyData.getApiflag()) && policyData.getRuleAlgorithmschoices() != null
248                     && !policyData.getRuleAlgorithmschoices().isEmpty()) {
249                 for (Object attribute : policyData.getRuleAlgorithmschoices()) {
250                     if (attribute instanceof LinkedHashMap<?, ?>) {
251                         String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString();
252                         if (((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField1") == null) {
253                             responseString.append(
254                                     "<b>Rule Algorithms</b>:<i>" + label + " : Field 1 value is not selected</i><br>");
255                             valid = false;
256                         }
257                         if (((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmCombo") == null) {
258                             responseString.append(
259                                     "<b>Rule Algorithms</b>:<i>" + label + " : Field 2 value is not selected</i><br>");
260                             valid = false;
261                         }
262                         if (((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2") != null) {
263                             String value =
264                                     ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2").toString();
265                             if (!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)) {
266                                 responseString.append("<b>Rule Algorithms</b>:<i>" + label
267                                         + " : Field 3 value has special characters</i><br>");
268                                 valid = false;
269                             }
270                         } else {
271                             responseString
272                             .append("<b>Rule Algorithms</b>:<i>" + label + " : Field 3 value is empty</i><br>");
273                             valid = false;
274                         }
275                     }
276                 }
277             }
278
279             if (CONFIG_POLICY.equalsIgnoreCase(policyData.getPolicyType())) {
280                 if ("Base".equals(policyData.getConfigPolicyType())
281                         || CLOSEDLOOP_POLICY.equals(policyData.getConfigPolicyType())
282                         || CLOSEDLOOP_PM.equals(policyData.getConfigPolicyType())
283                         || ENFORCER_CONFIG_POLICY.equals(policyData.getConfigPolicyType())
284                         || MICROSERVICES.equals(policyData.getConfigPolicyType())
285                         || OPTIMIZATION.equals(policyData.getConfigPolicyType())) {
286
287                     if (!Strings.isNullOrEmpty(policyData.getOnapName())) {
288                         String onapNameValidate = PolicyUtils.policySpecialCharWithDashValidator(policyData.getOnapName());
289                         if (!onapNameValidate.contains(SUCCESS)) {
290                             responseString.append("<b>OnapName</b>:<i>" + onapNameValidate + HTML_ITALICS_LNBREAK);
291                             valid = false;
292                         }
293                     } else {
294                         responseString.append("<b>Onap Name</b>: Onap Name Should not be empty" + HTML_ITALICS_LNBREAK);
295                         valid = false;
296                     }
297                 }
298
299                 if (!Strings.isNullOrEmpty(policyData.getRiskType())) {
300                     String riskTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getRiskType());
301                     if (!riskTypeValidate.contains(SUCCESS)) {
302                         responseString.append("<b>RiskType</b>:<i>" + riskTypeValidate + HTML_ITALICS_LNBREAK);
303                         valid = false;
304                     }
305                 } else {
306                     responseString.append("<b>RiskType</b>: Risk Type Should not be Empty" + HTML_ITALICS_LNBREAK);
307                     valid = false;
308                 }
309
310                 if (!Strings.isNullOrEmpty(policyData.getRiskLevel())) {
311                     String validateRiskLevel = PolicyUtils.policySpecialCharValidator(policyData.getRiskLevel());
312                     if (!validateRiskLevel.contains(SUCCESS)) {
313                         responseString.append("<b>RiskLevel</b>:<i>" + validateRiskLevel + HTML_ITALICS_LNBREAK);
314                         valid = false;
315                     }
316                 } else {
317                     responseString.append("<b>RiskLevel</b>: Risk Level Should not be Empty" + HTML_ITALICS_LNBREAK);
318                     valid = false;
319                 }
320
321                 if (!Strings.isNullOrEmpty(policyData.getGuard())) {
322                     String validateGuard = PolicyUtils.policySpecialCharValidator(policyData.getGuard());
323                     if (!validateGuard.contains(SUCCESS)) {
324                         responseString.append("<b>Guard</b>:<i>" + validateGuard + HTML_ITALICS_LNBREAK);
325                         valid = false;
326                     }
327                 } else {
328                     responseString.append("<b>Guard</b>: Guard Value Should not be Empty" + HTML_ITALICS_LNBREAK);
329                     valid = false;
330                 }
331
332                 // Validate Config Base Policy Data
333                 if ("Base".equalsIgnoreCase(policyData.getConfigPolicyType())) {
334                     if (!Strings.isNullOrEmpty(policyData.getConfigName())) {
335                         String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName());
336                         if (!configNameValidate.contains(SUCCESS)) {
337                             responseString.append("ConfigName:" + configNameValidate + HTML_ITALICS_LNBREAK);
338                             valid = false;
339                         }
340                     } else {
341                         responseString.append("Config Name: Config Name Should not be Empty" + HTML_ITALICS_LNBREAK);
342                         valid = false;
343                     }
344                     if (!Strings.isNullOrEmpty(policyData.getConfigType())) {
345                         String configTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigType());
346                         if (!configTypeValidate.contains(SUCCESS)) {
347                             responseString.append("ConfigType:" + configTypeValidate + HTML_ITALICS_LNBREAK);
348                             valid = false;
349                         }
350                     } else {
351                         responseString.append("Config Type: Config Type Should not be Empty" + HTML_ITALICS_LNBREAK);
352                         valid = false;
353                     }
354                     if (!Strings.isNullOrEmpty(policyData.getConfigBodyData())) {
355                         String configBodyData = policyData.getConfigBodyData();
356                         String configType = policyData.getConfigType();
357                         if (configType != null) {
358                             if ("JSON".equals(configType)) {
359                                 if (!PolicyUtils.isJSONValid(configBodyData)) {
360                                     responseString
361                                     .append("Config Body: JSON Content is not valid" + HTML_ITALICS_LNBREAK);
362                                     valid = false;
363                                 }
364                             } else if ("XML".equals(configType)) {
365                                 if (!PolicyUtils.isXMLValid(configBodyData)) {
366                                     responseString.append(
367                                             "Config Body: XML Content data is not valid" + HTML_ITALICS_LNBREAK);
368                                     valid = false;
369                                 }
370                             } else if ("PROPERTIES".equals(configType)) {
371                                 if (!PolicyUtils.isPropValid(configBodyData) || "".equals(configBodyData)) {
372                                     responseString
373                                     .append("Config Body: Property data is not valid" + HTML_ITALICS_LNBREAK);
374                                     valid = false;
375                                 }
376                             } else if ("OTHER".equals(configType) && ("".equals(configBodyData))) {
377                                 responseString
378                                 .append("Config Body: Config Body Should not be Empty" + HTML_ITALICS_LNBREAK);
379                                 valid = false;
380                             }
381                         }
382                     } else {
383                         responseString.append("Config Body: Config Body Should not be Empty" + HTML_ITALICS_LNBREAK);
384                         valid = false;
385                     }
386                 }
387                 // Validate Config Firewall Policy Data
388                 if (FIREWALL.equalsIgnoreCase(policyData.getConfigPolicyType())) {
389                     if (policyData.getConfigName() != null && !policyData.getConfigName().isEmpty()) {
390                         String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName());
391                         if (!configNameValidate.contains(SUCCESS)) {
392                             responseString.append("<b>ConfigName</b>:<i>" + configNameValidate + HTML_ITALICS_LNBREAK);
393                             valid = false;
394                         }
395                     } else {
396                         responseString.append("<b>Config Name</b>:<i> Config Name is required" + HTML_ITALICS_LNBREAK);
397                         valid = false;
398                     }
399                     if (policyData.getSecurityZone() == null || policyData.getSecurityZone().isEmpty()) {
400                         responseString
401                         .append("<b>Security Zone</b>:<i> Security Zone is required" + HTML_ITALICS_LNBREAK);
402                         valid = false;
403                     }
404                 }
405                 // Validate BRMS_Param Policy Data
406                 if (BRMSPARAM.equalsIgnoreCase(policyData.getConfigPolicyType())
407                         && Strings.isNullOrEmpty(policyData.getRuleName())) {
408                     responseString.append("<b>BRMS Template</b>:<i>BRMS Template is required" + HTML_ITALICS_LNBREAK);
409                     valid = false;
410                 }
411                 // Validate BRMS_Raw Policy Data
412                 if (BRMSRAW.equalsIgnoreCase(policyData.getConfigPolicyType())) {
413                     if (policyData.getConfigBodyData() != null && !policyData.getConfigBodyData().isEmpty()) {
414                         String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData());
415
416                         // If there are any error other than Annotations then this is not Valid
417                         if (message.contains("[ERR")) {
418                             responseString.append(
419                                     "<b>Raw Rule Validate</b>:<i>Raw Rule has error" + message + HTML_ITALICS_LNBREAK);
420                             valid = false;
421                         }
422                     } else {
423                         responseString.append("<b>Raw Rule</b>:<i>Raw Rule is required" + HTML_ITALICS_LNBREAK);
424                         valid = false;
425                     }
426                 }
427                 // Validate ClosedLoop_PM Policy Data
428                 if (CLOSEDLOOP_PM.equalsIgnoreCase(policyData.getConfigPolicyType())) {
429                     try {
430                         if (Strings.isNullOrEmpty(
431                                 policyData.getServiceTypePolicyName().get("serviceTypePolicyName").toString())) {
432                             responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required"
433                                     + HTML_ITALICS_LNBREAK);
434                             valid = false;
435                         }
436
437                     } catch (Exception e) {
438                         LOGGER.error("ERROR in ClosedLoop_PM PolicyName", e);
439                         responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required"
440                                 + HTML_ITALICS_LNBREAK);
441                         valid = false;
442                     }
443
444                     if (policyData.getJsonBody() != null) {
445
446                         ClosedLoopPMBody pmBody = mapper.readValue(policyData.getJsonBody(), ClosedLoopPMBody.class);
447                         if (pmBody.getEmailAddress() != null) {
448                             String result = emailValidation(pmBody.getEmailAddress(), responseString.toString());
449                             if (result != SUCCESS) {
450                                 responseString.append(result + HTML_ITALICS_LNBREAK);
451                                 valid = false;
452                             }
453                         }
454                         if ((pmBody.isGamma() || pmBody.isMcr() || pmBody.isTrinity() || pmBody.isvDNS()
455                                 || pmBody.isvUSP()) != true) {
456                             responseString
457                             .append("<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services"
458                                     + HTML_ITALICS_LNBREAK);
459                             valid = false;
460                         }
461                         if (pmBody.getGeoLink() != null && !pmBody.getGeoLink().isEmpty()) {
462                             String result = PolicyUtils.policySpecialCharValidator(pmBody.getGeoLink());
463                             if (!result.contains(SUCCESS)) {
464                                 responseString.append("<b>GeoLink</b>:<i>" + result + HTML_ITALICS_LNBREAK);
465                                 valid = false;
466                             }
467                         }
468                         if (pmBody.getAttributes() != null && !pmBody.getAttributes().isEmpty()) {
469                             for (Entry<String, String> entry : pmBody.getAttributes().entrySet()) {
470                                 String key = entry.getKey();
471                                 String value = entry.getValue();
472                                 if (!key.contains("Message")) {
473                                     String attributeValidate = PolicyUtils.policySpecialCharValidator(value);
474                                     if (!attributeValidate.contains(SUCCESS)) {
475                                         responseString.append("<b>Attributes</b>:<i>" + key
476                                                 + " : value has spaces or invalid characters" + HTML_ITALICS_LNBREAK);
477                                         valid = false;
478                                     }
479                                 }
480                             }
481                         }
482                     } else {
483                         responseString
484                         .append("<b>D2/Virtualized Services</b>:<i>Select atleast one D2/Virtualized Services"
485                                 + HTML_ITALICS_LNBREAK);
486                         valid = false;
487                     }
488                 }
489                 if (CLOSEDLOOP_POLICY.equalsIgnoreCase(policyData.getConfigPolicyType())) {
490                     if (policyData.getJsonBody() != null) {
491
492                         // For API we need to get the conditions key from the Json request and check it before
493                         // deserializing to POJO due to the enum
494                         if ("API".equals(policyData.getApiflag())) {
495                             JSONObject json = new JSONObject(policyData.getJsonBody());
496                             if (!json.isNull("conditions")) {
497                                 String apiCondition = (String) json.get("conditions");
498                                 if (Strings.isNullOrEmpty(apiCondition)) {
499                                     responseString.append("<b>Conditions</b>: <i>Select At least one Condition"
500                                             + HTML_ITALICS_LNBREAK);
501                                     return responseString;
502                                 }
503                             } else {
504                                 responseString
505                                 .append("<b>Conditions</b>: <i>There were no conditions provided in configBody json"
506                                         + HTML_ITALICS_LNBREAK);
507                                 return responseString;
508                             }
509                         } else {
510                             if (policyData.getTrapDatas().getTrap1() != null) {
511                                 if (policyData.getClearTimeOut() == null) {
512                                     responseString.append(
513                                             "<b>Trigger Clear TimeOut</b>: <i>Trigger Clear TimeOut is required when atleast One Trigger Signature is enabled</i><br>");
514                                     valid = false;
515                                 }
516                                 if (policyData.getTrapMaxAge() == null) {
517                                     responseString.append(
518                                             "<b>Trap Max Age</b>: <i>Trap Max Age is required when atleast One Trigger Signature is enabled</i><br>");
519                                     valid = false;
520                                 }
521                             }
522                             if (policyData.getFaultDatas().getTrap1() != null
523                                     && policyData.getVerificationclearTimeOut() == null) {
524                                 responseString.append(
525                                         "<b>Fault Clear TimeOut</b>: <i>Fault Clear TimeOut is required when atleast One Fault Signature is enabled</i><br>");
526                                 valid = false;
527                             }
528                         }
529
530                         ClosedLoopFaultBody faultBody =
531                                 mapper.readValue(policyData.getJsonBody(), ClosedLoopFaultBody.class);
532                         if (faultBody.getEmailAddress() != null && !faultBody.getEmailAddress().isEmpty()) {
533                             String result = emailValidation(faultBody.getEmailAddress(), responseString.toString());
534                             if (!SUCCESS.equals(result)) {
535                                 responseString.append(result + HTML_ITALICS_LNBREAK);
536                                 valid = false;
537                             }
538                         }
539                         if (!(faultBody.isGamma() || faultBody.isMcr() || faultBody.isTrinity() || faultBody.isvDNS()
540                                 || faultBody.isvUSP())) {
541                             responseString
542                             .append("<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services"
543                                     + HTML_ITALICS_LNBREAK);
544                             valid = false;
545                         }
546                         if (faultBody.getActions() == null || faultBody.getActions().isEmpty()) {
547                             responseString
548                             .append("<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK);
549                             valid = false;
550                         }
551                         if (faultBody.getClosedLoopPolicyStatus() == null
552                                 || faultBody.getClosedLoopPolicyStatus().isEmpty()) {
553                             responseString.append(
554                                     "<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK);
555                             valid = false;
556                         }
557                         if (faultBody.getConditions() == null) {
558                             responseString.append(
559                                     "<b>Conditions</b>: <i>Select At least one Condition" + HTML_ITALICS_LNBREAK);
560                             valid = false;
561                         }
562                         if (faultBody.getGeoLink() != null && !faultBody.getGeoLink().isEmpty()) {
563                             String result = PolicyUtils.policySpecialCharWithSpaceValidator(faultBody.getGeoLink());
564                             if (!result.contains(SUCCESS)) {
565                                 responseString.append("<b>GeoLink</b>:<i>" + result + HTML_ITALICS_LNBREAK);
566                                 valid = false;
567                             }
568                         }
569                         if (faultBody.getAgingWindow() == 0) {
570                             responseString
571                             .append("<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK);
572                             valid = false;
573                         }
574                         if (faultBody.getTimeInterval() == 0) {
575                             responseString.append(
576                                     "<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK);
577                             valid = false;
578                         }
579                         if (faultBody.getRetrys() == 0) {
580                             responseString.append("<b>Number of Retries</b>: <i>Number of Retries is required"
581                                     + HTML_ITALICS_LNBREAK);
582                             valid = false;
583                         }
584                         if (faultBody.getTimeOutvPRO() == 0) {
585                             responseString.append(
586                                     "<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK);
587                             valid = false;
588                         }
589                         if (faultBody.getTimeOutRuby() == 0) {
590                             responseString
591                             .append("<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK);
592                             valid = false;
593                         }
594                         if (faultBody.getVnfType() == null || faultBody.getVnfType().isEmpty()) {
595                             responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK);
596                             valid = false;
597                         }
598                     } else {
599                         responseString
600                         .append("<b>D2/Virtualized Services</b>: <i>Select atleast one D2/Virtualized Services"
601                                 + HTML_ITALICS_LNBREAK);
602                         responseString
603                         .append("<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK);
604                         responseString
605                         .append("<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK);
606                         responseString
607                         .append("<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK);
608                         responseString
609                         .append("<b>Conditions</b>: <i>Select Atleast one Condition" + HTML_ITALICS_LNBREAK);
610                         responseString.append("<b>PEP Name</b>: <i>PEP Name is required" + HTML_ITALICS_LNBREAK);
611                         responseString.append("<b>PEP Action</b>: <i>PEP Action is required" + HTML_ITALICS_LNBREAK);
612                         responseString
613                         .append("<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK);
614                         responseString.append(
615                                 "<b>Number of Retries</b>: <i>Number of Retries is required" + HTML_ITALICS_LNBREAK);
616                         responseString
617                         .append("<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK);
618                         responseString.append("<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK);
619                         responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK);
620                         valid = false;
621                     }
622                 }
623                 
624                 // Validate MicroService Policy Data
625                 if (MICROSERVICES.equals(policyData.getConfigPolicyType())) {
626                     boolean tmpValid = validateMsModel(policyData, responseString);
627                     if (!tmpValid) {
628                         valid = false;
629                     }
630                 }
631
632                 // Validate Optimization Policy Data
633                 if (OPTIMIZATION.equals(policyData.getConfigPolicyType())){
634                     boolean tmpValid = validateOptimization(policyData, responseString);
635                     if (!tmpValid) {
636                         valid = false;
637                     }
638                 }
639             }
640
641             if ((DECISION_POLICY.equalsIgnoreCase(policyData.getPolicyType()))
642                     || (DECISION_POLICY_MS.equalsIgnoreCase(policyData.getPolicyType()))) {
643                 if (!RAW.equalsIgnoreCase(policyData.getRuleProvider())) {
644                     if (!Strings.isNullOrEmpty(policyData.getOnapName())) {
645                         String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName());
646                         if (!onapNameValidate.contains(SUCCESS)) {
647                             responseString.append("OnapName:" + onapNameValidate + HTML_ITALICS_LNBREAK);
648                             valid = false;
649                         }
650                     } else {
651                         responseString.append("Onap Name: Onap Name Should not be empty" + HTML_ITALICS_LNBREAK);
652                         valid = false;
653                     }
654                 }
655                 if (RAW.equalsIgnoreCase(policyData.getRuleProvider())) {
656                     Object policy = XACMLPolicyScanner.readPolicy(new ByteArrayInputStream(StringEscapeUtils.unescapeXml(policyData.getRawXacmlPolicy()).getBytes(StandardCharsets.UTF_8)));
657                     if (!(policy instanceof PolicySetType || policy instanceof PolicyType)) {
658                         responseString.append("Raw XACML: The XACML Content is not valid" + HTML_ITALICS_LNBREAK);
659                         valid = false;
660                     }
661                 }
662
663                 if (DECISION_MS_MODEL.equals(policyData.getRuleProvider())) {
664                     LOGGER.info("Validating Decision MS Policy - ");
665                     boolean tmpValid = validateMsModel(policyData, responseString);
666                     if (!tmpValid) {
667                         valid = false;
668                     }
669                 }
670
671                 if ("Rainy_Day".equals(policyData.getRuleProvider())) {
672                     if (policyData.getRainyday() == null) {
673                         responseString.append("<b> Rainy Day Parameters are Required </b><br>");
674                         valid = false;
675                     } else {
676                         if (Strings.isNullOrEmpty(policyData.getRainyday().getServiceType())) {
677                             responseString.append("Rainy Day <b>Service Type</b> is Required<br>");
678                             valid = false;
679                         }
680                         if (Strings.isNullOrEmpty(policyData.getRainyday().getVnfType())) {
681                             responseString.append("Rainy Day <b>VNF Type</b> is Required<br>");
682                             valid = false;
683                         }
684                         if (Strings.isNullOrEmpty(policyData.getRainyday().getBbid())) {
685                             responseString.append("Rainy Day <b>Building Block ID</b> is Required<br>");
686                             valid = false;
687                         }
688                         if (Strings.isNullOrEmpty(policyData.getRainyday().getWorkstep())) {
689                             responseString.append("Rainy Day <b>Work Step</b> is Required<br>");
690                             valid = false;
691                         }
692                         if (!policyData.getRainyday().getTreatmentTableChoices().isEmpty()
693                                 && policyData.getRainyday().getTreatmentTableChoices() != null) {
694
695                             for (Object treatmentMap : policyData.getRainyday().getTreatmentTableChoices()) {
696                                 String errorCode = null;
697                                 String treatment = null;
698                                 if (treatmentMap instanceof LinkedHashMap<?, ?>) {
699
700                                     if (((LinkedHashMap<?, ?>) treatmentMap).containsKey("errorcode")) {
701                                         errorCode = ((LinkedHashMap<?, ?>) treatmentMap).get("errorcode").toString();
702                                     }
703                                     if (((LinkedHashMap<?, ?>) treatmentMap).containsKey("treatment")) {
704                                         treatment = ((LinkedHashMap<?, ?>) treatmentMap).get("treatment").toString();
705                                     }
706
707                                 }
708                                 if (Strings.isNullOrEmpty(errorCode) && Strings.isNullOrEmpty(treatment)) {
709                                     responseString.append(
710                                             "Rainy Day <b>Error Code</b> and <b>Desired Treatment</b> cannot be empty<br>");
711                                     valid = false;
712                                     break;
713                                 }
714                                 if (Strings.isNullOrEmpty(errorCode)) {
715                                     responseString.append(
716                                             "Rainy Day <b>Error Code</b> is Required for each Desired Treatment<br>");
717                                     valid = false;
718                                     break;
719                                 }
720                                 if (Strings.isNullOrEmpty(treatment)) {
721                                     responseString.append(
722                                             "Rainy Day <b>Desired Treatment</b> is Required for each Error Code<br>");
723                                     valid = false;
724                                     break;
725                                 }
726                             }
727
728                         } else {
729                             responseString.append("Rainy Day <b>Desired Automated Treatments</b> are Required<br>");
730                             valid = false;
731                         }
732                     }
733                 }
734
735                 if ("GUARD_YAML".equals(policyData.getRuleProvider())
736                         || "GUARD_BL_YAML".equals(policyData.getRuleProvider())
737                         || "GUARD_MIN_MAX".equals(policyData.getRuleProvider())) {
738                     if (policyData.getYamlparams() == null) {
739                         responseString.append("<b> Guard Params are Required </b>" + HTML_ITALICS_LNBREAK);
740                         valid = false;
741                     } else {
742                         if (Strings.isNullOrEmpty(policyData.getYamlparams().getActor())) {
743                             responseString.append("Guard Params <b>Actor</b> is Required " + HTML_ITALICS_LNBREAK);
744                             valid = false;
745                         }
746                         if (Strings.isNullOrEmpty(policyData.getYamlparams().getRecipe())) {
747                             responseString.append("Guard Params <b>Recipe</b> is Required " + HTML_ITALICS_LNBREAK);
748                             valid = false;
749                         }
750                         if (Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveStart())) {
751                             responseString.append(
752                                     "Guard Params <b>Guard Active Start</b> is Required " + HTML_ITALICS_LNBREAK);
753                             valid = false;
754                         }
755                         if (Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveEnd())) {
756                             responseString
757                             .append("Guard Params <b>Guard Active End</b> is Required " + HTML_ITALICS_LNBREAK);
758                             valid = false;
759                         }
760                         if ("GUARD_YAML".equals(policyData.getRuleProvider())) {
761                             if (Strings.isNullOrEmpty(policyData.getYamlparams().getLimit())) {
762                                 responseString.append(" Guard Params <b>Limit</b> is Required " + HTML_ITALICS_LNBREAK);
763                                 valid = false;
764                             } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getLimit())) {
765                                 responseString
766                                 .append(" Guard Params <b>Limit</b> Should be Integer " + HTML_ITALICS_LNBREAK);
767                                 valid = false;
768                             }
769                             if (Strings.isNullOrEmpty(policyData.getYamlparams().getTimeWindow())) {
770                                 responseString
771                                 .append("Guard Params <b>Time Window</b> is Required" + HTML_ITALICS_LNBREAK);
772                                 valid = false;
773                             } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getTimeWindow())) {
774                                 responseString.append(
775                                         " Guard Params <b>Time Window</b> Should be Integer " + HTML_ITALICS_LNBREAK);
776                                 valid = false;
777                             }
778                             if (Strings.isNullOrEmpty(policyData.getYamlparams().getTimeUnits())) {
779                                 responseString
780                                 .append("Guard Params <b>Time Units</b> is Required" + HTML_ITALICS_LNBREAK);
781                                 valid = false;
782                             }
783                         } else if ("GUARD_MIN_MAX".equals(policyData.getRuleProvider())) {
784                             if (Strings.isNullOrEmpty(policyData.getYamlparams().getMin())) {
785                                 responseString.append(" Guard Params <b>Min</b> is Required " + HTML_ITALICS_LNBREAK);
786                                 valid = false;
787                             } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getMin())) {
788                                 responseString
789                                 .append(" Guard Params <b>Min</b> Should be Integer " + HTML_ITALICS_LNBREAK);
790                                 valid = false;
791                             }
792                             if (Strings.isNullOrEmpty(policyData.getYamlparams().getMax())) {
793                                 responseString.append(" Guard Params <b>Max</b> is Required " + HTML_ITALICS_LNBREAK);
794                                 valid = false;
795                             } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getMax())) {
796                                 responseString
797                                 .append(" Guard Params <b>Max</b> Should be Integer " + HTML_ITALICS_LNBREAK);
798                                 valid = false;
799                             }
800                         } else if ("GUARD_BL_YAML".equals(policyData.getRuleProvider())
801                                 && "Use Manual Entry".equals(policyData.getBlackListEntryType())) {
802
803                             if (policyData.getYamlparams().getBlackList() == null
804                                     || policyData.getYamlparams().getBlackList().isEmpty()) {
805                                 responseString
806                                 .append(" Guard Params <b>BlackList</b> is Required " + HTML_ITALICS_LNBREAK);
807                                 valid = false;
808                             } else {
809                                 for (String blackList : policyData.getYamlparams().getBlackList()) {
810                                     if (blackList == null
811                                             || !(SUCCESS.equals(PolicyUtils.policySpecialCharValidator(blackList)))) {
812                                         responseString.append(" Guard Params <b>BlackList</b> Should be valid String"
813                                                 + HTML_ITALICS_LNBREAK);
814                                         valid = false;
815                                         break;
816                                     }
817                                 }
818                             }
819                         }
820                     }
821                 }
822             }
823
824             if (ACTION_POLICY.equalsIgnoreCase(policyData.getPolicyType())) {
825                 if (!Strings.isNullOrEmpty(policyData.getActionPerformer())) {
826                     String actionPerformer = PolicyUtils.policySpecialCharValidator(policyData.getActionPerformer());
827                     if (!actionPerformer.contains(SUCCESS)) {
828                         responseString.append("<b>ActionPerformer</b>:<i>" + actionPerformer + HTML_ITALICS_LNBREAK);
829                         valid = false;
830                     }
831                 } else {
832                     responseString.append(
833                             "<b>ActionPerformer</b>:<i> ActionPerformer Should not be empty" + HTML_ITALICS_LNBREAK);
834                     valid = false;
835                 }
836
837                 if (!Strings.isNullOrEmpty(policyData.getActionAttributeValue())) {
838                     String actionAttribute =
839                             PolicyUtils.policySpecialCharValidator(policyData.getActionAttributeValue());
840                     if (!actionAttribute.contains(SUCCESS)) {
841                         responseString.append("<b>ActionAttribute</b>:<i>" + actionAttribute + HTML_ITALICS_LNBREAK);
842                         valid = false;
843                     }
844                 } else {
845                     responseString.append(
846                             "<b>ActionAttribute</b>:<i> ActionAttribute Should not be empty" + HTML_ITALICS_LNBREAK);
847                     valid = false;
848                 }
849             }
850
851             if (CONFIG_POLICY.equals(policyData.getPolicyType())) {
852                 String value = "";
853                 if (valid) {
854                     if (commonClassDao != null) {
855                         List<Object> spData = commonClassDao.getDataById(SafePolicyWarning.class, "riskType",
856                                 policyData.getRiskType());
857                         if (!spData.isEmpty()) {
858                             SafePolicyWarning safePolicyWarningData = (SafePolicyWarning) spData.get(0);
859                             value = "<b>Message</b>:<i>" + safePolicyWarningData.getMessage() + "</i>";
860                         }
861                     }
862                     responseString.append(SUCCESS + "@#" + value);
863                 }
864             } else {
865                 if (valid) {
866                     responseString.append(SUCCESS);
867                 }
868             }
869
870             return responseString;
871         } catch (Exception e) {
872             LOGGER.error("Exception Occured during Policy Validation" + e);
873             return null;
874         }
875     }
876
877     protected String emailValidation(String email, String response) {
878         String res = response;
879         if (email != null) {
880             String validateEmail = PolicyUtils.validateEmailAddress(email.replace("\"", ""));
881             if (!validateEmail.contains(SUCCESS)) {
882                 res += "<b>Email</b>:<i>" + validateEmail + HTML_ITALICS_LNBREAK;
883             } else {
884                 return SUCCESS;
885             }
886         }
887         return res;
888     }
889
890     private MicroServiceModels getAttributeObject(String name, String version) {
891         MicroServiceModels workingModel = null;
892         try {
893             List<Object> microServiceModelsData =
894                     commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", name + ":" + version);
895             if (microServiceModelsData != null) {
896                 workingModel = (MicroServiceModels) microServiceModelsData.get(0);
897             }
898         } catch (Exception e) {
899             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Template.  The template name, " + name
900                     + " was not found in the dictionary: ";
901             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message + e);
902             return null;
903         }
904
905         return workingModel;
906     }
907
908     private OptimizationModels getOptimizationModelData(String name, String version) {  
909         OptimizationModels workingModel = null;
910         try{
911             List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, "modelName:version", name+":"+version);
912             if(optimizationModelsData != null){
913                 workingModel = (OptimizationModels) optimizationModelsData.get(0);
914             }
915         }catch(Exception e){
916             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Template.  The template name, " 
917                     + name + " was not found in the dictionary: ";
918             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message + e);
919             return null;
920         }
921
922         return workingModel;
923     }
924
925     private void pullJsonKeyPairs(JsonNode rootNode) {
926         Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
927
928         while (fieldsIterator.hasNext()) {
929             Map.Entry<String, JsonNode> field = fieldsIterator.next();
930             final String key = field.getKey();
931             final JsonNode value = field.getValue();
932             if (value.isContainerNode() && !value.isArray()) {
933                 pullJsonKeyPairs(value); // RECURSIVE CALL
934             } else {
935                 if (value.isArray()) {
936                     String newValue = StringUtils.replaceEach(value.toString(), new String[] {"[", "]", "\""},
937                             new String[] {"", "", ""});
938                     mapAttribute.put(key, newValue);
939                 } else {
940                     mapAttribute.put(key, value.toString().trim());
941                 }
942             }
943         }
944     }
945
946     private void pullModelJsonKeyPairs(JsonNode rootNode) {
947         Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
948
949         while (fieldsIterator.hasNext()) {
950             Map.Entry<String, JsonNode> field = fieldsIterator.next();
951             final String key = field.getKey();
952             final JsonNode value = field.getValue();
953
954             if (value.isContainerNode() && !value.isArray()) {
955                 jsonRequestMap.put(key.trim(), value.toString().trim());
956                 pullModelJsonKeyPairs(value); // RECURSIVE CALL
957             } else if (value.isArray()) {
958                 try {
959                     String valueStr = value.toString();
960                     String stringValue = valueStr.substring(valueStr.indexOf('[') + 1, valueStr.lastIndexOf(']'));
961                     
962                     if (stringValue.isEmpty()) {
963                         stringValue = "{\"test\":\"test\"}";
964                     }
965                     
966                     ObjectMapper mapper = new ObjectMapper();
967                     JsonNode newValue = mapper.readTree(stringValue);
968                     jsonRequestMap.put(key.trim(), value.toString().trim());
969                     pullModelJsonKeyPairs(newValue);
970                 } catch (IOException e) {
971                     LOGGER.info("PolicyValidation: Exception occurred while mapping string to JsonNode " + e);
972                 }
973             } else {
974                 jsonRequestMap.put(key.trim(), value.toString().trim());
975             }
976         }
977
978     }
979
980     private JsonObject stringToJsonObject(String value) {
981         try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
982             return jsonReader.readObject();
983         } catch (JsonException | IllegalStateException e) {
984             LOGGER.info(
985                     XACMLErrorConstants.ERROR_DATA_ISSUE
986                     + "Improper JSON format... may or may not cause issues in validating the policy: " + value,
987                     e);
988             return null;
989         }
990     }
991
992     private void findRequiredFields(JsonObject json) {
993         if (json == null) {
994             return;
995         }
996         for (Entry<String, JsonValue> keyMap : json.entrySet()) {
997             Object obj = keyMap.getValue();
998             String key = keyMap.getKey();
999             if (obj instanceof JsonObject) {
1000                 if (allReqTrueKeys.contains(key)) {
1001                     JsonObject jsonObj = (JsonObject) obj;
1002                     // only check fields in obj if obj itself is required.
1003                     for (Entry<String, JsonValue> jsonMap : jsonObj.entrySet()) {
1004                         if (jsonMap.getValue().toString().contains(REQUIRED_ATTRIBUTE)) {
1005                             modelRequiredFieldsList.add(jsonMap.getKey().trim());
1006                         }
1007                     }
1008                 }
1009             } else if (keyMap.getValue().toString().contains(REQUIRED_ATTRIBUTE)) {
1010                 modelRequiredFieldsList.add(key.trim());
1011
1012             }
1013         }
1014
1015     }
1016
1017     // call this method to start the recursive
1018     private Set<String> getAllKeys(JSONObject json) {
1019         return getAllKeys(json, new HashSet<>());
1020     }
1021
1022     private Set<String> getAllKeys(JSONArray arr) {
1023         return getAllKeys(arr, new HashSet<>());
1024     }
1025
1026     private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
1027         for (int i = 0; i < arr.length(); i++) {
1028             Object obj = arr.get(i);
1029             if (obj instanceof JSONObject) {
1030                 keys.addAll(getAllKeys(arr.getJSONObject(i)));
1031             }
1032             if (obj instanceof JSONArray) {
1033                 keys.addAll(getAllKeys(arr.getJSONArray(i)));
1034             }
1035         }
1036
1037         return keys;
1038     }
1039
1040     // this method returns a set of keys with "required-true" defined in their value.
1041     private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
1042         for (String key : json.keySet()) {
1043             Object obj = json.get(key);
1044             if (obj instanceof String && ((String) obj).contains(REQUIRED_ATTRIBUTE)) {
1045                 LOGGER.debug("key : " + key);
1046                 LOGGER.debug("obj : " + obj);
1047                 allReqTrueKeys.add(key);    //For MicroService policies
1048                 allOptReqTrueKeys.add(key); //For Optimization policies
1049                 
1050                 // get the type from value and add that one also
1051                 String type = StringUtils.substringBefore((String) obj, ":");
1052                 if (!StringUtils.isBlank(type) && !StringUtils.containsAny(type.toLowerCase(), MSModelUtils.STRING,
1053                         MSModelUtils.INTEGER, MSModelUtils.LIST, MSModelUtils.MAP, "java", "boolean")) {
1054                     allReqTrueKeys.add(type);
1055                 }
1056             }
1057             if (obj instanceof JSONObject) {
1058                 keys.addAll(getAllKeys(json.getJSONObject(key)));
1059             }
1060             if (obj instanceof JSONArray) {
1061                 keys.addAll(getAllKeys(json.getJSONArray(key)));
1062             }
1063         }
1064
1065         return keys;
1066     }
1067
1068     private boolean validateMsModel(PolicyRestAdapter policyData, StringBuilder responseString)
1069             throws JsonProcessingException {
1070         boolean valid = true;
1071         if (!Strings.isNullOrEmpty(policyData.getServiceType())) {
1072
1073             modelRequiredFieldsList.clear();
1074             pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON());
1075
1076             String service;
1077             String version;
1078             if (policyData.getServiceType().contains("-v")) {
1079                 service = policyData.getServiceType().split("-v")[0];
1080                 version = policyData.getServiceType().split("-v")[1];
1081             } else {
1082                 service = policyData.getServiceType();
1083                 version = policyData.getVersion();
1084             }
1085
1086             if (!Strings.isNullOrEmpty(version)) {
1087                 MicroServiceModels returnModel = getAttributeObject(service, version);
1088
1089                 if (returnModel != null) {
1090
1091                     String annotation = returnModel.getAnnotation();
1092                     String refAttributes = returnModel.getRefAttributes();
1093                     String subAttributes = returnModel.getSubAttributes();
1094                     String modelAttributes = returnModel.getAttributes();
1095
1096                     if (!Strings.isNullOrEmpty(annotation)) {
1097                         Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annotation);
1098                         for (Entry<String, String> raMap : rangeMap.entrySet()) {
1099                             if (raMap.getValue().contains("range::")) {
1100                                 String value = mapAttribute.get(raMap.getKey().trim());
1101                                 String[] tempString = raMap.getValue().split("::")[1].split("-");
1102                                 int startNum = Integer.parseInt(tempString[0]);
1103                                 int endNum = Integer.parseInt(tempString[1]);
1104                                 String returnString = "InvalidreturnModel Range:" + raMap.getKey() + " must be between "
1105                                         + startNum + " - " + endNum + ",";
1106
1107                                 if (value != null) {
1108                                     if (PolicyUtils.isInteger(value.replace("\"", ""))) {
1109                                         int result = Integer.parseInt(value.replace("\"", ""));
1110                                         if (result < startNum || result > endNum) {
1111                                             responseString.append(returnString);
1112                                             valid = false;
1113                                         }
1114                                     } else {
1115                                         responseString.append(returnString);
1116                                         valid = false;
1117                                     }
1118                                 } else {
1119                                     responseString.append("<b>" + raMap.getKey() + "</b>:<i>" + raMap.getKey()
1120                                     + " is required for the MicroService model " + service
1121                                     + HTML_ITALICS_LNBREAK);
1122                                     valid = false;
1123                                 }
1124
1125                             }
1126                         }
1127                     } else if (!DECISION_MS_MODEL.equals(policyData.getRuleProvider())) {
1128                         // Validate for configName, location, uuid, and policyScope if no annotations exist for this
1129                         // model
1130                         if (Strings.isNullOrEmpty(policyData.getLocation())) {
1131                             responseString.append("<b>Micro Service Model</b>:<i> location is required for this model"
1132                                     + HTML_ITALICS_LNBREAK);
1133                             valid = false;
1134                         }
1135
1136                         if (Strings.isNullOrEmpty(policyData.getConfigName())) {
1137                             responseString.append("<b>Micro Service Model</b>:<i> configName is required for this model"
1138                                     + HTML_ITALICS_LNBREAK);
1139                             valid = false;
1140                         }
1141
1142                         if (Strings.isNullOrEmpty(policyData.getUuid())) {
1143                             responseString.append("<b>Micro Service Model</b>:<i> uuid is required for this model"
1144                                     + HTML_ITALICS_LNBREAK);
1145                             valid = false;
1146                         }
1147                         
1148                         if (Strings.isNullOrEmpty(policyData.getPolicyScope())) {
1149                             responseString
1150                             .append("<b>Micro Service Model</b>:<i> policyScope is required for this model"
1151                                     + HTML_ITALICS_LNBREAK);
1152                             valid = false;
1153                         }
1154                     }
1155
1156                     // If request comes from the API we need to validate required fields in the Micro Service Model
1157                     // GUI request are already validated from the SDK-APP
1158                     if ("API".equals(policyData.getApiflag())) {
1159                         // first , get the complete set of required fields
1160                         populateReqFieldSet(new String[] {refAttributes, modelAttributes}, subAttributes);
1161
1162                         // ignore req fields in which parent is not reqd
1163                         populateRequiredFields(new String[] {refAttributes, modelAttributes}, subAttributes,
1164                                 modelAttributes);
1165
1166                         if (modelRequiredFieldsList != null && !modelRequiredFieldsList.isEmpty()) {
1167                             // create jsonRequestMap with all json keys and values from request
1168                             JsonNode rootNode = (JsonNode) policyData.getPolicyJSON();
1169                             jsonRequestMap.clear();
1170                             pullModelJsonKeyPairs(rootNode);
1171
1172                             // validate if the requiredFields are in the request
1173                             for (String requiredField : modelRequiredFieldsList) {
1174                                 if (jsonRequestMap.containsKey(requiredField)) {
1175                                     String value = jsonRequestMap.get(requiredField);
1176                                     if (StringUtils.isBlank(value) || "\"\"".equals(value)) {
1177                                         responseString.append("<b>Micro Service Model</b>:<i> " + requiredField
1178                                                 + ISREQUIRED + HTML_ITALICS_LNBREAK);
1179                                         valid = false;
1180                                     }
1181                                 } else {
1182                                     responseString.append("<b>Micro Service Model</b>:<i> " + requiredField
1183                                             + ISREQUIRED + HTML_ITALICS_LNBREAK);
1184                                     valid = false;
1185                                 }
1186                             }
1187                         }
1188                     }
1189                 } else {
1190                     responseString.append("<b>Micro Service Model</b>:<i> Invalid Model. The model name, " + service
1191                             + " of version, " + version + " was not found in the dictionary" + HTML_ITALICS_LNBREAK);
1192                     valid = false;
1193                 }
1194             } else {
1195                 responseString.append(
1196                         "<b>Micro Service Version</b>:<i> Micro Service Version is required" + HTML_ITALICS_LNBREAK);
1197                 valid = false;
1198             }
1199         } else {
1200             responseString.append("<b>Micro Service</b>:<i> Micro Service Model is required" + HTML_ITALICS_LNBREAK);
1201             valid = false;
1202         }
1203
1204         if (Strings.isNullOrEmpty(policyData.getPriority())
1205                 && !DECISION_MS_MODEL.equals(policyData.getRuleProvider())) {
1206             responseString.append("<b>Priority</b>:<i> Priority is required" + HTML_ITALICS_LNBREAK);
1207         }
1208
1209
1210         return valid;
1211     }
1212     
1213     private boolean validateOptimization(PolicyRestAdapter policyData, StringBuilder responseString) {
1214         boolean valid = true;
1215         
1216         // Checks for required policy data in request
1217         if (Strings.isNullOrEmpty(policyData.getServiceType())) {
1218             responseString.append(
1219                     "<b>Optimization Service</b>:<i> Optimization policy data is missing or invalid Json." 
1220                             + HTML_ITALICS_LNBREAK);
1221             return false;
1222         }
1223         
1224         modelRequiredFieldsList.clear();
1225         pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON());
1226         
1227         // parse the service and version from the policy data
1228         String service;
1229         String version;
1230         if (policyData.getServiceType().contains("-v")) {
1231             service = policyData.getServiceType().split("-v")[0];
1232             version = policyData.getServiceType().split("-v")[1];
1233         } else {
1234             service = policyData.getServiceType();
1235             version = policyData.getVersion();
1236         }
1237         
1238         // Checks for required version in policy data
1239         if (Strings.isNullOrEmpty(version)) {
1240             responseString.append(
1241                     "<b>Optimization Service Version</b>:<i> Optimization Service Version is required" 
1242                             + HTML_ITALICS_LNBREAK);
1243             return false;
1244         }
1245             
1246         OptimizationModels returnModel = getOptimizationModelData(service, version);
1247         
1248         // Checks if valid model exists in the database
1249         if (returnModel == null) {
1250             responseString.append("<b>Optimization Service Model</b>:<i> Invalid Model. The model name, " 
1251                     + service + " of version, " + version 
1252                     + " was not found in the dictionary" + HTML_ITALICS_LNBREAK);
1253             return false;
1254         }
1255
1256         String annotation = returnModel.getAnnotation();
1257         String refAttributes = returnModel.getRefattributes();
1258         String subAttributes = returnModel.getSubattributes();
1259         String modelAttributes = returnModel.getAttributes();
1260
1261         if (!Strings.isNullOrEmpty(annotation)) {
1262             Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=")
1263                     .split(annotation);
1264             for (Entry<String, String> rMap : rangeMap.entrySet()) {
1265                 if (rMap.getValue().contains("range::")) {
1266                     String value = mapAttribute.get(rMap.getKey().trim());
1267                     String[] tempString = rMap.getValue().split("::")[1].split("-");
1268                     int startNum = Integer.parseInt(tempString[0]);
1269                     int endNum = Integer.parseInt(tempString[1]);
1270                     String returnString = "InvalidreturnModel Range:" + rMap.getKey()
1271                     + " must be between " + startNum + " - " + endNum + ",";
1272
1273                     if (value != null) {
1274                         if (PolicyUtils.isInteger(value.replace("\"", ""))) {
1275                             int result = Integer.parseInt(value.replace("\"", ""));
1276                             if (result < startNum || result > endNum) {
1277                                 responseString.append(returnString);
1278                                 valid = false;
1279                             }
1280                         } else {
1281                             responseString.append(returnString);
1282                             valid = false;
1283                         }
1284                     } else {
1285                         responseString.append("<b>" + rMap.getKey() + "</b>:<i>" + rMap.getKey()
1286                         + " is required for the Optimization model " + service
1287                         + HTML_ITALICS_LNBREAK);
1288                         valid = false;
1289                     }
1290
1291                 }
1292             }
1293         }
1294
1295         // If request comes from the API we need to validate required fields in the Micro Service Modelvalid
1296         // GUI request are already validated from the SDK-APP
1297         if ("API".equals(policyData.getApiflag())) {
1298             // first , get the complete set of required fields
1299             populateReqFieldSet(new String[] {refAttributes, modelAttributes}, subAttributes);
1300
1301             modelRequiredFieldsList.addAll(allOptReqTrueKeys);
1302             
1303             if (modelRequiredFieldsList != null && !modelRequiredFieldsList.isEmpty()) {
1304                 
1305                 // create jsonRequestMap with all json keys and values from request
1306                 JsonNode rootNode = (JsonNode) policyData.getPolicyJSON();
1307                 jsonRequestMap.clear();
1308                 pullModelJsonKeyPairs(rootNode);
1309
1310                 // validate if the requiredFields are in the request
1311                 for (String requiredField : modelRequiredFieldsList) {
1312                     if (jsonRequestMap.containsKey(requiredField)) {
1313                         String value = jsonRequestMap.get(requiredField);
1314                         if (StringUtils.isBlank(value) || "\"\"".equals(value)) {
1315                             responseString.append("<b>Optimization Service Model</b>:<i> " 
1316                                     + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK);
1317                             valid = false;
1318                         }
1319                     } else {
1320                         responseString.append("<b>Optimization Service Model</b>:<i> " 
1321                                 + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK);
1322                         valid = false;
1323                     }
1324                 }
1325             }
1326         }
1327
1328         if (Strings.isNullOrEmpty(policyData.getPriority())) {
1329             responseString.append("<b>Priority</b>:<i> Priority is required" + HTML_ITALICS_LNBREAK);
1330             valid = false;
1331         }
1332
1333         return valid;
1334     }
1335
1336     private void populateRequiredFields(String[] attrArr, String subAttributes, String modelAttributes)
1337             throws JsonProcessingException {
1338         // get list of required fields from the ref_Attributes of the Model
1339         for (String attributes : attrArr) {
1340             if (!StringUtils.isBlank(attributes)) {
1341                 Map<String, String> attributesMap = null;
1342                 if (",".equals(attributes.substring(attributes.length() - 1))) {
1343                     String attributesString = attributes.substring(0, attributes.length() - 1);
1344                     attributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributesString);
1345                 } else if (!StringUtils.isBlank(modelAttributes)) {
1346                     attributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes);
1347                 } else {
1348                     continue;
1349                 }
1350                 String json = new ObjectMapper().writeValueAsString(attributesMap);
1351                 findRequiredFields(stringToJsonObject(json));
1352             }
1353
1354         }
1355
1356         // get list of required fields from the sub_Attributes of the Model
1357         if (!StringUtils.isBlank(subAttributes)) {
1358             JsonObject subAttributesJson = stringToJsonObject(subAttributes);
1359             findRequiredFields(subAttributesJson);
1360         }
1361
1362     }
1363
1364     private void populateReqFieldSet(String[] attrArr, String subAttributes) {
1365         allReqTrueKeys.clear();
1366         JSONObject jsonSub = new JSONObject(subAttributes);
1367         // Get all keys with "required-true" defined in their value from subAttribute
1368         getAllKeys(jsonSub);
1369
1370
1371         // parse refAttrbutes
1372         for (String attr : attrArr) {
1373             if (attr != null) {
1374                 String[] referAarray = attr.split(",");
1375                 String[] element = null;
1376                 for (int i = 0; i < referAarray.length; i++) {
1377                     element = referAarray[i].split("=");
1378                     if (element.length > 1 && element[1].contains(REQUIRED_ATTRIBUTE)) {
1379                         allReqTrueKeys.add(element[0]);
1380                     }
1381                 }
1382             }
1383         }
1384     }
1385
1386
1387 }