Address sonar issues in models
[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.apache.commons.lang3.StringUtils;
48 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
49 import org.onap.policy.models.base.PfAuthorative;
50 import org.onap.policy.models.base.PfConcept;
51 import org.onap.policy.models.base.PfConceptKey;
52 import org.onap.policy.models.base.PfKey;
53 import org.onap.policy.models.base.PfReferenceKey;
54 import org.onap.policy.models.base.PfUtils;
55 import org.onap.policy.models.base.PfValidationMessage;
56 import org.onap.policy.models.base.PfValidationResult;
57 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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     private PfConceptKey key;
77
78     @Column
79     private String description;
80
81     @Column
82     private PdpState pdpGroupState;
83
84     @ElementCollection
85     private Map<String, String> properties;
86
87     // @formatter:off
88     @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
89     @CollectionTable(joinColumns = {
90             @JoinColumn(name = "pdpGroupParentKeyName",    referencedColumnName = "parentKeyName"),
91             @JoinColumn(name = "pdpGroupParentKeyVersion", referencedColumnName = "parentKeyVersion"),
92             @JoinColumn(name = "pdpGroupParentLocalName",  referencedColumnName = "parentLocalName"),
93             @JoinColumn(name = "pdpGroupLocalName",        referencedColumnName = "localName")
94         })
95     // @formatter:on
96     private List<JpaPdpSubGroup> pdpSubGroups;
97
98     /**
99      * The Default Constructor creates a {@link JpaPdpGroup} object with a null key.
100      */
101     public JpaPdpGroup() {
102         this(new PfConceptKey());
103     }
104
105     /**
106      * The Key Constructor creates a {@link JpaPdpGroup} object with the given concept key.
107      *
108      * @param key the key
109      */
110     public JpaPdpGroup(@NonNull final PfConceptKey key) {
111         this(key, PdpState.PASSIVE, new ArrayList<>());
112     }
113
114     /**
115      * The Key Constructor creates a {@link JpaPdpGroup} object with all mandatory fields.
116      *
117      * @param key the key
118      * @param pdpGroupState State of the PDP group
119      */
120     public JpaPdpGroup(@NonNull final PfConceptKey key, @NonNull final PdpState pdpGroupState,
121             @NonNull final List<JpaPdpSubGroup> pdpSubGroups) {
122         this.key = key;
123         this.pdpGroupState = pdpGroupState;
124         this.pdpSubGroups = pdpSubGroups;
125     }
126
127     /**
128      * Copy constructor.
129      *
130      * @param copyConcept the concept to copy from
131      */
132     public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) {
133         super(copyConcept);
134         this.key = new PfConceptKey(copyConcept.key);
135         this.description = copyConcept.description;
136         this.pdpGroupState = copyConcept.pdpGroupState;
137         this.properties = (copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties));
138         this.pdpSubGroups = PfUtils.mapList(copyConcept.pdpSubGroups, JpaPdpSubGroup::new, new ArrayList<>(0));
139     }
140
141     /**
142      * Authorative constructor.
143      *
144      * @param authorativeConcept the authorative concept to copy from
145      */
146     public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) {
147         this.fromAuthorative(authorativeConcept);
148     }
149
150     @Override
151     public PdpGroup toAuthorative() {
152         PdpGroup pdpGroup = new PdpGroup();
153
154         pdpGroup.setName(getKey().getName());
155         pdpGroup.setVersion(getKey().getVersion());
156         pdpGroup.setDescription(description);
157         pdpGroup.setPdpGroupState(pdpGroupState);
158
159         pdpGroup.setProperties(properties == null ? null : new LinkedHashMap<>(properties));
160
161         pdpGroup.setPdpSubgroups(new ArrayList<>(pdpSubGroups.size()));
162         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
163             pdpGroup.getPdpSubgroups().add(jpaPdpSubgroup.toAuthorative());
164         }
165
166         return pdpGroup;
167     }
168
169     @Override
170     public void fromAuthorative(@NonNull final PdpGroup pdpGroup) {
171         if (this.key == null || this.getKey().isNullKey()) {
172             this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion()));
173         }
174
175         this.description = pdpGroup.getDescription();
176         this.pdpGroupState = pdpGroup.getPdpGroupState();
177
178         this.properties =
179                 (pdpGroup.getProperties() == null ? null : new LinkedHashMap<>(pdpGroup.getProperties()));
180
181         this.pdpSubGroups = new ArrayList<>();
182         for (PdpSubGroup pdpSubgroup : pdpGroup.getPdpSubgroups()) {
183             JpaPdpSubGroup jpaPdpSubGroup = new JpaPdpSubGroup();
184             jpaPdpSubGroup.setKey(new PfReferenceKey(getKey(), pdpSubgroup.getPdpType()));
185             jpaPdpSubGroup.fromAuthorative(pdpSubgroup);
186             this.pdpSubGroups.add(jpaPdpSubGroup);
187         }
188     }
189
190     @Override
191     public List<PfKey> getKeys() {
192         List<PfKey> keyList = getKey().getKeys();
193
194         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
195             keyList.addAll(jpaPdpSubgroup.getKeys());
196         }
197
198         return keyList;
199     }
200
201     @Override
202     public void clean() {
203         key.clean();
204
205         description = (description == null ? null : description.trim());
206
207         if (properties != null) {
208             Map<String, String> cleanedPropertyMap = new LinkedHashMap<>();
209             for (Entry<String, String> propertyEntry : properties.entrySet()) {
210                 cleanedPropertyMap.put(propertyEntry.getKey().trim(), propertyEntry.getValue().trim());
211             }
212             properties = cleanedPropertyMap;
213         }
214
215         for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
216             jpaPdpSubgroup.clean();
217         }
218     }
219
220     @Override
221     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
222         PfValidationResult result = resultIn;
223
224         if (key.isNullKey()) {
225             result.addValidationMessage(
226                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
227         }
228
229         result = key.validate(result);
230
231         if (description != null && StringUtils.isBlank(description)) {
232             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
233                     "description may not be blank"));
234         }
235
236         if (pdpGroupState == null) {
237             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
238                     "pdpGroupState may not be null"));
239         }
240
241         if (properties != null) {
242             validateProperties(result);
243         }
244
245         if (pdpSubGroups == null) {
246             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
247                     "a PDP group must have a list of PDP subgroups"));
248         } else {
249             for (JpaPdpSubGroup jpaPdpSubgroup : pdpSubGroups) {
250                 result = jpaPdpSubgroup.validate(result);
251             }
252         }
253
254         return result;
255     }
256
257     /**
258      * Validate the properties.
259      *
260      * @param result where to place any new validation results
261      */
262     private void validateProperties(PfValidationResult result) {
263
264         for (Entry<String, String> propertyEntry : properties.entrySet()) {
265             if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) {
266                 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
267                         "a property key may not be null or blank"));
268             }
269             if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getValue())) {
270                 result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
271                         "a property value may not be null or blank"));
272             }
273         }
274     }
275
276     @Override
277     public int compareTo(final PfConcept otherConcept) {
278         if (otherConcept == null) {
279             return -1;
280         }
281         if (this == otherConcept) {
282             return 0;
283         }
284         if (getClass() != otherConcept.getClass()) {
285             return this.getClass().getName().compareTo(otherConcept.getClass().getName());
286         }
287
288         final JpaPdpGroup other = (JpaPdpGroup) otherConcept;
289         if (!key.equals(other.key)) {
290             return key.compareTo(other.key);
291         }
292
293         int result = ObjectUtils.compare(description, other.description);
294         if (result != 0) {
295             return result;
296         }
297
298         result = ObjectUtils.compare(pdpGroupState, other.pdpGroupState);
299         if (result != 0) {
300             return result;
301         }
302
303         result = PfUtils.compareObjects(properties, other.properties);
304         if (result != 0) {
305             return result;
306         }
307
308         return PfUtils.compareObjects(pdpSubGroups, other.pdpSubGroups);
309     }
310 }