package org.onap.clamp.clds.client.req;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFLogger.Level;
+import com.att.eelf.configuration.EELFManager;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import org.jboss.resteasy.spi.BadRequestException;
+import javax.ws.rs.BadRequestException;
+
import org.onap.clamp.clds.model.prop.Global;
import org.onap.clamp.clds.model.prop.ModelProperties;
import org.onap.clamp.clds.model.prop.PolicyChain;
import org.onap.clamp.clds.model.prop.Tca;
import org.onap.clamp.clds.model.refprop.RefProp;
import org.onap.policy.api.AttributeType;
-import org.onap.policy.asdc.Resource;
-import org.onap.policy.asdc.ResourceType;
-import org.onap.policy.asdc.Service;
+import org.onap.policy.sdc.Resource;
+import org.onap.policy.sdc.ResourceType;
+import org.onap.policy.sdc.Service;
import org.onap.policy.controlloop.policy.OperationsAccumulateParams;
import org.onap.policy.controlloop.policy.Policy;
import org.onap.policy.controlloop.policy.PolicyResult;
import org.onap.policy.controlloop.policy.builder.Message;
import org.onap.policy.controlloop.policy.builder.Results;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFLogger.Level;
-import com.att.eelf.configuration.EELFManager;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-
/**
* Construct an Operational Policy request given CLDS objects.
*/
protected static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicyReq.class);
protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
+ private OperationalPolicyReq() {
+
+ }
+
/**
* Format Operational Policy attributes.
*
String operationTopic = "";
String notificationTopic = "";
String controller = "";
- Tca tca = prop.getTca();
- if (tca.isFound()) {
+ Tca tca = prop.getType(Tca.class);
+ if (tca != null && tca.isFound()) {
if (!global.getActionSet().equalsIgnoreCase("enbRecipe")) {
throw new BadRequestException(
"Operation Policy validation problem: action set is not selected properly.");
String recipeTopic = refProp.getStringValue("op.recipeTopic", global.getService());
// ruleAttributes
- Map<String, String> ruleAttributes = new HashMap<String, String>();
+ Map<String, String> ruleAttributes = new HashMap<>();
- if (operationTopic == null || operationTopic.length() == 0) {
+ if (operationTopic == null || operationTopic.isEmpty()) {
logger.info("templateName=" + templateName);
logger.info("recipeTopic=" + recipeTopic);
logger.info("notificationTopic=" + notificationTopic);
String recipe = policyItem.getRecipe();
String maxRetries = String.valueOf(policyItem.getMaxRetries());
String retryTimeLimit = String.valueOf(policyItem.getRetryTimeLimit());
+ String targetResourceId = String.valueOf(policyItem.getTargetResourceId());
logger.info("recipe=" + recipe);
logger.info("maxRetries=" + maxRetries);
logger.info("retryTimeLimit=" + retryTimeLimit);
+ logger.info("targetResourceId=" + targetResourceId);
ruleAttributes.put("Recipe", recipe);
ruleAttributes.put("MaxRetries", maxRetries);
ruleAttributes.put("RetryTimeLimit", retryTimeLimit);
+ ruleAttributes.put("ResourceId", targetResourceId);
+
} else {
logger.info("templateName=" + templateName);
logger.info("operationTopic=" + operationTopic);
logger.info("notificationTopic=" + notificationTopic);
// format yaml
- String yaml = tca.isFound() ? formateNodeBYaml(refProp, prop, modelElementId, policyChain)
+ String yaml = (tca != null && tca.isFound()) ? formateNodeBYaml(refProp, prop, modelElementId, policyChain)
: formatYaml(refProp, prop, modelElementId, policyChain);
ruleAttributes.put("templateName", templateName);
}
// matchingAttributes
- Map<String, String> matchingAttributes = new HashMap<String, String>();
+ Map<String, String> matchingAttributes = new HashMap<>();
matchingAttributes.put("controller", controller);
- Map<AttributeType, Map<String, String>> attributes = new HashMap<AttributeType, Map<String, String>>();
+ Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
attributes.put(AttributeType.RULE, ruleAttributes);
attributes.put(AttributeType.MATCHING, matchingAttributes);
* @throws BuilderException
* @throws UnsupportedEncodingException
*/
- private static String formatYaml(RefProp refProp, ModelProperties prop, String modelElementId,
+ public static String formatYaml(RefProp refProp, ModelProperties prop, String modelElementId,
PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException {
// get property objects
builder.addResource(vfcResources);
// process each policy
- HashMap<String, org.onap.policy.controlloop.policy.Policy> policyObjMap = new HashMap<String, org.onap.policy.controlloop.policy.Policy>();
+ Map<String, Policy> policyObjMap = new HashMap<>();
List<PolicyItem> policyItemList = orderParentFirst(policyChain.getPolicyItems());
- Target target = new Target();
- target.setType(TargetType.VM);
- for (int i = 0; i < policyItemList.size(); i++) {
-
- org.onap.policy.controlloop.policy.Policy policyObj;
- PolicyItem policyItem = policyItemList.get(i);
+ for (PolicyItem policyItem : policyItemList) {
String policyName = policyItem.getRecipe() + " Policy";
- if (i == 0) {
+ 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 {
- org.onap.policy.controlloop.policy.Policy parentPolicyObj = policyObjMap
- .get(policyItem.getParentPolicy());
+ Policy parentPolicyObj = policyObjMap.get(policyItem.getParentPolicy());
String policyDescription = policyItem.getRecipe() + " Policy - triggered conditionally by "
+ parentPolicyObj.getName() + " - created by CLDS";
policyObj = builder.setPolicyForPolicyResult(policyName, policyDescription,
logger.info("results.getSpecification()=" + results.getSpecification());
} else {
// throw exception with error info
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
sb.append("Operation Policy validation problem: ControlLoopPolicyBuilder failed with following messages: ");
for (Message message : results.getMessages()) {
sb.append(message.getMessage());
}
throw new BadRequestException(sb.toString());
}
-
- String encodedYaml = URLEncoder.encode(results.getSpecification(), "UTF-8");
-
- return encodedYaml;
+ return URLEncoder.encode(results.getSpecification(), "UTF-8");
}
/**
* @throws BuilderException
* @throws UnsupportedEncodingException
*/
- private static String formateNodeBYaml(RefProp refProp, ModelProperties prop, String modelElementId,
+ public static String formateNodeBYaml(RefProp refProp, ModelProperties prop, String modelElementId,
PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException {
// get property objects
builder.addResource(vfcResources);
// process each policy
- HashMap<String, Policy> policyObjMap = new HashMap<String, Policy>();
+ Map<String, Policy> policyObjMap = new HashMap<>();
List<PolicyItem> policyItemList = addAOTSActorRecipe(refProp, global.getService(),
policyChain.getPolicyItems());
- Target target = new Target();
- target.setType(TargetType.VM);
+
Policy lastPolicyObj = new Policy();
- for (int i = 0; i < policyItemList.size(); i++) {
- org.onap.policy.controlloop.policy.Policy policyObj;
- PolicyItem policyItem = policyItemList.get(i);
+ for (PolicyItem policyItem : policyItemList) {
+ Target target = new Target();
+ target.setType(TargetType.VM);
+ target.setResourceID(policyItem.getTargetResourceId());
String policyName = policyItem.getRecipe() + " Policy";
- if (i == 0) {
+ Policy policyObj;
+ if (policyItemList.indexOf(policyItem) == 0) {
// To set up time window payload for trigger policy
- Map<String, String> payloadMap = new HashMap<String, String>();
+ Map<String, String> payloadMap = new HashMap<>();
payloadMap.put("timeWindow", refProp.getStringValue("op.eNodeB.timeWindow"));
String policyDescription = policyItem.getRecipe()
+ " Policy - the trigger (no parent) policy - created by CLDS";
logger.info("results.getSpecification()=" + results.getSpecification());
} else {
// throw exception with error info
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
sb.append("Operation Policy validation problem: ControlLoopPolicyBuilder failed with following messages: ");
for (Message message : results.getMessages()) {
sb.append(message.getMessage());
}
throw new BadRequestException(sb.toString());
}
-
- String encodedYaml = URLEncoder.encode(results.getSpecification(), "UTF-8");
-
- return encodedYaml;
+ return URLEncoder.encode(results.getSpecification(), "UTF-8");
}
/**
* @return
*/
private static List<PolicyItem> addAOTSActorRecipe(RefProp refProp, String service, List<PolicyItem> inOrigList) {
- List<PolicyItem> outList = new ArrayList<PolicyItem>();
+ List<PolicyItem> outList = new ArrayList<>();
try {
PolicyItem policyItem = inOrigList.get(0);
ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("op.eNodeB.recipe", service);
policyItemObj.setRetryTimeLimit(Integer.parseInt(recipeNode.path("TimeLimit").asText()));
}
if (!recipeNode.path("PPConditions").asText().isEmpty()) {
- List<String> parentPolicyConditions = new ArrayList<String>();
+ List<String> parentPolicyConditions = new ArrayList<>();
for (String ppCondition : recipeNode.path("PPConditions").asText().split(",")) {
parentPolicyConditions.add(ppCondition);
}
// check for trigger policy (no parent)
String parent = inItem.getParentPolicy();
if (parent == null || parent.length() == 0) {
- if (outList.size() > 0) {
+ if (!outList.isEmpty()) {
throw new BadRequestException(
"Operation Policy validation problem: more than one trigger policy");
} else {
* @return
*/
private static Resource[] convertToResource(List<String> stringList, ResourceType resourceType) {
- int size = 0;
- if (stringList != null) {
- size = stringList.size();
- }
- Resource[] resourceArray = new Resource[size];
- for (int i = 0; i < size; i++) {
- String rString = stringList.get(i);
- resourceArray[i] = new Resource(rString, resourceType);
+ if (stringList == null || stringList.isEmpty()) {
+ return new Resource[0];
}
- return resourceArray;
+ return stringList.stream().map(stringElem -> new Resource(stringElem, resourceType)).toArray(Resource[]::new);
}
/**
* @return
*/
private static PolicyResult[] convertToPolicyResult(List<String> prList) {
- int size = 0;
- if (prList != null) {
- size = prList.size();
- }
- PolicyResult[] prArray = new PolicyResult[size];
- for (int i = 0; i < size; i++) {
- String prString = prList.get(i);
- prArray[i] = PolicyResult.toResult(prString);
+ if (prList == null || prList.isEmpty()) {
+ return new PolicyResult[0];
}
- return prArray;
+ return prList.stream().map(stringElem -> PolicyResult.toResult(stringElem)).toArray(PolicyResult[]::new);
}
}
\ No newline at end of file