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.models.base.PfAuthorative;
48 import org.onap.policy.models.base.PfConcept;
49 import org.onap.policy.models.base.PfConceptKey;
50 import org.onap.policy.models.base.PfKey;
51 import org.onap.policy.models.base.PfKeyUse;
52 import org.onap.policy.models.base.PfReferenceKey;
53 import org.onap.policy.models.base.PfSearchableKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.Validated;
56 import org.onap.policy.models.pdp.concepts.Pdp;
57 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
59 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
62 * Class to represent a PDP subgroup in the database.
64 * @author Liam Fallon (liam.fallon@est.tech)
67 @Table(name = "PdpSubGroup")
68 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @EqualsAndHashCode(callSuper = false)
71 public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGroup> {
72 private static final long serialVersionUID = -357224425637789775L;
75 private PfReferenceKey key;
78 private List<PfSearchableKey> supportedPolicyTypes;
81 private List<PfConceptKey> policies;
84 private int currentInstanceCount;
87 private int desiredInstanceCount;
90 private Map<String, String> properties;
93 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
96 @JoinColumn(name = "pdpParentKeyName", referencedColumnName = "parentKeyName"),
97 @JoinColumn(name = "pdpParentKeyVersion", referencedColumnName = "parentKeyVersion"),
98 @JoinColumn(name = "pdpParentLocalName", referencedColumnName = "parentLocalName"),
99 @JoinColumn(name = "pdpLocalName", referencedColumnName = "localName")
103 private List<JpaPdp> pdpInstances;
106 * The Default Constructor creates a {@link JpaPdpSubGroup} object with a null key.
108 public JpaPdpSubGroup() {
109 this(new PfReferenceKey());
113 * The Key Constructor creates a {@link JpaPdpSubGroup} object with the given concept key.
117 public JpaPdpSubGroup(@NonNull final PfReferenceKey key) {
118 this(key, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
122 * The Key Constructor creates a {@link JpaPdpSubGroup} object with all mandatory fields.
125 * @param supportedPolicyTypes Supported policy types
126 * @param policies policies deployed on this PDP subgroups
127 * @param pdpInstances the PDP instances on this PDP subgroups
129 public JpaPdpSubGroup(@NonNull final PfReferenceKey key, @NonNull final List<PfSearchableKey> supportedPolicyTypes,
130 @NonNull List<PfConceptKey> policies, @NonNull final List<JpaPdp> pdpInstances) {
132 this.supportedPolicyTypes = supportedPolicyTypes;
133 this.policies = policies;
134 this.pdpInstances = pdpInstances;
140 * @param copyConcept the concept to copy from
142 public JpaPdpSubGroup(@NonNull final JpaPdpSubGroup copyConcept) {
144 this.key = new PfReferenceKey(copyConcept.key);
145 this.supportedPolicyTypes = PfUtils.mapList(copyConcept.supportedPolicyTypes,
146 PfSearchableKey::new, new ArrayList<>(0));
147 this.policies = PfUtils.mapList(copyConcept.policies, PfConceptKey::new, new ArrayList<>(0));
148 this.currentInstanceCount = copyConcept.currentInstanceCount;
149 this.desiredInstanceCount = copyConcept.desiredInstanceCount;
150 this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null);
151 this.pdpInstances = PfUtils.mapList(copyConcept.pdpInstances, JpaPdp::new, new ArrayList<>(0));
155 * Authorative constructor.
157 * @param authorativeConcept the authorative concept to copy from
159 public JpaPdpSubGroup(@NonNull final PdpSubGroup authorativeConcept) {
160 this.fromAuthorative(authorativeConcept);
164 public PdpSubGroup toAuthorative() {
165 PdpSubGroup pdpSubgroup = new PdpSubGroup();
167 pdpSubgroup.setPdpType(getKey().getLocalName());
169 pdpSubgroup.setSupportedPolicyTypes(new ArrayList<>());
170 for (PfSearchableKey supportedPolicyTypeKey : supportedPolicyTypes) {
171 ToscaPolicyTypeIdentifier supportedPolicyTypeIdent = new ToscaPolicyTypeIdentifier(
172 supportedPolicyTypeKey.getName(), supportedPolicyTypeKey.getVersion());
173 pdpSubgroup.getSupportedPolicyTypes().add(supportedPolicyTypeIdent);
176 pdpSubgroup.setPolicies(new ArrayList<>());
177 for (PfConceptKey policyKey : policies) {
178 ToscaPolicyIdentifier toscaPolicyIdentifier = new ToscaPolicyIdentifier();
179 toscaPolicyIdentifier.setName(policyKey.getName());
180 toscaPolicyIdentifier.setVersion(policyKey.getVersion());
181 pdpSubgroup.getPolicies().add(toscaPolicyIdentifier);
184 pdpSubgroup.setCurrentInstanceCount(currentInstanceCount);
185 pdpSubgroup.setDesiredInstanceCount(desiredInstanceCount);
186 pdpSubgroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
188 pdpSubgroup.setPdpInstances(new ArrayList<>());
189 for (JpaPdp jpaPdp : pdpInstances) {
190 pdpSubgroup.getPdpInstances().add(jpaPdp.toAuthorative());
197 public void fromAuthorative(@NonNull final PdpSubGroup pdpSubgroup) {
198 if (this.key == null || this.getKey().isNullKey()) {
199 this.setKey(new PfReferenceKey());
200 getKey().setLocalName(pdpSubgroup.getPdpType());
203 this.supportedPolicyTypes = new ArrayList<>();
204 if (pdpSubgroup.getSupportedPolicyTypes() != null) {
205 for (ToscaPolicyTypeIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) {
206 this.supportedPolicyTypes
207 .add(new PfSearchableKey(supportedPolicyType.getName(), supportedPolicyType.getVersion()));
211 this.policies = new ArrayList<>();
212 if (pdpSubgroup.getPolicies() != null) {
213 for (ToscaPolicyIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) {
215 .add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion()));
218 this.currentInstanceCount = pdpSubgroup.getCurrentInstanceCount();
219 this.desiredInstanceCount = pdpSubgroup.getDesiredInstanceCount();
221 (pdpSubgroup.getProperties() == null ? null : new LinkedHashMap<>(pdpSubgroup.getProperties()));
223 this.pdpInstances = new ArrayList<>();
224 if (pdpSubgroup.getPdpInstances() != null) {
225 for (Pdp pdp : pdpSubgroup.getPdpInstances()) {
226 JpaPdp jpaPdp = new JpaPdp();
227 jpaPdp.setKey(new PfReferenceKey(getKey(), pdp.getInstanceId()));
228 jpaPdp.fromAuthorative(pdp);
229 this.pdpInstances.add(jpaPdp);
235 public List<PfKey> getKeys() {
236 List<PfKey> keyList = getKey().getKeys();
238 for (PfSearchableKey ptkey : supportedPolicyTypes) {
239 keyList.add(new PfKeyUse(ptkey));
242 for (PfConceptKey pkey : policies) {
243 keyList.add(new PfKeyUse(pkey));
246 for (JpaPdp jpaPdp : pdpInstances) {
247 keyList.addAll(jpaPdp.getKeys());
255 public void clean() {
258 for (PfSearchableKey ptkey : supportedPolicyTypes) {
262 for (PfConceptKey pkey : policies) {
266 if (properties != null) {
267 Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
268 for (Entry<String, String> propertyEntry : properties.entrySet()) {
269 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
271 properties = cleanedPropertyMap;
274 for (JpaPdp jpaPdp : pdpInstances) {
280 public BeanValidationResult validate(@NonNull String fieldName) {
281 BeanValidationResult result = new BeanValidationResult(fieldName, this);
283 result.addResult(validateKeyNotNull("key", key));
284 result.addResult(validateKeyNotNull("parent of key", key.getParentConceptKey()));
286 if (currentInstanceCount < 0) {
287 addResult(result, "currentInstanceCount", currentInstanceCount, "is negative");
290 if (desiredInstanceCount < 0) {
291 addResult(result, "desiredInstanceCount", desiredInstanceCount, "is negative");
294 validateMap(result, "properties", properties, Validated::validateEntryNotBlankNotBlank);
296 if (supportedPolicyTypes == null || supportedPolicyTypes.isEmpty()) {
297 addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty");
299 validateList(result, "supportedPolicyTypes", supportedPolicyTypes, Validated::validateNotNull);
302 result.validateNotNull("policies", policies);
303 validateList(result, "policies", policies, Validated::validateNotNull);
305 result.validateNotNull("pdpInstances", pdpInstances);
306 validateList(result, "pdpInstances", pdpInstances, Validated::validateNotNull);
312 public int compareTo(final PfConcept otherConcept) {
313 if (otherConcept == null) {
316 if (this == otherConcept) {
319 if (getClass() != otherConcept.getClass()) {
320 return getClass().getName().compareTo(otherConcept.getClass().getName());
323 final JpaPdpSubGroup other = (JpaPdpSubGroup) otherConcept;
324 if (!key.equals(other.key)) {
325 return key.compareTo(other.key);
328 int result = PfUtils.compareObjects(supportedPolicyTypes, other.supportedPolicyTypes);
333 result = PfUtils.compareObjects(policies, other.policies);
338 if (currentInstanceCount != other.currentInstanceCount) {
339 return currentInstanceCount - other.currentInstanceCount;
342 if (desiredInstanceCount != other.desiredInstanceCount) {
343 return desiredInstanceCount - other.desiredInstanceCount;
346 result = PfUtils.compareObjects(properties, other.properties);
351 return PfUtils.compareObjects(pdpInstances, other.pdpInstances);