2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.rest.util;
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;
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;
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;
43 import java.util.Map.Entry;
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;
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;
72 public class PolicyValidation {
74 private static final Logger LOGGER = FlexLogger.getLogger(PolicyValidation.class);
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";
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<>();
104 private static CommonClassDao commonClassDao;
107 public PolicyValidation(CommonClassDao commonClassDao) {
108 PolicyValidation.commonClassDao = commonClassDao;
112 * This is an empty constructor
114 public PolicyValidation() {
122 * @param policyData the policy data
123 * @return the string builder
124 * @throws IOException Signals that an I/O exception has occurred.
126 public StringBuilder validatePolicy(PolicyRestAdapter policyData) throws IOException {
128 boolean valid = true;
129 StringBuilder responseString = new StringBuilder();
130 ObjectMapper mapper = new ObjectMapper();
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);
139 responseString.append("<b>PolicyName</b>: PolicyName Should not be empty" + HTML_ITALICS_LNBREAK);
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);
150 if (!"API".equals(policyData.getApiflag()) && policyData.getAttributes() != null
151 && !policyData.getAttributes().isEmpty()) {
152 for (Object attribute : policyData.getAttributes()) {
153 if (attribute instanceof LinkedHashMap<?, ?>) {
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);
164 if (CONFIG_POLICY.equals(policyData.getPolicyType())) {
165 if ("Base".equals(policyData.getConfigPolicyType())) {
167 .append("<b>Attributes</b>:<i> has one missing Attribute key</i><br>");
169 if (BRMSPARAM.equals(policyData.getConfigPolicyType())
170 || BRMSRAW.equals(policyData.getConfigPolicyType())) {
172 .append("<b>Rule Attributes</b>:<i> has one missing Attribute key</i><br>");
175 responseString.append(
176 "<b>Component Attributes</b>:<i> has one missing Component Attribute key</i><br>");
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);
188 if (BRMSPARAM.equals(policyData.getConfigPolicyType())
189 || BRMSRAW.equals(policyData.getConfigPolicyType())) {
190 responseString.append("<b>Rule Attributes</b>:<i>" + value
191 + SPACESINVALIDCHARS);
194 responseString.append("<b>Component Attributes</b>:<i>" + value
195 + SPACESINVALIDCHARS);
200 if (CONFIG_POLICY.equals(policyData.getPolicyType())) {
201 if ("Base".equals(policyData.getConfigPolicyType())) {
203 .append("<b>Attributes</b>:<i> has one missing Attribute value</i><br>");
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>");
211 responseString.append(
212 "<b>Component Attributes</b>:<i> has one missing Component Attribute value</i><br>");
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<?, ?>) {
226 if (((LinkedHashMap<?, ?>) attribute).get("key") == null) {
228 .append("<b>Settings Attributes</b>:<i> has one missing Attribute key</i><br>");
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);
240 .append("<b>Settings Attributes</b>:<i> has one missing Attribute Value</i><br>");
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>");
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>");
262 if (((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2") != null) {
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>");
272 .append("<b>Rule Algorithms</b>:<i>" + label + " : Field 3 value is empty</i><br>");
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())) {
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);
294 responseString.append("<b>Onap Name</b>: Onap Name Should not be empty" + HTML_ITALICS_LNBREAK);
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);
306 responseString.append("<b>RiskType</b>: Risk Type Should not be Empty" + HTML_ITALICS_LNBREAK);
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);
317 responseString.append("<b>RiskLevel</b>: Risk Level Should not be Empty" + HTML_ITALICS_LNBREAK);
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);
328 responseString.append("<b>Guard</b>: Guard Value Should not be Empty" + HTML_ITALICS_LNBREAK);
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);
341 responseString.append("Config Name: Config Name Should not be Empty" + HTML_ITALICS_LNBREAK);
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);
351 responseString.append("Config Type: Config Type Should not be Empty" + HTML_ITALICS_LNBREAK);
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)) {
361 .append("Config Body: JSON Content is not valid" + HTML_ITALICS_LNBREAK);
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);
370 } else if ("PROPERTIES".equals(configType)) {
371 if (!PolicyUtils.isPropValid(configBodyData) || "".equals(configBodyData)) {
373 .append("Config Body: Property data is not valid" + HTML_ITALICS_LNBREAK);
376 } else if ("OTHER".equals(configType) && ("".equals(configBodyData))) {
378 .append("Config Body: Config Body Should not be Empty" + HTML_ITALICS_LNBREAK);
383 responseString.append("Config Body: Config Body Should not be Empty" + HTML_ITALICS_LNBREAK);
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);
396 responseString.append("<b>Config Name</b>:<i> Config Name is required" + HTML_ITALICS_LNBREAK);
399 if (policyData.getSecurityZone() == null || policyData.getSecurityZone().isEmpty()) {
401 .append("<b>Security Zone</b>:<i> Security Zone is required" + HTML_ITALICS_LNBREAK);
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);
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());
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);
423 responseString.append("<b>Raw Rule</b>:<i>Raw Rule is required" + HTML_ITALICS_LNBREAK);
427 // Validate ClosedLoop_PM Policy Data
428 if (CLOSEDLOOP_PM.equalsIgnoreCase(policyData.getConfigPolicyType())) {
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);
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);
444 if (policyData.getJsonBody() != null) {
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);
454 if ((pmBody.isGamma() || pmBody.isMcr() || pmBody.isTrinity() || pmBody.isvDNS()
455 || pmBody.isvUSP()) != true) {
457 .append("<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services"
458 + HTML_ITALICS_LNBREAK);
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);
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);
484 .append("<b>D2/Virtualized Services</b>:<i>Select atleast one D2/Virtualized Services"
485 + HTML_ITALICS_LNBREAK);
489 if (CLOSEDLOOP_POLICY.equalsIgnoreCase(policyData.getConfigPolicyType())) {
490 if (policyData.getJsonBody() != null) {
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;
505 .append("<b>Conditions</b>: <i>There were no conditions provided in configBody json"
506 + HTML_ITALICS_LNBREAK);
507 return responseString;
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>");
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>");
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>");
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);
539 if (!(faultBody.isGamma() || faultBody.isMcr() || faultBody.isTrinity() || faultBody.isvDNS()
540 || faultBody.isvUSP())) {
542 .append("<b>D2/Virtualized Services</b>: <i>Select at least one D2/Virtualized Services"
543 + HTML_ITALICS_LNBREAK);
546 if (faultBody.getActions() == null || faultBody.getActions().isEmpty()) {
548 .append("<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK);
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);
557 if (faultBody.getConditions() == null) {
558 responseString.append(
559 "<b>Conditions</b>: <i>Select At least one Condition" + HTML_ITALICS_LNBREAK);
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);
569 if (faultBody.getAgingWindow() == 0) {
571 .append("<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK);
574 if (faultBody.getTimeInterval() == 0) {
575 responseString.append(
576 "<b>Time Interval</b>: <i>Time Interval is required" + HTML_ITALICS_LNBREAK);
579 if (faultBody.getRetrys() == 0) {
580 responseString.append("<b>Number of Retries</b>: <i>Number of Retries is required"
581 + HTML_ITALICS_LNBREAK);
584 if (faultBody.getTimeOutvPRO() == 0) {
585 responseString.append(
586 "<b>APP-C Timeout</b>: <i>APP-C Timeout is required" + HTML_ITALICS_LNBREAK);
589 if (faultBody.getTimeOutRuby() == 0) {
591 .append("<b>TimeOutRuby</b>: <i>TimeOutRuby is required" + HTML_ITALICS_LNBREAK);
594 if (faultBody.getVnfType() == null || faultBody.getVnfType().isEmpty()) {
595 responseString.append("<b>Vnf Type</b>: <i>Vnf Type is required" + HTML_ITALICS_LNBREAK);
600 .append("<b>D2/Virtualized Services</b>: <i>Select atleast one D2/Virtualized Services"
601 + HTML_ITALICS_LNBREAK);
603 .append("<b>vPRO Actions</b>: <i>vPRO Actions is required" + HTML_ITALICS_LNBREAK);
605 .append("<b>Aging Window</b>: <i>Aging Window is required" + HTML_ITALICS_LNBREAK);
607 .append("<b>Policy Status</b>: <i>Policy Status is required" + HTML_ITALICS_LNBREAK);
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);
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);
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);
624 // Validate MicroService Policy Data
625 if (MICROSERVICES.equals(policyData.getConfigPolicyType())) {
626 boolean tmpValid = validateMsModel(policyData, responseString);
632 // Validate Optimization Policy Data
633 if (OPTIMIZATION.equals(policyData.getConfigPolicyType())){
634 boolean tmpValid = validateOptimization(policyData, responseString);
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);
651 responseString.append("Onap Name: Onap Name Should not be empty" + HTML_ITALICS_LNBREAK);
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);
663 if (DECISION_MS_MODEL.equals(policyData.getRuleProvider())) {
664 LOGGER.info("Validating Decision MS Policy - ");
665 boolean tmpValid = validateMsModel(policyData, responseString);
671 if ("Rainy_Day".equals(policyData.getRuleProvider())) {
672 if (policyData.getRainyday() == null) {
673 responseString.append("<b> Rainy Day Parameters are Required </b><br>");
676 if (Strings.isNullOrEmpty(policyData.getRainyday().getServiceType())) {
677 responseString.append("Rainy Day <b>Service Type</b> is Required<br>");
680 if (Strings.isNullOrEmpty(policyData.getRainyday().getVnfType())) {
681 responseString.append("Rainy Day <b>VNF Type</b> is Required<br>");
684 if (Strings.isNullOrEmpty(policyData.getRainyday().getBbid())) {
685 responseString.append("Rainy Day <b>Building Block ID</b> is Required<br>");
688 if (Strings.isNullOrEmpty(policyData.getRainyday().getWorkstep())) {
689 responseString.append("Rainy Day <b>Work Step</b> is Required<br>");
692 if (!policyData.getRainyday().getTreatmentTableChoices().isEmpty()
693 && policyData.getRainyday().getTreatmentTableChoices() != null) {
695 for (Object treatmentMap : policyData.getRainyday().getTreatmentTableChoices()) {
696 String errorCode = null;
697 String treatment = null;
698 if (treatmentMap instanceof LinkedHashMap<?, ?>) {
700 if (((LinkedHashMap<?, ?>) treatmentMap).containsKey("errorcode")) {
701 errorCode = ((LinkedHashMap<?, ?>) treatmentMap).get("errorcode").toString();
703 if (((LinkedHashMap<?, ?>) treatmentMap).containsKey("treatment")) {
704 treatment = ((LinkedHashMap<?, ?>) treatmentMap).get("treatment").toString();
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>");
714 if (Strings.isNullOrEmpty(errorCode)) {
715 responseString.append(
716 "Rainy Day <b>Error Code</b> is Required for each Desired Treatment<br>");
720 if (Strings.isNullOrEmpty(treatment)) {
721 responseString.append(
722 "Rainy Day <b>Desired Treatment</b> is Required for each Error Code<br>");
729 responseString.append("Rainy Day <b>Desired Automated Treatments</b> are Required<br>");
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);
742 if (Strings.isNullOrEmpty(policyData.getYamlparams().getActor())) {
743 responseString.append("Guard Params <b>Actor</b> is Required " + HTML_ITALICS_LNBREAK);
746 if (Strings.isNullOrEmpty(policyData.getYamlparams().getRecipe())) {
747 responseString.append("Guard Params <b>Recipe</b> is Required " + HTML_ITALICS_LNBREAK);
750 if (Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveStart())) {
751 responseString.append(
752 "Guard Params <b>Guard Active Start</b> is Required " + HTML_ITALICS_LNBREAK);
755 if (Strings.isNullOrEmpty(policyData.getYamlparams().getGuardActiveEnd())) {
757 .append("Guard Params <b>Guard Active End</b> is Required " + HTML_ITALICS_LNBREAK);
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);
764 } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getLimit())) {
766 .append(" Guard Params <b>Limit</b> Should be Integer " + HTML_ITALICS_LNBREAK);
769 if (Strings.isNullOrEmpty(policyData.getYamlparams().getTimeWindow())) {
771 .append("Guard Params <b>Time Window</b> is Required" + HTML_ITALICS_LNBREAK);
773 } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getTimeWindow())) {
774 responseString.append(
775 " Guard Params <b>Time Window</b> Should be Integer " + HTML_ITALICS_LNBREAK);
778 if (Strings.isNullOrEmpty(policyData.getYamlparams().getTimeUnits())) {
780 .append("Guard Params <b>Time Units</b> is Required" + HTML_ITALICS_LNBREAK);
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);
787 } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getMin())) {
789 .append(" Guard Params <b>Min</b> Should be Integer " + HTML_ITALICS_LNBREAK);
792 if (Strings.isNullOrEmpty(policyData.getYamlparams().getMax())) {
793 responseString.append(" Guard Params <b>Max</b> is Required " + HTML_ITALICS_LNBREAK);
795 } else if (!PolicyUtils.isInteger(policyData.getYamlparams().getMax())) {
797 .append(" Guard Params <b>Max</b> Should be Integer " + HTML_ITALICS_LNBREAK);
800 } else if ("GUARD_BL_YAML".equals(policyData.getRuleProvider())
801 && "Use Manual Entry".equals(policyData.getBlackListEntryType())) {
803 if (policyData.getYamlparams().getBlackList() == null
804 || policyData.getYamlparams().getBlackList().isEmpty()) {
806 .append(" Guard Params <b>BlackList</b> is Required " + HTML_ITALICS_LNBREAK);
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);
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);
832 responseString.append(
833 "<b>ActionPerformer</b>:<i> ActionPerformer Should not be empty" + HTML_ITALICS_LNBREAK);
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);
845 responseString.append(
846 "<b>ActionAttribute</b>:<i> ActionAttribute Should not be empty" + HTML_ITALICS_LNBREAK);
851 if (CONFIG_POLICY.equals(policyData.getPolicyType())) {
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>";
862 responseString.append(SUCCESS + "@#" + value);
866 responseString.append(SUCCESS);
870 return responseString;
871 } catch (Exception e) {
872 LOGGER.error("Exception Occured during Policy Validation" + e);
877 protected String emailValidation(String email, String response) {
878 String res = response;
880 String validateEmail = PolicyUtils.validateEmailAddress(email.replace("\"", ""));
881 if (!validateEmail.contains(SUCCESS)) {
882 res += "<b>Email</b>:<i>" + validateEmail + HTML_ITALICS_LNBREAK;
890 private MicroServiceModels getAttributeObject(String name, String version) {
891 MicroServiceModels workingModel = null;
893 List<Object> microServiceModelsData =
894 commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", name + ":" + version);
895 if (microServiceModelsData != null) {
896 workingModel = (MicroServiceModels) microServiceModelsData.get(0);
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);
908 private OptimizationModels getOptimizationModelData(String name, String version) {
909 OptimizationModels workingModel = null;
911 List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, "modelName:version", name+":"+version);
912 if(optimizationModelsData != null){
913 workingModel = (OptimizationModels) optimizationModelsData.get(0);
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);
925 private void pullJsonKeyPairs(JsonNode rootNode) {
926 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
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
935 if (value.isArray()) {
936 String newValue = StringUtils.replaceEach(value.toString(), new String[] {"[", "]", "\""},
937 new String[] {"", "", ""});
938 mapAttribute.put(key, newValue);
940 mapAttribute.put(key, value.toString().trim());
946 private void pullModelJsonKeyPairs(JsonNode rootNode) {
947 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
949 while (fieldsIterator.hasNext()) {
950 Map.Entry<String, JsonNode> field = fieldsIterator.next();
951 final String key = field.getKey();
952 final JsonNode value = field.getValue();
954 if (value.isContainerNode() && !value.isArray()) {
955 jsonRequestMap.put(key.trim(), value.toString().trim());
956 pullModelJsonKeyPairs(value); // RECURSIVE CALL
957 } else if (value.isArray()) {
959 String valueStr = value.toString();
960 String stringValue = valueStr.substring(valueStr.indexOf('[') + 1, valueStr.lastIndexOf(']'));
962 if (stringValue.isEmpty()) {
963 stringValue = "{\"test\":\"test\"}";
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);
974 jsonRequestMap.put(key.trim(), value.toString().trim());
980 private JsonObject stringToJsonObject(String value) {
981 try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
982 return jsonReader.readObject();
983 } catch (JsonException | IllegalStateException e) {
985 XACMLErrorConstants.ERROR_DATA_ISSUE
986 + "Improper JSON format... may or may not cause issues in validating the policy: " + value,
992 private void findRequiredFields(JsonObject json) {
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());
1009 } else if (keyMap.getValue().toString().contains(REQUIRED_ATTRIBUTE)) {
1010 modelRequiredFieldsList.add(key.trim());
1017 // call this method to start the recursive
1018 private Set<String> getAllKeys(JSONObject json) {
1019 return getAllKeys(json, new HashSet<>());
1022 private Set<String> getAllKeys(JSONArray arr) {
1023 return getAllKeys(arr, new HashSet<>());
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)));
1032 if (obj instanceof JSONArray) {
1033 keys.addAll(getAllKeys(arr.getJSONArray(i)));
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
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);
1057 if (obj instanceof JSONObject) {
1058 keys.addAll(getAllKeys(json.getJSONObject(key)));
1060 if (obj instanceof JSONArray) {
1061 keys.addAll(getAllKeys(json.getJSONArray(key)));
1068 private boolean validateMsModel(PolicyRestAdapter policyData, StringBuilder responseString)
1069 throws JsonProcessingException {
1070 boolean valid = true;
1071 if (!Strings.isNullOrEmpty(policyData.getServiceType())) {
1073 modelRequiredFieldsList.clear();
1074 pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON());
1078 if (policyData.getServiceType().contains("-v")) {
1079 service = policyData.getServiceType().split("-v")[0];
1080 version = policyData.getServiceType().split("-v")[1];
1082 service = policyData.getServiceType();
1083 version = policyData.getVersion();
1086 if (!Strings.isNullOrEmpty(version)) {
1087 MicroServiceModels returnModel = getAttributeObject(service, version);
1089 if (returnModel != null) {
1091 String annotation = returnModel.getAnnotation();
1092 String refAttributes = returnModel.getRef_attributes();
1093 String subAttributes = returnModel.getSub_attributes();
1094 String modelAttributes = returnModel.getAttributes();
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 + ",";
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);
1115 responseString.append(returnString);
1119 responseString.append("<b>" + raMap.getKey() + "</b>:<i>" + raMap.getKey()
1120 + " is required for the MicroService model " + service
1121 + HTML_ITALICS_LNBREAK);
1127 } else if (!DECISION_MS_MODEL.equals(policyData.getRuleProvider())) {
1128 // Validate for configName, location, uuid, and policyScope if no annotations exist for this
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);
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);
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);
1148 if (Strings.isNullOrEmpty(policyData.getPolicyScope())) {
1150 .append("<b>Micro Service Model</b>:<i> policyScope is required for this model"
1151 + HTML_ITALICS_LNBREAK);
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);
1162 // ignore req fields in which parent is not reqd
1163 populateRequiredFields(new String[] {refAttributes, modelAttributes}, subAttributes,
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);
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);
1182 responseString.append("<b>Micro Service Model</b>:<i> " + requiredField
1183 + ISREQUIRED + HTML_ITALICS_LNBREAK);
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);
1195 responseString.append(
1196 "<b>Micro Service Version</b>:<i> Micro Service Version is required" + HTML_ITALICS_LNBREAK);
1200 responseString.append("<b>Micro Service</b>:<i> Micro Service Model is required" + HTML_ITALICS_LNBREAK);
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);
1213 private boolean validateOptimization(PolicyRestAdapter policyData, StringBuilder responseString) {
1214 boolean valid = true;
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);
1224 modelRequiredFieldsList.clear();
1225 pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON());
1227 // parse the service and version from the policy data
1230 if (policyData.getServiceType().contains("-v")) {
1231 service = policyData.getServiceType().split("-v")[0];
1232 version = policyData.getServiceType().split("-v")[1];
1234 service = policyData.getServiceType();
1235 version = policyData.getVersion();
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);
1246 OptimizationModels returnModel = getOptimizationModelData(service, version);
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);
1256 String annotation = returnModel.getAnnotation();
1257 String refAttributes = returnModel.getRefattributes();
1258 String subAttributes = returnModel.getSubattributes();
1259 String modelAttributes = returnModel.getAttributes();
1261 if (!Strings.isNullOrEmpty(annotation)) {
1262 Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=")
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 + ",";
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);
1281 responseString.append(returnString);
1285 responseString.append("<b>" + rMap.getKey() + "</b>:<i>" + rMap.getKey()
1286 + " is required for the Optimization model " + service
1287 + HTML_ITALICS_LNBREAK);
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);
1301 modelRequiredFieldsList.addAll(allOptReqTrueKeys);
1303 if (modelRequiredFieldsList != null && !modelRequiredFieldsList.isEmpty()) {
1305 // create jsonRequestMap with all json keys and values from request
1306 JsonNode rootNode = (JsonNode) policyData.getPolicyJSON();
1307 jsonRequestMap.clear();
1308 pullModelJsonKeyPairs(rootNode);
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);
1320 responseString.append("<b>Optimization Service Model</b>:<i> "
1321 + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK);
1328 if (Strings.isNullOrEmpty(policyData.getPriority())) {
1329 responseString.append("<b>Priority</b>:<i> Priority is required" + HTML_ITALICS_LNBREAK);
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);
1350 String json = new ObjectMapper().writeValueAsString(attributesMap);
1351 findRequiredFields(stringToJsonObject(json));
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);
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);
1371 // parse refAttrbutes
1372 for (String attr : attrArr) {
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]);