+ public PolicyType getGuardPolicy(Map<String, String> yamlParams, String ruleProvider) {
+ try {
+ ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
+ GuardPolicy policy1 = new GuardPolicy((policyAdapter.getUuid()!=null? policyAdapter.getUuid(): UUID.randomUUID().toString()) ,yamlParams.get(POLICY_NAME), yamlParams.get(DESCRIPTION), yamlParams.get("actor"), yamlParams.get("recipe"));
+ builder = builder.addGuardPolicy(policy1);
+ Map<String, String> time_in_range = new HashMap<>();
+ time_in_range.put("arg2", yamlParams.get("guardActiveStart"));
+ time_in_range.put("arg3", yamlParams.get("guardActiveEnd"));
+ String blackListString = yamlParams.get("blackList");
+ List<String> blackList = null;
+ if(blackListString!=null){
+ if (blackListString.contains(",")){
+ blackList = Arrays.asList(blackListString.split(","));
+ }
+ else{
+ blackList = new ArrayList<>();
+ blackList.add(blackListString);
+ }
+ }
+ File templateFile;
+ Path xacmlTemplatePath;
+ Constraint cons;
+ ClassLoader classLoader = getClass().getClassLoader();
+ switch (ruleProvider){
+ case GUARD_BL_YAML:
+ templateFile = new File(classLoader.getResource(XACML_BLGUARD_TEMPLATE).getFile());
+ xacmlTemplatePath = templateFile.toPath();
+ cons = new Constraint(time_in_range,blackList);
+ break;
+ default:
+ templateFile = new File(classLoader.getResource(XACML_GUARD_TEMPLATE).getFile());
+ xacmlTemplatePath = templateFile.toPath();
+ cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), yamlParams.get("timeWindow"), time_in_range);
+ break;
+ }
+ builder = builder.addLimitConstraint(policy1.getId(), cons);
+ // Build the specification
+ Results results = builder.buildSpecification();
+ // YAML TO XACML
+ ControlLoopGuard yamlGuardObject = SafePolicyBuilder.loadYamlGuard(results.getSpecification());
+
+ String xacmlTemplateContent;
+ try {
+ xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath));
+ HashMap<String, String> yamlSpecs = new HashMap<>();
+ yamlSpecs.put(POLICY_NAME, yamlParams.get(POLICY_NAME));
+ yamlSpecs.put(DESCRIPTION, yamlParams.get(DESCRIPTION));
+ yamlSpecs.put(ECOMPNAME, yamlParams.get(ECOMPNAME));
+ yamlSpecs.put("actor", yamlGuardObject.getGuards().getFirst().getActor());
+ yamlSpecs.put("recipe", yamlGuardObject.getGuards().getFirst().getRecipe());
+ if(yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getNum()!=null){
+ yamlSpecs.put("limit", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getNum().toString());
+ }
+ if(yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getDuration()!=null){
+ yamlSpecs.put("timeWindow", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getDuration());
+ }
+ yamlSpecs.put("guardActiveStart", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getTime_in_range().get("arg2"));
+ yamlSpecs.put("guardActiveEnd", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getTime_in_range().get("arg3"));
+ String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent,yamlSpecs, yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getBlacklist());
+ // Convert the Policy into Stream input to Policy Adapter.
+ Object policy = XACMLPolicyScanner.readPolicy(new ByteArrayInputStream(xacmlPolicyContent.getBytes(StandardCharsets.UTF_8)));
+ return (PolicyType) policy;
+ } catch (IOException e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage() + e);
+ }
+ } catch (BuilderException e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage() +e);
+ }
+ return null;
+ }
+