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 * ================================================================================
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.Column;
33 import javax.persistence.ElementCollection;
34 import javax.persistence.EmbeddedId;
35 import javax.persistence.Entity;
36 import javax.persistence.FetchType;
37 import javax.persistence.Inheritance;
38 import javax.persistence.InheritanceType;
39 import javax.persistence.JoinColumn;
40 import javax.persistence.JoinTable;
41 import javax.persistence.OneToMany;
42 import javax.persistence.Table;
44 import lombok.EqualsAndHashCode;
45 import lombok.NonNull;
46 import org.onap.policy.common.parameters.BeanValidationResult;
47 import org.onap.policy.common.parameters.annotations.Min;
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.PfKeyUse;
56 import org.onap.policy.models.base.PfReferenceKey;
57 import org.onap.policy.models.base.PfSearchableKey;
58 import org.onap.policy.models.base.PfUtils;
59 import org.onap.policy.models.base.validation.annotations.VerifyKey;
60 import org.onap.policy.models.pdp.concepts.Pdp;
61 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
62 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
65 * Class to represent a PDP subgroup in the database.
67 * @author Liam Fallon (liam.fallon@est.tech)
70 @Table(name = "PdpSubGroup")
71 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
73 @EqualsAndHashCode(callSuper = false)
74 public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGroup> {
75 private static final long serialVersionUID = -357224425637789775L;
80 private PfReferenceKey key;
84 private List<@NotNull @Valid PfSearchableKey> supportedPolicyTypes;
88 private List<PfConceptKey> policies;
92 private int currentInstanceCount;
96 private int desiredInstanceCount;
99 private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> properties;
102 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
105 @JoinColumn(name = "pdpParentKeyName", referencedColumnName = "parentKeyName"),
106 @JoinColumn(name = "pdpParentKeyVersion", referencedColumnName = "parentKeyVersion"),
107 @JoinColumn(name = "pdpParentLocalName", referencedColumnName = "parentLocalName"),
108 @JoinColumn(name = "pdpLocalName", referencedColumnName = "localName")
113 private List<@NotNull @Valid JpaPdp> pdpInstances;
116 * The Default Constructor creates a {@link JpaPdpSubGroup} object with a null key.
118 public JpaPdpSubGroup() {
119 this(new PfReferenceKey());
123 * The Key Constructor creates a {@link JpaPdpSubGroup} object with the given concept key.
127 public JpaPdpSubGroup(@NonNull final PfReferenceKey key) {
128 this(key, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
132 * The Key Constructor creates a {@link JpaPdpSubGroup} object with all mandatory fields.
135 * @param supportedPolicyTypes Supported policy types
136 * @param policies policies deployed on this PDP subgroups
137 * @param pdpInstances the PDP instances on this PDP subgroups
139 public JpaPdpSubGroup(@NonNull final PfReferenceKey key, @NonNull final List<PfSearchableKey> supportedPolicyTypes,
140 @NonNull List<PfConceptKey> policies, @NonNull final List<JpaPdp> pdpInstances) {
142 this.supportedPolicyTypes = supportedPolicyTypes;
143 this.policies = policies;
144 this.pdpInstances = pdpInstances;
150 * @param copyConcept the concept to copy from
152 public JpaPdpSubGroup(@NonNull final JpaPdpSubGroup copyConcept) {
154 this.key = new PfReferenceKey(copyConcept.key);
155 this.supportedPolicyTypes = PfUtils.mapList(copyConcept.supportedPolicyTypes,
156 PfSearchableKey::new, new ArrayList<>(0));
157 this.policies = PfUtils.mapList(copyConcept.policies, PfConceptKey::new, new ArrayList<>(0));
158 this.currentInstanceCount = copyConcept.currentInstanceCount;
159 this.desiredInstanceCount = copyConcept.desiredInstanceCount;
160 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
161 this.pdpInstances = PfUtils.mapList(copyConcept.pdpInstances, JpaPdp::new, new ArrayList<>(0));
165 * Authorative constructor.
167 * @param authorativeConcept the authorative concept to copy from
169 public JpaPdpSubGroup(@NonNull final PdpSubGroup authorativeConcept) {
170 this.fromAuthorative(authorativeConcept);
174 public PdpSubGroup toAuthorative() {
175 PdpSubGroup pdpSubgroup = new PdpSubGroup();
177 pdpSubgroup.setPdpType(getKey().getLocalName());
179 pdpSubgroup.setSupportedPolicyTypes(new ArrayList<>());
180 for (PfSearchableKey supportedPolicyTypeKey : supportedPolicyTypes) {
181 ToscaConceptIdentifier supportedPolicyTypeIdent = new ToscaConceptIdentifier(
182 supportedPolicyTypeKey.getName(), supportedPolicyTypeKey.getVersion());
183 pdpSubgroup.getSupportedPolicyTypes().add(supportedPolicyTypeIdent);
186 pdpSubgroup.setPolicies(new ArrayList<>());
187 for (PfConceptKey policyKey : policies) {
188 ToscaConceptIdentifier toscaPolicyIdentifier = new ToscaConceptIdentifier();
189 toscaPolicyIdentifier.setName(policyKey.getName());
190 toscaPolicyIdentifier.setVersion(policyKey.getVersion());
191 pdpSubgroup.getPolicies().add(toscaPolicyIdentifier);
194 pdpSubgroup.setCurrentInstanceCount(currentInstanceCount);
195 pdpSubgroup.setDesiredInstanceCount(desiredInstanceCount);
196 pdpSubgroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
198 pdpSubgroup.setPdpInstances(new ArrayList<>());
199 for (JpaPdp jpaPdp : pdpInstances) {
200 pdpSubgroup.getPdpInstances().add(jpaPdp.toAuthorative());
207 public void fromAuthorative(@NonNull final PdpSubGroup pdpSubgroup) {
208 if (this.key == null || this.getKey().isNullKey()) {
209 this.setKey(new PfReferenceKey());
210 getKey().setLocalName(pdpSubgroup.getPdpType());
213 this.supportedPolicyTypes = new ArrayList<>();
214 if (pdpSubgroup.getSupportedPolicyTypes() != null) {
215 for (ToscaConceptIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) {
216 this.supportedPolicyTypes
217 .add(new PfSearchableKey(supportedPolicyType.getName(), supportedPolicyType.getVersion()));
221 this.policies = new ArrayList<>();
222 if (pdpSubgroup.getPolicies() != null) {
223 for (ToscaConceptIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) {
225 .add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion()));
228 this.currentInstanceCount = pdpSubgroup.getCurrentInstanceCount();
229 this.desiredInstanceCount = pdpSubgroup.getDesiredInstanceCount();
231 (pdpSubgroup.getProperties() == null ? null : new LinkedHashMap<>(pdpSubgroup.getProperties()));
233 this.pdpInstances = new ArrayList<>();
234 if (pdpSubgroup.getPdpInstances() != null) {
235 for (Pdp pdp : pdpSubgroup.getPdpInstances()) {
236 JpaPdp jpaPdp = new JpaPdp();
237 jpaPdp.setKey(new PfReferenceKey(getKey(), pdp.getInstanceId()));
238 jpaPdp.fromAuthorative(pdp);
239 this.pdpInstances.add(jpaPdp);
245 public List<PfKey> getKeys() {
246 List<PfKey> keyList = getKey().getKeys();
248 for (PfSearchableKey ptkey : supportedPolicyTypes) {
249 keyList.add(new PfKeyUse(ptkey));
252 for (PfConceptKey pkey : policies) {
253 keyList.add(new PfKeyUse(pkey));
256 for (JpaPdp jpaPdp : pdpInstances) {
257 keyList.addAll(jpaPdp.getKeys());
265 public void clean() {
268 for (PfSearchableKey ptkey : supportedPolicyTypes) {
272 for (PfConceptKey pkey : policies) {
276 if (properties != null) {
277 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
278 for (Entry<String, String> propertyEntry : properties.entrySet()) {
279 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
281 properties = cleanedPropertyMap;
284 for (JpaPdp jpaPdp : pdpInstances) {
290 public BeanValidationResult validate(@NonNull String fieldName) {
291 BeanValidationResult result = super.validate(fieldName);
293 validateKeyNotNull(result, "parent of key", key.getParentConceptKey());
295 if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
296 addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty");
303 public int compareTo(final PfConcept otherConcept) {
304 if (otherConcept == null) {
307 if (this == otherConcept) {
310 if (getClass() != otherConcept.getClass()) {
311 return getClass().getName().compareTo(otherConcept.getClass().getName());
314 final JpaPdpSubGroup other = (JpaPdpSubGroup) otherConcept;
315 if (!key.equals(other.key)) {
316 return key.compareTo(other.key);
319 int result = PfUtils.compareObjects(supportedPolicyTypes, other.supportedPolicyTypes);
324 result = PfUtils.compareObjects(policies, other.policies);
329 if (currentInstanceCount != other.currentInstanceCount) {
330 return currentInstanceCount - other.currentInstanceCount;
333 if (desiredInstanceCount != other.desiredInstanceCount) {
334 return desiredInstanceCount - other.desiredInstanceCount;
337 result = PfUtils.compareObjects(properties, other.properties);
342 return PfUtils.compareObjects(pdpInstances, other.pdpInstances);