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.apache.commons.lang3.StringUtils;
48 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfReferenceKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.PfValidationMessage;
56 import org.onap.policy.models.base.PfValidationResult;
57 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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;
76 private PfConceptKey key;
79 private String description;
82 private PdpState pdpGroupState;
85 private Map<String, String> properties;
88 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
89 @CollectionTable(joinColumns = {
90 @JoinColumn(name = "pdpGroupParentKeyName", referencedColumnName = "parentKeyName"),
91 @JoinColumn(name = "pdpGroupParentKeyVersion", referencedColumnName = "parentKeyVersion"),
92 @JoinColumn(name = "pdpGroupParentLocalName", referencedColumnName = "parentLocalName"),
93 @JoinColumn(name = "pdpGroupLocalName", referencedColumnName = "localName")
96 private List<JpaPdpSubGroup> pdpSubGroups;
99 * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
101 public JpaPdpGroup() {
102 this(new PfConceptKey());
106 * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
110 public JpaPdpGroup(@NonNull final PfConceptKey key) {
111 this(key, PdpState.PASSIVE, new ArrayList<>());
115 * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
118 * @param pdpGroupState State of the PDP group
120 public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
121 @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
123 this.pdpGroupState = pdpGroupState;
124 this.pdpSubGroups = pdpSubGroups;
130 * @param copyConcept the concept to copy from
132 public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
134 this.key = new PfConceptKey(copyConcept.key);
135 this.description = copyConcept.description;
136 this.pdpGroupState = copyConcept.pdpGroupState;
137 this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
138 this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
142 * Authorative constructor.
144 * @param authorativeConcept the authorative concept to copy from
146 public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
147 this.fromAuthorative(authorativeConcept);
151 public PdpGroup toAuthorative() {
152 PdpGroup pdpGroup = new PdpGroup();
154 pdpGroup.setName(getKey().getName());
155 pdpGroup.setVersion(getKey().getVersion());
156 pdpGroup.setDescription(description);
157 pdpGroup.setPdpGroupState(pdpGroupState);
159 pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
161 pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
162 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
163 pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
170 public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
171 if (this.key == null || this.getKey().isNullKey()) {
172 this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
175 this.description = pdpGroup.getDescription();
176 this.pdpGroupState = pdpGroup.getPdpGroupState();
179 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
181 this.pdpSubGroups = new ArrayList<>();
182 for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
183 JpaPdpSubGroup jpaPdpSubGroup = new JpaPdpSubGroup();
184 jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
185 jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
186 this.pdpSubGroups.add(jpaPdpSubGroup);
191 public List<PfKey> getKeys() {
192 List<PfKey> keyList = getKey().getKeys();
194 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
195 keyList.addAll(jpaPdpSubgroup.getKeys());
202 public void clean() {
205 description = (description == null ? null : description.trim());
207 if (properties != null) {
208 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
209 for (Entry<String, String> propertyEntry : properties.entrySet()) {
210 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
212 properties = cleanedPropertyMap;
215 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
216 jpaPdpSubgroup.clean();
221 public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
222 PfValidationResult result = resultIn;
224 if (key.isNullKey()) {
225 result.addValidationMessage(
226 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
229 result = key.validate(result);
231 if (description != null && StringUtils.isBlank(description)) {
232 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
233 "description may not be blank"));
236 if (pdpGroupState == null) {
237 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
238 "pdpGroupState may not be null"));
241 if (properties != null) {
242 validateProperties(result);
245 if (pdpSubGroups == null) {
246 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
247 "a PDP group must have a list of PDP subgroups"));
249 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
250 result = jpaPdpSubgroup.validate(result);
258 * Validate the properties.
260 * @param result where to place any new validation results
262 private void validateProperties(PfValidationResult result) {
264 for (Entry<String, String> propertyEntry : properties.entrySet()) {
265 if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
266 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
267 "a property key may not be null or blank"));
269 if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getValue())) {
270 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
271 "a property value may not be null or blank"));
277 public int compareTo(final PfConcept otherConcept) {
278 if (otherConcept == null) {
281 if (this == otherConcept) {
284 if (getClass() != otherConcept.getClass()) {
285 return this.getClass().getName().compareTo(otherConcept.getClass().getName());
288 final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
289 if (!key.equals(other.key)) {
290 return key.compareTo(other.key);
293 int result = ObjectUtils.compare(description, other.description);
298 result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
303 result = PfUtils.compareObjects(properties, other.properties);
308 return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);