Add support for legacy guard policies
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaEntityType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.TreeMap;
27
28 import javax.persistence.AttributeOverride;
29 import javax.persistence.AttributeOverrides;
30 import javax.persistence.Column;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.MappedSuperclass;
34
35 import lombok.Data;
36 import lombok.EqualsAndHashCode;
37 import lombok.NonNull;
38
39 import org.apache.commons.lang3.ObjectUtils;
40 import org.onap.policy.common.utils.validation.Assertions;
41 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfUtils;
46 import org.onap.policy.models.base.PfValidationMessage;
47 import org.onap.policy.models.base.PfValidationResult;
48 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
49
50 /**
51  * Class to represent the EntrySchema of list/map property in TOSCA definition.
52  */
53 @MappedSuperclass
54 @Data
55 @EqualsAndHashCode(callSuper = false)
56 public class JpaToscaEntityType extends PfConcept {
57     private static final long serialVersionUID = -1330661834220739393L;
58
59     @EmbeddedId
60     private PfConceptKey key;
61
62     // @formatter:off
63     @Column
64     @AttributeOverrides({
65         @AttributeOverride(name = "name",
66                            column = @Column(name = "derived_from_name")),
67         @AttributeOverride(name = "version",
68                            column = @Column(name = "derived_from_version"))
69         })
70     private PfConceptKey derivedFrom;
71
72     @ElementCollection
73     private Map<String, String> metadata;
74
75     @Column
76     private String description;
77     // @formatter:on
78
79     /**
80      * The Default Constructor creates a {@link JpaToscaEntityType} object with a null key.
81      */
82     public JpaToscaEntityType() {
83         this(new PfConceptKey());
84     }
85
86     /**
87      * The Key Constructor creates a {@link JpaToscaEntityType} object with the given concept key.
88      *
89      * @param key the key
90      */
91     public JpaToscaEntityType(@NonNull final PfConceptKey key) {
92         this.key = key;
93     }
94
95     /**
96      * Copy constructor.
97      *
98      * @param copyConcept the concept to copy from
99      */
100     public JpaToscaEntityType(final JpaToscaEntityType copyConcept) {
101         super(copyConcept);
102     }
103
104     @Override
105     public List<PfKey> getKeys() {
106         final List<PfKey> keyList = getKey().getKeys();
107         if (derivedFrom != null) {
108             keyList.addAll(derivedFrom.getKeys());
109         }
110         return keyList;
111     }
112
113     @Override
114     public void clean() {
115         key.clean();
116
117         if (derivedFrom != null) {
118             derivedFrom.clean();
119         }
120
121         if (metadata != null) {
122             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
123                 metadataEntry.setValue(metadataEntry.getValue().trim());
124             }
125         }
126
127         description = (description != null ? description.trim() : null);
128     }
129
130     @Override
131     public PfValidationResult validate(PfValidationResult resultIn) {
132         PfValidationResult result = resultIn;
133
134         if (key.isNullKey()) {
135             result.addValidationMessage(
136                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
137         }
138
139         result = key.validate(result);
140
141         if (derivedFrom != null && derivedFrom.isNullKey()) {
142             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
143                     "derived from key is a null key"));
144         }
145
146         if (metadata != null) {
147             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
148                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
149                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
150                             "property metadata key may not be null"));
151                 }
152                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
153                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
154                             "property metadata value may not be null"));
155                 }
156             }
157         }
158
159         if (description != null && description.trim().length() == 0) {
160             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
161                     "property description may not be blank"));
162         }
163
164         return result;
165     }
166
167     @Override
168     public int compareTo(final PfConcept otherConcept) {
169         if (otherConcept == null) {
170             return -1;
171         }
172         if (this == otherConcept) {
173             return 0;
174         }
175         if (getClass() != otherConcept.getClass()) {
176             return this.hashCode() - otherConcept.hashCode();
177         }
178
179         final JpaToscaEntityType other = (JpaToscaEntityType) otherConcept;
180         if (!key.equals(other.key)) {
181             return key.compareTo(other.key);
182         }
183
184         int result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
185         if (result != 0) {
186             return result;
187         }
188
189         result = PfUtils.compareObjects(metadata, other.metadata);
190         if (result != 0) {
191             return result;
192         }
193
194         return ObjectUtils.compare(description, other.description);
195     }
196
197     @Override
198     public PfConcept copyTo(@NonNull PfConcept target) {
199         final Object copyObject = target;
200         Assertions.instanceOf(copyObject, PfConcept.class);
201
202         final JpaToscaEntityType copy = ((JpaToscaEntityType) copyObject);
203         copy.setKey(new PfConceptKey(key));
204         copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null);
205
206         if (metadata != null) {
207             final Map<String, String> newMatadata = new TreeMap<>();
208             for (final Entry<String, String> metadataEntry : metadata.entrySet()) {
209                 newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue());
210             }
211             copy.setMetadata(newMatadata);
212         }
213
214         copy.setDescription(description);
215
216         return copy;
217     }
218 }