2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.Entries;
48 import org.onap.policy.common.parameters.annotations.Items;
49 import org.onap.policy.common.parameters.annotations.Min;
50 import org.onap.policy.common.parameters.annotations.NotBlank;
51 import org.onap.policy.common.parameters.annotations.NotNull;
52 import org.onap.policy.common.parameters.annotations.Valid;
53 import org.onap.policy.models.base.PfAuthorative;
54 import org.onap.policy.models.base.PfConcept;
55 import org.onap.policy.models.base.PfConceptKey;
56 import org.onap.policy.models.base.PfKey;
57 import org.onap.policy.models.base.PfKeyUse;
58 import org.onap.policy.models.base.PfReferenceKey;
59 import org.onap.policy.models.base.PfSearchableKey;
60 import org.onap.policy.models.base.PfUtils;
61 import org.onap.policy.models.base.validation.annotations.VerifyKey;
62 import org.onap.policy.models.pdp.concepts.Pdp;
63 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
64 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
67 * Class to represent a PDP subgroup in the database.
69 * @author Liam Fallon (liam.fallon@est.tech)
72 @Table(name = "PdpSubGroup")
73 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
75 @EqualsAndHashCode(callSuper = false)
76 public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGroup> {
77 private static final long serialVersionUID = -357224425637789775L;
82 private PfReferenceKey key;
86 @Items(notNull = {@NotNull}, valid = {@Valid})
87 private List<PfSearchableKey> supportedPolicyTypes;
91 @Items(notNull = {@NotNull}, valid = {@Valid})
92 private List<PfConceptKey> policies;
96 private int currentInstanceCount;
100 private int desiredInstanceCount;
103 @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
104 value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
105 private Map<String, String> properties;
108 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
111 @JoinColumn(name = "pdpParentKeyName", referencedColumnName = "parentKeyName"),
112 @JoinColumn(name = "pdpParentKeyVersion", referencedColumnName = "parentKeyVersion"),
113 @JoinColumn(name = "pdpParentLocalName", referencedColumnName = "parentLocalName"),
114 @JoinColumn(name = "pdpLocalName", referencedColumnName = "localName")
119 @Items(notNull = {@NotNull}, valid = {@Valid})
120 private List<JpaPdp> pdpInstances;
123 * The Default Constructor creates a {@link JpaPdpSubGroup} object with a null key.
125 public JpaPdpSubGroup() {
126 this(new PfReferenceKey());
130 * The Key Constructor creates a {@link JpaPdpSubGroup} object with the given concept key.
134 public JpaPdpSubGroup(@NonNull final PfReferenceKey key) {
135 this(key, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
139 * The Key Constructor creates a {@link JpaPdpSubGroup} object with all mandatory fields.
142 * @param supportedPolicyTypes Supported policy types
143 * @param policies policies deployed on this PDP subgroups
144 * @param pdpInstances the PDP instances on this PDP subgroups
146 public JpaPdpSubGroup(@NonNull final PfReferenceKey key, @NonNull final List<PfSearchableKey> supportedPolicyTypes,
147 @NonNull List<PfConceptKey> policies, @NonNull final List<JpaPdp> pdpInstances) {
149 this.supportedPolicyTypes = supportedPolicyTypes;
150 this.policies = policies;
151 this.pdpInstances = pdpInstances;
157 * @param copyConcept the concept to copy from
159 public JpaPdpSubGroup(@NonNull final JpaPdpSubGroup copyConcept) {
161 this.key = new PfReferenceKey(copyConcept.key);
162 this.supportedPolicyTypes = PfUtils.mapList(copyConcept.supportedPolicyTypes,
163 PfSearchableKey::new, new ArrayList<>(0));
164 this.policies = PfUtils.mapList(copyConcept.policies, PfConceptKey::new, new ArrayList<>(0));
165 this.currentInstanceCount = copyConcept.currentInstanceCount;
166 this.desiredInstanceCount = copyConcept.desiredInstanceCount;
167 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
168 this.pdpInstances = PfUtils.mapList(copyConcept.pdpInstances, JpaPdp::new, new ArrayList<>(0));
172 * Authorative constructor.
174 * @param authorativeConcept the authorative concept to copy from
176 public JpaPdpSubGroup(@NonNull final PdpSubGroup authorativeConcept) {
177 this.fromAuthorative(authorativeConcept);
181 public PdpSubGroup toAuthorative() {
182 PdpSubGroup pdpSubgroup = new PdpSubGroup();
184 pdpSubgroup.setPdpType(getKey().getLocalName());
186 pdpSubgroup.setSupportedPolicyTypes(new ArrayList<>());
187 for (PfSearchableKey supportedPolicyTypeKey : supportedPolicyTypes) {
188 ToscaConceptIdentifier supportedPolicyTypeIdent = new ToscaConceptIdentifier(
189 supportedPolicyTypeKey.getName(), supportedPolicyTypeKey.getVersion());
190 pdpSubgroup.getSupportedPolicyTypes().add(supportedPolicyTypeIdent);
193 pdpSubgroup.setPolicies(new ArrayList<>());
194 for (PfConceptKey policyKey : policies) {
195 ToscaConceptIdentifier toscaPolicyIdentifier = new ToscaConceptIdentifier();
196 toscaPolicyIdentifier.setName(policyKey.getName());
197 toscaPolicyIdentifier.setVersion(policyKey.getVersion());
198 pdpSubgroup.getPolicies().add(toscaPolicyIdentifier);
201 pdpSubgroup.setCurrentInstanceCount(currentInstanceCount);
202 pdpSubgroup.setDesiredInstanceCount(desiredInstanceCount);
203 pdpSubgroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
205 pdpSubgroup.setPdpInstances(new ArrayList<>());
206 for (JpaPdp jpaPdp : pdpInstances) {
207 pdpSubgroup.getPdpInstances().add(jpaPdp.toAuthorative());
214 public void fromAuthorative(@NonNull final PdpSubGroup pdpSubgroup) {
215 if (this.key == null || this.getKey().isNullKey()) {
216 this.setKey(new PfReferenceKey());
217 getKey().setLocalName(pdpSubgroup.getPdpType());
220 this.supportedPolicyTypes = new ArrayList<>();
221 if (pdpSubgroup.getSupportedPolicyTypes() != null) {
222 for (ToscaConceptIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) {
223 this.supportedPolicyTypes
224 .add(new PfSearchableKey(supportedPolicyType.getName(), supportedPolicyType.getVersion()));
228 this.policies = new ArrayList<>();
229 if (pdpSubgroup.getPolicies() != null) {
230 for (ToscaConceptIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) {
232 .add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion()));
235 this.currentInstanceCount = pdpSubgroup.getCurrentInstanceCount();
236 this.desiredInstanceCount = pdpSubgroup.getDesiredInstanceCount();
238 (pdpSubgroup.getProperties() == null ? null : new LinkedHashMap<>(pdpSubgroup.getProperties()));
240 this.pdpInstances = new ArrayList<>();
241 if (pdpSubgroup.getPdpInstances() != null) {
242 for (Pdp pdp : pdpSubgroup.getPdpInstances()) {
243 JpaPdp jpaPdp = new JpaPdp();
244 jpaPdp.setKey(new PfReferenceKey(getKey(), pdp.getInstanceId()));
245 jpaPdp.fromAuthorative(pdp);
246 this.pdpInstances.add(jpaPdp);
252 public List<PfKey> getKeys() {
253 List<PfKey> keyList = getKey().getKeys();
255 for (PfSearchableKey ptkey : supportedPolicyTypes) {
256 keyList.add(new PfKeyUse(ptkey));
259 for (PfConceptKey pkey : policies) {
260 keyList.add(new PfKeyUse(pkey));
263 for (JpaPdp jpaPdp : pdpInstances) {
264 keyList.addAll(jpaPdp.getKeys());
272 public void clean() {
275 for (PfSearchableKey ptkey : supportedPolicyTypes) {
279 for (PfConceptKey pkey : policies) {
283 if (properties != null) {
284 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
285 for (Entry<String, String> propertyEntry : properties.entrySet()) {
286 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
288 properties = cleanedPropertyMap;
291 for (JpaPdp jpaPdp : pdpInstances) {
297 public BeanValidationResult validate(@NonNull String fieldName) {
298 BeanValidationResult result = super.validate(fieldName);
300 result.addResult(validateKeyNotNull("parent of key", key.getParentConceptKey()));
302 if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
303 addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty");
310 public int compareTo(final PfConcept otherConcept) {
311 if (otherConcept == null) {
314 if (this == otherConcept) {
317 if (getClass() != otherConcept.getClass()) {
318 return getClass().getName().compareTo(otherConcept.getClass().getName());
321 final JpaPdpSubGroup other = (JpaPdpSubGroup) otherConcept;
322 if (!key.equals(other.key)) {
323 return key.compareTo(other.key);
326 int result = PfUtils.compareObjects(supportedPolicyTypes, other.supportedPolicyTypes);
331 result = PfUtils.compareObjects(policies, other.policies);
336 if (currentInstanceCount != other.currentInstanceCount) {
337 return currentInstanceCount - other.currentInstanceCount;
340 if (desiredInstanceCount != other.desiredInstanceCount) {
341 return desiredInstanceCount - other.desiredInstanceCount;
344 result = PfUtils.compareObjects(properties, other.properties);
349 return PfUtils.compareObjects(pdpInstances, other.pdpInstances);