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