diff options
author | xuegao <xg353y@intl.att.com> | 2020-02-18 14:20:23 +0100 |
---|---|---|
committer | xuegao <xg353y@intl.att.com> | 2020-02-26 15:59:25 +0100 |
commit | 3a26471260e56f7a87533f0147fc63530d6ea08c (patch) | |
tree | 400059c05ccb58b0e10d3ce1309085a944e394b8 /src/main/java | |
parent | e0d0fdb5484d2c02669358160a11d6751ada4f3a (diff) |
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 <xg353y@intl.att.com>
Diffstat (limited to 'src/main/java')
8 files changed, 424 insertions, 12 deletions
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 02e2dd0b6..b3fcb6f18 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<String, Object> policyTypeEntry = (Map.Entry<String, Object>) new ArrayList(policyType.entrySet()).get(0); - policyModelsSService.createPolicyInDbIfNeeded( + policyModelsService.createPolicyInDbIfNeeded( createPolicyModelFromPolicyEngine(policyTypeEntry.getKey(), ((String) ((LinkedHashMap<String, Object>) 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<PdpGroup> 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 fd0110c79..7334eceb8 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<PolicyModel> { /** @@ -78,6 +85,10 @@ public class PolicyModel extends AuditEntity implements Serializable, Comparable @ManyToMany(mappedBy = "policyModels", fetch = FetchType.EAGER) private Set<LoopElementModel> usedByElementModels = new HashSet<>(); + @Type(type = "json") + @Column(columnDefinition = "json", name = "policy_pdp_group") + private JsonObject policyPdpGroup; + /** * usedByElementModels getter. * @@ -88,6 +99,24 @@ public class PolicyModel extends AuditEntity implements Serializable, Comparable } /** + * 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. * * @return the policyModelTosca 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 eb83c6608..b38be942e 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<PdpGroup> pdpGroupList) { + List<PolicyModel> 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 e1c08eeaa..cce9e567f 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; /** @@ -155,6 +159,24 @@ public abstract class Policy extends AuditEntity { } /** + * 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. * * @param policyType The policy type 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 bd20eccb6..ac054d8af 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 000000000..a3cf4e053 --- /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<PdpSubgroup> 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<PdpSubgroup> getPdpSubgroups() { + return pdpSubgroups; + } + + public void setPdpSubgroups(List<PdpSubgroup> 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 000000000..28de79abf --- /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<PolicyModelKey> supportedPolicyTypes; + + public String getPdpType() { + return pdpType; + } + + public void setPdpType(String pdpType) { + this.pdpType = pdpType; + } + + public List<PolicyModelKey> getSupportedPolicyTypes() { + return supportedPolicyTypes; + } + + public void setSupportedPolicyTypes(List<PolicyModelKey> 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 000000000..707b3bd2f --- /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; + } +} |