2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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;
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.Inheritance;
38 import javax.persistence.InheritanceType;
39 import javax.persistence.JoinColumn;
40 import javax.persistence.OneToMany;
41 import javax.persistence.Table;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
47 import org.apache.commons.lang3.ObjectUtils;
48 import org.apache.commons.lang3.StringUtils;
49 import org.onap.policy.common.utils.validation.Assertions;
50 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
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.PfValidationMessage;
58 import org.onap.policy.models.base.PfValidationResult;
59 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
60 import org.onap.policy.models.pdp.concepts.PdpGroup;
61 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
62 import org.onap.policy.models.pdp.enums.PdpState;
65 * Class to represent a PDP group in the database.
67 * @author Liam Fallon (liam.fallon@est.tech)
70 @Table(name = "PdpGroup")
71 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
73 @EqualsAndHashCode(callSuper = false)
74 public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
75 private static final long serialVersionUID = -357224425637789775L;
78 private PfConceptKey key;
81 private String description;
84 private PdpState pdpGroupState;
87 private Map<String, String> properties;
91 @CollectionTable(joinColumns = {
92 @JoinColumn(name = "pdpGroupParentKeyName", referencedColumnName = "parentKeyName"),
93 @JoinColumn(name = "pdpGroupParentKeyVersion", referencedColumnName = "parentKeyVersion"),
94 @JoinColumn(name = "pdpGroupParentLocalName", referencedColumnName = "parentLocalName"),
95 @JoinColumn(name = "pdpGroupLocalName", referencedColumnName = "localName")
98 private List<JpaPdpSubGroup> pdpSubGroups;
101 * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
103 public JpaPdpGroup() {
104 this(new PfConceptKey());
108 * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
112 public JpaPdpGroup(@NonNull final PfConceptKey key) {
113 this(key, PdpState.PASSIVE, new ArrayList<>());
117 * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
120 * @param pdpGroupState State of the PDP group
122 public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
123 @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
125 this.pdpGroupState = pdpGroupState;
126 this.pdpSubGroups = pdpSubGroups;
132 * @param copyConcept the concept to copy from
134 public JpaPdpGroup(final JpaPdpGroup copyConcept) {
139 * Authorative constructor.
141 * @param authorativeConcept the authorative concept to copy from
143 public JpaPdpGroup(final PdpGroup authorativeConcept) {
144 this.fromAuthorative(authorativeConcept);
148 public PdpGroup toAuthorative() {
149 PdpGroup pdpGroup = new PdpGroup();
151 pdpGroup.setName(getKey().getName());
152 pdpGroup.setVersion(getKey().getVersion());
153 pdpGroup.setDescription(description);
154 pdpGroup.setPdpGroupState(pdpGroupState);
156 pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
158 pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
159 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
160 pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
167 public void fromAuthorative(final PdpGroup pdpGroup) {
168 this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
170 this.description = pdpGroup.getDescription();
171 this.pdpGroupState = pdpGroup.getPdpGroupState();
174 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
176 this.pdpSubGroups = new ArrayList<>();
177 for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
178 JpaPdpSubGroup jpaPdpSubGroup = new JpaPdpSubGroup();
179 jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
180 jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
181 this.pdpSubGroups.add(jpaPdpSubGroup);
186 public List<PfKey> getKeys() {
187 List<PfKey> keyList = getKey().getKeys();
189 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
190 keyList.addAll(jpaPdpSubgroup.getKeys());
198 public void clean() {
201 description = (description == null ? null : description.trim());
203 if (properties != null) {
204 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
205 for (Entry<String, String> propertyEntry : properties.entrySet()) {
206 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
208 properties = cleanedPropertyMap;
211 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
212 jpaPdpSubgroup.clean();
217 public PfValidationResult validate(final PfValidationResult resultIn) {
218 PfValidationResult result = resultIn;
220 if (key.isNullKey()) {
221 result.addValidationMessage(
222 new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
225 result = key.validate(result);
227 if (StringUtils.isBlank(description)) {
228 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
229 "description may not be blank"));
232 if (properties != null) {
233 for (Entry<String, String> propertyEntry : properties.entrySet()) {
234 if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
235 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
236 "a property key may not be null or blank"));
238 if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getValue())) {
239 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
240 "a property value may not be null or blank"));
246 if (pdpSubGroups == null) {
247 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
248 "a PDP group must have a list of PDP subgroups"));
250 for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
251 result = jpaPdpSubgroup.validate(result);
259 public int compareTo(final PfConcept otherConcept) {
260 if (otherConcept == null) {
263 if (this == otherConcept) {
266 if (getClass() != otherConcept.getClass()) {
267 return this.getClass().getCanonicalName().compareTo(otherConcept.getClass().getCanonicalName());
270 final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
271 if (!key.equals(other.key)) {
272 return key.compareTo(other.key);
275 int result = ObjectUtils.compare(description, other.description);
280 result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
285 result = PfUtils.compareObjects(properties, other.properties);
290 return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);
294 public PfConcept copyTo(@NonNull final PfConcept target) {
295 Assertions.instanceOf(target, JpaPdpGroup.class);
297 final JpaPdpGroup copy = ((JpaPdpGroup) target);
298 copy.setKey(new PfConceptKey(key));
300 copy.setDescription(description);
301 copy.setPdpGroupState(pdpGroupState);
302 copy.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
303 copy.setPdpSubGroups(PfUtils.mapList(pdpSubGroups, JpaPdpSubGroup::new));