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.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.ToscaPolicyIdentifier;
65 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
68 * Class to represent a PDP subgroup in the database.
70 * @author Liam Fallon (liam.fallon@est.tech)
73 @Table(name = "PdpSubGroup")
74 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
76 @EqualsAndHashCode(callSuper = false)
77 public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGroup> {
78 private static final long serialVersionUID = -357224425637789775L;
83 private PfReferenceKey key;
87 @Items(notNull = {@NotNull}, valid = {@Valid})
88 private List<PfSearchableKey> supportedPolicyTypes;
92 @Items(notNull = {@NotNull}, valid = {@Valid})
93 private List<PfConceptKey> policies;
97 private int currentInstanceCount;
101 private int desiredInstanceCount;
104 @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
105 value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
106 private Map<String, String> properties;
109 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
112 @JoinColumn(name = "pdpParentKeyName", referencedColumnName = "parentKeyName"),
113 @JoinColumn(name = "pdpParentKeyVersion", referencedColumnName = "parentKeyVersion"),
114 @JoinColumn(name = "pdpParentLocalName", referencedColumnName = "parentLocalName"),
115 @JoinColumn(name = "pdpLocalName", referencedColumnName = "localName")
120 @Items(notNull = {@NotNull}, valid = {@Valid})
121 private List<JpaPdp> pdpInstances;
124 * The Default Constructor creates a {@link JpaPdpSubGroup} object with a null key.
126 public JpaPdpSubGroup() {
127 this(new PfReferenceKey());
131 * The Key Constructor creates a {@link JpaPdpSubGroup} object with the given concept key.
135 public JpaPdpSubGroup(@NonNull final PfReferenceKey key) {
136 this(key, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
140 * The Key Constructor creates a {@link JpaPdpSubGroup} object with all mandatory fields.
143 * @param supportedPolicyTypes Supported policy types
144 * @param policies policies deployed on this PDP subgroups
145 * @param pdpInstances the PDP instances on this PDP subgroups
147 public JpaPdpSubGroup(@NonNull final PfReferenceKey key, @NonNull final List<PfSearchableKey> supportedPolicyTypes,
148 @NonNull List<PfConceptKey> policies, @NonNull final List<JpaPdp> pdpInstances) {
150 this.supportedPolicyTypes = supportedPolicyTypes;
151 this.policies = policies;
152 this.pdpInstances = pdpInstances;
158 * @param copyConcept the concept to copy from
160 public JpaPdpSubGroup(@NonNull final JpaPdpSubGroup copyConcept) {
162 this.key = new PfReferenceKey(copyConcept.key);
163 this.supportedPolicyTypes = PfUtils.mapList(copyConcept.supportedPolicyTypes,
164 PfSearchableKey::new, new ArrayList<>(0));
165 this.policies = PfUtils.mapList(copyConcept.policies, PfConceptKey::new, new ArrayList<>(0));
166 this.currentInstanceCount = copyConcept.currentInstanceCount;
167 this.desiredInstanceCount = copyConcept.desiredInstanceCount;
168 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
169 this.pdpInstances = PfUtils.mapList(copyConcept.pdpInstances, JpaPdp::new, new ArrayList<>(0));
173 * Authorative constructor.
175 * @param authorativeConcept the authorative concept to copy from
177 public JpaPdpSubGroup(@NonNull final PdpSubGroup authorativeConcept) {
178 this.fromAuthorative(authorativeConcept);
182 public PdpSubGroup toAuthorative() {
183 PdpSubGroup pdpSubgroup = new PdpSubGroup();
185 pdpSubgroup.setPdpType(getKey().getLocalName());
187 pdpSubgroup.setSupportedPolicyTypes(new ArrayList<>());
188 for (PfSearchableKey supportedPolicyTypeKey : supportedPolicyTypes) {
189 ToscaPolicyTypeIdentifier supportedPolicyTypeIdent = new ToscaPolicyTypeIdentifier(
190 supportedPolicyTypeKey.getName(), supportedPolicyTypeKey.getVersion());
191 pdpSubgroup.getSupportedPolicyTypes().add(supportedPolicyTypeIdent);
194 pdpSubgroup.setPolicies(new ArrayList<>());
195 for (PfConceptKey policyKey : policies) {
196 ToscaPolicyIdentifier toscaPolicyIdentifier = new ToscaPolicyIdentifier();
197 toscaPolicyIdentifier.setName(policyKey.getName());
198 toscaPolicyIdentifier.setVersion(policyKey.getVersion());
199 pdpSubgroup.getPolicies().add(toscaPolicyIdentifier);
202 pdpSubgroup.setCurrentInstanceCount(currentInstanceCount);
203 pdpSubgroup.setDesiredInstanceCount(desiredInstanceCount);
204 pdpSubgroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
206 pdpSubgroup.setPdpInstances(new ArrayList<>());
207 for (JpaPdp jpaPdp : pdpInstances) {
208 pdpSubgroup.getPdpInstances().add(jpaPdp.toAuthorative());
215 public void fromAuthorative(@NonNull final PdpSubGroup pdpSubgroup) {
216 if (this.key == null || this.getKey().isNullKey()) {
217 this.setKey(new PfReferenceKey());
218 getKey().setLocalName(pdpSubgroup.getPdpType());
221 this.supportedPolicyTypes = new ArrayList<>();
222 if (pdpSubgroup.getSupportedPolicyTypes() != null) {
223 for (ToscaPolicyTypeIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) {
224 this.supportedPolicyTypes
225 .add(new PfSearchableKey(supportedPolicyType.getName(), supportedPolicyType.getVersion()));
229 this.policies = new ArrayList<>();
230 if (pdpSubgroup.getPolicies() != null) {
231 for (ToscaPolicyIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) {
233 .add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion()));
236 this.currentInstanceCount = pdpSubgroup.getCurrentInstanceCount();
237 this.desiredInstanceCount = pdpSubgroup.getDesiredInstanceCount();
239 (pdpSubgroup.getProperties() == null ? null : new LinkedHashMap<>(pdpSubgroup.getProperties()));
241 this.pdpInstances = new ArrayList<>();
242 if (pdpSubgroup.getPdpInstances() != null) {
243 for (Pdp pdp : pdpSubgroup.getPdpInstances()) {
244 JpaPdp jpaPdp = new JpaPdp();
245 jpaPdp.setKey(new PfReferenceKey(getKey(), pdp.getInstanceId()));
246 jpaPdp.fromAuthorative(pdp);
247 this.pdpInstances.add(jpaPdp);
253 public List<PfKey> getKeys() {
254 List<PfKey> keyList = getKey().getKeys();
256 for (PfSearchableKey ptkey : supportedPolicyTypes) {
257 keyList.add(new PfKeyUse(ptkey));
260 for (PfConceptKey pkey : policies) {
261 keyList.add(new PfKeyUse(pkey));
264 for (JpaPdp jpaPdp : pdpInstances) {
265 keyList.addAll(jpaPdp.getKeys());
273 public void clean() {
276 for (PfSearchableKey ptkey : supportedPolicyTypes) {
280 for (PfConceptKey pkey : policies) {
284 if (properties != null) {
285 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
286 for (Entry<String, String> propertyEntry : properties.entrySet()) {
287 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
289 properties = cleanedPropertyMap;
292 for (JpaPdp jpaPdp : pdpInstances) {
298 public BeanValidationResult validate(@NonNull String fieldName) {
299 BeanValidationResult result = super.validate(fieldName);
301 result.addResult(validateKeyNotNull("parent of key", key.getParentConceptKey()));
303 if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
304 addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty");
311 public int compareTo(final PfConcept otherConcept) {
312 if (otherConcept == null) {
315 if (this == otherConcept) {
318 if (getClass() != otherConcept.getClass()) {
319 return getClass().getName().compareTo(otherConcept.getClass().getName());
322 final JpaPdpSubGroup other = (JpaPdpSubGroup) otherConcept;
323 if (!key.equals(other.key)) {
324 return key.compareTo(other.key);
327 int result = PfUtils.compareObjects(supportedPolicyTypes, other.supportedPolicyTypes);
332 result = PfUtils.compareObjects(policies, other.policies);
337 if (currentInstanceCount != other.currentInstanceCount) {
338 return currentInstanceCount - other.currentInstanceCount;
341 if (desiredInstanceCount != other.desiredInstanceCount) {
342 return desiredInstanceCount - other.desiredInstanceCount;
345 result = PfUtils.compareObjects(properties, other.properties);
350 return PfUtils.compareObjects(pdpInstances, other.pdpInstances);