+ public PolicyType getGuardPolicy(Map<String, String> yamlParams) {
+ try {
+ ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard());
+ GuardPolicy policy1 = new GuardPolicy((policyAdapter.getUuid()!=null? policyAdapter.getUuid(): UUID.randomUUID().toString()) ,yamlParams.get("PolicyName"), yamlParams.get("description"), yamlParams.get("actor"), yamlParams.get("recipe"));
+ builder = builder.addGuardPolicy(policy1);
+ Map<String, String> time_in_range = new HashMap<String, String>();
+ time_in_range.put("arg2", yamlParams.get("guardActiveStart"));
+ time_in_range.put("arg3", yamlParams.get("guardActiveEnd"));
+ Constraint cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), yamlParams.get("timeWindow"), time_in_range);
+ builder = builder.addLimitConstraint(policy1.id, cons);
+ // Build the specification
+ Results results = builder.buildSpecification();
+ // YAML TO XACML
+ ControlLoopGuard yamlGuardObject = SafePolicyBuilder.loadYamlGuard(results.getSpecification());
+ Path xacmlTemplatePath = Paths.get(XACMLTEMPLATE);
+ String xacmlTemplateContent;
+ try {
+ xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath));
+ HashMap<String, String> yamlSpecs = new HashMap<String, String>();
+ yamlSpecs.put("PolicyName", yamlParams.get("PolicyName"));
+ yamlSpecs.put("description", yamlParams.get("description"));
+ yamlSpecs.put("ECOMPName", yamlParams.get("ECOMPName"));
+ yamlSpecs.put("actor", yamlGuardObject.guards.getFirst().actor);
+ yamlSpecs.put("recipe", yamlGuardObject.guards.getFirst().recipe);
+ yamlSpecs.put("limit", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().num.toString());
+ yamlSpecs.put("timeWindow", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().duration);
+ yamlSpecs.put("guardActiveStart", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg2"));
+ yamlSpecs.put("guardActiveEnd", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg3"));
+ String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent,yamlSpecs);
+ // 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) {
+ PolicyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage());
+ }
+ } catch (BuilderException e) {
+ PolicyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage());
+ }
+ return null;
+ }
+