import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
-
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
-
+import java.util.Map.Entry;
import javax.persistence.Transient;
-
import org.apache.camel.Exchange;
import org.onap.clamp.loop.Loop;
import org.onap.clamp.policy.microservice.MicroServicePolicy;
"The policies defined have been created but NOT deployed on the policy engine", 50);
public static final ExternalComponentState SENT_AND_DEPLOYED = new ExternalComponentState("SENT_AND_DEPLOYED",
"The policies defined have been created and deployed on the policy engine", 10);
+ public static final ExternalComponentState UNKNOWN = new ExternalComponentState("UNKNOWN",
+ "The current status is not clear. Need to regresh the status to get the current status.", 0);
+ /**
+ * Default constructor.
+ */
public PolicyComponent() {
/*
* We assume it's good by default as we will receive the state for each policy
* on by one, each time we increase the level we can't decrease it anymore.
* That's why it starts with the lowest one SENT_AND_DEPLOYED.
*/
- super(SENT_AND_DEPLOYED);
+ super(UNKNOWN);
}
@Override
* @return The json, payload to send
*/
public static String createPoliciesPayloadPdpGroup(Loop loop) {
- JsonObject jsonObject = new JsonObject();
- JsonArray jsonArray = new JsonArray();
- jsonObject.add("policies", jsonArray);
+ HashMap<String, HashMap<String, List<JsonObject>>> pdpGroupMap = new HashMap<>();
+ for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) {
+ pdpGroupMap = updatePdpGroupMap(opPolicy.getPdpGroup(), opPolicy.getPdpSubgroup(),
+ opPolicy.getName(),
+ opPolicy.getPolicyModel().getVersion(), pdpGroupMap);
+ }
- for (String policyName : PolicyComponent.listPolicyNamesPdpGroup(loop)) {
- JsonObject policyNode = new JsonObject();
- jsonArray.add(policyNode);
- policyNode.addProperty("policy-id", policyName);
+ for (MicroServicePolicy msPolicy : loop.getMicroServicePolicies()) {
+ pdpGroupMap = updatePdpGroupMap(msPolicy.getPdpGroup(), msPolicy.getPdpSubgroup(),
+ msPolicy.getName(),
+ msPolicy.getPolicyModel().getVersion(), pdpGroupMap);
}
- String payload = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
+
+ String payload = new GsonBuilder().setPrettyPrinting().create()
+ .toJson(generateActivatePdpGroupPayload(pdpGroupMap));
logger.info("PdpGroup policy payload: " + payload);
- return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
+ return payload;
+ }
+
+ private static HashMap<String, HashMap<String, List<JsonObject>>> updatePdpGroupMap(String pdpGroup,
+ String pdpSubGroup,
+ String policyName,
+ String policyModelVersion,
+ HashMap<String, HashMap<String,
+ List<JsonObject>>> pdpGroupMap) {
+
+ JsonObject policyJson = new JsonObject();
+ policyJson.addProperty("name", policyName);
+ policyJson.addProperty("version", policyModelVersion);
+ HashMap<String, List<JsonObject>> pdpSubGroupMap;
+ List<JsonObject> policyList;
+ if (pdpGroupMap.get(pdpGroup) == null) {
+ pdpSubGroupMap = new HashMap<String, List<JsonObject>>();
+ policyList = new LinkedList<JsonObject>();
+ }
+ else {
+ pdpSubGroupMap = pdpGroupMap.get(pdpGroup);
+ if (pdpSubGroupMap.get(pdpSubGroup) == null) {
+ policyList = new LinkedList<JsonObject>();
+ }
+ else {
+ policyList = (List<JsonObject>) pdpSubGroupMap.get(pdpSubGroup);
+ }
+ }
+ policyList.add(policyJson);
+ pdpSubGroupMap.put(pdpSubGroup, policyList);
+ pdpGroupMap.put(pdpGroup, pdpSubGroupMap);
+
+ return pdpGroupMap;
+ }
+
+ private static JsonObject generateActivatePdpGroupPayload(
+ HashMap<String, HashMap<String, List<JsonObject>>> pdpGroupMap) {
+ JsonArray payloadArray = new JsonArray();
+ for (Entry<String, HashMap<String, List<JsonObject>>> pdpGroupInfo : pdpGroupMap.entrySet()) {
+ JsonObject pdpGroupNode = new JsonObject();
+ JsonArray subPdpArray = new JsonArray();
+ pdpGroupNode.addProperty("name", pdpGroupInfo.getKey());
+ pdpGroupNode.add("deploymentSubgroups", subPdpArray);
+
+ JsonObject pdpSubGroupNode = new JsonObject();
+ subPdpArray.add(pdpSubGroupNode);
+
+ for (Entry<String, List<JsonObject>> pdpSubGroupInfo : pdpGroupInfo.getValue().entrySet()) {
+ pdpSubGroupNode.addProperty("pdpType", pdpSubGroupInfo.getKey());
+ pdpSubGroupNode.addProperty("action", "POST");
+
+ JsonArray policyArray = new JsonArray();
+ pdpSubGroupNode.add("policies", policyArray);
+
+ for (JsonObject policy : pdpSubGroupInfo.getValue()) {
+ policyArray.add(policy);
+ }
+ }
+ payloadArray.add(pdpGroupNode);
+ }
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.add("groups", payloadArray);
+ return jsonObject;
}
/**
ExternalComponentState newState = NOT_SENT;
if (found && deployed) {
newState = SENT_AND_DEPLOYED;
- } else if (found) {
+ }
+ else if (found) {
newState = SENT;
- } else if (deployed) {
+ }
+ else if (deployed) {
newState = IN_ERROR;
}
return newState;
}
private static ExternalComponentState mergeStates(ExternalComponentState oldState,
- ExternalComponentState newState) {
+ ExternalComponentState newState) {
return (oldState.compareTo(newState) < 0) ? newState : oldState;
}
* this method is called multiple times from the camel route and must be reset
* for a new global policy state retrieval. The state to compute the global
* policy state is stored in this class.
- *
*/
@Override
public ExternalComponentState computeState(Exchange camelExchange) {