From 3a26471260e56f7a87533f0147fc63530d6ea08c Mon Sep 17 00:00:00 2001 From: xuegao Date: Tue, 18 Feb 2020 14:20:23 +0100 Subject: Create get Pdp Groups flow Create a camel flow to get the list of Pdp Groups info from Policy. Create a scheduler to trigger the camel flow regularly and store the Pdp Groups info into DB. Issue-ID: CLAMP-644, CLAMP-649 Change-Id: I6427202cc0186cd85428d5d25b28a8622e4d7ca4 Signed-off-by: xuegao --- .../clamp/clds/client/PolicyEngineServices.java | 55 +++++++-- .../org/onap/clamp/loop/template/PolicyModel.java | 29 +++++ .../clamp/loop/template/PolicyModelsService.java | 49 +++++++- src/main/java/org/onap/clamp/policy/Policy.java | 22 ++++ .../policy/downloader/PolicyEngineController.java | 6 +- .../org/onap/clamp/policy/pdpgroup/PdpGroup.java | 93 +++++++++++++++ .../onap/clamp/policy/pdpgroup/PdpSubgroup.java | 56 +++++++++ .../onap/clamp/policy/pdpgroup/PolicyModelKey.java | 126 +++++++++++++++++++++ 8 files changed, 424 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/onap/clamp/policy/pdpgroup/PdpGroup.java create mode 100644 src/main/java/org/onap/clamp/policy/pdpgroup/PdpSubgroup.java create mode 100644 src/main/java/org/onap/clamp/policy/pdpgroup/PolicyModelKey.java (limited to 'src/main/java') diff --git a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java index 02e2dd0b..b3fcb6f1 100644 --- a/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java +++ b/src/main/java/org/onap/clamp/clds/client/PolicyEngineServices.java @@ -25,17 +25,28 @@ package org.onap.clamp.clds.client; 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.ArrayList; +import java.util.Iterator; 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; import org.apache.camel.builder.ExchangeBuilder; +import org.json.simple.parser.ParseException; import org.onap.clamp.clds.config.ClampProperties; import org.onap.clamp.clds.sdc.controller.installer.BlueprintMicroService; +import org.onap.clamp.clds.util.JsonUtils; import org.onap.clamp.loop.template.PolicyModel; +import org.onap.clamp.loop.template.PolicyModelId; import org.onap.clamp.loop.template.PolicyModelsService; +import org.onap.clamp.policy.pdpgroup.PdpGroup; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.yaml.snakeyaml.Yaml; @@ -53,11 +64,9 @@ import org.yaml.snakeyaml.Yaml; public class PolicyEngineServices { private final CamelContext camelContext; - private final PolicyModelsService policyModelsSService; + private final PolicyModelsService policyModelsService; private static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyEngineServices.class); - private static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); - private static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); private static int retryInterval = 0; private static int retryLimit = 1; @@ -70,12 +79,13 @@ public class PolicyEngineServices { * @param camelContext Camel context bean * @param clampProperties ClampProperties bean * @param policyModelsSService policyModel repository bean + * @param policyModelsService policyModel service */ @Autowired public PolicyEngineServices(CamelContext camelContext, ClampProperties clampProperties, PolicyModelsService policyModelsSService) { this.camelContext = camelContext; - this.policyModelsSService = policyModelsSService; + this.policyModelsService = policyModelsSService; if (clampProperties.getStringValue(POLICY_RETRY_LIMIT) != null) { retryLimit = Integer.parseInt(clampProperties.getStringValue(POLICY_RETRY_LIMIT)); } @@ -122,7 +132,7 @@ public class PolicyEngineServices { policyTypesList.parallelStream().forEach(policyType -> { Map.Entry policyTypeEntry = (Map.Entry) new ArrayList(policyType.entrySet()).get(0); - policyModelsSService.createPolicyInDbIfNeeded( + policyModelsService.createPolicyInDbIfNeeded( createPolicyModelFromPolicyEngine(policyTypeEntry.getKey(), ((String) ((LinkedHashMap) policyTypeEntry.getValue()).get("version")))); }); @@ -135,7 +145,7 @@ public class PolicyEngineServices { * @return A yaml containing all policy Types and all data types */ public String downloadAllPolicies() { - return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models"); + return callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-policy-models", "Get all policies"); } /** @@ -147,16 +157,43 @@ public class PolicyEngineServices { */ public String downloadOnePolicy(String policyType, String policyVersion) { return callCamelRoute(ExchangeBuilder.anExchange(camelContext).withProperty("policyModelName", policyType) - .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model"); + .withProperty("policyModelVersion", policyVersion).build(), "direct:get-policy-model", "Get one policy"); + } + + /** + * This method can be used to download all Pdp Groups data from policy engine. + * + */ + public void downloadPdpGroups() { + String responseBody = callCamelRoute(ExchangeBuilder.anExchange(camelContext).build(), "direct:get-all-pdp-groups", "Get Pdp Groups"); + + if (responseBody == null || responseBody.isEmpty()) { + logger.warn("getPdpGroups returned by policy engine could not be decoded, as it's null or empty"); + return; + } + + JsonObject jsonObj = JsonUtils.GSON.fromJson(responseBody, JsonObject.class); + + List pdpGroupList = new LinkedList<>(); + JsonArray itemsArray = (JsonArray) jsonObj.get("groups"); + + Iterator it = itemsArray.iterator(); + while (it.hasNext()) { + JsonObject item = (JsonObject) it.next(); + PdpGroup pdpGroup = JsonUtils.GSON.fromJson(item.toString(), PdpGroup.class); + pdpGroupList.add(pdpGroup); + } + + policyModelsService.updatePdpGroupInfo(pdpGroupList); } - private String callCamelRoute(Exchange exchange, String camelFlow) { + private String callCamelRoute(Exchange exchange, String camelFlow, String logMsg) { for (int i = 0; i < retryLimit; i++) { Exchange exchangeResponse = camelContext.createProducerTemplate().send(camelFlow, exchange); if (Integer.valueOf(200).equals(exchangeResponse.getIn().getHeader("CamelHttpResponseCode"))) { return (String) exchangeResponse.getIn().getBody(); } else { - logger.info("Policy query " + retryInterval + "ms before retrying ..."); + logger.info(logMsg + " query " + retryInterval + "ms before retrying ..."); // wait for a while and try to connect to DCAE again try { Thread.sleep(retryInterval); diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModel.java b/src/main/java/org/onap/clamp/loop/template/PolicyModel.java index fd0110c7..7334eceb 100644 --- a/src/main/java/org/onap/clamp/loop/template/PolicyModel.java +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModel.java @@ -23,6 +23,7 @@ package org.onap.clamp.loop.template; +import com.google.gson.JsonObject; import com.google.gson.annotations.Expose; import java.io.Serializable; import java.util.HashSet; @@ -34,6 +35,11 @@ import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.ManyToMany; import javax.persistence.Table; + +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; +import org.hibernate.annotations.TypeDefs; +import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.common.AuditEntity; import org.onap.clamp.util.SemanticVersioning; @@ -44,6 +50,7 @@ import org.onap.clamp.util.SemanticVersioning; @Entity @Table(name = "policy_models") @IdClass(PolicyModelId.class) +@TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)}) public class PolicyModel extends AuditEntity implements Serializable, Comparable { /** @@ -78,6 +85,10 @@ public class PolicyModel extends AuditEntity implements Serializable, Comparable @ManyToMany(mappedBy = "policyModels", fetch = FetchType.EAGER) private Set usedByElementModels = new HashSet<>(); + @Type(type = "json") + @Column(columnDefinition = "json", name = "policy_pdp_group") + private JsonObject policyPdpGroup; + /** * usedByElementModels getter. * @@ -87,6 +98,24 @@ public class PolicyModel extends AuditEntity implements Serializable, Comparable return usedByElementModels; } + /** + * policyPdpGroup getter. + * + * @return the policyPdpGroup + */ + public JsonObject getPolicyPdpGroup() { + return policyPdpGroup; + } + + /** + * policyPdpGroup setter. + * + * @param policyPdpGroup the policyPdpGroup to set + */ + public void setPolicyPdpGroup(JsonObject policyPdpGroup) { + this.policyPdpGroup = policyPdpGroup; + } + /** * policyModelTosca getter. * diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java index eb83c660..b38be942 100644 --- a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java @@ -23,12 +23,14 @@ package org.onap.clamp.loop.template; +import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.onap.clamp.clds.tosca.ToscaSchemaConstants; import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor; +import org.onap.clamp.policy.pdpgroup.PdpGroup; import org.onap.clamp.util.SemanticVersioning; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -47,13 +49,30 @@ public class PolicyModelsService { toscaYamlToJsonConvertor = convertor; } + /** + * Save or Update Policy Model. + * + * @param policyModel + * The policyModel + * @return The Policy Model + */ public PolicyModel saveOrUpdatePolicyModel(PolicyModel policyModel) { return policyModelsRepository.save(policyModel); } /** - * Creates the Tosca Policy Model from a policy tosca file, - * if the same type already exists in database, it increases the version automatically. + * Verify whether Policy Model exist by ID. + * + * @param policyModelId + * The policyModel Id + * @return The flag indicates whether Policy Model exist + */ + public boolean existsById(PolicyModelId policyModelId) { + return policyModelsRepository.existsById(policyModelId); + } + + /** + * Creates or updates the Tosca Policy Model. * * @param policyModelType The policyModeltype in Tosca yaml * @param policyModelTosca The Policymodel object @@ -137,4 +156,30 @@ public class PolicyModelsService { policyModelsRepository.save(policyModel); } } + + /** + * Update the Pdp Group info in Policy Model DB. + * + * @param pdpGroupList The list of Pdp Group info received from Policy Engine + */ + public void updatePdpGroupInfo(List pdpGroupList) { + List policyModelList = policyModelsRepository.findAll(); + for (PolicyModel policyModel : policyModelList) { + JsonArray supportedPdpGroups = new JsonArray(); + for (PdpGroup pdpGroup : pdpGroupList) { + JsonObject supportedPdpGroup = pdpGroup.getSupportedSubgroups(policyModel.getPolicyModelType(), + policyModel.getVersion()); + if (supportedPdpGroup != null) { + supportedPdpGroups.add(supportedPdpGroup); + } + } + + if (supportedPdpGroups.size() > 0) { + JsonObject supportedPdpJson = new JsonObject (); + supportedPdpJson.add("supportedPdpGroups", supportedPdpGroups); + policyModel.setPolicyPdpGroup(supportedPdpJson); + policyModelsRepository.save(policyModel); + } + } + } } diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index e1c08eea..cce9e567 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -68,6 +68,10 @@ public abstract class Policy extends AuditEntity { @Column(name = "pdp_group") private String pdpGroup; + @Expose + @Column(name = "pdp_sub_group") + private String pdpSubGroup; + public abstract String createPolicyPayload() throws UnsupportedEncodingException; /** @@ -154,6 +158,24 @@ public abstract class Policy extends AuditEntity { this.pdpGroup = pdpGroup; } + /** + * pdpSubGroup getter. + * + * @return the pdpSubGroup + */ + public String getPdpSubGroup() { + return pdpSubGroup; + } + + /** + * pdpSubGroup setter. + * + * @param pdpSubGroup the pdpSubGroup to set + */ + public void setPdpSubGroup(String pdpSubGroup) { + this.pdpSubGroup = pdpSubGroup; + } + /** * Generate the policy name. * diff --git a/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java b/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java index bd20eccb..ac054d8a 100644 --- a/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java +++ b/src/main/java/org/onap/clamp/policy/downloader/PolicyEngineController.java @@ -28,6 +28,7 @@ import com.att.eelf.configuration.EELFManager; import java.time.Instant; +import org.json.simple.parser.ParseException; import org.onap.clamp.clds.client.PolicyEngineServices; import org.onap.clamp.loop.template.PolicyModelsRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -70,5 +71,8 @@ public class PolicyEngineController { return lastInstantExecuted; } - + @Scheduled(fixedRate = 300000) + public synchronized void downloadPdpGroups() throws ParseException { + policyEngineServices.downloadPdpGroups(); + } } diff --git a/src/main/java/org/onap/clamp/policy/pdpgroup/PdpGroup.java b/src/main/java/org/onap/clamp/policy/pdpgroup/PdpGroup.java new file mode 100644 index 00000000..a3cf4e05 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/pdpgroup/PdpGroup.java @@ -0,0 +1,93 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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.clamp.policy.pdpgroup; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.annotations.Expose; + +import java.util.List; + +/** + * This class maps the get Pdp Group response to a nice pojo. + */ +public class PdpGroup { + + @Expose + private String name; + + @Expose + private String pdpGroupState; + + @Expose + private List pdpSubgroups; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPdpGroupState() { + return pdpGroupState; + } + + public void setPdpGroupState(String pdpGroupState) { + this.pdpGroupState = pdpGroupState; + } + + public List getPdpSubgroups() { + return pdpSubgroups; + } + + public void setPdpSubgroups(List pdpSubgroups) { + this.pdpSubgroups = pdpSubgroups; + } + + /** + * Get supported subGroups based on the defined policy type and version. + * @param policyType The policy type + * @param version The version + * @return The supported subGroup list in Json format + */ + public JsonObject getSupportedSubgroups(String policyType, String version) { + if (!pdpGroupState.equalsIgnoreCase("ACTIVE")) { + return null; + } + JsonArray supportedSubgroups = new JsonArray(); + for (PdpSubgroup subGroup : pdpSubgroups) { + if (subGroup.getSupportedPolicyTypes().contains(new PolicyModelKey(policyType, version))) { + supportedSubgroups.add(subGroup.getPdpType()); + } + } + if (supportedSubgroups.size() > 0) { + JsonObject supportedPdpGroup = new JsonObject(); + supportedPdpGroup.add(this.name, supportedSubgroups); + return supportedPdpGroup; + } + return null; + } +} diff --git a/src/main/java/org/onap/clamp/policy/pdpgroup/PdpSubgroup.java b/src/main/java/org/onap/clamp/policy/pdpgroup/PdpSubgroup.java new file mode 100644 index 00000000..28de79ab --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/pdpgroup/PdpSubgroup.java @@ -0,0 +1,56 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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.clamp.policy.pdpgroup; + +import com.google.gson.annotations.Expose; +import java.util.List; + +/** + * This class maps the Policy get PDP Group response to a nice pojo. + */ +public class PdpSubgroup { + + @Expose + private String pdpType; + + @Expose + private List supportedPolicyTypes; + + public String getPdpType() { + return pdpType; + } + + public void setPdpType(String pdpType) { + this.pdpType = pdpType; + } + + public List getSupportedPolicyTypes() { + return supportedPolicyTypes; + } + + public void setSupportedPolicyTypes(List supportedPolicyTypes) { + this.supportedPolicyTypes = supportedPolicyTypes; + } + +} diff --git a/src/main/java/org/onap/clamp/policy/pdpgroup/PolicyModelKey.java b/src/main/java/org/onap/clamp/policy/pdpgroup/PolicyModelKey.java new file mode 100644 index 00000000..707b3bd2 --- /dev/null +++ b/src/main/java/org/onap/clamp/policy/pdpgroup/PolicyModelKey.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 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.clamp.policy.pdpgroup; + +import com.google.gson.annotations.Expose; + +import java.io.Serializable; + +public class PolicyModelKey implements Serializable { + + /** + * The serial version ID. + */ + private static final long serialVersionUID = 3307410842013230886L; + + @Expose + private String name; + + @Expose + private String version; + + /** + * Constructor. + */ + public PolicyModelKey(String name, String version) { + this.name = name; + this.version = version; + } + + /** + * name getter. + * + * @return the name + */ + public String getName() { + return name; + } + + /** + * name setter. + * + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * version getter. + * + * @return the version + */ + public String getVersion() { + return version; + } + + /** + * version setter. + * + * @param version the version to set + */ + public void setVersion(String version) { + this.version = version; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((version == null) ? 0 : version.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + PolicyModelKey other = (PolicyModelKey) obj; + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + if (!name.matches(other.name)) { + return false; + } + } + if (version == null) { + if (other.version != null) { + return false; + } + } else if (!version.equals(other.version)) { + return false; + } + return true; + } +} -- cgit 1.2.3-korg