Merge "Sonar cleanup for PolicyEngineUtils"
[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 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.util.HashMap;
24 import java.util.Iterator;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.json.JSONObject;
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.rest.adapter.ClosedLoopFaultBody;
35 import org.onap.policy.rest.adapter.ClosedLoopPMBody;
36 import org.onap.policy.rest.adapter.PolicyRestAdapter;
37 import org.onap.policy.rest.dao.CommonClassDao;
38 import org.onap.policy.rest.jpa.MicroServiceModels;
39 import org.onap.policy.rest.jpa.SafePolicyWarning;
40 import org.onap.policy.utils.PolicyUtils;
41 import org.onap.policy.xacml.api.XACMLErrorConstants;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Service;
44
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.google.common.base.Splitter;
48 import com.google.common.base.Strings;
49
50 @Service
51 public class PolicyValidation {
52         
53         private static final Logger LOGGER      = FlexLogger.getLogger(PolicyValidation.class);
54         
55         public static final String CONFIG_POLICY = "Config";
56         public static final String ACTION_POLICY = "Action";
57         public static final String DECISION_POLICY = "Decision";
58         public static final String CLOSEDLOOP_POLICY = "ClosedLoop_Fault";
59         public static final String CLOSEDLOOP_PM = "ClosedLoop_PM";
60         public static final String ENFORCER_CONFIG_POLICY= "Enforcer Config";
61         public static final String MICROSERVICES="Micro Service";
62         public static final String FIREWALL="Firewall Config";
63         public static final String HTML_ITALICS_LNBREAK = "</i><br>";
64         public static final String SUCCESS = "success";
65         public static final String EMPTY_COMPONENT_ATTR = "Component Attributes: One or more Fields in Component Attributes is Empty.";
66         
67         private static Map<String, String> mapAttribute = new HashMap<>();
68         
69         private static CommonClassDao commonClassDao;
70         
71         @Autowired
72         public PolicyValidation(CommonClassDao commonClassDao){
73                 PolicyValidation.commonClassDao = commonClassDao;
74         }
75         
76         /*
77          * This is an empty constructor
78          */
79         public PolicyValidation(){}     
80         
81         
82         public StringBuilder validatePolicy(PolicyRestAdapter policyData) throws IOException{
83                 try{
84                         boolean valid = true;
85                         StringBuilder responseString = new StringBuilder();
86                         ObjectMapper mapper = new ObjectMapper();
87                         
88                         if(policyData.getPolicyName() != null){
89                                 String policyNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getPolicyName());
90                                 if(!policyNameValidate.contains(SUCCESS)){
91                                         responseString.append("PolicyName:" +  policyNameValidate + "<br>");
92                                         valid = false;
93                                 };
94                         }else{
95                                 responseString.append( "PolicyName: PolicyName Should not be empty" + "<br>");
96                                 valid = false;
97                         }
98                         if(policyData.getPolicyDescription() != null){
99                                 String descriptionValidate = PolicyUtils.descriptionValidator(policyData.getPolicyDescription());
100                                 if(!descriptionValidate.contains(SUCCESS)){
101                                         responseString.append("Description:" +  descriptionValidate + "<br>");
102                                         valid = false;
103                                 }       
104                         }
105
106                         if(CONFIG_POLICY.equalsIgnoreCase(policyData.getPolicyType())){
107                                 if ("Base".equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_POLICY.equals(policyData.getConfigPolicyType())
108                                                 ||  CLOSEDLOOP_PM.equals(policyData.getConfigPolicyType()) || ENFORCER_CONFIG_POLICY.equals(policyData.getConfigPolicyType()) 
109                                                 || MICROSERVICES.equals(policyData.getConfigPolicyType())) {
110                                         
111                                         if(!Strings.isNullOrEmpty(policyData.getOnapName())) {
112                                                 String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName());
113                                                 if(!onapNameValidate.contains(SUCCESS)){
114                                                         responseString.append("OnapName:" +  onapNameValidate + "<br>");
115                                                         valid = false;
116                                                 }
117                                         }else{
118                                                 responseString.append("Onap Name: Onap Name Should not be empty" + "<br>");
119                                                 valid = false;
120                                         }
121                                 }
122
123                                 if(!Strings.isNullOrEmpty(policyData.getRiskType())) {
124                                         String riskTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getRiskType());
125                                         if(!riskTypeValidate.contains(SUCCESS)){
126                                                 responseString.append("RiskType:" +  riskTypeValidate + "<br>");
127                                                 valid = false;
128                                         }
129                                 }else {
130                                         responseString.append("Risk Type: Risk Type Should not be Empty" + "<br>");
131                                         valid = false;
132                                 }
133
134                                 if(!Strings.isNullOrEmpty(policyData.getRiskLevel())) {
135                                         String validateRiskLevel = PolicyUtils.policySpecialCharValidator(policyData.getRiskLevel());
136                                         if(!validateRiskLevel.contains(SUCCESS)){
137                                                 responseString.append("RiskLevel:" +  validateRiskLevel + "<br>");
138                                                 valid = false;
139                                         }
140                                 }else {
141                                         responseString.append("Risk Level: Risk Level Should not be Empty" + "<br>");
142                                         valid = false;
143                                 }
144
145                                 if(!Strings.isNullOrEmpty(policyData.getGuard())) {
146                                         String validateGuard = PolicyUtils.policySpecialCharValidator(policyData.getGuard());
147                                         if(!validateGuard.contains(SUCCESS)){
148                                                 responseString.append("Guard:" +  validateGuard + "<br>");
149                                                 valid = false;
150                                         }
151                                 }else {
152                                         responseString.append("Guard: Guard Value Should not be Empty" + "<br>");
153                                         valid = false;
154                                 }
155
156                                 if("Base".equalsIgnoreCase(policyData.getConfigPolicyType())){
157                                         if(!Strings.isNullOrEmpty(policyData.getConfigName())) {
158                                                 String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName());
159                                                 if(!configNameValidate.contains(SUCCESS)){
160                                                         responseString.append("ConfigName:" +  configNameValidate + "<br>");
161                                                         valid = false;
162                                                 }
163                                         }else{
164                                                 responseString.append("Config Name: Config Name Should not be Empty" + "<br>");
165                                                 valid = false;
166                                         }
167                                         if(!Strings.isNullOrEmpty(policyData.getConfigType())) {
168                                                 String configTypeValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigType());
169                                                 if(!configTypeValidate.contains(SUCCESS)){
170                                                         responseString.append("ConfigType:" +  configTypeValidate + "<br>");
171                                                         valid = false;
172                                                 }
173                                         }else{
174                                                 responseString.append("Config Type: Config Type Should not be Empty" + "<br>");
175                                                 valid = false;
176                                         }
177                                         if(!Strings.isNullOrEmpty(policyData.getConfigBodyData())) {
178                                                 String configBodyData = policyData.getConfigBodyData();
179                                                 String configType = policyData.getConfigType();
180                                                 if (configType != null) {
181                                                         if (configType.equals("JSON")) {
182                                                                 if (!PolicyUtils.isJSONValid(configBodyData)) {
183                                                                         responseString.append("Config Body: JSON Content is not valid" + "<br>");
184                                                                         valid = false;
185                                                                 }
186                                                         } else if (configType.equals("XML")) {
187                                                                 if (!PolicyUtils.isXMLValid(configBodyData)) {
188                                                                         responseString.append("Config Body: XML Content data is not valid" + "<br>");
189                                                                         valid = false;
190                                                                 }
191                                                         } else if (configType.equals("PROPERTIES")) {
192                                                                 if (!PolicyUtils.isPropValid(configBodyData)||configBodyData.equals("")) {
193                                                                         responseString.append("Config Body: Property data is not valid" + "<br>");
194                                                                         valid = false;
195                                                                 } 
196                                                         } else if ("OTHER".equals(configType) && ("".equals(configBodyData))) {
197                                                                 responseString.append("Config Body: Config Body Should not be Empty" + "<br>");
198                                                                 valid = false;
199                                                         }
200                                                 }
201                                         }else{
202                                                 responseString.append("Config Body: Config Body Should not be Empty" + "<br>");
203                                                 valid = false;
204                                         }
205                                 }
206
207                                 if(FIREWALL.equalsIgnoreCase(policyData.getConfigPolicyType())){
208                                         if(policyData.getConfigName() != null && !policyData.getConfigName().isEmpty()){
209                                                 String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName());
210                                                 if(!configNameValidate.contains(SUCCESS)){
211                                                         responseString.append("<b>ConfigName</b>:<i>" +  configNameValidate + HTML_ITALICS_LNBREAK);
212                                                         valid = false;
213                                                 }
214                                         }else{
215                                                 responseString.append("<b>Config Name</b>:<i> Config Name is required" + HTML_ITALICS_LNBREAK);
216                                                 valid = false;
217                                         }
218                                         if(policyData.getSecurityZone() == null || policyData.getSecurityZone().isEmpty()){
219                                                 responseString.append("<b>Security Zone</b>:<i> Security Zone is required" + HTML_ITALICS_LNBREAK);
220                                                 valid = false;
221                                         }
222                                 }
223                                 if("BRMS_Param".equalsIgnoreCase(policyData.getConfigPolicyType()) && Strings.isNullOrEmpty(policyData.getRuleName())){
224                                         responseString.append("<b>BRMS Template</b>:<i>BRMS Template is required" + HTML_ITALICS_LNBREAK);
225                                         valid = false;
226                                 }
227                                 if("BRMS_Raw".equalsIgnoreCase(policyData.getConfigPolicyType())){
228                                         if(policyData.getConfigBodyData() != null && !policyData.getConfigBodyData().isEmpty()){
229                                                 String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData());
230                                                 
231                                                 // If there are any error other than Annotations then this is not Valid
232                                                 if(message.contains("[ERR")){
233                                                         responseString.append("<b>Raw Rule Validate</b>:<i>Raw Rule has error"+ message + HTML_ITALICS_LNBREAK);
234                                                         valid = false;
235                                                 }
236                                         }else{
237                                                 responseString.append("<b>Raw Rule</b>:<i>Raw Rule is required" + HTML_ITALICS_LNBREAK);
238                                                 valid = false;
239                                         }
240                                 }
241                                 if(CLOSEDLOOP_PM.equalsIgnoreCase(policyData.getConfigPolicyType())){
242                                         try{
243                                                 if(Strings.isNullOrEmpty(policyData.getServiceTypePolicyName().get("serviceTypePolicyName").toString())){
244                                                         responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required" + HTML_ITALICS_LNBREAK);
245                                                         valid = false; 
246                                                 }
247                                                 
248                                         }catch(Exception e){
249                                             LOGGER.error("ERROR in ClosedLoop_PM PolicyName" , e);
250                                                 responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required" + HTML_ITALICS_LNBREAK);
251                                                 valid = false;
252                                         }
253
254                                         if(policyData.getJsonBody() != null){
255                                                 
256                                                 ClosedLoopPMBody pmBody = mapper.readValue(policyData.getJsonBody(), ClosedLoopPMBody.class);
257                                                 if(pmBody.getEmailAddress() != null){
258                                                         String result = emailValidation(pmBody.getEmailAddress(), responseString.toString());
259                                                         if(result != SUCCESS){
260                                                                 responseString.append(result + "<br>");
261                                                                 valid = false;
262                                                         }
263                                                 }
264                                                 if((pmBody.isGamma() || pmBody.isMcr() || pmBody.isTrinity() || pmBody.isvDNS() || pmBody.isvUSP()) != true){
265                                                         responseString.append("<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services" + HTML_ITALICS_LNBREAK);
266                                                         valid = false; 
267                                                 }
268                                                 if(pmBody.getGeoLink() != null && !pmBody.getGeoLink().isEmpty()){
269                                                         String result = PolicyUtils.policySpecialCharValidator(pmBody.getGeoLink());
270                                                         if(!result.contains(SUCCESS)){
271                                                                 responseString.append("<b>GeoLink</b>:<i>" +  result + HTML_ITALICS_LNBREAK);
272                                                                 valid = false;
273                                                         };
274                                                 }
275                                                 if(pmBody.getAttributes() != null && !pmBody.getAttributes().isEmpty()){
276                                                         for(Entry<String, String> entry : pmBody.getAttributes().entrySet()){
277                                                                 String key = entry.getKey();
278                                                                 String value = entry.getValue();
279                                                                 if(!key.contains("Message")){
280                                                                         String attributeValidate = PolicyUtils.policySpecialCharValidator(value);
281                                                                         if(!attributeValidate.contains(SUCCESS)){
282                                                                                 responseString.append("<b>Attributes</b>:<i>" +  key + " : value has spaces or invalid characters" + HTML_ITALICS_LNBREAK);
283                                                                                 valid = false;
284                                                                         };
285                                                                 }
286                                                         }       
287                                                 }
288                                         }else{
289                                                 responseString.append("<b>D2/Virtualized Services</b>:<i>Select atleast one D2/Virtualized Services" + HTML_ITALICS_LNBREAK);
290                                                 valid = false;
291                                         }
292                                 }
293                                 if(CLOSEDLOOP_POLICY.equalsIgnoreCase(policyData.getConfigPolicyType())){
294                                         if(policyData.getJsonBody() != null){
295
296                                                 // For API we need to get the conditions key from the Json request and check it before deserializing to POJO due to the enum
297                                                 if("API".equals(policyData.getApiflag())){
298                                                         JSONObject json = new JSONObject(policyData.getJsonBody());
299                                                         if(!json.isNull("conditions")){
300                                                                 String apiCondition = (String) json.get("conditions");
301                                                                 if(Strings.isNullOrEmpty(apiCondition)){
302                                                                         responseString.append("<b>Conditions</b>: <i>Select At least one Condition" + HTML_ITALICS_LNBREAK);
303                                                                         return responseString;
304                                                                 }
305                                                         } else {
306                                                                 responseString.append("<b>Conditions</b>: <i>There were no conditions provided in configBody json" + HTML_ITALICS_LNBREAK);
307                                                                 return responseString;
308                                                         }
309                                                 }
310
311                                                 ClosedLoopFaultBody faultBody = mapper.readValue(policyData.getJsonBody(), ClosedLoopFaultBody.class);
312                                                 if(faultBody.getEmailAddress() != null && !faultBody.getEmailAddress().isEmpty()){
313                                                         String result = emailValidation(faultBody.getEmailAddress(), responseString.toString());
314                                                         if(result != SUCCESS){
315                                                                 responseString.append(result+ "<br>");
316                                                                 valid = false;
317                                                         }
318                                                 }
319                                                 if((faultBody.isGamma() || faultBody.isMcr() || faultBody.isTrinity() || faultBody.isvDNS() || faultBody.isvUSP()) != true){
320                                                         responseString.append("<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services" + HTML_ITALICS_LNBREAK);
321                                                         valid = false; 
322                                                 }
323                                                 if(faultBody.getActions() == null || faultBody.getActions().isEmpty()){
324                                                         responseString.append("<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK);
325                                                         valid = false;
326                                                 }
327                                                 if(faultBody.getClosedLoopPolicyStatus() == null || faultBody.getClosedLoopPolicyStatus().isEmpty()){
328                                                         responseString.append("<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK);
329                                                         valid = false;
330                                                 }
331                                                 if(faultBody.getConditions() == null){
332                                                         responseString.append("<b>Conditions</b>: <i>Select At least one Condition" + HTML_ITALICS_LNBREAK);
333                                                         valid = false;
334                                                 }
335                                                 if(faultBody.getGeoLink() != null && !faultBody.getGeoLink().isEmpty()){
336                                                         String result = PolicyUtils.policySpecialCharWithSpaceValidator(faultBody.getGeoLink());
337                                                         if(!result.contains(SUCCESS)){
338                                                                 responseString.append("<b>GeoLink</b>:<i>" +  result + HTML_ITALICS_LNBREAK);
339                                                                 valid = false;
340                                                         }
341                                                 }
342                                                 if(faultBody.getAgingWindow() == 0){
343                                                         responseString.append("<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK);
344                                                         valid = false;
345                                                 }
346                                                 if(faultBody.getTimeInterval() == 0){
347                                                         responseString.append("<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK);
348                                                         valid = false;
349                                                 }
350                                                 if(faultBody.getRetrys() == 0){
351                                                         responseString.append("<b>Number of Retries</b>: <i>Number of Retries is required" + HTML_ITALICS_LNBREAK);
352                                                         valid = false;
353                                                 }
354                                                 if(faultBody.getTimeOutvPRO() == 0){
355                                                         responseString.append("<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK);
356                                                         valid = false;
357                                                 }
358                                                 if(faultBody.getTimeOutRuby() == 0){
359                                                         responseString.append("<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK);
360                                                         valid = false;
361                                                 }
362                                                 if(faultBody.getVnfType() == null || faultBody.getVnfType().isEmpty()){
363                                                         responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK);
364                                                         valid = false;
365                                                 }
366                                         }else{
367                                                 responseString.append("<b>D2/Virtualized Services</b>: <i>Select atleast one D2/Virtualized Services" + HTML_ITALICS_LNBREAK);
368                                                 responseString.append("<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK);
369                                                 responseString.append("<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK);
370                                                 responseString.append("<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK);
371                                                 responseString.append("<b>Conditions</b>: <i>Select Atleast one Condition" + HTML_ITALICS_LNBREAK);
372                                                 responseString.append("<b>PEP Name</b>: <i>PEP Name is required" + HTML_ITALICS_LNBREAK);
373                                                 responseString.append("<b>PEP Action</b>: <i>PEP Action is required" + HTML_ITALICS_LNBREAK);
374                                                 responseString.append("<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK);
375                                                 responseString.append("<b>Number of Retries</b>: <i>Number of Retries is required" + HTML_ITALICS_LNBREAK);
376                                                 responseString.append("<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK);
377                                                 responseString.append("<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK);
378                                                 responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK);
379                                                 valid = false; 
380                                         }
381                                 }
382
383                                 if (MICROSERVICES.equals(policyData.getConfigPolicyType())){
384                                         if(!Strings.isNullOrEmpty(policyData.getServiceType())){
385                                                 pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON());
386
387                                                 String service = null;
388                                                 String version = null;
389                                                 if (policyData.getServiceType().contains("-v")){
390                                                         service = policyData.getServiceType().split("-v")[0];
391                                                         version = policyData.getServiceType().split("-v")[1];
392                                                 }else {
393                                                         service = policyData.getServiceType();
394                                                         version = policyData.getVersion();
395                                                 }
396                                                 
397                                                 if(!Strings.isNullOrEmpty(version)) {
398                                                         MicroServiceModels returnModel = getAttributeObject(service, version);
399                                                         
400                                                         if(returnModel != null) {
401                                                                 String annotation = returnModel.getAnnotation();
402                                                                 if (!Strings.isNullOrEmpty(annotation)){ 
403                                                                         Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annotation);
404                                                                         for (Entry<String, String> rMap : rangeMap.entrySet()){
405                                                                                 if (rMap.getValue().contains("range::")){
406                                                                                         String value = mapAttribute.get(rMap.getKey().trim());
407                                                                                         String[] tempString = rMap.getValue().split("::")[1].split("-");
408                                                                                         int startNum = Integer.parseInt(tempString[0]);
409                                                                                         int endNum = Integer.parseInt(tempString[1]);
410                                                                                         String returnString = "InvalidreturnModel Range:" + rMap.getKey() + " must be between " 
411                                                                                                         + startNum + " - "  + endNum + ",";
412                                                                                         
413                                                                                         if(value != null) {
414                                                                                                 if (PolicyUtils.isInteger(value.replace("\"", ""))){
415                                                                                                         int result = Integer.parseInt(value.replace("\"", ""));
416                                                                                                         if (result < startNum || result > endNum){
417                                                                                                                 responseString.append(returnString);                                                                    
418                                                                                                                 valid = false;
419                                                                                                         }
420                                                                                                 }else {
421                                                                                                         responseString.append(returnString);
422                                                                                                         valid = false;
423                                                                                                 }
424                                                                                         } else {
425                                                                                                 responseString.append("<b>"+rMap.getKey()+"</b>:<i>" + rMap.getKey() 
426                                                                                                 + " is required for the MicroService model " + service + HTML_ITALICS_LNBREAK);
427                                                                                                 valid = false;
428                                                                                         }
429
430                                                                                 }
431                                                                         }
432                                                                 }
433                                                         } else {
434                                                                 responseString.append("<b>Micro Service Model</b>:<i> Invalid Model. The model name, " + service + 
435                                                                                 " of version, " + version + " was not found in the dictionary" + HTML_ITALICS_LNBREAK);
436                                                                 valid = false;
437                                                         }
438                                                 } else {
439                                                         responseString.append("<b>Micro Version</b>:<i> Micro Service Version is required" + HTML_ITALICS_LNBREAK);
440                                                         valid = false;
441                                                 }
442                                         } else {
443                                                 responseString.append("<b>Micro Service</b>:<i> Micro Service is required" + HTML_ITALICS_LNBREAK);
444                                                 valid = false;
445                                         }
446
447                                         if(Strings.isNullOrEmpty(policyData.getPriority())){
448                                                 responseString.append("<b>Priority</b>:<i> Priority is required" + HTML_ITALICS_LNBREAK);
449                                                 valid = false;
450                                         }
451                                 }       
452                         }
453                         if (DECISION_POLICY.equalsIgnoreCase(policyData.getPolicyType())){
454                                 if(!Strings.isNullOrEmpty(policyData.getOnapName())){
455                                         String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName());
456                                         if(!onapNameValidate.contains(SUCCESS)){
457                                                 responseString.append("OnapName:" +  onapNameValidate + "<br>");
458                                                 valid = false;
459                                         }
460                                 }else{
461                                         responseString.append("Onap Name: Onap Name Should not be empty" + "<br>");
462                                         valid = false;
463                                 }
464
465                                 if("Rainy_Day".equals(policyData.getRuleProvider())){
466                                         if(policyData.getRainyday()==null){
467                                                 responseString.append("<b> Rainy Day Parameters are Required </b><br>");
468                                                 valid = false;
469                                         }else{
470                                                 if(Strings.isNullOrEmpty(policyData.getRainyday().getServiceType())){
471                                                         responseString.append("Rainy Day <b>Service Type</b> is Required<br>");
472                                                         valid = false;
473                                                 }
474                                                 if(Strings.isNullOrEmpty(policyData.getRainyday().getVnfType())){
475                                                         responseString.append("Rainy Day <b>VNF Type</b> is Required<br>");
476                                                         valid = false;
477                                                 }                                               
478                                                 if(Strings.isNullOrEmpty(policyData.getRainyday().getBbid())){
479                                                         responseString.append("Rainy Day <b>Building Block ID</b> is Required<br>");
480                                                         valid = false;
481                                                 }
482                                                 if(Strings.isNullOrEmpty(policyData.getRainyday().getWorkstep())){
483                                                         responseString.append("Rainy Day <b>Work Step</b> is Required<br>");
484                                                         valid = false;
485                                                 }
486                                                 if(!policyData.getRainyday().getTreatmentTableChoices().isEmpty() &&
487                                                                 policyData.getRainyday().getTreatmentTableChoices() != null){
488                                                         
489                                                         for(Object treatmentMap: policyData.getRainyday().getTreatmentTableChoices()){
490                                                                 String errorCode = null;
491                                                                 String treatment = null;
492                                                                 if(treatmentMap instanceof LinkedHashMap<?, ?>){
493                                                                         
494                                                                         if(((LinkedHashMap<?, ?>) treatmentMap).containsKey("errorcode")){
495                                                                                 errorCode = ((LinkedHashMap<?, ?>) treatmentMap).get("errorcode").toString();
496                                                                         }
497                                                                         if(((LinkedHashMap<?, ?>) treatmentMap).containsKey("treatment")){
498                                                                                 treatment = ((LinkedHashMap<?, ?>) treatmentMap).get("treatment").toString();
499                                                                         }
500                                                                         
501                                                                 }
502                                                                 if(Strings.isNullOrEmpty(errorCode) && Strings.isNullOrEmpty(treatment)){
503                                                                         responseString.append("Rainy Day <b>Error Code</b> and <b>Desired Treatment</b> cannot be empty<br>");
504                                                                         valid = false;
505                                                                         break;
506                                                                 }
507                                                                 if(Strings.isNullOrEmpty(errorCode)){
508                                                                         responseString.append("Rainy Day <b>Error Code</b> is Required for each Desired Treatment<br>");
509                                                                         valid = false;
510                                                                         break;
511                                                                 }
512                                                                 if(Strings.isNullOrEmpty(treatment)){
513                                                                         responseString.append("Rainy Day <b>Desired Treatment</b> is Required for each Error Code<br>");
514                                                                         valid = false;
515                                                                         break;
516                                                                 }
517                                                         }
518                                                         
519                                                 } else {
520                                                         responseString.append("Rainy Day <b>Desired Automated Treatments</b> are Required<br>");
521                                                         valid = false;
522                                                 }
523                                         }
524                                 }
525                                 
526                                 if("GUARD_YAML".equals(policyData.getRuleProvider()) || "GUARD_BL_YAML".equals(policyData.getRuleProvider())){
527                                         if(policyData.getYamlparams()==null){
528                                                 responseString.append("<b> Guard Params are Required </b>" + "<br>");
529                                                 valid = false;
530                                         }else{
531                                                 if(Strings.isNullOrEmpty(policyData.getYamlparams().getActor())){
532                                                         responseString.append("Guard Params <b>Actor</b> is Required " + "<br>");
533                                                         valid = false;
534                                                 }
535                                                 if(Strings.isNullOrEmpty(policyData.getYamlparams().getRecipe())){
536                                                         responseString.append("Guard Params <b>Recipe</b> is Required " + "<br>");
537                                                         valid = false;
538                                                 }
539                                                 if(Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveStart())){
540                                                         responseString.append("Guard Params <b>Guard Active Start</b> is Required " + "<br>");
541                                                         valid = false;
542                                                 }
543                                                 if(Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveEnd())){
544                                                         responseString.append("Guard Params <b>Guard Active End</b> is Required " + "<br>");
545                                                         valid = false;
546                                                 }
547                                                 if("GUARD_YAML".equals(policyData.getRuleProvider())){
548                                                         if(Strings.isNullOrEmpty(policyData.getYamlparams().getLimit())){
549                                                                 responseString.append(" Guard Params <b>Limit</b> is Required " + "<br>");
550                                                                 valid = false;
551                                                         }else if(!PolicyUtils.isInteger(policyData.getYamlparams().getLimit())){
552                                                                 responseString.append(" Guard Params <b>Limit</b> Should be Integer " + "<br>");
553                                                                 valid = false;
554                                                         }
555                                                         if(Strings.isNullOrEmpty(policyData.getYamlparams().getTimeWindow())){
556                                                                 responseString.append("Guard Params <b>Time Window</b> is Required" + "<br>");
557                                                                 valid = false;
558                                                         }else if(!PolicyUtils.isInteger(policyData.getYamlparams().getTimeWindow())){
559                                                                 responseString.append(" Guard Params <b>Time Window</b> Should be Integer " + "<br>");
560                                                                 valid = false;
561                                                         }
562                                                         if(Strings.isNullOrEmpty(policyData.getYamlparams().getTimeUnits())){
563                                                                 responseString.append("Guard Params <b>Time Units</b> is Required" + "<br>");
564                                                                 valid = false;
565                                                         }
566                                                 }else if("GUARD_BL_YAML".equals(policyData.getRuleProvider())){
567                                                         if(policyData.getYamlparams().getBlackList()==null || policyData.getYamlparams().getBlackList().isEmpty()){
568                                                                 responseString.append(" Guard Params <b>BlackList</b> is Required " + "<br>");
569                                                                 valid = false;
570                                                         }else{
571                                                                 for(String blackList: policyData.getYamlparams().getBlackList()){
572                                                                         if(blackList==null || !(SUCCESS.equals(PolicyUtils.policySpecialCharValidator(blackList)))){
573                                                                                 responseString.append(" Guard Params <b>BlackList</b> Should be valid String" + "<br>");
574                                                                                 valid = false;
575                                                                                 break;
576                                                                         }
577                                                                 }
578                                                         }
579                                                 }
580                                         }
581                                 }
582                         }
583
584                         if(ACTION_POLICY.equalsIgnoreCase(policyData.getPolicyType())){
585                                 if(!Strings.isNullOrEmpty(policyData.getActionPerformer())){
586                                         String actionPerformer = PolicyUtils.policySpecialCharValidator(policyData.getActionPerformer());
587                                         if(!actionPerformer.contains(SUCCESS)){
588                                                 responseString.append("ActionPerformer:" +  actionPerformer + "<br>");
589                                                 valid = false;
590                                         }
591                                 }else{
592                                         responseString.append("ActionPerformer: ActionPerformer Should not be empty" + "<br>");
593                                         valid = false;
594                                 }
595                                 if(policyData.getAttributes() != null){
596                                         for(Object attribute : policyData.getAttributes()){
597                                                 if(attribute instanceof LinkedHashMap<?, ?>){
598                                                         try{
599                                                                 //This is for validation check if the value exists or not
600                                                                 String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
601                                                                 String value =  ((LinkedHashMap<?, ?>) attribute).get("value").toString();
602                                                                 if("".equals(key) || "".equals(value)){
603                                                                         responseString.append(EMPTY_COMPONENT_ATTR + "<br>");
604                                                                         valid = false;
605                                                                         break;  
606                                                                 }
607                                                         }catch(Exception e){
608                                                                 LOGGER.error("This is a Policy Validation check" +e);
609                                                                 responseString.append(EMPTY_COMPONENT_ATTR + "<br>");
610                                                                 valid = false;
611                                                                 break;
612                                                         }
613                                                 }
614                                         }
615                                 }else{
616                                         responseString.append(EMPTY_COMPONENT_ATTR + "<br>");
617                                         valid = false;
618                                 }
619                                 if(!Strings.isNullOrEmpty(policyData.getActionAttributeValue())){
620                                         String actionAttribute = PolicyUtils.policySpecialCharValidator(policyData.getActionAttributeValue());
621                                         if(!actionAttribute.contains(SUCCESS)){
622                                                 responseString.append("ActionAttribute:" +  actionAttribute + "<br>");
623                                                 valid = false;
624                                         };
625                                 }else{
626                                         responseString.append("ActionAttribute: ActionAttribute Should not be empty" + "<br>");
627                                         valid = false;
628                                 }
629                                 
630                                 if(!policyData.getRuleAlgorithmschoices().isEmpty()){
631                                         for(Object attribute : policyData.getRuleAlgorithmschoices()){
632                                                 if(attribute instanceof LinkedHashMap<?, ?>){
633                                                         try{
634                                                                 String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString();
635                                                                 String key = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField1").toString();
636                                                                 String rule = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmCombo").toString();
637                                                                 String value = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2").toString();
638
639                                                                 if(Strings.isNullOrEmpty(label) || Strings.isNullOrEmpty(key) || Strings.isNullOrEmpty(rule) || Strings.isNullOrEmpty(value)){
640                                                                         responseString.append("Rule Algorithms: One or more Fields in Rule Algorithms is Empty." + "<br>");
641                                                                         valid = false;
642                                                                 }
643                                                         }catch(Exception e){
644                                                                 LOGGER.error("This is a Policy Validation check" +e);
645                                                                 responseString.append("Rule Algorithms: One or more Fields in Rule Algorithms is Empty." + "<br>");
646                                                                 valid = false;
647                                                                 break;
648                                                         }
649                                                 }
650                                         }
651                                 }
652                         }
653
654                         if(CONFIG_POLICY.equals(policyData.getPolicyType())){
655                                 String value = "";
656                                 if(valid){
657                                         if(commonClassDao!=null){
658                                                 List<Object> spData = commonClassDao.getDataById(SafePolicyWarning.class, "riskType", policyData.getRiskType());
659                                                 if (!spData.isEmpty()){
660                                                         SafePolicyWarning safePolicyWarningData  = (SafePolicyWarning) spData.get(0);
661                                                         value = "Message:" +  safePolicyWarningData.getMessage();
662                                                 }
663                                         }
664                                         responseString.append(SUCCESS + "@#"+ value);
665                                 }
666                         }else{
667                                 if(valid){
668                                         responseString.append(SUCCESS);
669                                 }
670                         }
671
672                         return responseString;
673                 }
674                 catch (Exception e){
675                         LOGGER.error("Exception Occured during Policy Validation" +e);
676                         return null;
677                 }
678         }
679
680         protected String emailValidation(String email, String response){
681                 if(email != null){
682                         String validateEmail = PolicyUtils.validateEmailAddress(email.replace("\"", ""));
683                         if(!validateEmail.contains(SUCCESS)){
684                                 response += "<b>Email</b>:<i>" +  validateEmail + HTML_ITALICS_LNBREAK;
685                         }else{
686                                 return SUCCESS;
687                         }
688                 }
689                 return response;
690         }
691
692         private MicroServiceModels getAttributeObject(String name, String version) {    
693                 MicroServiceModels workingModel = null;
694                 try{
695                         List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", name+":"+version);
696                         if(microServiceModelsData != null){
697                                 workingModel = (MicroServiceModels) microServiceModelsData.get(0);
698                         }
699                 }catch(Exception e){
700                         String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Template.  The template name, " 
701                     + name + " was not found in the dictionary: ";
702                         LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message + e);
703                         return null;
704                 }
705
706                 return workingModel;
707         }
708
709         private void pullJsonKeyPairs(JsonNode rootNode) {
710                 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
711
712                 while (fieldsIterator.hasNext()) {
713                         Map.Entry<String, JsonNode> field = fieldsIterator.next();
714                         final String key = field.getKey();
715                         final JsonNode value = field.getValue();
716                         if (value.isContainerNode() && !value.isArray()) {
717                                 pullJsonKeyPairs(value); // RECURSIVE CALL
718                         } else {
719                                 if (value.isArray()){
720                                         String newValue = StringUtils.replaceEach(value.toString(), new String[]{"[", "]", "\""}, new String[]{"", "", ""});
721                                         mapAttribute.put(key, newValue);
722                                 }else {
723                                         mapAttribute.put(key, value.toString().trim());
724                                 }
725                         }
726                 }
727         }
728
729 }