From 676194789a8b880e2416f9d3bf2484a9fc6be1bc Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Tue, 15 Feb 2022 12:16:30 +0000 Subject: Fix issue with GeneratedValue in PfGeneratedIdKey PfGeneratedIdKey class (which is used as a composite key in JpaPolicyAudit and JpaPdpStatistics) uses GeneratedValue in a wrong way and not according to the specification. This review fixes it. PfGeneratedIdKey class is removed, and the generatedId is directly specified in the JpaPolicyAudit and JpaPdpStatistics classes. Note: These classes are only used by PAP, so the related methods for db interaction is removed as PAP directly talks to DB using spring repository layer. Also the only end result this change brings is that the 'generatedId' alone will be used as the primary key instead of 'generatedId, name and version' together. Corresponding changes in DB Migrator: https://gerrit.onap.org/r/c/policy/docker/+/127139 PAP: https://gerrit.onap.org/r/c/policy/pap/+/127130 Change-Id: Ib4ea8b60ffe5c2480746569c0354bf474a6b7006 Issue-ID: POLICY-3897 Signed-off-by: a.sreekumar --- .../pap/persistence/concepts/JpaPolicyAudit.java | 83 +++++++++--- .../persistence/provider/PolicyAuditProvider.java | 146 --------------------- 2 files changed, 63 insertions(+), 166 deletions(-) delete mode 100644 models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java (limited to 'models-pap/src/main/java/org/onap') diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/concepts/JpaPolicyAudit.java b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/concepts/JpaPolicyAudit.java index e151441da..35e385637 100644 --- a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/concepts/JpaPolicyAudit.java +++ b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/concepts/JpaPolicyAudit.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021-2022 Bell Canada. 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. @@ -22,28 +22,36 @@ package org.onap.policy.models.pap.persistence.concepts; import java.time.Instant; +import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.persistence.Column; -import javax.persistence.EmbeddedId; import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; import javax.persistence.Index; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; +import javax.persistence.TableGenerator; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.validation.constraints.NotNull; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NonNull; import org.apache.commons.lang3.builder.CompareToBuilder; +import org.onap.policy.common.parameters.BeanValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.parameters.annotations.Pattern; import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; -import org.onap.policy.models.base.PfGeneratedIdKey; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfReferenceKey; -import org.onap.policy.models.base.validation.annotations.VerifyKey; +import org.onap.policy.models.base.Validated; import org.onap.policy.models.pap.concepts.PolicyAudit; import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; @@ -62,11 +70,24 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; public class JpaPolicyAudit extends PfConcept implements PfAuthorative { private static final long serialVersionUID = -2935734300607322191L; - @EmbeddedId - @Column - @NotNull - @VerifyKey(versionNotNull = true) - private PfGeneratedIdKey key; + @Id + @Column(name = "ID") + @GeneratedValue(strategy = GenerationType.TABLE, generator = "auditIdGen") + @TableGenerator( + name = "auditIdGen", + table = "sequence", + pkColumnName = "SEQ_NAME", + valueColumnName = "SEQ_COUNT", + pkColumnValue = "SEQ_GEN") + private Long generatedId; + + @Column(name = "name", length = 120) + @Pattern(regexp = PfKey.NAME_REGEXP) + private String name; + + @Column(name = "version", length = 20) + @Pattern(regexp = PfKey.VERSION_REGEXP) + private String version; @Column private String pdpGroup; @@ -90,7 +111,8 @@ public class JpaPolicyAudit extends PfConcept implements PfAuthorative getKeys() { - return getKey().getKeys(); + final List keyList = new ArrayList<>(); + keyList.add(getKey()); + return keyList; } @Override - public void clean() { - key.clean(); + public PfKey getKey() { + return new PfConceptKey(name, version); + } + @Override + public void clean() { pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP); pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP); user = Assertions.validateStringParameter("user", user, PfReferenceKey.LOCAL_NAME_REGEXP); } + + @Override + public BeanValidationResult validate(@NonNull String fieldName) { + BeanValidationResult result = super.validate(fieldName); + if (PfKey.NULL_KEY_NAME.equals(name)) { + result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL); + } + return result; + } } diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java deleted file mode 100644 index ec759d34d..000000000 --- a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java +++ /dev/null @@ -1,146 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation. - * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021 Bell Canada. 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.pap.persistence.provider; - -import java.time.Instant; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import javax.ws.rs.core.Response; -import lombok.Builder; -import lombok.Data; -import lombok.NonNull; -import org.apache.commons.lang3.StringUtils; -import org.onap.policy.common.parameters.BeanValidationResult; -import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.dao.PfDao; -import org.onap.policy.models.dao.PfFilterParametersIntfc; -import org.onap.policy.models.pap.concepts.PolicyAudit; -import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction; -import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit; - -/** - * Provider for Policy Audit. - * - * @author Adheli Tavares (adheli.tavares@est.tech) - * - */ -public class PolicyAuditProvider { - - private static final Integer DEFAULT_MAX_RECORDS = 100; - private static final Integer DEFAULT_MIN_RECORDS = 10; - - /** - * Create audit records. - * - * @param audits list of policy audit - */ - public void createAuditRecords(@NonNull PfDao dao, @NonNull final List audits) { - List jpaAudits = audits.stream().map(JpaPolicyAudit::new).collect(Collectors.toList()); - - var result = new BeanValidationResult("createAuditRecords", jpaAudits); - - var count = 0; - for (JpaPolicyAudit jpaAudit : jpaAudits) { - result.addResult(jpaAudit.validate(String.valueOf(count++))); - } - - if (!result.isValid()) { - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, result.getResult()); - } - - dao.createCollection(jpaAudits); - } - - /** - * Collect audit records based on filters at {@link AuditFilter}. - * - * @param auditFilter {@link AuditFilter} object with filters for search - * @return list of {@link PolicyAudit} records - */ - public List getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter) { - if (auditFilter.getRecordNum() < 1) { - auditFilter.setRecordNum(DEFAULT_MIN_RECORDS); - } else if (auditFilter.getRecordNum() > DEFAULT_MAX_RECORDS) { - auditFilter.setRecordNum(DEFAULT_MAX_RECORDS); - } - - return dao.getFiltered(JpaPolicyAudit.class, auditFilter).stream().map(JpaPolicyAudit::toAuthorative) - .collect(Collectors.toList()); - } - - /** - * Create a filter for looking for audit records. - * name - policy name - * version - policy version - * pdpGroup - PDP group that policy might be related - * action - type of action/operation realized on policy - * fromDate - start of period in case of time interval search - */ - @Data - @Builder - public static class AuditFilter implements PfFilterParametersIntfc { - private String name; - private String version; - private AuditAction action; - private String pdpGroup; - private Instant fromDate; - private Instant toDate; - private int recordNum; - @Builder.Default - private String sortOrder = "DESC"; - - // initialized lazily, if not set via the builder - private Map filterMap; - - @Override - public Instant getStartTime() { - return fromDate; - } - - @Override - public Instant getEndTime() { - return toDate; - } - - @Override - public Map getFilterMap() { - if (filterMap != null) { - return filterMap; - } - - filterMap = new HashMap<>(); - - if (StringUtils.isNotBlank(pdpGroup)) { - filterMap.put("pdpGroup", pdpGroup); - } - - if (action != null) { - filterMap.put("action", action); - } - - return filterMap; - } - } -} -- cgit 1.2.3-korg