Changed identifiers to concept identifiers
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpSubGroup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.pdp.persistence.concepts;
25
26 import java.util.ArrayList;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
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;
43 import lombok.Data;
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;
65
66 /**
67  * Class to represent a PDP subgroup in the database.
68  *
69  * @author Liam Fallon (liam.fallon@est.tech)
70  */
71 @Entity
72 @Table(name = "PdpSubGroup")
73 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
74 @Data
75 @EqualsAndHashCode(callSuper = false)
76 public class JpaPdpSubGroup extends PfConcept implements PfAuthorative<PdpSubGroup> {
77     private static final long serialVersionUID = -357224425637789775L;
78
79     @EmbeddedId
80     @VerifyKey
81     @NotNull
82     private PfReferenceKey key;
83
84     @ElementCollection
85     @NotNull
86     @Items(notNull = {@NotNull}, valid = {@Valid})
87     private List<PfSearchableKey> supportedPolicyTypes;
88
89     @ElementCollection
90     @NotNull
91     @Items(notNull = {@NotNull}, valid = {@Valid})
92     private List<PfConceptKey> policies;
93
94     @Column
95     @Min(0)
96     private int currentInstanceCount;
97
98     @Column
99     @Min(0)
100     private int desiredInstanceCount;
101
102     @ElementCollection
103     @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
104                 value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
105     private Map<String, String> properties;
106
107     // @formatter:off
108     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
109     @JoinTable (
110             joinColumns = {
111                 @JoinColumn(name = "pdpParentKeyName",    referencedColumnName = "parentKeyName"),
112                 @JoinColumn(name = "pdpParentKeyVersion", referencedColumnName = "parentKeyVersion"),
113                 @JoinColumn(name = "pdpParentLocalName",  referencedColumnName = "parentLocalName"),
114                 @JoinColumn(name = "pdpLocalName",        referencedColumnName = "localName")
115             }
116         )
117     // formatter:on
118     @NotNull
119     @Items(notNull = {@NotNull}, valid = {@Valid})
120     private List<JpaPdp> pdpInstances;
121
122     /**
123      * The Default Constructor creates a {@link JpaPdpSubGroup} object with a null key.
124      */
125     public JpaPdpSubGroup() {
126         this(new PfReferenceKey());
127     }
128
129     /**
130      * The Key Constructor creates a {@link JpaPdpSubGroup} object with the given concept key.
131      *
132      * @param key the key
133      */
134     public JpaPdpSubGroup(@NonNull final PfReferenceKey key) {
135         this(key, new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
136     }
137
138     /**
139      * The Key Constructor creates a {@link JpaPdpSubGroup} object with all mandatory fields.
140      *
141      * @param key the key
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
145      */
146     public JpaPdpSubGroup(@NonNull final PfReferenceKey key, @NonNull final List<PfSearchableKey> supportedPolicyTypes,
147             @NonNull List<PfConceptKey> policies, @NonNull final List<JpaPdp> pdpInstances) {
148         this.key = key;
149         this.supportedPolicyTypes = supportedPolicyTypes;
150         this.policies = policies;
151         this.pdpInstances = pdpInstances;
152     }
153
154     /**
155      * Copy constructor.
156      *
157      * @param copyConcept the concept to copy from
158      */
159     public JpaPdpSubGroup(@NonNull final JpaPdpSubGroup copyConcept) {
160         super(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));
169     }
170
171     /**
172      * Authorative constructor.
173      *
174      * @param authorativeConcept the authorative concept to copy from
175      */
176     public JpaPdpSubGroup(@NonNull final PdpSubGroup authorativeConcept) {
177         this.fromAuthorative(authorativeConcept);
178     }
179
180     @Override
181     public PdpSubGroup toAuthorative() {
182         PdpSubGroup pdpSubgroup = new PdpSubGroup();
183
184         pdpSubgroup.setPdpType(getKey().getLocalName());
185
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);
191         }
192
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);
199         }
200
201         pdpSubgroup.setCurrentInstanceCount(currentInstanceCount);
202         pdpSubgroup.setDesiredInstanceCount(desiredInstanceCount);
203         pdpSubgroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
204
205         pdpSubgroup.setPdpInstances(new ArrayList<>());
206         for (JpaPdp jpaPdp : pdpInstances) {
207             pdpSubgroup.getPdpInstances().add(jpaPdp.toAuthorative());
208         }
209
210         return pdpSubgroup;
211     }
212
213     @Override
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());
218         }
219
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()));
225             }
226         }
227
228         this.policies = new ArrayList<>();
229         if (pdpSubgroup.getPolicies() != null) {
230             for (ToscaConceptIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) {
231                 this.policies
232                         .add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion()));
233             }
234         }
235         this.currentInstanceCount = pdpSubgroup.getCurrentInstanceCount();
236         this.desiredInstanceCount = pdpSubgroup.getDesiredInstanceCount();
237         this.properties =
238                 (pdpSubgroup.getProperties() == null ? null : new LinkedHashMap<>(pdpSubgroup.getProperties()));
239
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);
247             }
248         }
249     }
250
251     @Override
252     public List<PfKey> getKeys() {
253         List<PfKey> keyList = getKey().getKeys();
254
255         for (PfSearchableKey ptkey : supportedPolicyTypes) {
256             keyList.add(new PfKeyUse(ptkey));
257         }
258
259         for (PfConceptKey pkey : policies) {
260             keyList.add(new PfKeyUse(pkey));
261         }
262
263         for (JpaPdp jpaPdp : pdpInstances) {
264             keyList.addAll(jpaPdp.getKeys());
265         }
266
267
268         return keyList;
269     }
270
271     @Override
272     public void clean() {
273         key.clean();
274
275         for (PfSearchableKey ptkey : supportedPolicyTypes) {
276             ptkey.clean();
277         }
278
279         for (PfConceptKey pkey : policies) {
280             pkey.clean();
281         }
282
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());
287             }
288             properties = cleanedPropertyMap;
289         }
290
291         for (JpaPdp jpaPdp : pdpInstances) {
292             jpaPdp.clean();
293         }
294     }
295
296     @Override
297     public BeanValidationResult validate(@NonNull String fieldName) {
298         BeanValidationResult result = super.validate(fieldName);
299
300         result.addResult(validateKeyNotNull("parent of key", key.getParentConceptKey()));
301
302         if (supportedPolicyTypes != null && supportedPolicyTypes.isEmpty()) {
303             addResult(result, "supportedPolicyTypes", supportedPolicyTypes, "is empty");
304         }
305
306         return result;
307     }
308
309     @Override
310     public int compareTo(final PfConcept otherConcept) {
311         if (otherConcept == null) {
312             return -1;
313         }
314         if (this == otherConcept) {
315             return 0;
316         }
317         if (getClass() != otherConcept.getClass()) {
318             return getClass().getName().compareTo(otherConcept.getClass().getName());
319         }
320
321         final JpaPdpSubGroup other = (JpaPdpSubGroup) otherConcept;
322         if (!key.equals(other.key)) {
323             return key.compareTo(other.key);
324         }
325
326         int result = PfUtils.compareObjects(supportedPolicyTypes, other.supportedPolicyTypes);
327         if (result != 0) {
328             return result;
329         }
330
331         result = PfUtils.compareObjects(policies, other.policies);
332         if (result != 0) {
333             return result;
334         }
335
336         if (currentInstanceCount != other.currentInstanceCount) {
337             return currentInstanceCount - other.currentInstanceCount;
338         }
339
340         if (desiredInstanceCount != other.desiredInstanceCount) {
341             return desiredInstanceCount - other.desiredInstanceCount;
342         }
343
344         result = PfUtils.compareObjects(properties, other.properties);
345         if (result != 0) {
346             return result;
347         }
348
349         return PfUtils.compareObjects(pdpInstances, other.pdpInstances);
350     }
351 }