2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019, 2021, 2023 Nordix Foundation.
7 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * SPDX-License-Identifier: Apache-2.0
22 * ============LICENSE_END=========================================================
25 package org.onap.policy.models.pdp.persistence.concepts;
27 import jakarta.persistence.CascadeType;
28 import jakarta.persistence.CollectionTable;
29 import jakarta.persistence.Column;
30 import jakarta.persistence.ElementCollection;
31 import jakarta.persistence.EmbeddedId;
32 import jakarta.persistence.Entity;
33 import jakarta.persistence.FetchType;
34 import jakarta.persistence.Inheritance;
35 import jakarta.persistence.InheritanceType;
36 import jakarta.persistence.JoinColumn;
37 import jakarta.persistence.OneToMany;
38 import jakarta.persistence.Table;
39 import java.io.Serial;
40 import java.util.ArrayList;
41 import java.util.LinkedHashMap;
42 import java.util.List;
44 import java.util.Map.Entry;
46 import lombok.EqualsAndHashCode;
47 import lombok.NonNull;
48 import org.apache.commons.lang3.ObjectUtils;
49 import org.onap.policy.common.parameters.annotations.NotBlank;
50 import org.onap.policy.common.parameters.annotations.NotNull;
51 import org.onap.policy.common.parameters.annotations.Valid;
52 import org.onap.policy.models.base.PfAuthorative;
53 import org.onap.policy.models.base.PfConcept;
54 import org.onap.policy.models.base.PfConceptKey;
55 import org.onap.policy.models.base.PfKey;
56 import org.onap.policy.models.base.PfReferenceKey;
57 import org.onap.policy.models.base.PfUtils;
58 import org.onap.policy.models.base.validation.annotations.VerifyKey;
59 import org.onap.policy.models.pdp.concepts.PdpGroup;
60 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
61 import org.onap.policy.models.pdp.enums.PdpState;
64 * Class to represent a PDP group in the database.
66 * @author Liam Fallon (liam.fallon@est.tech)
69 @Table(name = "PdpGroup")
70 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
72 @EqualsAndHashCode(callSuper = false)
73 public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
75 private static final long serialVersionUID = -357224425637789775L;
80 private PfConceptKey key;
84 private String description;
88 private PdpState pdpGroupState;
91 private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> properties;
94 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
95 @CollectionTable(joinColumns = {
96 @JoinColumn(name = "name", referencedColumnName = "name"),
97 @JoinColumn(name = "version", referencedColumnName = "version")
101 private List<@NotNull @Valid JpaPdpSubGroup> pdpSubGroups;
104 * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
106 public JpaPdpGroup() {
107 this(new PfConceptKey());
111 * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
115 public JpaPdpGroup(@NonNull final PfConceptKey key) {
116 this(key, PdpState.PASSIVE, new ArrayList<>());
120 * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
123 * @param pdpGroupState State of the PDP group
125 public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
126 @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
128 this.pdpGroupState = pdpGroupState;
129 this.pdpSubGroups = pdpSubGroups;
135 * @param copyConcept the concept to copy from
137 public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
139 this.key = new PfConceptKey(copyConcept.key);
140 this.description = copyConcept.description;
141 this.pdpGroupState = copyConcept.pdpGroupState;
142 this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
143 this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
147 * Authorative constructor.
149 * @param authorativeConcept the authorative concept to copy from
151 public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
152 this.fromAuthorative(authorativeConcept);
156 public PdpGroup toAuthorative() {
157 var pdpGroup = new PdpGroup();
159 pdpGroup.setName(getKey().getName());
160 pdpGroup.setVersion(getKey().getVersion());
161 pdpGroup.setDescription(description);
162 pdpGroup.setPdpGroupState(pdpGroupState);
164 pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
166 pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
167 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
168 pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
175 public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
176 if (this.key == null || this.getKey().isNullKey()) {
177 this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
180 this.description = pdpGroup.getDescription();
181 this.pdpGroupState = pdpGroup.getPdpGroupState();
184 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
186 this.pdpSubGroups = new ArrayList<>();
187 for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
188 var jpaPdpSubGroup = new JpaPdpSubGroup();
189 jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
190 jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
191 this.pdpSubGroups.add(jpaPdpSubGroup);
196 public List<PfKey> getKeys() {
197 List<PfKey> keyList = getKey().getKeys();
199 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
200 keyList.addAll(jpaPdpSubgroup.getKeys());
207 public void clean() {
210 description = (description == null ? null : description.trim());
212 if (properties != null) {
213 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
214 for (Entry<String, String> propertyEntry : properties.entrySet()) {
215 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
217 properties = cleanedPropertyMap;
220 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
221 jpaPdpSubgroup.clean();
226 public int compareTo(final PfConcept otherConcept) {
227 if (otherConcept == null) {
230 if (this == otherConcept) {
233 if (getClass() != otherConcept.getClass()) {
234 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
237 final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
238 if (!key.equals(other.key)) {
239 return key.compareTo(other.key);
242 int result = ObjectUtils.compare(description, other.description);
247 result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
252 result = PfUtils.compareObjects(properties, other.properties);
257 return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);