07f9261bb07612b78eda81cab6f1afd888e2cdb2
[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 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 java.util.ArrayList;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import javax.persistence.CascadeType;
33 import javax.persistence.CollectionTable;
34 import javax.persistence.Column;
35 import javax.persistence.ElementCollection;
36 import javax.persistence.EmbeddedId;
37 import javax.persistence.Entity;
38 import javax.persistence.FetchType;
39 import javax.persistence.Inheritance;
40 import javax.persistence.InheritanceType;
41 import javax.persistence.JoinColumn;
42 import javax.persistence.OneToMany;
43 import javax.persistence.Table;
44 import lombok.Data;
45 import lombok.EqualsAndHashCode;
46 import lombok.NonNull;
47 import org.apache.commons.lang3.ObjectUtils;
48 import org.onap.policy.common.parameters.annotations.NotBlank;
49 import org.onap.policy.common.parameters.annotations.NotNull;
50 import org.onap.policy.common.parameters.annotations.Valid;
51 import org.onap.policy.models.base.PfAuthorative;
52 import org.onap.policy.models.base.PfConcept;
53 import org.onap.policy.models.base.PfConceptKey;
54 import org.onap.policy.models.base.PfKey;
55 import org.onap.policy.models.base.PfReferenceKey;
56 import org.onap.policy.models.base.PfUtils;
57 import org.onap.policy.models.base.validation.annotations.VerifyKey;
58 import org.onap.policy.models.pdp.concepts.PdpGroup;
59 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
60 import org.onap.policy.models.pdp.enums.PdpState;
61
62 /**
63  * Class to represent a PDP group in the database.
64  *
65  * @author Liam Fallon (liam.fallon@est.tech)
66  */
67 @Entity
68 @Table(name = "PdpGroup")
69 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 @Data
71 @EqualsAndHashCode(callSuper = false)
72 public class JpaPdpGroup extends PfConcept implements PfAuthorative<PdpGroup> {
73     private static final long serialVersionUID = -357224425637789775L;
74
75     @EmbeddedId
76     @VerifyKey
77     @NotNull
78     private PfConceptKey key;
79
80     @Column
81     @NotBlank
82     private String description;
83
84     @Column
85     @NotNull
86     private PdpState pdpGroupState;
87
88     @ElementCollection
89     private Map<@NotNull @NotBlank String, @NotNull @NotBlank String> properties;
90
91     // @formatter:off
92     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
93     @CollectionTable(joinColumns = {
94         @JoinColumn(name = "name",    referencedColumnName = "name"),
95         @JoinColumn(name = "version", referencedColumnName = "version")
96     })
97     // @formatter:on
98     @NotNull
99     private List<@NotNull @Valid JpaPdpSubGroup> pdpSubGroups;
100
101     /**
102      * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
103      */
104     public JpaPdpGroup() {
105         this(new PfConceptKey());
106     }
107
108     /**
109      * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
110      *
111      * @param key the key
112      */
113     public JpaPdpGroup(@NonNull final PfConceptKey key) {
114         this(key, PdpState.PASSIVE, new ArrayList<>());
115     }
116
117     /**
118      * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
119      *
120      * @param key the key
121      * @param pdpGroupState State of the PDP group
122      */
123     public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
124             @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
125         this.key = key;
126         this.pdpGroupState = pdpGroupState;
127         this.pdpSubGroups = pdpSubGroups;
128     }
129
130     /**
131      * Copy constructor.
132      *
133      * @param copyConcept the concept to copy from
134      */
135     public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
136         super(copyConcept);
137         this.key = new PfConceptKey(copyConcept.key);
138         this.description = copyConcept.description;
139         this.pdpGroupState = copyConcept.pdpGroupState;
140         this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
141         this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
142     }
143
144     /**
145      * Authorative constructor.
146      *
147      * @param authorativeConcept the authorative concept to copy from
148      */
149     public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
150         this.fromAuthorative(authorativeConcept);
151     }
152
153     @Override
154     public PdpGroup toAuthorative() {
155         var pdpGroup = new PdpGroup();
156
157         pdpGroup.setName(getKey().getName());
158         pdpGroup.setVersion(getKey().getVersion());
159         pdpGroup.setDescription(description);
160         pdpGroup.setPdpGroupState(pdpGroupState);
161
162         pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
163
164         pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
165         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
166             pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
167         }
168
169         return pdpGroup;
170     }
171
172     @Override
173     public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
174         if (this.key == null || this.getKey().isNullKey()) {
175             this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
176         }
177
178         this.description = pdpGroup.getDescription();
179         this.pdpGroupState = pdpGroup.getPdpGroupState();
180
181         this.properties =
182                 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
183
184         this.pdpSubGroups = new ArrayList<>();
185         for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
186             var jpaPdpSubGroup = new JpaPdpSubGroup();
187             jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
188             jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
189             this.pdpSubGroups.add(jpaPdpSubGroup);
190         }
191     }
192
193     @Override
194     public List<PfKey> getKeys() {
195         List<PfKey> keyList = getKey().getKeys();
196
197         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
198             keyList.addAll(jpaPdpSubgroup.getKeys());
199         }
200
201         return keyList;
202     }
203
204     @Override
205     public void clean() {
206         key.clean();
207
208         description = (description == null ? null : description.trim());
209
210         if (properties != null) {
211             Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
212             for (Entry<String, String> propertyEntry : properties.entrySet()) {
213                 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
214             }
215             properties = cleanedPropertyMap;
216         }
217
218         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
219             jpaPdpSubgroup.clean();
220         }
221     }
222
223     @Override
224     public int compareTo(final PfConcept otherConcept) {
225         if (otherConcept == null) {
226             return -1;
227         }
228         if (this == otherConcept) {
229             return 0;
230         }
231         if (getClass() != otherConcept.getClass()) {
232             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
233         }
234
235         final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
236         if (!key.equals(other.key)) {
237             return key.compareTo(other.key);
238         }
239
240         int result = ObjectUtils.compare(description, other.description);
241         if (result != 0) {
242             return result;
243         }
244
245         result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
246         if (result != 0) {
247             return result;
248         }
249
250         result = PfUtils.compareObjects(properties, other.properties);
251         if (result != 0) {
252             return result;
253         }
254
255         return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);
256     }
257 }