ClosedLoopPolicy extends Policy { @Override public boolean prepareToSave() throws PAPException { if (isPreparedToSave()) { return true; } int version = 0; String policyID = policyAdapter.getPolicyID(); version = policyAdapter.getHighestVersion(); if (policyAdapter.getPolicyType().equals("Config")) { PolicyType policyConfig = new PolicyType(); policyConfig.setVersion(Integer.toString(version)); policyConfig.setPolicyId(policyID); policyConfig.setTarget(new TargetType()); policyAdapter.setData(policyConfig); } policyName = policyAdapter.getNewFileName(); if (policyAdapter.getData() != null) { String jsonBody = policyAdapter.getJsonBody(); saveConfigurations(policyName, jsonBody); if (policyName.endsWith(".xml") == false) { policyName = policyName + ".xml"; } PolicyType faultPolicy = (PolicyType) policyAdapter.getData(); faultPolicy.setDescription(policyAdapter.getPolicyDescription()); faultPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); AllOfType allOfOne = new AllOfType(); String fileName = policyAdapter.getNewFileName(); String name = fileName.substring(fileName.lastIndexOf("\\") + 1); if ((name == null) || (name.equals(""))) { name = fileName.substring(fileName.lastIndexOf("/") + 1); } allOfOne.getMatch().add(createMatch("PolicyName", name)); AllOfType allOf = new AllOfType(); allOf.getMatch().add(createMatch("ONAPName", policyAdapter.getOnapName())); allOf.getMatch().add(createDynamicMatch("RiskType", policyAdapter.getRiskType())); allOf.getMatch().add(createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel()))); allOf.getMatch().add(createDynamicMatch("guard", policyAdapter.getGuard())); allOf.getMatch().add(createDynamicMatch("TTLDate", policyAdapter.getTtlDate())); AnyOfType anyOf = new AnyOfType(); anyOf.getAllOf().add(allOfOne); anyOf.getAllOf().add(allOf); TargetType target = new TargetType(); ((TargetType) target).getAnyOf().add(anyOf); faultPolicy.setTarget((TargetType) target); RuleType rule = new RuleType(); rule.setRuleId(policyAdapter.getRuleID()); rule.setEffect(EffectType.PERMIT); AllOfType allOfInRule = new AllOfType(); MatchType accessMatch = new MatchType(); AttributeValueType accessAttributeValue = new AttributeValueType(); accessAttributeValue.setDataType(STRING_DATATYPE); accessAttributeValue.getContent().add("ACCESS"); accessMatch.setAttributeValue(accessAttributeValue); AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType(); URI accessURI = null; try { accessURI = new URI(ACTION_ID); } catch (URISyntaxException e) { PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "CreateClosedLoopPolicy", "Exception creating ACCESS URI"); } accessAttributeDesignator.setCategory(CATEGORY_ACTION); accessAttributeDesignator.setDataType(STRING_DATATYPE); accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue()); accessMatch.setAttributeDesignator(accessAttributeDesignator); accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE); MatchType closedMatch = new MatchType(); AttributeValueType closedAttributeValue = new AttributeValueType(); closedAttributeValue.setDataType(STRING_DATATYPE); closedAttributeValue.getContent().add("Config"); closedMatch.setAttributeValue(closedAttributeValue); AttributeDesignatorType closedAttributeDesignator = new AttributeDesignatorType(); URI closedURI = null; try { closedURI = new URI(RESOURCE_ID); } catch (URISyntaxException e) { PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "CreateClosedLoopPolicy", "Exception creating closed URI"); } closedAttributeDesignator.setCategory(CATEGORY_RESOURCE); closedAttributeDesignator.setDataType(STRING_DATATYPE); closedAttributeDesignator.setAttributeId(new IdentifierImpl(closedURI).stringValue()); closedMatch.setAttributeDesignator(closedAttributeDesignator); closedMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE); allOfInRule.getMatch().add(accessMatch); allOfInRule.getMatch().add(closedMatch); AnyOfType anyOfInRule = new AnyOfType(); anyOfInRule.getAllOf().add(allOfInRule); TargetType targetInRule = new TargetType(); targetInRule.getAnyOf().add(anyOfInRule); rule.setTarget(targetInRule); rule.setAdviceExpressions(getAdviceExpressions(version, policyName)); faultPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule); policyAdapter.setPolicyData(faultPolicy); } else { PolicyLogger.error("Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName()); } setPreparedToSave(true); return true; } ClosedLoopPolicy();  ClosedLoopPolicy(PolicyRestAdapter policyAdapter); @Override Map<String, String> savePolicies(); @Override boolean prepareToSave(); @Override Object getCorrectPolicyDataObject();  }
@Test public void testPrepareToSave() throws PAPException { PolicyRestAdapter policyAdapter = new PolicyRestAdapter(); ClosedLoopPolicy policy = new ClosedLoopPolicy(policyAdapter); policyAdapter.setHighestVersion(1); policyAdapter.setPolicyType("Config"); policyAdapter.setNewFileName("foo.xml"); policy.prepareToSave(); assertEquals(policy.isPreparedToSave(), true); }