X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2Freq%2Fpolicy%2FOperationalPolicyYamlFormatter.java;h=1fc3608203b05373442f6936eb7287b9e6048d01;hb=d1ff5b9dcfce8c3a69b80832ad2cbe4dbaf1d9cc;hp=550c4dccdfd81e06374d1f78bfe2571c8f8fcb27;hpb=6651e17eab83a2231e1502f8083689f0ddc4751b;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatter.java b/src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatter.java index 550c4dcc..1fc36082 100644 --- a/src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatter.java +++ b/src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatter.java @@ -25,6 +25,17 @@ package org.onap.clamp.clds.client.req.policy; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.BadRequestException; + import org.onap.clamp.clds.model.properties.Global; import org.onap.clamp.clds.model.properties.ModelProperties; import org.onap.clamp.clds.model.properties.PolicyChain; @@ -40,24 +51,43 @@ import org.onap.policy.controlloop.policy.builder.Results; import org.onap.policy.sdc.Resource; import org.onap.policy.sdc.ResourceType; import org.onap.policy.sdc.Service; -import org.springframework.stereotype.Component; -import javax.ws.rs.BadRequestException; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -@Component -class OperationalPolicyYamlFormatter { +public class OperationalPolicyYamlFormatter { private static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicyYamlFormatter.class); + protected OperationalPolicyYamlFormatter() { + } + /** + * Format Operational OpenLoop Policy yaml. + * + * @param prop + * @param modelElementId + * @param policyChain + * @return + * @throws BuilderException + * @throws UnsupportedEncodingException + */ + public static String formatOpenLoopYaml(ModelProperties prop, String modelElementId, + PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { + // get property objects + Global global = prop.getGlobal(); + prop.setCurrentModelElementId(modelElementId); + prop.setPolicyUniqueId(policyChain.getPolicyId()); + // convert values to SDC objects + Service service = new Service(global.getService()); + Resource[] vfResources = convertToResources(global.getResourceVf(), ResourceType.VF); + // create builder + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(prop.getControlName(), + policyChain.getTimeout(), service, vfResources); + // builder.setTriggerPolicy(refProp.getStringValue("op.openloop.policy")); + // Build the specification + Results results = builder.buildSpecification(); + validate(results); + return URLEncoder.encode(results.getSpecification(), "UTF-8"); + } - String formatYaml(ModelProperties prop, String modelElementId, - PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { + public static String formatYaml(ModelProperties prop, String modelElementId, + PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { // get property objects Global global = prop.getGlobal(); prop.setCurrentModelElementId(modelElementId); @@ -68,10 +98,10 @@ class OperationalPolicyYamlFormatter { Resource[] vfcResources = convertToResources(global.getResourceVfc(), ResourceType.VFC); // create builder ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(prop.getControlName(), - policyChain.getTimeout(), service, vfResources); + policyChain.getTimeout(), service, vfResources); builder.addResource(vfcResources); // process each policy - Map policyObjMap = new HashMap(); + Map policyObjMap = new HashMap<>(); List policyItemList = orderParentFirst(policyChain.getPolicyItems()); for (PolicyItem policyItem : policyItemList) { String policyName = policyItem.getRecipe() + " Policy"; @@ -87,16 +117,16 @@ class OperationalPolicyYamlFormatter { Policy policyObj; if (policyItemList.indexOf(policyItem) == 0) { String policyDescription = policyItem.getRecipe() - + " Policy - the trigger (no parent) policy - created by CLDS"; + + " Policy - the trigger (no parent) policy - created by CLDS"; policyObj = builder.setTriggerPolicy(policyName, policyDescription, actor, target, - policyItem.getRecipe(), payloadMap, policyItem.getMaxRetries(), policyItem.getRetryTimeLimit()); + policyItem.getRecipe(), payloadMap, policyItem.getMaxRetries(), policyItem.getRetryTimeLimit()); } else { Policy parentPolicyObj = policyObjMap.get(policyItem.getParentPolicy()); String policyDescription = policyItem.getRecipe() + " Policy - triggered conditionally by " - + parentPolicyObj.getName() + " - created by CLDS"; + + parentPolicyObj.getName() + " - created by CLDS"; policyObj = builder.setPolicyForPolicyResult(policyName, policyDescription, actor, target, - policyItem.getRecipe(), payloadMap, policyItem.getMaxRetries(), policyItem.getRetryTimeLimit(), - parentPolicyObj.getId(), convertToPolicyResults(policyItem.getParentPolicyConditions())); + policyItem.getRecipe(), payloadMap, policyItem.getMaxRetries(), policyItem.getRetryTimeLimit(), + parentPolicyObj.getId(), convertToPolicyResults(policyItem.getParentPolicyConditions())); logger.info("policyObj.id=" + policyObj.getId() + "; parentPolicyObj.id=" + parentPolicyObj.getId()); } policyObjMap.put(policyItem.getId(), policyObj); @@ -107,7 +137,7 @@ class OperationalPolicyYamlFormatter { return URLEncoder.encode(results.getSpecification(), "UTF-8"); } - private List orderParentFirst(List inOrigList) { + protected static List orderParentFirst(List inOrigList) { List inList = new ArrayList<>(); inList.addAll(inOrigList); List outList = new ArrayList<>(); @@ -129,7 +159,7 @@ class OperationalPolicyYamlFormatter { if (parent == null || parent.length() == 0) { if (!outList.isEmpty()) { throw new BadRequestException( - "Operation Policy validation problem: more than one trigger policy"); + "Operation Policy validation problem: more than one trigger policy"); } else { outList.add(inItem); inListItr.remove(); @@ -151,7 +181,7 @@ class OperationalPolicyYamlFormatter { return outList; } - private void validate(Results results) { + protected static void validate(Results results) { if (results.isValid()) { logger.info("results.getSpecification()=" + results.getSpecification()); } else { @@ -167,14 +197,14 @@ class OperationalPolicyYamlFormatter { } - Resource[] convertToResources(List stringList, ResourceType resourceType) { + protected static Resource[] convertToResources(List stringList, ResourceType resourceType) { if (stringList == null || stringList.isEmpty()) { return new Resource[0]; } return stringList.stream().map(stringElem -> new Resource(stringElem, resourceType)).toArray(Resource[]::new); } - PolicyResult[] convertToPolicyResults(List prList) { + protected static PolicyResult[] convertToPolicyResults(List prList) { if (prList == null || prList.isEmpty()) { return new PolicyResult[0]; }