- @SuppressWarnings("unchecked")
- protected PolicyType convertPolicy(Entry<String, Object> entrySet) throws ToscaPolicyConversionException {
- //
- // Policy name should be at the root
- //
- String policyName = entrySet.getKey();
- Map<String, Object> policyDefinition = (Map<String, Object>) entrySet.getValue();
- //
- // Set it as the policy ID
- //
- PolicyType newPolicyType = new PolicyType();
- newPolicyType.setPolicyId(policyName);
- //
- // Optional description
- //
- if (policyDefinition.containsKey("description")) {
- newPolicyType.setDescription(policyDefinition.get("description").toString());
- }
- //
- // There should be a metadata section
- //
- if (! policyDefinition.containsKey("metadata")) {
- throw new ToscaPolicyConversionException(policyName + " missing metadata section");
- }
- this.fillMetadataSection(newPolicyType,
- (Map<String, Object>) policyDefinition.get("metadata"));
- //
- // Set the combining rule
- //
- newPolicyType.setRuleCombiningAlgId(XACML3.ID_RULE_FIRST_APPLICABLE.stringValue());
- //
- // Generate the TargetType
- //
- if (! policyDefinition.containsKey("type")) {
- throw new ToscaPolicyConversionException(policyName + " missing type value");
- }
- if (! policyDefinition.containsKey("version")) {
- throw new ToscaPolicyConversionException(policyName + " missing version value");
- }
- TargetType target = this.generateTargetType(policyName,
- policyDefinition.get("type").toString(),
- policyDefinition.get("version").toString());
- newPolicyType.setTarget(target);
- //
- // Now create the Permit Rule
- // No target since the policy has a target
- // With obligations.
- //
- RuleType rule = new RuleType();
- rule.setDescription("Default is to PERMIT if the policy matches.");
- rule.setRuleId(policyName + ":rule");
- rule.setEffect(EffectType.PERMIT);
- rule.setTarget(new TargetType());
- //
- // Now represent the policy as Json
- //
- JSONObject jsonObligation = new JSONObject();
- jsonObligation.put(policyName, policyDefinition);
- addObligation(rule, jsonObligation);
- //
- // Add the rule to the policy
- //
- newPolicyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
- //
- // Return our new policy
- //
- return newPolicyType;
- }
-