+ String key = keyMap.getKey();
+ if (obj instanceof JsonObject) {
+ if (allReqTrueKeys.contains(key)) {
+ JsonObject jsonObj = (JsonObject) obj;
+ // only check fields in obj if obj itself is required.
+ for (Entry<String, JsonValue> jsonMap : jsonObj.entrySet()) {
+ if (jsonMap.getValue().toString().contains(REQUIRED_ATTRIBUTE)) {
+ modelRequiredFieldsList.add(jsonMap.getKey().trim());
+ }
+ }
+ }
+ } else if (keyMap.getValue().toString().contains(REQUIRED_ATTRIBUTE)) {
+ modelRequiredFieldsList.add(key.trim());
+
+ }
+ }
+
+ }
+
+ // call this method to start the recursive
+ private Set<String> getAllKeys(JSONObject json) {
+ return getAllKeys(json, new HashSet<>());
+ }
+
+ private Set<String> getAllKeys(JSONArray arr) {
+ return getAllKeys(arr, new HashSet<>());
+ }
+
+ private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
+ for (int i = 0; i < arr.length(); i++) {
+ Object obj = arr.get(i);
+ if (obj instanceof JSONObject) {
+ keys.addAll(getAllKeys(arr.getJSONObject(i)));
+ }
+ if (obj instanceof JSONArray) {
+ keys.addAll(getAllKeys(arr.getJSONArray(i)));
+ }
+ }
+
+ return keys;
+ }
+
+ // this method returns a set of keys with "required-true" defined in their value.
+ private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
+ for (String key : json.keySet()) {
+ Object obj = json.get(key);
+ if (obj instanceof String && ((String) obj).contains(REQUIRED_ATTRIBUTE)) {
+ LOGGER.debug("key : " + key);
+ LOGGER.debug("obj : " + obj);
+ allReqTrueKeys.add(key); // For MicroService policies
+ allOptReqTrueKeys.add(key); // For Optimization policies
+
+ // get the type from value and add that one also
+ String type = StringUtils.substringBefore((String) obj, ":");
+ if (!StringUtils.isBlank(type) && !StringUtils.containsAny(type.toLowerCase(), STRING, INTEGER, LIST,
+ MAP, JAVA, BOOLEAN)) {
+ allReqTrueKeys.add(type);
+ }
+ }
+ if (obj instanceof JSONObject) {
+ keys.addAll(getAllKeys(json.getJSONObject(key)));
+ }
+ if (obj instanceof JSONArray) {
+ keys.addAll(getAllKeys(json.getJSONArray(key)));
+ }
+ }
+
+ return keys;
+ }
+
+ private boolean validateMsModel(PolicyRestAdapter policyData, StringBuilder responseString)
+ throws JsonProcessingException {
+ boolean valid = true;
+ if (!Strings.isNullOrEmpty(policyData.getServiceType())) {
+
+ modelRequiredFieldsList.clear();
+ pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON());
+
+ String service;
+ String version;
+ if (policyData.getServiceType().contains("-v")) {
+ service = policyData.getServiceType().split("-v")[0];
+ version = policyData.getServiceType().split("-v")[1];
+ } else {
+ service = policyData.getServiceType();
+ version = policyData.getVersion();
+ }
+
+ if (!Strings.isNullOrEmpty(version)) {
+ MicroServiceModels returnModel = getAttributeObject(service, version);
+
+ if (returnModel != null) {
+
+ String annotation = returnModel.getAnnotation();
+ String refAttributes = returnModel.getRefAttributes();
+ String subAttributes = returnModel.getSubAttributes();
+ String modelAttributes = returnModel.getAttributes();
+
+ if (!Strings.isNullOrEmpty(annotation)) {
+ Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annotation);
+ for (Entry<String, String> raMap : rangeMap.entrySet()) {
+ if (raMap.getValue().contains("range::")) {
+ String value = mapAttribute.get(raMap.getKey().trim());
+ String[] tempString = raMap.getValue().split("::")[1].split("-");
+ int startNum = Integer.parseInt(tempString[0]);
+ int endNum = Integer.parseInt(tempString[1]);
+ String returnString = "InvalidreturnModel Range:" + raMap.getKey() + " must be between "
+ + startNum + " - " + endNum + ",";
+
+ if (value != null) {
+ if (PolicyUtils.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;
+ }
+ } else {
+ responseString.append("<b>" + raMap.getKey() + "</b>:<i>" + raMap.getKey()
+ + " is required for the MicroService model " + service
+ + HTML_ITALICS_LNBREAK);
+ valid = false;
+ }
+
+ }
+ }
+ } else if (!DECISION_MS_MODEL.equals(policyData.getRuleProvider())) {
+ // Validate for configName, location, uuid, and policyScope if no annotations exist for this
+ // model
+ if (Strings.isNullOrEmpty(policyData.getLocation())) {
+ responseString.append("<b>Micro Service Model</b>:<i> location is required for this model"
+ + HTML_ITALICS_LNBREAK);
+ valid = false;
+ }
+
+ if (Strings.isNullOrEmpty(policyData.getConfigName())) {
+ responseString.append("<b>Micro Service Model</b>:<i> configName is required for this model"
+ + HTML_ITALICS_LNBREAK);
+ valid = false;
+ }
+
+ if (Strings.isNullOrEmpty(policyData.getUuid())) {
+ responseString.append("<b>Micro Service Model</b>:<i> uuid is required for this model"
+ + HTML_ITALICS_LNBREAK);
+ valid = false;
+ }
+
+ if (Strings.isNullOrEmpty(policyData.getPolicyScope())) {
+ responseString.append(
+ "<b>Micro Service Model</b>:<i> policyScope is required for this model"
+ + HTML_ITALICS_LNBREAK);
+ valid = false;
+ }
+ }
+
+ // If request comes from the API we need to validate required fields in the Micro Service Model
+ // GUI request are already validated from the SDK-APP
+ if ("API".equals(policyData.getApiflag())) {
+ // first , get the complete set of required fields
+ populateReqFieldSet(new String[]
+ { refAttributes, modelAttributes }, subAttributes);
+
+ // ignore req fields in which parent is not reqd
+ populateRequiredFields(new String[]
+ { refAttributes, modelAttributes }, subAttributes, modelAttributes);
+
+ if (modelRequiredFieldsList != null && !modelRequiredFieldsList.isEmpty()) {
+ // create jsonRequestMap with all json keys and values from request
+ JsonNode rootNode = (JsonNode) policyData.getPolicyJSON();
+ jsonRequestMap.clear();
+ pullModelJsonKeyPairs(rootNode);
+
+ // validate if the requiredFields are in the request
+ for (String requiredField : modelRequiredFieldsList) {
+ if (jsonRequestMap.containsKey(requiredField)) {
+ String value = jsonRequestMap.get(requiredField);
+ if (StringUtils.isBlank(value) || "\"\"".equals(value)) {
+ responseString.append("<b>Micro Service Model</b>:<i> " + requiredField
+ + ISREQUIRED + HTML_ITALICS_LNBREAK);
+ valid = false;
+ }
+ } else {
+ responseString.append("<b>Micro Service Model</b>:<i> " + requiredField + ISREQUIRED
+ + HTML_ITALICS_LNBREAK);
+ valid = false;
+ }
+ }
+ }