Use annotations to do validation
[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-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
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.CollectionTable;
33 import javax.persistence.Column;
34 import javax.persistence.ElementCollection;
35 import javax.persistence.EmbeddedId;
36 import javax.persistence.Entity;
37 import javax.persistence.FetchType;
38 import javax.persistence.Inheritance;
39 import javax.persistence.InheritanceType;
40 import javax.persistence.JoinColumn;
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.apache.commons.lang3.ObjectUtils;
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.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     private static final long serialVersionUID = -357224425637789775L;
75
76     @EmbeddedId
77     @VerifyKey
78     @NotNull
79     private PfConceptKey key;
80
81     @Column
82     @NotBlank
83     private String description;
84
85     @Column
86     @NotNull
87     private PdpState pdpGroupState;
88
89     @ElementCollection
90     @Entries(key = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}),
91                 value = @Items(notNull = {@NotNull}, notBlank = {@NotBlank}))
92     private Map<String, String> properties;
93
94     // @formatter:off
95     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
96     @CollectionTable(joinColumns = {
97             @JoinColumn(name = "pdpGroupParentKeyName",    referencedColumnName = "parentKeyName"),
98             @JoinColumn(name = "pdpGroupParentKeyVersion", referencedColumnName = "parentKeyVersion"),
99             @JoinColumn(name = "pdpGroupParentLocalName",  referencedColumnName = "parentLocalName"),
100             @JoinColumn(name = "pdpGroupLocalName",        referencedColumnName = "localName")
101         })
102     // @formatter:on
103     @NotNull
104     @Items(notNull = {@NotNull}, valid = {@Valid})
105     private List<JpaPdpSubGroup> pdpSubGroups;
106
107     /**
108      * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
109      */
110     public JpaPdpGroup() {
111         this(new PfConceptKey());
112     }
113
114     /**
115      * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
116      *
117      * @param key the key
118      */
119     public JpaPdpGroup(@NonNull final PfConceptKey key) {
120         this(key, PdpState.PASSIVE, new ArrayList<>());
121     }
122
123     /**
124      * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
125      *
126      * @param key the key
127      * @param pdpGroupState State of the PDP group
128      */
129     public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
130             @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
131         this.key = key;
132         this.pdpGroupState = pdpGroupState;
133         this.pdpSubGroups = pdpSubGroups;
134     }
135
136     /**
137      * Copy constructor.
138      *
139      * @param copyConcept the concept to copy from
140      */
141     public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
142         super(copyConcept);
143         this.key = new PfConceptKey(copyConcept.key);
144         this.description = copyConcept.description;
145         this.pdpGroupState = copyConcept.pdpGroupState;
146         this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
147         this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
148     }
149
150     /**
151      * Authorative constructor.
152      *
153      * @param authorativeConcept the authorative concept to copy from
154      */
155     public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
156         this.fromAuthorative(authorativeConcept);
157     }
158
159     @Override
160     public PdpGroup toAuthorative() {
161         PdpGroup pdpGroup = new PdpGroup();
162
163         pdpGroup.setName(getKey().getName());
164         pdpGroup.setVersion(getKey().getVersion());
165         pdpGroup.setDescription(description);
166         pdpGroup.setPdpGroupState(pdpGroupState);
167
168         pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
169
170         pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
171         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
172             pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
173         }
174
175         return pdpGroup;
176     }
177
178     @Override
179     public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
180         if (this.key == null || this.getKey().isNullKey()) {
181             this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
182         }
183
184         this.description = pdpGroup.getDescription();
185         this.pdpGroupState = pdpGroup.getPdpGroupState();
186
187         this.properties =
188                 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
189
190         this.pdpSubGroups = new ArrayList<>();
191         for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
192             JpaPdpSubGroup jpaPdpSubGroup = new JpaPdpSubGroup();
193             jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
194             jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
195             this.pdpSubGroups.add(jpaPdpSubGroup);
196         }
197     }
198
199     @Override
200     public List<PfKey> getKeys() {
201         List<PfKey> keyList = getKey().getKeys();
202
203         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
204             keyList.addAll(jpaPdpSubgroup.getKeys());
205         }
206
207         return keyList;
208     }
209
210     @Override
211     public void clean() {
212         key.clean();
213
214         description = (description == null ? null : description.trim());
215
216         if (properties != null) {
217             Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
218             for (Entry<String, String> propertyEntry : properties.entrySet()) {
219                 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
220             }
221             properties = cleanedPropertyMap;
222         }
223
224         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
225             jpaPdpSubgroup.clean();
226         }
227     }
228
229     @Override
230     public int compareTo(final PfConcept otherConcept) {
231         if (otherConcept == null) {
232             return -1;
233         }
234         if (this == otherConcept) {
235             return 0;
236         }
237         if (getClass() != otherConcept.getClass()) {
238             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
239         }
240
241         final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
242         if (!key.equals(other.key)) {
243             return key.compareTo(other.key);
244         }
245
246         int result = ObjectUtils.compare(description, other.description);
247         if (result != 0) {
248             return result;
249         }
250
251         result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
252         if (result != 0) {
253             return result;
254         }
255
256         result = PfUtils.compareObjects(properties, other.properties);
257         if (result != 0) {
258             return result;
259         }
260
261         return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);
262     }
263 }