+ /**
+ * Format Operational Policy yaml.
+ *
+ * @param refProp
+ * @param prop
+ * @return
+ * @throws BuilderException
+ * @throws UnsupportedEncodingException
+ */
+ private static String formatYaml(RefProp refProp, 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 = convertToResource(global.getResourceVf(), ResourceType.VF);
+ Resource[] vfcResources = convertToResource(global.getResourceVfc(), ResourceType.VFC);
+
+ // create builder
+ ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(prop.getControlName(),
+ policyChain.getTimeout(), service, vfResources);
+ builder.addResource(vfcResources);
+
+ // process each policy
+ HashMap<String, Policy> policyObjMap = new HashMap<>();
+ List<PolicyItem> policyItemList = orderParentFirst(policyChain.getPolicyItems());
+ for (PolicyItem policyItem : policyItemList) {
+ String policyName = policyItem.getRecipe() + " Policy";
+ Target target = new Target();
+ target.setType(TargetType.VM);
+ target.setResourceID(policyItem.getTargetResourceId());
+ Policy policyObj;
+ if (policyItemList.indexOf(policyItem) == 0) {
+ String policyDescription = policyItem.getRecipe()
+ + " Policy - the trigger (no parent) policy - created by CLDS";
+ policyObj = builder.setTriggerPolicy(policyName, policyDescription,
+ refProp.getStringValue("op.policy.appc"), target, policyItem.getRecipe(), null,
+ policyItem.getMaxRetries(), policyItem.getRetryTimeLimit());
+ } else {
+ Policy parentPolicyObj = policyObjMap.get(policyItem.getParentPolicy());
+ String policyDescription = policyItem.getRecipe() + " Policy - triggered conditionally by "
+ + parentPolicyObj.getName() + " - created by CLDS";
+ policyObj = builder.setPolicyForPolicyResult(policyName, policyDescription,
+ refProp.getStringValue("op.policy.appc"), target, policyItem.getRecipe(), null,
+ policyItem.getMaxRetries(), policyItem.getRetryTimeLimit(), parentPolicyObj.getId(),
+ convertToPolicyResult(policyItem.getParentPolicyConditions()));
+
+ logger.info("policyObj.id=" + policyObj.getId() + "; parentPolicyObj.id=" + parentPolicyObj.getId());
+ }
+ policyObjMap.put(policyItem.getId(), policyObj);
+ }
+
+ //
+ // Build the specification
+ //
+ Results results = builder.buildSpecification();
+ if (results.isValid()) {
+ logger.info("results.getSpecification()=" + results.getSpecification());
+ } else {
+ // throw exception with error info
+ StringBuilder sb = new StringBuilder();
+ sb.append("Operation Policy validation problem: ControlLoopPolicyBuilder failed with following messages: ");
+ for (Message message : results.getMessages()) {
+ sb.append(message.getMessage());
+ sb.append("; ");
+ }
+ throw new BadRequestException(sb.toString());
+ }
+ return URLEncoder.encode(results.getSpecification(), "UTF-8");
+ }