+
+ private PolicyRestAdapter populateConfigOptimizationParameters(PolicyParameters parameters,
+ PolicyRestAdapter policyData, JsonObject json) {
+ policyData.setConfigPolicyType("Optimization");
+
+ // get values and attributes from the JsonObject
+ if (json != null) {
+ return getJsonObjectValuesAndAttributes(parameters, policyData, json);
+ } else {
+ return policyData;
+ }
+ }
+
+ private PolicyRestAdapter populateConfigClosedLoopFaultParameters(PolicyRestAdapter policyData, JsonObject json) {
+ policyData.setConfigPolicyType("ClosedLoop_Fault");
+
+ if (json != null) {
+ policyData.setJsonBody(json.toString());
+ policyData.setOnapName(getNewOrExistingKeyValue(json, ONAPNAME, policyData.getOnapName()));
+ }
+ return policyData;
+ }
+
+ private PolicyRestAdapter populateConfigClosedLoopPmParameters(PolicyRestAdapter policyData, JsonObject json) {
+ policyData.setConfigPolicyType("ClosedLoop_PM");
+
+ if (json != null) {
+ policyData.setJsonBody(json.toString());
+ policyData.setOnapName(getNewOrExistingKeyValue(json, ONAPNAME, policyData.getOnapName()));
+ if (json.get(SERVICETYPE_POLICY_NAME) != null) {
+ String serviceType = json.get(SERVICETYPE_POLICY_NAME).toString().replace("\"", "");
+ LinkedHashMap<String, String> serviceTypePolicyName = new LinkedHashMap<>();
+ serviceTypePolicyName.put(SERVICETYPE_POLICY_NAME, serviceType);
+ policyData.setServiceTypePolicyName(serviceTypePolicyName);
+ }
+ }
+ return policyData;
+ }
+
+ private PolicyRestAdapter populateConfigBrmsParameters(PolicyParameters parameters, PolicyRestAdapter policyData) {
+ Map<AttributeType, Map<String, String>> drlRuleAndUiParams = parameters.getAttributes();
+ Map<String, String> rule = drlRuleAndUiParams.get(AttributeType.RULE);
+ policyData.setRuleName(rule.get("templateName"));
+
+ return policyData;
+ }
+
+ private PolicyRestAdapter populateDecisionParameters(PolicyParameters parameters, PolicyRestAdapter policyData,
+ Map<String, String> matching) {
+ policyData.setRuleProvider(parameters.getRuleProvider().toString());
+
+ switch (parameters.getRuleProvider()) {
+ case RAINY_DAY:
+ return populateDecisionRainyDayParameters(parameters, policyData, matching);
+
+ case GUARD_BL_YAML:
+ case GUARD_MIN_MAX:
+ case GUARD_YAML:
+ return populateDecisionGuardParameters(policyData, matching);
+
+ case AAF:
+ case CUSTOM:
+ case RAW:
+ default:
+ return policyData;
+ }
+ }
+
+ private PolicyRestAdapter populateDecisionRainyDayParameters(PolicyParameters parameters,
+ PolicyRestAdapter policyData, Map<String, String> matching) {
+ // Set Matching attributes in RainyDayParams in adapter
+ RainyDayParams rainyday = new RainyDayParams();
+
+ if (matching != null) {
+ rainyday.setServiceType(matching.get("ServiceType"));
+ rainyday.setVnfType(matching.get("VNFType"));
+ rainyday.setBbid(matching.get("BB_ID"));
+ rainyday.setWorkstep(matching.get("WorkStep"));
+ }
+
+ Map<String, String> treatments = parameters.getTreatments();
+ ArrayList<Object> treatmentsTableChoices = new ArrayList<>();
+
+ for (String keyField : treatments.keySet()) {
+ LinkedHashMap<String, String> treatmentMap = new LinkedHashMap<>();
+ String errorcode = keyField;
+ String treatment = treatments.get(errorcode);
+ treatmentMap.put("errorcode", errorcode);
+ treatmentMap.put("treatment", treatment);
+ treatmentsTableChoices.add(treatmentMap);
+ }
+ rainyday.setTreatmentTableChoices(treatmentsTableChoices);
+ policyData.setRainyday(rainyday);
+
+ return policyData;
+ }
+
+ private PolicyRestAdapter populateDecisionGuardParameters(PolicyRestAdapter policyData,
+ Map<String, String> matching) {
+ // Set Matching attributes in YAMLParams in adapter
+ YAMLParams yamlparams = new YAMLParams();
+
+ if (matching == null) {
+ policyData.setYamlparams(yamlparams);
+ return policyData;
+ }
+
+ yamlparams.setActor(matching.get("actor"));
+ yamlparams.setRecipe(matching.get("recipe"));
+ yamlparams.setGuardActiveStart(matching.get("guardActiveStart"));
+ yamlparams.setGuardActiveEnd(matching.get("guardActiveEnd"));
+
+ yamlparams.setLimit(matching.get("limit"));
+ yamlparams.setTimeWindow(matching.get("timeWindow"));
+ yamlparams.setTimeUnits(matching.get("timeUnits"));
+
+ yamlparams.setMin(matching.get("min"));
+ yamlparams.setMax(matching.get("max"));
+
+ List<String> blackList = new ArrayList<>();
+
+ if (!Strings.isNullOrEmpty(matching.get("blackList"))) {
+ String[] blackListArray = matching.get("blackList").split(",");
+ for (String element : blackListArray) {
+ blackList.add(element);
+ }
+ }
+
+ yamlparams.setBlackList(blackList);
+
+ policyData.setYamlparams(yamlparams);
+ return policyData;
+ }
+
+ private PolicyRestAdapter populateActionParameters(PolicyParameters parameters, PolicyRestAdapter policyData,
+ Map<String, String> matching) {
+ ArrayList<Object> ruleAlgorithmChoices = new ArrayList<>();
+
+ List<String> dynamicLabelRuleAlgorithms = parameters.getDynamicRuleAlgorithmLabels();
+ List<String> dynamicFieldFunctionRuleAlgorithms = parameters.getDynamicRuleAlgorithmFunctions();
+ List<String> dynamicFieldOneRuleAlgorithms = parameters.getDynamicRuleAlgorithmField1();
+ List<String> dynamicFieldTwoRuleAlgorithms = parameters.getDynamicRuleAlgorithmField2();
+
+ if (dynamicLabelRuleAlgorithms != null && !dynamicLabelRuleAlgorithms.isEmpty()) {
+
+ for (int i = dynamicLabelRuleAlgorithms.size() - 1; i >= 0; i--) {
+ LinkedHashMap<String, String> ruleAlgorithm = new LinkedHashMap<>();
+
+ String id = dynamicLabelRuleAlgorithms.get(i);
+ String dynamicRuleAlgorithmField1 = dynamicFieldOneRuleAlgorithms.get(i);
+ String dynamicRuleAlgorithmCombo = dynamicFieldFunctionRuleAlgorithms.get(i);
+ String dynamicRuleAlgorithmField2 = dynamicFieldTwoRuleAlgorithms.get(i);
+
+ ruleAlgorithm.put("id", id);
+ ruleAlgorithm.put("dynamicRuleAlgorithmField1", dynamicRuleAlgorithmField1);
+ ruleAlgorithm.put("dynamicRuleAlgorithmCombo", dynamicRuleAlgorithmCombo);
+ ruleAlgorithm.put("dynamicRuleAlgorithmField2", dynamicRuleAlgorithmField2);
+
+ ruleAlgorithmChoices.add(ruleAlgorithm);
+ }
+ }
+
+ policyData.setRuleAlgorithmschoices(ruleAlgorithmChoices);
+
+ ArrayList<Object> attributeList = new ArrayList<>();
+ if (matching != null) {
+ for (Map.Entry<String, String> entry : matching.entrySet()) {
+ LinkedHashMap<String, String> attributeMap = new LinkedHashMap<>();
+ String key = entry.getKey();
+ String value = entry.getValue();
+ attributeMap.put("key", key);
+ attributeMap.put("value", value);
+ attributeList.add(attributeMap);
+ }
+ }
+
+ policyData.setAttributes(attributeList);
+ policyData.setActionAttributeValue(parameters.getActionAttribute());
+ policyData.setActionPerformer(parameters.getActionPerformer());
+
+ return policyData;
+ }
+
+ private PolicyRestAdapter getJsonObjectValuesAndAttributes(PolicyParameters parameters,
+ PolicyRestAdapter policyData, JsonObject json) {
+ if (json.containsKey(CONTENT)) {
+ String content = json.get(CONTENT).toString();
+ JsonNode policyJson = null;
+ try {
+ policyJson = new ObjectMapper().readTree(content);
+ } catch (IOException e) {
+ String message = XACMLErrorConstants.ERROR_DATA_ISSUE + INVALIDJSON + parameters.getConfigBody();
+ LOGGER.error(message, e);
+ return null;
+ }
+ policyData.setPolicyJSON(policyJson);
+ }
+
+ // @formatter:off
+ policyData.setServiceType(getNewOrExistingKeyValue(json, SERVICE, policyData.getServiceType()));
+ policyData.setUuid(getNewOrExistingKeyValue( json, UUID, policyData.getUuid()));
+ policyData.setLocation(getNewOrExistingKeyValue( json, LOCATION, policyData.getLocation()));
+ policyData.setConfigName(getNewOrExistingKeyValue( json, CONFIG_NAME, policyData.getConfigName()));
+ policyData.setPriority(getNewOrExistingKeyValue( json, PRIORITY, policyData.getPriority()));
+ policyData.setVersion(getNewOrExistingKeyValue( json, VERSION, policyData.getVersion()));
+ policyData.setPolicyScope(getNewOrExistingKeyValue(json, POLICYSCOPE, policyData.getPolicyScope()));
+ policyData.setRiskType(getNewOrExistingKeyValue( json, RISKTYPE, policyData.getRiskType()));
+ policyData.setRiskLevel(getNewOrExistingKeyValue( json, RISKLEVEL, policyData.getRiskLevel()));
+ policyData.setGuard(getNewOrExistingKeyValue( json, GUARD, policyData.getGuard()));
+ // @formatter:on
+
+ return policyData;
+ }
+
+ private String getNewOrExistingKeyValue(final JsonObject json, final String key, final String existingValue) {
+ if (json.containsKey(key)) {
+ return json.get(key).toString().replace("\"", "");
+ } else {
+ return existingValue;
+ }
+ }
+
+ private Map<String, String> getMatchingAttributeValues(PolicyParameters parameters) {
+ // Get Matching attribute values
+ Map<AttributeType, Map<String, String>> attributes = parameters.getAttributes();
+
+ if (attributes != null) {
+ return attributes.get(AttributeType.MATCHING);
+ }
+ return null;
+ }
+
+ private JsonObject stringToJsonObject(String value) {
+ try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
+ return jsonReader.readObject();
+ } catch (JsonException | IllegalStateException jsonHandlingException) {
+ LOGGER.info(XACMLErrorConstants.ERROR_DATA_ISSUE
+ + "Improper JSON format... may or may not cause issues in validating the policy: " + value,
+ jsonHandlingException);
+ throw jsonHandlingException;
+ }
+ }
+