2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019,2021 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 java.util.ArrayList;
28 import java.util.LinkedHashMap;
29 import java.util.List;
31 import java.util.Map.Entry;
32 import javax.persistence.CascadeType;
33 import javax.persistence.CollectionTable;
34 import javax.persistence.Column;
35 import javax.persistence.ElementCollection;
36 import javax.persistence.EmbeddedId;
37 import javax.persistence.Entity;
38 import javax.persistence.FetchType;
39 import javax.persistence.Inheritance;
40 import javax.persistence.InheritanceType;
41 import javax.persistence.JoinColumn;
42 import javax.persistence.OneToMany;
43 import javax.persistence.Table;
45 import lombok.EqualsAndHashCode;
46 import lombok.NonNull;
47 import org.apache.commons.lang3.ObjectUtils;
48 import org.onap.policy.common.parameters.annotations.NotBlank;
49 import org.onap.policy.common.parameters.annotations.NotNull;
50 import org.onap.policy.common.parameters.annotations.Valid;
51 import org.onap.policy.models.base.PfAuthorative;
52 import org.onap.policy.models.base.PfConcept;
53 import org.onap.policy.models.base.PfConceptKey;
54 import org.onap.policy.models.base.PfKey;
55 import org.onap.policy.models.base.PfReferenceKey;
56 import org.onap.policy.models.base.PfUtils;
57 import org.onap.policy.models.base.validation.annotations.VerifyKey;
58 import org.onap.policy.models.pdp.concepts.PdpGroup;
59 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
60 import org.onap.policy.models.pdp.enums.PdpState;
63 * Class to represent a PDP group in the database.
65 * @author Liam Fallon (liam.fallon@est.tech)
68 @Table(name = "PdpGroup")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
71 @EqualsAndHashCode(callSuper = false)
72 public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
73 private static final long serialVersionUID = -357224425637789775L;
78 private PfConceptKey key;
82 private String description;
86 private PdpState pdpGroupState;
89 private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> properties;
92 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
93 @CollectionTable(joinColumns = {
94 @JoinColumn(name = "name", referencedColumnName = "name"),
95 @JoinColumn(name = "version", referencedColumnName = "version")
99 private List<@NotNull @Valid JpaPdpSubGroup> pdpSubGroups;
102 * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
104 public JpaPdpGroup() {
105 this(new PfConceptKey());
109 * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
113 public JpaPdpGroup(@NonNull final PfConceptKey key) {
114 this(key, PdpState.PASSIVE, new ArrayList<>());
118 * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
121 * @param pdpGroupState State of the PDP group
123 public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
124 @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
126 this.pdpGroupState = pdpGroupState;
127 this.pdpSubGroups = pdpSubGroups;
133 * @param copyConcept the concept to copy from
135 public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
137 this.key = new PfConceptKey(copyConcept.key);
138 this.description = copyConcept.description;
139 this.pdpGroupState = copyConcept.pdpGroupState;
140 this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
141 this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
145 * Authorative constructor.
147 * @param authorativeConcept the authorative concept to copy from
149 public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
150 this.fromAuthorative(authorativeConcept);
154 public PdpGroup toAuthorative() {
155 var pdpGroup = new PdpGroup();
157 pdpGroup.setName(getKey().getName());
158 pdpGroup.setVersion(getKey().getVersion());
159 pdpGroup.setDescription(description);
160 pdpGroup.setPdpGroupState(pdpGroupState);
162 pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
164 pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
165 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
166 pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
173 public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
174 if (this.key == null || this.getKey().isNullKey()) {
175 this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
178 this.description = pdpGroup.getDescription();
179 this.pdpGroupState = pdpGroup.getPdpGroupState();
182 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
184 this.pdpSubGroups = new ArrayList<>();
185 for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
186 var jpaPdpSubGroup = new JpaPdpSubGroup();
187 jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
188 jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
189 this.pdpSubGroups.add(jpaPdpSubGroup);
194 public List<PfKey> getKeys() {
195 List<PfKey> keyList = getKey().getKeys();
197 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
198 keyList.addAll(jpaPdpSubgroup.getKeys());
205 public void clean() {
208 description = (description == null ? null : description.trim());
210 if (properties != null) {
211 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
212 for (Entry<String, String> propertyEntry : properties.entrySet()) {
213 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
215 properties = cleanedPropertyMap;
218 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
219 jpaPdpSubgroup.clean();
224 public int compareTo(final PfConcept otherConcept) {
225 if (otherConcept == null) {
228 if (this == otherConcept) {
231 if (getClass() != otherConcept.getClass()) {
232 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
235 final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
236 if (!key.equals(other.key)) {
237 return key.compareTo(other.key);
240 int result = ObjectUtils.compare(description, other.description);
245 result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
250 result = PfUtils.compareObjects(properties, other.properties);
255 return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);