2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.pdp.persistence.concepts;
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import javax.persistence.CascadeType;
32 import javax.persistence.CollectionTable;
33 import javax.persistence.Column;
34 import javax.persistence.ElementCollection;
35 import javax.persistence.EmbeddedId;
36 import javax.persistence.Entity;
37 import javax.persistence.FetchType;
38 import javax.persistence.Inheritance;
39 import javax.persistence.InheritanceType;
40 import javax.persistence.JoinColumn;
41 import javax.persistence.OneToMany;
42 import javax.persistence.Table;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
46 import org.apache.commons.lang3.ObjectUtils;
47 import org.onap.policy.common.parameters.annotations.Entries;
48 import org.onap.policy.common.parameters.annotations.Items;
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> {
74 private static final long serialVersionUID = -357224425637789775L;
79 private PfConceptKey key;
83 private String description;
87 private PdpState pdpGroupState;
90 @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
91 value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
92 private Map<String, String> properties;
95 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
96 @CollectionTable(joinColumns = {
97 @JoinColumn(name = "pdpGroupParentKeyName", referencedColumnName = "parentKeyName"),
98 @JoinColumn(name = "pdpGroupParentKeyVersion", referencedColumnName = "parentKeyVersion"),
99 @JoinColumn(name = "pdpGroupParentLocalName", referencedColumnName = "parentLocalName"),
100 @JoinColumn(name = "pdpGroupLocalName", referencedColumnName = "localName")
104 @Items(notNull = {@NotNull}, valid = {@Valid})
105 private List<JpaPdpSubGroup> pdpSubGroups;
108 * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
110 public JpaPdpGroup() {
111 this(new PfConceptKey());
115 * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
119 public JpaPdpGroup(@NonNull final PfConceptKey key) {
120 this(key, PdpState.PASSIVE, new ArrayList<>());
124 * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
127 * @param pdpGroupState State of the PDP group
129 public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
130 @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
132 this.pdpGroupState = pdpGroupState;
133 this.pdpSubGroups = pdpSubGroups;
139 * @param copyConcept the concept to copy from
141 public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
143 this.key = new PfConceptKey(copyConcept.key);
144 this.description = copyConcept.description;
145 this.pdpGroupState = copyConcept.pdpGroupState;
146 this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
147 this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
151 * Authorative constructor.
153 * @param authorativeConcept the authorative concept to copy from
155 public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
156 this.fromAuthorative(authorativeConcept);
160 public PdpGroup toAuthorative() {
161 PdpGroup pdpGroup = new PdpGroup();
163 pdpGroup.setName(getKey().getName());
164 pdpGroup.setVersion(getKey().getVersion());
165 pdpGroup.setDescription(description);
166 pdpGroup.setPdpGroupState(pdpGroupState);
168 pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
170 pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
171 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
172 pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
179 public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
180 if (this.key == null || this.getKey().isNullKey()) {
181 this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
184 this.description = pdpGroup.getDescription();
185 this.pdpGroupState = pdpGroup.getPdpGroupState();
188 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
190 this.pdpSubGroups = new ArrayList<>();
191 for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
192 JpaPdpSubGroup jpaPdpSubGroup = new JpaPdpSubGroup();
193 jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
194 jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
195 this.pdpSubGroups.add(jpaPdpSubGroup);
200 public List<PfKey> getKeys() {
201 List<PfKey> keyList = getKey().getKeys();
203 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
204 keyList.addAll(jpaPdpSubgroup.getKeys());
211 public void clean() {
214 description = (description == null ? null : description.trim());
216 if (properties != null) {
217 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
218 for (Entry<String, String> propertyEntry : properties.entrySet()) {
219 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
221 properties = cleanedPropertyMap;
224 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
225 jpaPdpSubgroup.clean();
230 public int compareTo(final PfConcept otherConcept) {
231 if (otherConcept == null) {
234 if (this == otherConcept) {
237 if (getClass() != otherConcept.getClass()) {
238 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
241 final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
242 if (!key.equals(other.key)) {
243 return key.compareTo(other.key);
246 int result = ObjectUtils.compare(description, other.description);
251 result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
256 result = PfUtils.compareObjects(properties, other.properties);
261 return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);