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