Java 17 Upgrade
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / persistence / concepts / JpaPdpGroup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019, 2021, 2023 Nordix Foundation.
7  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * SPDX-License-Identifier: Apache-2.0
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.policy.models.pdp.persistence.concepts;
26
27 import jakarta.persistence.CascadeType;
28 import jakarta.persistence.CollectionTable;
29 import jakarta.persistence.Column;
30 import jakarta.persistence.ElementCollection;
31 import jakarta.persistence.EmbeddedId;
32 import jakarta.persistence.Entity;
33 import jakarta.persistence.FetchType;
34 import jakarta.persistence.Inheritance;
35 import jakarta.persistence.InheritanceType;
36 import jakarta.persistence.JoinColumn;
37 import jakarta.persistence.OneToMany;
38 import jakarta.persistence.Table;
39 import java.io.Serial;
40 import java.util.ArrayList;
41 import java.util.LinkedHashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Map.Entry;
45 import lombok.Data;
46 import lombok.EqualsAndHashCode;
47 import lombok.NonNull;
48 import org.apache.commons.lang3.ObjectUtils;
49 import org.onap.policy.common.parameters.annotations.NotBlank;
50 import org.onap.policy.common.parameters.annotations.NotNull;
51 import org.onap.policy.common.parameters.annotations.Valid;
52 import org.onap.policy.models.base.PfAuthorative;
53 import org.onap.policy.models.base.PfConcept;
54 import org.onap.policy.models.base.PfConceptKey;
55 import org.onap.policy.models.base.PfKey;
56 import org.onap.policy.models.base.PfReferenceKey;
57 import org.onap.policy.models.base.PfUtils;
58 import org.onap.policy.models.base.validation.annotations.VerifyKey;
59 import org.onap.policy.models.pdp.concepts.PdpGroup;
60 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
61 import org.onap.policy.models.pdp.enums.PdpState;
62
63 /**
64  * Class to represent a PDP group in the database.
65  *
66  * @author Liam Fallon (liam.fallon@est.tech)
67  */
68 @Entity
69 @Table(name = "PdpGroup")
70 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
71 @Data
72 @EqualsAndHashCode(callSuper = false)
73 public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
74     @Serial
75     private static final long serialVersionUID = -357224425637789775L;
76
77     @EmbeddedId
78     @VerifyKey
79     @NotNull
80     private PfConceptKey key;
81
82     @Column
83     @NotBlank
84     private String description;
85
86     @Column
87     @NotNull
88     private PdpState pdpGroupState;
89
90     @ElementCollection
91     private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> properties;
92
93     // @formatter:off
94     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
95     @CollectionTable(joinColumns = {
96         @JoinColumn(name = "name",    referencedColumnName = "name"),
97         @JoinColumn(name = "version", referencedColumnName = "version")
98     })
99     // @formatter:on
100     @NotNull
101     private List<@NotNull @Valid JpaPdpSubGroup> pdpSubGroups;
102
103     /**
104      * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
105      */
106     public JpaPdpGroup() {
107         this(new PfConceptKey());
108     }
109
110     /**
111      * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
112      *
113      * @param key the key
114      */
115     public JpaPdpGroup(@NonNull final PfConceptKey key) {
116         this(key, PdpState.PASSIVE, new ArrayList<>());
117     }
118
119     /**
120      * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
121      *
122      * @param key the key
123      * @param pdpGroupState State of the PDP group
124      */
125     public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
126             @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
127         this.key = key;
128         this.pdpGroupState = pdpGroupState;
129         this.pdpSubGroups = pdpSubGroups;
130     }
131
132     /**
133      * Copy constructor.
134      *
135      * @param copyConcept the concept to copy from
136      */
137     public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
138         super(copyConcept);
139         this.key = new PfConceptKey(copyConcept.key);
140         this.description = copyConcept.description;
141         this.pdpGroupState = copyConcept.pdpGroupState;
142         this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
143         this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
144     }
145
146     /**
147      * Authorative constructor.
148      *
149      * @param authorativeConcept the authorative concept to copy from
150      */
151     public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
152         this.fromAuthorative(authorativeConcept);
153     }
154
155     @Override
156     public PdpGroup toAuthorative() {
157         var pdpGroup = new PdpGroup();
158
159         pdpGroup.setName(getKey().getName());
160         pdpGroup.setVersion(getKey().getVersion());
161         pdpGroup.setDescription(description);
162         pdpGroup.setPdpGroupState(pdpGroupState);
163
164         pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
165
166         pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
167         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
168             pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
169         }
170
171         return pdpGroup;
172     }
173
174     @Override
175     public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
176         if (this.key == null || this.getKey().isNullKey()) {
177             this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
178         }
179
180         this.description = pdpGroup.getDescription();
181         this.pdpGroupState = pdpGroup.getPdpGroupState();
182
183         this.properties =
184                 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
185
186         this.pdpSubGroups = new ArrayList<>();
187         for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
188             var jpaPdpSubGroup = new JpaPdpSubGroup();
189             jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
190             jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
191             this.pdpSubGroups.add(jpaPdpSubGroup);
192         }
193     }
194
195     @Override
196     public List<PfKey> getKeys() {
197         List<PfKey> keyList = getKey().getKeys();
198
199         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
200             keyList.addAll(jpaPdpSubgroup.getKeys());
201         }
202
203         return keyList;
204     }
205
206     @Override
207     public void clean() {
208         key.clean();
209
210         description = (description == null ? null : description.trim());
211
212         if (properties != null) {
213             Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
214             for (Entry<String, String> propertyEntry : properties.entrySet()) {
215                 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
216             }
217             properties = cleanedPropertyMap;
218         }
219
220         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
221             jpaPdpSubgroup.clean();
222         }
223     }
224
225     @Override
226     public int compareTo(final PfConcept otherConcept) {
227         if (otherConcept == null) {
228             return -1;
229         }
230         if (this == otherConcept) {
231             return 0;
232         }
233         if (getClass() != otherConcept.getClass()) {
234             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
235         }
236
237         final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
238         if (!key.equals(other.key)) {
239             return key.compareTo(other.key);
240         }
241
242         int result = ObjectUtils.compare(description, other.description);
243         if (result != 0) {
244             return result;
245         }
246
247         result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
248         if (result != 0) {
249             return result;
250         }
251
252         result = PfUtils.compareObjects(properties, other.properties);
253         if (result != 0) {
254             return result;
255         }
256
257         return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);
258     }
259 }