1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
|
/*-
* ============LICENSE_START=======================================================
* ECOMP Policy Engine
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.policy.controller;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.json.Json;
import javax.json.JsonReader;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.lang.StringUtils;
import org.dom4j.util.XMLErrorHandler;
import org.json.JSONObject;
import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
import org.openecomp.policy.common.logging.flexlogger.Logger;
import org.openecomp.policy.rest.adapter.ClosedLoopFaultBody;
import org.openecomp.policy.rest.adapter.ClosedLoopPMBody;
import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
import org.openecomp.policy.rest.dao.CommonClassDao;
import org.openecomp.policy.rest.jpa.MicroServiceModels;
import org.openecomp.policy.rest.jpa.SafePolicyWarning;
import org.openecomp.policy.utils.PolicyUtils;
import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
import org.openecomp.portalsdk.core.web.support.JsonMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
@Controller
@RequestMapping("/")
public class PolicyValidationController extends RestrictedBaseController {
private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidationController.class);
public static final String CONFIG_POLICY = "Config";
public static final String ACTION_POLICY = "Action";
public static final String DECISION_POLICY = "Decision";
public static final String CLOSEDLOOP_POLICY = "ClosedLoop_Fault";
public static final String CLOSEDLOOP_PM = "ClosedLoop_PM";
public static final String ENFORCER_CONFIG_POLICY= "Enforcer Config";
public static final String MICROSERVICES="Micro Service";
private Pattern pattern;
private Matcher matcher;
private static Map<String, String> mapAttribute = new HashMap<>();
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
@Autowired
CommonClassDao commonClassDao;
@RequestMapping(value={"/policyController/validate_policy.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
public ModelAndView validatePolicy(HttpServletRequest request, HttpServletResponse response) throws Exception{
try{
boolean valid = true;
StringBuilder responseString = new StringBuilder();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode root = mapper.readTree(request.getReader());
PolicyRestAdapter policyData = (PolicyRestAdapter)mapper.readValue(root.get("policyData").toString(), PolicyRestAdapter.class);
if(policyData.getPolicyName() != null){
String policyNameValidate = emptyValidator(policyData.getPolicyName());
if(!policyNameValidate.contains("success")){
responseString.append("PolicyName:" + policyNameValidate + "<br>");
valid = false;
};
}else{
responseString.append( "PolicyName: PolicyName Should not be empty" + "<br>");
valid = false;
}
if(policyData.getPolicyDescription() != null){
String descriptionValidate = descriptionValidator(policyData.getPolicyDescription());
if(!descriptionValidate.contains("success")){
responseString.append("Description:" + descriptionValidate + "<br>");
valid = false;
}
}
if(policyData.getPolicyType().equals(CONFIG_POLICY)){
if (policyData.getConfigPolicyType().equals("Base") || policyData.getConfigPolicyType().equals(CLOSEDLOOP_POLICY)
|| policyData.getConfigPolicyType().equals(CLOSEDLOOP_PM) || policyData.getConfigPolicyType().equals(ENFORCER_CONFIG_POLICY) || policyData.getConfigPolicyType().equals(MICROSERVICES)) {
if(policyData.getEcompName() != null){
String ecompNameValidate = emptyValidator(policyData.getEcompName());
if(!ecompNameValidate.contains("success")){
responseString.append("EcompName:" + ecompNameValidate + "<br>");
valid = false;
}
}else{
responseString.append("Ecomp Name: Ecomp Name Should not be empty" + "<br>");
valid = false;
}
}
if(policyData.getRiskType() != null){
String riskTypeValidate = emptyValidator(policyData.getRiskType());
if(!riskTypeValidate.contains("success")){
responseString.append("RiskType:" + riskTypeValidate + "<br>");
valid = false;
}
}else {
responseString.append("Risk Type: Risk Type Should not be Empty" + "<br>");
valid = false;
}
if(policyData.getRiskLevel() != null){
String validateRiskLevel = emptyValidator(policyData.getRiskLevel());
if(!validateRiskLevel.contains("success")){
responseString.append("RiskLevel:" + validateRiskLevel + "<br>");
valid = false;
}
}else {
responseString.append("Risk Level: Risk Level Should not be Empty" + "<br>");
valid = false;
}
if(policyData.getGuard() != null){
String validateGuard = emptyValidator(policyData.getGuard());
if(!validateGuard.contains("success")){
responseString.append("Guard:" + validateGuard + "<br>");
valid = false;
}
}else {
responseString.append("Guard: Guard Value Should not be Empty" + "<br>");
valid = false;
}
if(policyData.getConfigPolicyType().equals("Base")){
if(policyData.getConfigName() != null){
String configNameValidate = emptyValidator(policyData.getConfigName());
if(!configNameValidate.contains("success")){
responseString.append("ConfigName:" + configNameValidate + "<br>");
valid = false;
}
}else{
responseString.append("Config Name: Config Name Should not be Empty" + "<br>");
valid = false;
}
if(policyData.getConfigType() != null){
String configTypeValidate = emptyValidator(policyData.getConfigType());
if(!configTypeValidate.contains("success")){
responseString.append("ConfigType:" + configTypeValidate + "<br>");
valid = false;
}
}else{
responseString.append("Config Type: Config Type Should not be Empty" + "<br>");
valid = false;
}
if(policyData.getConfigBodyData() != null){
String configBodyData = policyData.getConfigBodyData();
String policyType = policyData.getConfigType();
if (policyType != null) {
if (policyType.equals("JSON")) {
if (!isJSONValid(configBodyData)) {
responseString.append("Config Body: JSON Content is not valid" + "<br>");
valid = false;
}
} else if (policyType.equals("XML")) {
if (!isXMLValid(configBodyData)) {
responseString.append("Config Body: XML Content data is not valid" + "<br>");
valid = false;
}
} else if (policyType.equals("PROPERTIES")) {
if (!isPropValid(configBodyData)||configBodyData.equals("")) {
responseString.append("Config Body: Property data is not valid" + "<br>");
valid = false;
}
} else if (policyType.equals("OTHER")) {
if (configBodyData.equals("")) {
responseString.append("Config Body: Config Body Should not be Empty" + "<br>");
valid = false;
}
}
}
}else{
responseString.append("Config Body: Config Body Should not be Empty" + "<br>");
valid = false;
}
}
if(policyData.getConfigPolicyType().equals("Firewall Config")){
if(policyData.getConfigName() != null){
String configNameValidate = PolicyUtils.emptyPolicyValidator(policyData.getConfigName());
if(!configNameValidate.contains("success")){
responseString.append("<b>ConfigName</b>:<i>" + configNameValidate + "</i><br>");
valid = false;
}
}else{
responseString.append("<b>Config Name</b>:<i> Config Name is required" + "</i><br>");
valid = false;
}
if(policyData.getSecurityZone() == null){
responseString.append("<b>Security Zone</b>:<i> Security Zone is required" + "</i><br>");
valid = false;
}
}
if(policyData.getConfigPolicyType().equals("BRMS_Param")){
if(policyData.getRuleName() == null){
responseString.append("<b>BRMS Template</b>:<i>BRMS Template is required</i><br>");
valid = false;
}
}
if(policyData.getConfigPolicyType().equals("BRMS_Raw")){
if(policyData.getConfigBodyData() != null){
String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData());
// If there are any error other than Annotations then this is not Valid
if(message.contains("[ERR")){
responseString.append("<b>Raw Rule Validate</b>:<i>Raw Rule has error"+ message +"</i><br>");
valid = false;
}
}else{
responseString.append("<b>Raw Rule</b>:<i>Raw Rule is required</i><br>");
valid = false;
}
}
if(policyData.getConfigPolicyType().equals("ClosedLoop_PM")){
try{
if(root.get("policyData").get("verticaMetrics").get("serviceTypePolicyName") == null && policyData.getServiceTypePolicyName().isEmpty()){
responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required</i><br>");
valid = false;
}
}catch(Exception e){
responseString.append("<b>ServiceType PolicyName</b>:<i>ServiceType PolicyName is required</i><br>");
valid = false;
}
if(root.get("policyData").get("jsonBodyData") != null){
ClosedLoopPMBody pmBody = (ClosedLoopPMBody)mapper.readValue(root.get("policyData").get("jsonBodyData").toString(), ClosedLoopPMBody.class);
if(pmBody.getEmailAddress() != null){
String result = emailValidation(pmBody.getEmailAddress(), responseString.toString());
if(result != "success"){
responseString.append(result + "<br>");
valid = false;
}
}
if(pmBody.getGeoLink() != null){
String result = PolicyUtils.emptyPolicyValidator(pmBody.getGeoLink());
if(!result.contains("success")){
responseString.append("<b>GeoLink</b>:<i>" + result + "</i><br>");
valid = false;
};
}
if(pmBody.getAttributes() != null){
for(Entry<String, String> entry : pmBody.getAttributes().entrySet()){
String key = entry.getKey();
String value = entry.getValue();
if(!key.contains("Message")){
String attributeValidate = PolicyUtils.emptyPolicyValidator(value);
if(!attributeValidate.contains("success")){
responseString.append("<b>Attributes</b>:<i>" + key + " : value has spaces</i><br>");
valid = false;
};
}
}
}
}else{
responseString.append("<b>D2/Virtualized Services</b>:<i>Select atleast one D2/Virtualized Services</i><br>");
valid = false;
}
}
if(policyData.getConfigPolicyType().equals("ClosedLoop_Fault")){
if(root.get("policyData").get("jsonBodyData") != null){
ClosedLoopFaultBody faultBody = (ClosedLoopFaultBody)mapper.readValue(root.get("policyData").get("jsonBodyData").toString(), ClosedLoopFaultBody.class);
if(faultBody.getEmailAddress() != null){
String result = emailValidation(faultBody.getEmailAddress(), responseString.toString());
if(result != "success"){
responseString.append(result+ "<br>");
valid = false;
}
}
if((faultBody.isGama() || faultBody.isMcr() || faultBody.isTrinity() || faultBody.isvDNS() || faultBody.isvUSP()) != true){
responseString.append("<b>D2/Virtualized Services</b>:<i>Select atleast one D2/Virtualized Services</i><br>");
valid = false;
}
if(faultBody.getActions() == null){
responseString.append("<b>vPRO Actions</b>:<i>vPRO Actions is required</i><br>");
valid = false;
}
if(faultBody.getClosedLoopPolicyStatus() == null){
responseString.append("<b>Policy Status</b>:<i>Policy Status is required</i><br>");
valid = false;
}
if(faultBody.getConditions() == null){
responseString.append("<b>Conditions</b>:<i>Select Atleast one Condition</i><br>");
valid = false;
}
if(faultBody.getGeoLink() != null){
String result = PolicyUtils.emptyPolicyValidatorWithSpaceAllowed(faultBody.getGeoLink());
if(!result.contains("success")){
responseString.append("<b>GeoLink</b>:<i>" + result + "</i><br>");
valid = false;
};
}
if(faultBody.getTimeInterval() == 0){
responseString.append("<b>Time Interval</b>:<i>Time Interval is required</i><br>");
valid = false;
}
if(faultBody.getRetrys() == 0){
responseString.append("<b>Number of Retries</b>:<i>Number of Retries is required</i><br>");
valid = false;
}
if(faultBody.getTimeOutvPRO() == 0){
responseString.append("<b>APP-C Timeout</b>:<i>APP-C Timeout is required</i><br>");
valid = false;
}
if(faultBody.getTimeOutRuby() == 0){
responseString.append("<b>TimeOutRuby</b>:<i>TimeOutRuby is required</i><br>");
valid = false;
}
if(faultBody.getVnfType() == null){
responseString.append("<b>Vnf Type</b>:<i>Vnf Type is required</i><br>");
valid = false;
}
}else{
responseString.append("<b>D2/Virtualized Services</b>:<i>Select atleast one D2/Virtualized Services</i><br>");
responseString.append("<b>vPRO Actions</b>:<i>vPRO Actions is required</i><br>");
responseString.append("<b>Aging Window</b>:<i>Aging Window is required</i><br>");
responseString.append("<b>Policy Status</b>:<i>Policy Status is required</i><br>");
responseString.append("<b>Conditions</b>:<i>Select Atleast one Condition</i><br>");
responseString.append("<b>PEP Name</b>:<i>PEP Name is required</i><br>");
responseString.append("<b>PEP Action</b>:<i>PEP Action is required</i><br>");
responseString.append("<b>Time Interval</b>:<i>Time Interval is required</i><br>");
responseString.append("<b>Number of Retries</b>:<i>Number of Retries is required</i><br>");
responseString.append("<b>APP-C Timeout</b>:<i>APP-C Timeout is required</i><br>");
responseString.append("<b>TimeOutRuby</b>:<i>TimeOutRuby is required</i><br>");
responseString.append("<b>Vnf Type</b>:<i>Vnf Type is required</i><br>");
valid = false;
}
}
if (policyData.getConfigPolicyType().contains("Micro Service")){
if(policyData.getServiceType() != null){
pullJsonKeyPairs(root.get("policyJSON"));
MicroServiceModels returnModel = new MicroServiceModels();
String service = null;
String version = null;
if (policyData.getServiceType().contains("-v")){
service = policyData.getServiceType().split("-v")[0];
version = policyData.getServiceType().split("-v")[1];
}else {
service = policyData.getServiceType();
version = policyData.getVersion();
}
returnModel = getAttributeObject(service, version);
String annoation = returnModel.getAnnotation();
if (!Strings.isNullOrEmpty(annoation)){
Map<String, String> rangeMap = new HashMap<>();
rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annoation);
for (Entry<String, String> rMap : rangeMap.entrySet()){
if (rMap.getValue().contains("range::")){
String value = mapAttribute.get(rMap.getKey().trim());
String[] tempString = rMap.getValue().split("::")[1].split("-");
int startNum = Integer.parseInt(tempString[0]);
int endNum = Integer.parseInt(tempString[1]);
String returnString = "Invalid Range:" + rMap.getKey() + " must be between "
+ startNum + " - " + endNum + ",";
if (isInteger(value.replace("\"", ""))){
int result = Integer.parseInt(value.replace("\"", ""));
if (result < startNum || result > endNum){
responseString.append(returnString);
valid = false;
}
}else {
responseString.append(returnString);
valid = false;
}
}
}
}
//for continue testing for Dkat, just blocked this validation until fixing it. gw1218 on 3/30/17
//if (!checkAttributeValues()){
//responseString.append("<b>Micro Service</b>:<i> Attribute Values Missing" + "</i><br>");
//valid = false;
//}
}else{
responseString.append("<b>Micro Service</b>:<i> Micro Service is required" + "</i><br>");
valid = false;
}
if(policyData.getPriority() == null){
responseString.append("<b>Priority</b>:<i> Priority is required" + "</i><br>");
valid = false;
}
}
}
if (policyData.getPolicyType().equals(DECISION_POLICY)){
if(policyData.getEcompName() != null){
String ecompNameValidate = emptyValidator(policyData.getEcompName());
if(!ecompNameValidate.contains("success")){
responseString.append("EcompName:" + ecompNameValidate + "<br>");
valid = false;
}
}else{
responseString.append("Ecomp Name: Ecomp Name Should not be empty" + "<br>");
valid = false;
}
if("GUARD_YAML".equals(policyData.getRuleProvider()) || "GUARD_BL_YAML".equals(policyData.getRuleProvider())){
if(policyData.getYamlparams()==null){
responseString.append("<b> Guard Params are Required </b>" + "<br>");
valid = false;
}else{
if(policyData.getYamlparams().getActor()==null){
responseString.append("Guard Params <b>Actor</b> is Required " + "<br>");
valid = false;
}
if(policyData.getYamlparams().getRecipe()==null){
responseString.append("Guard Params <b>Recipe</b> is Required " + "<br>");
valid = false;
}
if(policyData.getYamlparams().getGuardActiveStart()==null){
responseString.append("Guard Params <b>Guard Active Start/b>is Required " + "<br>");
valid = false;
}
if(policyData.getYamlparams().getGuardActiveEnd()==null){
responseString.append("Guard Params <b>Guard Active End</b>is Required " + "<br>");
valid = false;
}
if("GUARD_YAML".equals(policyData.getRuleProvider())){
if(policyData.getYamlparams().getLimit()==null){
responseString.append(" Guard Params <b>Limit</b> is Required " + "<br>");
valid = false;
}else if(!isInteger(policyData.getYamlparams().getLimit())){
responseString.append(" Guard Params <b>Limit</b> Should be Integer " + "<br>");
valid = false;
}
if(policyData.getYamlparams().getTimeWindow()==null){
responseString.append("Guard Params <b>Time Window</b> is Required" + "<br>");
valid = false;
}
}else if("GUARD_BL_YAML".equals(policyData.getRuleProvider())){
if(policyData.getYamlparams().getBlackList()==null || policyData.getYamlparams().getBlackList().isEmpty()){
responseString.append(" Guard Params <b>BlackList</b> is Required " + "<br>");
valid = false;
}else{
for(String blackList: policyData.getYamlparams().getBlackList()){
if(blackList==null || !("success".equals(emptyValidator(blackList)))){
responseString.append(" Guard Params <b>BlackList</b> Should be valid String" + "<br>");
valid = false;
break;
}
}
}
}
}
}
}
if(policyData.getPolicyType().equals(ACTION_POLICY)){
if(policyData.getActionPerformer() != null){
String actionPerformer = emptyValidator(policyData.getActionPerformer());
if(!actionPerformer.contains("success")){
responseString.append("ActionPerformer:" + actionPerformer + "<br>");
valid = false;
};
}else{
responseString.append("ActionPerformer: ActionPerformer Should not be empty" + "<br>");
valid = false;
}
if(policyData.getAttributes() != null){
for(Object attribute : policyData.getAttributes()){
if(attribute instanceof LinkedHashMap<?, ?>){
try{
//This is for validation check if the value exists or not
String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
String value = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
if("".equals(key) || "".equals(value)){
responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "<br>");
valid = false;
break;
}
}catch(Exception e){
LOGGER.error("This is a Policy Validation check" +e);
responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "<br>");
valid = false;
break;
}
}
}
}else{
responseString.append("Component Attributes: One or more Fields in Component Attributes is Empty." + "<br>");
valid = false;
}
if(policyData.getActionAttributeValue() != null){
String actionAttribute = emptyValidator(policyData.getActionAttributeValue());
if(!actionAttribute.contains("success")){
responseString.append("ActionAttribute:" + actionAttribute + "<br>");
valid = false;
};
}else{
responseString.append("ActionAttribute: ActionAttribute Should not be empty" + "<br>");
valid = false;
}
}
if(policyData.getPolicyType().equals(ACTION_POLICY) || policyData.getPolicyType().equals(DECISION_POLICY)){
if(!policyData.getRuleAlgorithmschoices().isEmpty()){
for(Object attribute : policyData.getRuleAlgorithmschoices()){
if(attribute instanceof LinkedHashMap<?, ?>){
try{
String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString();
String key = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField1").toString();
String rule = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmCombo").toString();
String value = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2").toString();
if("".equals(label) || "".equals(key) || "".equals(rule) || "".equals(value)){
responseString.append("Rule Algorithms: One or more Fields in Rule Algorithms is Empty." + "<br>");
valid = false;
}
}catch(Exception e){
LOGGER.error("This is a Policy Validation check" +e);
responseString.append("Rule Algorithms: One or more Fields in Rule Algorithms is Empty." + "<br>");
valid = false;
break;
}
}
}
}
}
if(policyData.getPolicyType().equals(CONFIG_POLICY)){
String value = "";
if(valid){
List<Object> spData = commonClassDao.getDataById(SafePolicyWarning.class, "riskType", policyData.getRiskType());
if (!spData.isEmpty()){
SafePolicyWarning safePolicyWarningData = (SafePolicyWarning) spData.get(0);
safePolicyWarningData.getMessage();
value = "Messaage:" + safePolicyWarningData.getMessage();
}
responseString.append("success" + "@#"+ value);
}
}else{
if(valid){
responseString.append("success");
}
}
PrintWriter out = response.getWriter();
JsonMessage msg = new JsonMessage(mapper.writeValueAsString(responseString.toString()));
JSONObject j = new JSONObject(msg);
out.write(j.toString());
return null;
}
catch (Exception e){
LOGGER.error("Exception Occured while Policy Validation" +e);
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write(e.getMessage());
}
return null;
}
protected boolean isInteger(String number) {
try{
Integer.parseInt(number);
}catch(NumberFormatException e){
return false;
}
return true;
}
protected String emptyValidator(String field){
String error;
if ("".equals(field) || field.contains(" ") || !field.matches("^[a-zA-Z0-9_]*$")) {
error = "The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}, _' following set of Combinations";
return error;
} else {
if(CharMatcher.ASCII.matchesAllOf((CharSequence) field)){
error = "success";
}else{
error = "The Value Contains Non ASCII Characters";
return error;
}
}
return error;
}
protected String descriptionValidator(String field) {
String error;
if (field.contains("@CreatedBy:") || field.contains("@ModifiedBy:")) {
error = "The value in the description shouldn't contain @CreatedBy: or @ModifiedBy:";
return error;
} else {
error = "success";
}
return error;
}
public String validateEmailAddress(String emailAddressValue) {
String error = "success";
List<String> emailList = Arrays.asList(emailAddressValue.toString().split(","));
for(int i =0 ; i < emailList.size() ; i++){
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(emailList.get(i).trim());
if(!matcher.matches()){
error = "Please check the Following Email Address is not Valid .... " +emailList.get(i).toString();
return error;
}else{
error = "success";
}
}
return error;
}
protected String emailValidation(String email, String response){
if(email != null){
String validateEmail = PolicyUtils.validateEmailAddress(email.replace("\"", ""));
if(!validateEmail.contains("success")){
response += "<b>Email</b>:<i>" + validateEmail+ "</i><br>";
}else{
return "success";
}
}
return response;
}
private MicroServiceModels getAttributeObject(String name, String version) {
MicroServiceModels workingModel = new MicroServiceModels();
List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", name+":"+version);
if(microServiceModelsData != null){
workingModel = (MicroServiceModels) microServiceModelsData.get(0);
}
return workingModel;
}
private void pullJsonKeyPairs(JsonNode rootNode) {
Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
while (fieldsIterator.hasNext()) {
Map.Entry<String, JsonNode> field = fieldsIterator.next();
final String key = field.getKey();
final JsonNode value = field.getValue();
if (value.isContainerNode() && !value.isArray()) {
pullJsonKeyPairs(value); // RECURSIVE CALL
} else {
if (value.isArray()){
String newValue = StringUtils.replaceEach(value.toString(), new String[]{"[", "]", "\""}, new String[]{"", "", ""});
mapAttribute.put(key, newValue);
}else {
mapAttribute.put(key, value.toString().trim());
}
}
}
}
// Validation for json.
protected static boolean isJSONValid(String data) {
try {
new JSONObject(data);
InputStream stream = new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
JsonReader jsonReader = Json.createReader(stream);
System.out.println("Json Value is: " + jsonReader.read().toString() );
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
// Validation for XML.
private boolean isXMLValid(String data) {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(true);
try {
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setErrorHandler(new XMLErrorHandler());
reader.parse(new InputSource(new StringReader(data)));
} catch (ParserConfigurationException e) {
return false;
} catch (SAXException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
// Validation for Properties file.
public boolean isPropValid(String prop) {
Scanner scanner = new Scanner(prop);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
line = line.replaceAll("\\s+", "");
if (line.startsWith("#")) {
continue;
} else {
if (line.contains("=")) {
String[] parts = line.split("=");
if (parts.length < 2) {
scanner.close();
return false;
}
} else {
scanner.close();
return false;
}
}
}
scanner.close();
return true;
}
}
|