diff options
-rw-r--r-- | src/main/java/org/onap/policy/clamp/loop/CsarInstaller.java | 4 | ||||
-rw-r--r-- | src/main/java/org/onap/policy/clamp/loop/template/PolicyModelsService.java | 4 | ||||
-rw-r--r-- | src/main/java/org/onap/policy/clamp/policy/PolicyEngineServices.java (renamed from src/main/java/org/onap/policy/clamp/clds/client/PolicyEngineServices.java) | 23 | ||||
-rw-r--r-- | src/main/java/org/onap/policy/clamp/policy/downloader/PolicyEngineController.java | 4 | ||||
-rw-r--r-- | src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java | 79 | ||||
-rw-r--r-- | src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java | 101 | ||||
-rw-r--r-- | src/main/resources/clds/camel/routes/policy-flows.xml | 6 | ||||
-rw-r--r-- | src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java | 82 | ||||
-rw-r--r-- | src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java | 102 | ||||
-rw-r--r-- | src/test/resources/clds/policy-merger.json | 215 | ||||
-rw-r--r-- | src/test/resources/http-cache/example/policy/pap/v1/pdps/test.json | 152 |
11 files changed, 541 insertions, 231 deletions
diff --git a/src/main/java/org/onap/policy/clamp/loop/CsarInstaller.java b/src/main/java/org/onap/policy/clamp/loop/CsarInstaller.java index 753789a68..f46f4227b 100644 --- a/src/main/java/org/onap/policy/clamp/loop/CsarInstaller.java +++ b/src/main/java/org/onap/policy/clamp/loop/CsarInstaller.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights * reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -31,7 +31,6 @@ import java.util.List; import java.util.Map.Entry; import org.json.simple.parser.ParseException; import org.onap.policy.clamp.clds.client.DcaeInventoryServices; -import org.onap.policy.clamp.clds.client.PolicyEngineServices; import org.onap.policy.clamp.clds.exception.sdc.controller.BlueprintParserException; import org.onap.policy.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException; import org.onap.policy.clamp.clds.model.dcae.DcaeInventoryResponse; @@ -48,6 +47,7 @@ import org.onap.policy.clamp.loop.template.LoopTemplate; import org.onap.policy.clamp.loop.template.LoopTemplatesRepository; import org.onap.policy.clamp.loop.template.PolicyModel; import org.onap.policy.clamp.loop.template.PolicyModelsRepository; +import org.onap.policy.clamp.policy.PolicyEngineServices; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; diff --git a/src/main/java/org/onap/policy/clamp/loop/template/PolicyModelsService.java b/src/main/java/org/onap/policy/clamp/loop/template/PolicyModelsService.java index f0830360e..c4182bd2f 100644 --- a/src/main/java/org/onap/policy/clamp/loop/template/PolicyModelsService.java +++ b/src/main/java/org/onap/policy/clamp/loop/template/PolicyModelsService.java @@ -23,7 +23,6 @@ package org.onap.policy.clamp.loop.template; -import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.Collections; @@ -32,7 +31,6 @@ import org.onap.policy.clamp.clds.tosca.ToscaSchemaConstants; import org.onap.policy.clamp.clds.tosca.ToscaYamlToJsonConvertor; import org.onap.policy.clamp.policy.pdpgroup.PdpGroupsAnalyzer; import org.onap.policy.clamp.util.SemanticVersioning; -import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -157,7 +155,7 @@ public class PolicyModelsService { */ public void updatePdpGroupInfo(PdpGroups pdpGroups) { List<PolicyModel> policyModelsList = policyModelsRepository.findAll(); - PdpGroupsAnalyzer.updatePdpGroup(policyModelsList, pdpGroups); + PdpGroupsAnalyzer.updatePdpGroupOfPolicyModels(policyModelsList, pdpGroups); this.policyModelsRepository.saveAll(policyModelsList); } } diff --git a/src/main/java/org/onap/policy/clamp/clds/client/PolicyEngineServices.java b/src/main/java/org/onap/policy/clamp/policy/PolicyEngineServices.java index 46c55ea45..196642589 100644 --- a/src/main/java/org/onap/policy/clamp/clds/client/PolicyEngineServices.java +++ b/src/main/java/org/onap/policy/clamp/policy/PolicyEngineServices.java @@ -21,15 +21,11 @@ * */ -package org.onap.policy.clamp.clds.client; +package org.onap.policy.clamp.policy; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.Exchange; @@ -39,7 +35,6 @@ import org.onap.policy.clamp.clds.sdc.controller.installer.BlueprintMicroService import org.onap.policy.clamp.clds.util.JsonUtils; import org.onap.policy.clamp.loop.template.PolicyModel; import org.onap.policy.clamp.loop.template.PolicyModelsService; -import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -98,7 +93,7 @@ public class PolicyEngineServices { public PolicyModel createPolicyModelFromPolicyEngine(String policyType, String policyVersion) { PolicyModel policyModelFound = policyModelsService.getPolicyModel(policyType, policyVersion); if (policyModelFound == null) { - String policyTosca = this.downloadOnePolicy(policyType, policyVersion); + String policyTosca = this.downloadOnePolicyToscaModel(policyType, policyVersion); if (policyTosca != null && !policyTosca.isEmpty()) { return policyModelsService.savePolicyModelInNewTransaction( new PolicyModel(policyType, policyTosca, policyVersion)); @@ -130,7 +125,7 @@ public class PolicyEngineServices { */ public void synchronizeAllPolicies() { LinkedHashMap<String, Object> loadedYaml; - loadedYaml = new Yaml().load(downloadAllPolicies()); + loadedYaml = new Yaml().load(downloadAllPolicyModels()); if (loadedYaml == null || loadedYaml.isEmpty()) { logger.warn("getAllPolicyType yaml returned by policy engine could not be decoded, as it's null or empty"); return; @@ -149,9 +144,9 @@ public class PolicyEngineServices { * * @return A yaml containing all policy Types and all data types */ - public String downloadAllPolicies() { + public String downloadAllPolicyModels() { return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models", - "Get all policies"); + "Get all policies models"); } /** @@ -161,8 +156,8 @@ public class PolicyEngineServices { * @param policyVersion The policy version * @return A string with the whole policy tosca model */ - public String downloadOnePolicy(String policyType, String policyVersion) { - logger.info("Downloading the policy model " + policyType + "/" + policyVersion); + public String downloadOnePolicyToscaModel(String policyType, String policyVersion) { + logger.info("Downloading the policy tosca model " + policyType + "/" + policyVersion); DumperOptions options = new DumperOptions(); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); options.setIndent(4); @@ -171,11 +166,11 @@ public class PolicyEngineServices { Yaml yamlParser = new Yaml(options); String responseBody = callCamelRoute( ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) - .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model", + .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-tosca-model", "Get one policy"); if (responseBody == null || responseBody.isEmpty()) { - logger.warn("getPolicyModel returned by policy engine could not be decoded, as it's null or empty"); + logger.warn("getPolicyToscaModel returned by policy engine could not be decoded, as it's null or empty"); return null; } diff --git a/src/main/java/org/onap/policy/clamp/policy/downloader/PolicyEngineController.java b/src/main/java/org/onap/policy/clamp/policy/downloader/PolicyEngineController.java index 569678dc7..81775e5da 100644 --- a/src/main/java/org/onap/policy/clamp/policy/downloader/PolicyEngineController.java +++ b/src/main/java/org/onap/policy/clamp/policy/downloader/PolicyEngineController.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ - * Copyright (C) 2020 AT&T Intellectual Property. All rights + * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights * reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,8 +27,8 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import java.time.Instant; import org.json.simple.parser.ParseException; -import org.onap.policy.clamp.clds.client.PolicyEngineServices; import org.onap.policy.clamp.loop.template.PolicyModelsRepository; +import org.onap.policy.clamp.policy.PolicyEngineServices; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java index 51cdb149e..83ece5795 100644 --- a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java +++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupsAnalyzer.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * ONAP CLAMP + * ONAP POLICY-CLAMP * ================================================================================ * Copyright (C) 2021 AT&T Intellectual Property. All rights * reserved. @@ -27,19 +27,19 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.List; import java.util.Objects; -import java.util.stream.Collectors; +import java.util.Optional; import org.onap.policy.clamp.loop.template.PolicyModel; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.enums.PdpState; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; -import org.onap.sdc.toscaparser.api.elements.PolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; /** * This is an utility class to do searching in pdp groups. */ public class PdpGroupsAnalyzer { + /** * Get supported subGroups based on the defined policy type and version for s specific PDPgroup. * It returns null if the Group is not ACTIVE or if the policytype/version has not been found in the PDPSubgroups. @@ -51,7 +51,8 @@ public class PdpGroupsAnalyzer { * @see org.onap.policy.models.pdp.concepts.PdpGroup * @see org.onap.policy.models.pdp.enums.PdpState */ - private static JsonObject getSupportedSubgroups(PdpGroup pdpGroup, String policyType, String version) { + private static JsonObject getSupportedPdpSubgroupsForModelType(PdpGroup pdpGroup, String policyType, + String version) { if (!PdpState.ACTIVE.equals(pdpGroup.getPdpGroupState())) { return null; } @@ -65,10 +66,27 @@ public class PdpGroupsAnalyzer { supportedSubgroups.add(pdpSubGroup.getPdpType()); } }); - if (supportedSubgroups.size() == 0) { - return null; - } - return supportedPdpGroup; + return supportedSubgroups.size() == 0 ? null : supportedPdpGroup; + } + + /** + * This method retrieves all supported pdpGroups and subgroups for a specific policy type/version. + * + * @param pdpGroups The PdpGroups object containing all PEF pdp groups info + * @param policyType The policy type that must be used for searching + * @param version THe policy type version that must be used for searching + * @return It returns a JsonObject containing each pdpGroup and subgroups associated + */ + public static JsonObject getSupportedPdpGroupsForModelType(PdpGroups pdpGroups, String policyType, String version) { + JsonObject supportedPdpGroups = new JsonObject(); + JsonArray pdpGroupsArray = new JsonArray(); + supportedPdpGroups.add("supportedPdpGroups", pdpGroupsArray); + + pdpGroups.getGroups().stream().map(pdpGroup -> PdpGroupsAnalyzer.getSupportedPdpSubgroupsForModelType(pdpGroup, + policyType, version)).filter(Objects::nonNull) + .forEach(jsonPdpGroup -> pdpGroupsArray.add(jsonPdpGroup)); + + return pdpGroupsArray.size() != 0 ? supportedPdpGroups : null; } /** @@ -77,18 +95,39 @@ public class PdpGroupsAnalyzer { * @param policyModelsList The list of Policy Models where each PolicyModel will be updated * @param pdpGroups The PdpGroups containing all PDP group definition */ - public static void updatePdpGroup(List<PolicyModel> policyModelsList, PdpGroups pdpGroups) { + public static void updatePdpGroupOfPolicyModels(List<PolicyModel> policyModelsList, PdpGroups pdpGroups) { policyModelsList.parallelStream().forEach(policyModel -> { - JsonObject jsonResult = new JsonObject(); - JsonArray supportedPdpGroups = new JsonArray(); - jsonResult.add("supportedPdpGroups", supportedPdpGroups); - policyModel.setPolicyPdpGroup(jsonResult); - pdpGroups.getGroups().stream().map(pdpGroup -> PdpGroupsAnalyzer.getSupportedSubgroups(pdpGroup, - policyModel.getPolicyModelType(), policyModel.getVersion())).filter(Objects::nonNull) - .forEach(jsonPdpGroup -> supportedPdpGroups.add(jsonPdpGroup)); - if (supportedPdpGroups.size() == 0) { - policyModel.setPolicyPdpGroup(null); - } + policyModel.setPolicyPdpGroup(getSupportedPdpGroupsForModelType(pdpGroups, policyModel.getPolicyModelType(), + policyModel.getVersion())); }); } + + /** + * This method searches for the PdpGroup/subgroup where the policy given is currently deployed. + * + * @param pdpGroups The pdpGroups info from PEF + * @param policyName The policy Id + * @param version The policy version + * @return It returns a JsonObject containing the pdpGroup/subgroup info + */ + public static JsonObject getPdpGroupDeploymentOfOnePolicy(PdpGroups pdpGroups, String policyName, String version) { + JsonObject pdpGroupInfo = new JsonObject(); + JsonObject assignedPdpGroups = new JsonObject(); + pdpGroupInfo.add("pdpGroupInfo", assignedPdpGroups); + + ToscaPolicyIdentifier toscaPolicyIdentifier = new ToscaPolicyIdentifier(policyName, version); + pdpGroups.getGroups().stream().anyMatch(pdpGroup -> + pdpGroup.getPdpSubgroups().stream().anyMatch( + pdpSubGroup -> { + if (pdpSubGroup.getPolicies() != null && pdpSubGroup.getPolicies() + .contains(toscaPolicyIdentifier)) { + assignedPdpGroups.addProperty("pdpGroup", pdpGroup.getName()); + assignedPdpGroups.addProperty("pdpSubGroup", pdpSubGroup.getPdpType()); + return true; + } + return false; + }) + ); + return assignedPdpGroups.entrySet().isEmpty() ? null : pdpGroupInfo; + } }
\ No newline at end of file diff --git a/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java new file mode 100644 index 000000000..4b94355ee --- /dev/null +++ b/src/main/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMerger.java @@ -0,0 +1,101 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP POLICY-CLAMP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.policy.clamp.policy.pdpgroup; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.util.Map; +import java.util.stream.StreamSupport; +import org.onap.policy.clamp.clds.util.JsonUtils; +import org.onap.policy.models.pdp.concepts.PdpGroups; + +/** + * This is an utility class that contains methods to work on the different results provided by the PEF. + * Mainly used to aggregate the results. + */ +public class PoliciesPdpMerger { + + /** + * This method extract the content of a policy without knowing the key (policy Id). + * This JsonElement normally contains only the policy ID then the content, + * there is only one member in the Json element. + * + * @param policyJsonElement The policy as JsonElement + * @return It return the content as JsonObject + */ + public static JsonObject getPolicyContentOutOfJsonElement(JsonElement policyJsonElement) { + return policyJsonElement.getAsJsonObject() + .get(((String) policyJsonElement.getAsJsonObject().keySet().toArray()[0])).getAsJsonObject(); + } + + /** + * This method merges 2 JsonElement together. If the jsonToMerge is null nothign is changed. + * + * @param json The initial json that will received the data + * @param jsonToMerge The json that will be added to the first json object + */ + public static void mergeJsonElement(JsonObject json, JsonObject jsonToMerge) { + if (jsonToMerge != null) { + jsonToMerge.entrySet().stream().forEach(entry -> json.add(entry.getKey(), entry.getValue())); + } + } + + /** + * This method merges the result of the policy listing and the associated Pdp Group info. + * It can be seen as an enrichment of the policy listing. + * + * @param jsonPoliciesList The Json containing the policies from the PEF + * @param pdpGroupsJson The json containing the PDP groups info from the PEF + * @return It returns a String containing the policies list enriched with PdpGroup info + */ + public static String mergePoliciesAndPdpGroupStates(String jsonPoliciesList, String pdpGroupsJson) { + PdpGroups pdpGroups = JsonUtils.GSON.fromJson(pdpGroupsJson, PdpGroups.class); + JsonObject policiesListJson = + JsonUtils.GSON.fromJson(jsonPoliciesList, JsonObject.class).get("topology_template") + .getAsJsonObject(); + StreamSupport.stream(policiesListJson.get("policies").getAsJsonArray().spliterator(), true) + .forEach(policyJson -> enrichOnePolicy(pdpGroups, getPolicyContentOutOfJsonElement(policyJson))); + return policiesListJson.toString(); + } + + /** + * Enrich one policy json node object with pdpGroup info. + * + * @param pdpGroups The pdpGroups from PEF to search the policy + * @param policyJsonNode The policy json node that must be enriched + */ + private static void enrichOnePolicy(PdpGroups pdpGroups, JsonObject policyJsonNode) { + JsonObject deploymentPdpJson = PdpGroupsAnalyzer + .getPdpGroupDeploymentOfOnePolicy(pdpGroups, policyJsonNode.get("name").getAsString(), + policyJsonNode.get("version").getAsString()); + mergeJsonElement(policyJsonNode, deploymentPdpJson); + + JsonObject supportedPdpGroupsJson = PdpGroupsAnalyzer + .getSupportedPdpGroupsForModelType(pdpGroups, policyJsonNode.get("type").getAsString(), + policyJsonNode.get("type_version").getAsString()); + mergeJsonElement(policyJsonNode, supportedPdpGroupsJson); + } +} diff --git a/src/main/resources/clds/camel/routes/policy-flows.xml b/src/main/resources/clds/camel/routes/policy-flows.xml index 4d1eba614..7048a5e03 100644 --- a/src/main/resources/clds/camel/routes/policy-flows.xml +++ b/src/main/resources/clds/camel/routes/policy-flows.xml @@ -170,11 +170,11 @@ </doFinally> </doTry> </route> - <route id="get-policy-model"> - <from uri="direct:get-policy-model"/> + <route id="get-policy-tosca-model"> + <from uri="direct:get-policy-tosca-model"/> <doTry> <log loggingLevel="INFO" - message="Getting the policy model: ${exchangeProperty[policyModelName]}/${exchangeProperty[policyModelVersion]}"/> + message="Getting the policy tosca model: ${exchangeProperty[policyModelName]}/${exchangeProperty[policyModelVersion]}"/> <to uri="bean:org.onap.policy.clamp.flow.log.FlowLogOperation?method=invokeLog('Policy', 'Getting the policy model')"/> <setHeader headerName="CamelHttpMethod"> diff --git a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java index 1af048da5..5a9cac07b 100644 --- a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java +++ b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PdpGroupAnalyzerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * ONAP CLAMP + * ONAP POLICY-CLAMP * ================================================================================ * Copyright (C) 2021 AT&T Intellectual Property. All rights * reserved. @@ -25,19 +25,15 @@ package org.onap.policy.clamp.policy.pdpgroup; import static org.assertj.core.api.Assertions.assertThat; -import com.google.gson.Gson; -import com.google.gson.JsonObject; import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import javax.transaction.Transactional; +import org.junit.BeforeClass; import org.junit.Test; -import org.onap.policy.clamp.clds.util.JsonUtils; import org.onap.policy.clamp.loop.template.PolicyModel; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; /** @@ -45,33 +41,13 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi */ public class PdpGroupAnalyzerTest { - private PolicyModel createPolicyModel(String policyType, String policyModelTosca, String version, - String policyAcronym, String policyVariant, String createdBy) { - PolicyModel policyModel = new PolicyModel(); - policyModel.setCreatedBy(createdBy); - policyModel.setPolicyAcronym(policyAcronym); - policyModel.setPolicyModelTosca(policyModelTosca); - policyModel.setPolicyModelType(policyType); - policyModel.setUpdatedBy(createdBy); - policyModel.setVersion(version); - return policyModel; - } + private static PdpGroups pdpGroups; /** - * This tests the pdpgroup GSON encode/decode and saving. + * This method preloads the pdpGroups for the tests. */ - @Test - @Transactional - public void testUpdatePdpGroup() { - // Create policyModel - PolicyModel policyModel = new PolicyModel(); - policyModel.setCreatedBy("user"); - policyModel.setPolicyAcronym("TEST"); - policyModel.setPolicyModelTosca("yaml"); - policyModel.setPolicyModelType("org.onap.test"); - policyModel.setUpdatedBy("user"); - policyModel.setVersion("1.0.0"); - + @BeforeClass + public static void setupPdpGroup() { // Create Pdp Groups // Those that do not work first PdpSubGroup pdpSubgroupBad = new PdpSubGroup(); @@ -83,7 +59,11 @@ public class PdpGroupAnalyzerTest { new ToscaPolicyTypeIdentifier("org.onap.tes", "1.0.0"), new ToscaPolicyTypeIdentifier("org.onap", "1.0.0") )); - + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap", "1.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "1.0.1"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos.new", "1.0.0"))); + pdpSubgroupBad.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.*", "1.0.0"))); PdpSubGroup pdpSubgroup1 = new PdpSubGroup(); pdpSubgroup1.setPdpType("subGroup1"); @@ -93,38 +73,70 @@ public class PdpGroupAnalyzerTest { PdpSubGroup pdpSubgroup2 = new PdpSubGroup(); pdpSubgroup2.setPdpType("subGroup2"); pdpSubgroup2.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.test", "1.0.0"))); + pdpSubgroup2.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "1.0.0"))); PdpSubGroup pdpSubgroup3 = new PdpSubGroup(); pdpSubgroup3.setPdpType("subGroup3"); pdpSubgroup3.setSupportedPolicyTypes(Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.test*", "1.0.0"))); + pdpSubgroup3.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"))); - + // Should match pdpSubgroup1 PdpGroup pdpGroup1 = new PdpGroup(); pdpGroup1.setName("pdpGroup1"); pdpGroup1.setPdpGroupState(PdpState.ACTIVE); pdpGroup1.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroupBad)); + // Should match pdpSubgroup1, pdpSubgroup2, pdpSubgroup3 + // Should match also for the policy (pdpSubgroup2) PdpGroup pdpGroup2 = new PdpGroup(); pdpGroup2.setName("pdpGroup2"); pdpGroup2.setPdpGroupState(PdpState.ACTIVE); pdpGroup2.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroup2, pdpSubgroup3, pdpSubgroupBad)); + /// Should not match PdpGroup pdpGroup3 = new PdpGroup(); pdpGroup3.setName("pdpGroup3"); pdpGroup3.setPdpGroupState(PdpState.ACTIVE); pdpGroup3.setPdpSubgroups(Arrays.asList(pdpSubgroupBad)); + // Should not match PdpGroup pdpGroup4 = new PdpGroup(); pdpGroup4.setName("pdpGroup4"); pdpGroup4.setPdpGroupState(PdpState.PASSIVE); pdpGroup4.setPdpSubgroups(Arrays.asList(pdpSubgroup1)); - PdpGroups pdpGroups = new PdpGroups(); + pdpGroups = new PdpGroups(); pdpGroups.setGroups(Arrays.asList(pdpGroup1, pdpGroup2, pdpGroup3, pdpGroup4)); - PdpGroupsAnalyzer.updatePdpGroup(Arrays.asList(policyModel), pdpGroups); + } + + @Test + public void testUpdatePdpGroupOfPolicyModels() { + // Create policyModel + PolicyModel policyModel = new PolicyModel(); + policyModel.setCreatedBy("user"); + policyModel.setPolicyAcronym("TEST"); + policyModel.setPolicyModelTosca("yaml"); + policyModel.setPolicyModelType("org.onap.test"); + policyModel.setUpdatedBy("user"); + policyModel.setVersion("1.0.0"); + PdpGroupsAnalyzer.updatePdpGroupOfPolicyModels(Arrays.asList(policyModel), pdpGroups); assertThat(policyModel.getPolicyPdpGroup().toString()).isEqualTo( "{\"supportedPdpGroups\":[{\"pdpGroup1\":[\"subGroup1\"]}," + "{\"pdpGroup2\":[\"subGroup1\",\"subGroup2\",\"subGroup3\"]}]}"); } + + @Test + public void testGetPdpGroupDeploymentsOfOnePolicy() { + assertThat( + PdpGroupsAnalyzer.getPdpGroupDeploymentOfOnePolicy(pdpGroups, "org.onap.testos", "1.0.0").toString()) + .isEqualTo("{\"pdpGroupInfo\":{\"pdpGroup\":\"pdpGroup2\",\"pdpSubGroup\":\"subGroup2\"}}"); + } + + @Test + public void testGetPdpGroupDeploymentsOfOnePolicyNull() { + assertThat( + PdpGroupsAnalyzer.getPdpGroupDeploymentOfOnePolicy(pdpGroups, "org.onap.DoNotExist", "1.0.0")) + .isNull(); + } } diff --git a/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java new file mode 100644 index 000000000..b3fd3f19a --- /dev/null +++ b/src/test/java/org/onap/policy/clamp/policy/pdpgroup/PoliciesPdpMergerTest.java @@ -0,0 +1,102 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP POLICY-CLAMP + * ================================================================================ + * Copyright (C) 2021 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.policy.clamp.policy.pdpgroup; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.util.Arrays; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.clamp.clds.util.JsonUtils; +import org.onap.policy.clamp.clds.util.ResourceFileUtils; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.skyscreamer.jsonassert.JSONAssert; + +/** + * This test class validates the PdpGroupAnalyzer class. + */ +public class PoliciesPdpMergerTest { + + private static String pdpGroupsJson; + + /** + * This method setups the pdpGroups required for the tests. + */ + @BeforeClass + public static void setupPdpGroup() { + // Create Pdp Groups + PdpSubGroup pdpSubgroup1 = new PdpSubGroup(); + pdpSubgroup1.setPdpType("subGroup1"); + pdpSubgroup1.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("org.onap.*", "1.0.0"))); + + PdpSubGroup pdpSubgroup2 = new PdpSubGroup(); + pdpSubgroup2.setPdpType("subGroup2"); + pdpSubgroup2.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"), + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools", "1.0.0"))); + pdpSubgroup2.setPolicies(Arrays.asList( + new ToscaPolicyIdentifier("MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", "1.0.0"))); + + PdpSubGroup pdpSubgroup3 = new PdpSubGroup(); + pdpSubgroup3.setPdpType("subGroup3"); + pdpSubgroup3.setSupportedPolicyTypes( + Arrays.asList(new ToscaPolicyTypeIdentifier("onap.policies.monitoring.tcagen2", "1.0.0"), + new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools", "1.0.0"))); + pdpSubgroup3.setPolicies(Arrays.asList(new ToscaPolicyIdentifier("org.onap.testos", "2.0.0"), + new ToscaPolicyIdentifier("OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", "1.0.0"))); + + // Should match pdpSubgroup1 + PdpGroup pdpGroup1 = new PdpGroup(); + pdpGroup1.setName("pdpGroup1"); + pdpGroup1.setPdpGroupState(PdpState.ACTIVE); + pdpGroup1.setPdpSubgroups(Arrays.asList(pdpSubgroup1)); + + // Should match pdpSubgroup1, pdpSubgroup2, pdpSubgroup3 + // Should match also for the policy (pdpSubgroup2) + PdpGroup pdpGroup2 = new PdpGroup(); + pdpGroup2.setName("pdpGroup2"); + pdpGroup2.setPdpGroupState(PdpState.ACTIVE); + pdpGroup2.setPdpSubgroups(Arrays.asList(pdpSubgroup1, pdpSubgroup2, pdpSubgroup3)); + + PdpGroups pdpGroups = new PdpGroups(); + pdpGroups.setGroups(Arrays.asList(pdpGroup1, pdpGroup2)); + + pdpGroupsJson = JsonUtils.GSON.toJson(pdpGroups); + } + + @Test + public void testUpdatePdpGroupOfPolicyModels() throws IOException { + JSONAssert.assertEquals(ResourceFileUtils.getResourceAsString("clds/policy-merger.json"), PoliciesPdpMerger + .mergePoliciesAndPdpGroupStates( + ResourceFileUtils.getResourceAsString("http-cache/example/policy/api/v1/policies/.file"), + pdpGroupsJson), true); + } +} diff --git a/src/test/resources/clds/policy-merger.json b/src/test/resources/clds/policy-merger.json new file mode 100644 index 000000000..5c6bd5b6b --- /dev/null +++ b/src/test/resources/clds/policy-merger.json @@ -0,0 +1,215 @@ +{ + "policies": [ + { + "MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0": { + "type": "onap.policies.monitoring.tcagen2", + "type_version": "1.0.0", + "properties": { + "tca.policy": { + "domain": "measurementsForVfScaling", + "metricsPerEventName": [ + { + "policyScope": "DCAE", + "thresholds": [ + { + "version": "1.0.2", + "severity": "MAJOR", + "thresholdValue": 200, + "closedLoopEventStatus": "ONSET", + "closedLoopControlName": "LOOP_test", + "direction": "LESS_OR_EQUAL", + "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta" + } + ], + "eventName": "vLoadBalancer", + "policyVersion": "v0.0.1", + "controlLoopSchemaType": "VM", + "policyName": "DCAE.Config_tca-hi-lo" + } + ] + } + }, + "name": "MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", + "version": "1.0.0", + "metadata": { + "policy-id": "MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", + "policy-version": "1.0.0" + }, + "pdpGroupInfo": { + "pdpGroup": "pdpGroup2", + "pdpSubGroup": "subGroup2" + }, + "supportedPdpGroups": [ + { + "pdpGroup2": [ + "subGroup2", + "subGroup3" + ] + } + ] + } + }, + { + "OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd": { + "type": "onap.policies.controlloop.operational.common.Drools", + "type_version": "1.0.0", + "properties": { + "abatement": false, + "operations": [ + { + "failure_retries": "final_failure_retries", + "id": "test1", + "failure_timeout": "final_failure_timeout", + "failure": "final_failure", + "operation": { + "payload": { + "artifact_name": "baseconfiguration", + "artifact_version": "1.0.0", + "mode": "async", + "data": "{\"resource-assignment-properties\":{\"request-id\":\"\",\"service-instance-id\":\"\",\"hostname\":\"\",\"request-info\":{\"prop1\":\"\",\"prop2\":\"\"}}}" + }, + "target": { + "entityIds": { + "resourceID": "Vloadbalancerms..vdns..module-3", + "modelInvariantId": "4c10ba9b-f88f-415e-9de3-5d33336047fa", + "modelVersionId": "4fa73b49-8a6c-493e-816b-eb401567b720", + "modelName": "Vloadbalancerms..vdns..module-3", + "modelVersion": "1", + "modelCustomizationId": "bafcdab0-801d-4d81-9ead-f464640a38b1" + }, + "targetType": "VNF" + }, + "actor": "SDNR", + "operation": "BandwidthOnDemand" + }, + "failure_guard": "final_failure_guard", + "retries": 0, + "timeout": 0, + "failure_exception": "final_failure_exception", + "description": "test", + "success": "final_success" + } + ], + "trigger": "test1", + "timeout": 0, + "id": "LOOP_test" + }, + "name": "OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", + "version": "1.0.0", + "metadata": { + "policy-id": "OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", + "policy-version": "1.0.0" + }, + "pdpGroupInfo": { + "pdpGroup": "pdpGroup2", + "pdpSubGroup": "subGroup3" + }, + "supportedPdpGroups": [ + { + "pdpGroup2": [ + "subGroup2", + "subGroup3" + ] + } + ] + } + }, + { + "SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP": { + "type": "onap.policies.Naming", + "type_version": "1.0.0", + "properties": { + "naming-models": [ + { + "naming-type": "VNF", + "naming-recipe": "AIC_CLOUD_REGION|DELIMITER|CONSTANT|DELIMITER|TIMESTAMP", + "name-operation": "to_lower_case()", + "naming-properties": [ + { + "property-name": "AIC_CLOUD_REGION" + }, + { + "property-name": "CONSTANT", + "property-value": "onap-nf" + }, + { + "property-name": "TIMESTAMP" + }, + { + "property-value": "-", + "property-name": "DELIMITER" + } + ] + }, + { + "naming-type": "VNFC", + "naming-recipe": "VNF_NAME|DELIMITER|NFC_NAMING_CODE|DELIMITER|SEQUENCE", + "name-operation": "to_lower_case()", + "naming-properties": [ + { + "property-name": "VNF_NAME" + }, + { + "property-name": "SEQUENCE", + "increment-sequence": { + "max": "zzz", + "scope": "ENTIRETY", + "start-value": "1", + "length": "3", + "increment": "1", + "sequence-type": "alpha-numeric" + } + }, + { + "property-name": "NFC_NAMING_CODE" + }, + { + "property-value": "-", + "property-name": "DELIMITER" + } + ] + }, + { + "naming-type": "VF-MODULE", + "naming-recipe": "VNF_NAME|DELIMITER|VF_MODULE_LABEL|DELIMITER|VF_MODULE_TYPE|DELIMITER|SEQUENCE", + "name-operation": "to_lower_case()", + "naming-properties": [ + { + "property-name": "VNF_NAME" + }, + { + "property-value": "-", + "property-name": "DELIMITER" + }, + { + "property-name": "VF_MODULE_LABEL" + }, + { + "property-name": "VF_MODULE_TYPE" + }, + { + "property-name": "SEQUENCE", + "increment-sequence": { + "max": "zzz", + "scope": "PRECEEDING", + "start-value": "1", + "length": "3", + "increment": "1", + "sequence-type": "alpha-numeric" + } + } + ] + } + ], + "policy-instance-name": "ONAP_NF_NAMING_TIMESTAMP" + }, + "name": "SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", + "version": "1.0.0", + "metadata": { + "policy-id": "SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", + "policy-version": "1.0.0" + } + } + } + ] +}
\ No newline at end of file diff --git a/src/test/resources/http-cache/example/policy/pap/v1/pdps/test.json b/src/test/resources/http-cache/example/policy/pap/v1/pdps/test.json deleted file mode 100644 index c22508441..000000000 --- a/src/test/resources/http-cache/example/policy/pap/v1/pdps/test.json +++ /dev/null @@ -1,152 +0,0 @@ -{ - "groups": [ - { - "name": "defaultGroup", - "description": "The default group that registers all supported policy types and pdps.", - "pdpGroupState": "ACTIVE", - "properties": {}, - "pdpSubgroups": [ - { - "pdpType": "apex", - "supportedPolicyTypes": [ - { - "name": "onap.policies.controlloop.operational.common.Apex", - "version": "1.0.0" - }, - { - "name": "onap.policies.native.Apex", - "version": "1.0.0" - } - ], - "policies": [], - "currentInstanceCount": 0, - "desiredInstanceCount": 1, - "properties": {}, - "pdpInstances": [] - }, - { - "pdpType": "drools", - "supportedPolicyTypes": [ - { - "name": "onap.policies.controlloop.operational.common.Drools", - "version": "1.0.0" - }, - { - "name": "onap.policies.native.drools.Controller", - "version": "1.0.0" - }, - { - "name": "onap.policies.native.drools.Artifact", - "version": "1.0.0" - } - ], - "policies": [ - { - "name": "OPERATIONAL_vLoadBalancerMS_v1_0_Drools_1_0_0_7xd", - "version": "1.0.0" - } - ], - "currentInstanceCount": 0, - "desiredInstanceCount": 1, - "properties": {}, - "pdpInstances": [] - }, - { - "pdpType": "xacml", - "supportedPolicyTypes": [ - { - "name": "onap.policies.controlloop.guard.common.FrequencyLimiter", - "version": "1.0.0" - }, - { - "name": "onap.policies.controlloop.guard.common.MinMax", - "version": "1.0.0" - }, - { - "name": "onap.policies.controlloop.guard.common.Blacklist", - "version": "1.0.0" - }, - { - "name": "onap.policies.controlloop.guard.common.Filter", - "version": "1.0.0" - }, - { - "name": "onap.policies.controlloop.guard.coordination.FirstBlocksSecond", - "version": "1.0.0" - }, - { - "name": "onap.policies.monitoring.*", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.*", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.resource.AffinityPolicy", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.resource.DistancePolicy", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.resource.HpaPolicy", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.resource.OptimizationPolicy", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.resource.PciPolicy", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.service.QueryPolicy", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.service.SubscriberPolicy", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.resource.Vim_fit", - "version": "1.0.0" - }, - { - "name": "onap.policies.optimization.resource.VnfPolicy", - "version": "1.0.0" - }, - { - "name": "onap.policies.native.Xacml", - "version": "1.0.0" - }, - { - "name": "onap.policies.Naming", - "version": "1.0.0" - }, - { - "name": "onap.policies.match.*", - "version": "1.0.0" - } - ], - "policies": [ - { - "name": "SDNC_Policy.ONAP_NF_NAMING_TIMESTAMP", - "version": "1.0.0" - }, - { - "name": "MICROSERVICE_vLoadBalancerMS_v1_0_tcagen2_1_0_0_AV0", - "version": "1.0.0" - } - ], - "currentInstanceCount": 0, - "desiredInstanceCount": 1, - "properties": {}, - "pdpInstances": [] - } - ] - } - ] -}
\ No newline at end of file |