X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Floop%2Fcomponents%2Fexternal%2FPolicyComponent.java;h=8270a96e3ad6fa10bfc7f6bd05a897a95533d5a6;hb=e47a29578a2287a96ae13d1867925feab53a6526;hp=acd6115fe3b02abc9a435e8d763267aaa236f4f5;hpb=70e4a3fc5be81abaae4d1b9bd17a2e8134e5862e;p=clamp.git diff --git a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java index acd6115f..8270a96e 100644 --- a/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java +++ b/src/main/java/org/onap/clamp/loop/components/external/PolicyComponent.java @@ -28,12 +28,13 @@ import com.att.eelf.configuration.EELFManager; 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; +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; @@ -44,17 +45,27 @@ public class PolicyComponent extends ExternalComponent { @Transient private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyComponent.class); + public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR", + "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent", 100); public static final ExternalComponentState NOT_SENT = new ExternalComponentState("NOT_SENT", - "The policies defined have NOT yet been created on the policy engine"); + "The policies defined have NOT yet been created on the policy engine", 90); public static final ExternalComponentState SENT = new ExternalComponentState("SENT", - "The policies defined have been created but NOT deployed on the policy engine"); + "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"); - public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR", - "There was an error during the sending to policy, the policy engine may be corrupted or inconsistent"); + "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 refresh the status to get the current status.", 0); + /** + * Default constructor. + */ public PolicyComponent() { - super(NOT_SENT); + /* + * 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(UNKNOWN); } @Override @@ -69,18 +80,81 @@ public class PolicyComponent extends ExternalComponent { * @return The json, payload to send */ public static String createPoliciesPayloadPdpGroup(Loop loop) { - JsonObject jsonObject = new JsonObject(); - JsonArray jsonArray = new JsonArray(); - jsonObject.add("policies", jsonArray); + Map>> pdpGroupMap = new HashMap<>(); + for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) { + 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()) { + 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 void updatePdpGroupMap(String pdpGroup, + String pdpSubGroup, + String policyName, + String policyModelVersion, + Map>> pdpGroupMap) { + JsonObject policyJson = new JsonObject(); + policyJson.addProperty("name", policyName); + policyJson.addProperty("version", policyModelVersion); + Map> pdpSubGroupMap; + List policyList; + if (pdpGroupMap.get(pdpGroup) == null) { + pdpSubGroupMap = new HashMap<>(); + policyList = new LinkedList<>(); + } + else { + pdpSubGroupMap = pdpGroupMap.get(pdpGroup); + if (pdpSubGroupMap.get(pdpSubGroup) == null) { + policyList = new LinkedList<>(); + } + else { + policyList = (List) pdpSubGroupMap.get(pdpSubGroup); + } + } + policyList.add(policyJson); + pdpSubGroupMap.put(pdpSubGroup, policyList); + pdpGroupMap.put(pdpGroup, pdpSubGroupMap); + } + + private static JsonObject generateActivatePdpGroupPayload( + Map>> pdpGroupMap) { + JsonArray payloadArray = new JsonArray(); + for (Entry>> pdpGroupInfo : pdpGroupMap.entrySet()) { + JsonObject pdpGroupNode = new JsonObject(); + JsonArray subPdpArray = new JsonArray(); + pdpGroupNode.addProperty("name", pdpGroupInfo.getKey()); + pdpGroupNode.add("deploymentSubgroups", subPdpArray); + + for (Entry> pdpSubGroupInfo : pdpGroupInfo.getValue().entrySet()) { + JsonObject pdpSubGroupNode = new JsonObject(); + subPdpArray.add(pdpSubGroupNode); + 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; } /** @@ -93,31 +167,48 @@ public class PolicyComponent extends ExternalComponent { List policyNamesList = new ArrayList<>(); for (OperationalPolicy opPolicy : loop.getOperationalPolicies()) { policyNamesList.add(opPolicy.getName()); - for (String guardName : opPolicy.createGuardPolicyPayloads().keySet()) { - policyNamesList.add(guardName); - } + policyNamesList.addAll(opPolicy.createGuardPolicyPayloads().keySet()); } for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) { policyNamesList.add(microServicePolicy.getName()); } + logger.info("Policies that will be removed from PDP: " + policyNamesList); return policyNamesList; } + private static ExternalComponentState findNewState(boolean found, boolean deployed) { + + ExternalComponentState newState = NOT_SENT; + if (found && deployed) { + newState = SENT_AND_DEPLOYED; + } + else if (found) { + newState = SENT; + } + else if (deployed) { + newState = IN_ERROR; + } + return newState; + } + + private static ExternalComponentState mergeStates(ExternalComponentState oldState, + ExternalComponentState newState) { + return (oldState.compareTo(newState) < 0) ? newState : oldState; + } + + /** + * This is a method that expect the results of the queries getPolicy and + * getPolicyDeployed for a unique policy (op,guard, config, etc ...). It + * re-computes the global policy state for each policy results given. Therefore + * 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) { - boolean oneNotFound = (boolean) camelExchange.getIn().getExchange().getProperty("atLeastOnePolicyNotFound"); - boolean oneNotDeployed = (boolean) camelExchange.getIn().getExchange() - .getProperty("atLeastOnePolicyNotDeployed"); - - if (oneNotFound && oneNotDeployed) { - this.setState(NOT_SENT); - } else if (!oneNotFound && oneNotDeployed) { - this.setState(SENT); - } else if (!oneNotFound && !oneNotDeployed) { - this.setState(SENT_AND_DEPLOYED); - } else { - this.setState(IN_ERROR); - } + this.setState(mergeStates(this.getState(), + findNewState((boolean) camelExchange.getIn().getExchange().getProperty("policyFound"), + (boolean) camelExchange.getIn().getExchange().getProperty("policyDeployed")))); return this.getState(); } }