8b6dd40d60589dfdc556208c95ecf17cdd3de665
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / ToscaEntityType.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 com.google.gson.annotations.SerializedName;
24
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.TreeMap;
29
30 import javax.persistence.CascadeType;
31 import javax.persistence.Column;
32 import javax.persistence.EmbeddedId;
33 import javax.persistence.Entity;
34 import javax.persistence.Inheritance;
35 import javax.persistence.InheritanceType;
36 import javax.persistence.OneToMany;
37 import javax.persistence.Table;
38
39 import lombok.Data;
40 import lombok.EqualsAndHashCode;
41 import lombok.NonNull;
42
43 import org.apache.commons.lang3.ObjectUtils;
44 import org.onap.policy.common.utils.validation.Assertions;
45 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
46 import org.onap.policy.models.base.PfConcept;
47 import org.onap.policy.models.base.PfConceptKey;
48 import org.onap.policy.models.base.PfKey;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.base.PfValidationMessage;
51 import org.onap.policy.models.base.PfValidationResult;
52 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
53
54 /**
55  * Class to represent the EntrySchema of list/map property in TOSCA definition.
56  */
57 @Entity
58 @Table(name = "ToscaEntityType")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @Data
61 @EqualsAndHashCode(callSuper = false)
62 public class ToscaEntityType extends PfConcept {
63     private static final long serialVersionUID = -1330661834220739393L;
64
65     @EmbeddedId
66     private PfConceptKey key;
67
68     @SerializedName("derived_from")
69     @Column(name = "derivedFrom")
70     private PfConceptKey derivedFrom;
71
72     @OneToMany(cascade = CascadeType.ALL)
73     private Map<String, String> metadata;
74
75     @Column(name = "description")
76     private String description;
77
78     /**
79      * The Default Constructor creates a {@link ToscaEntityType} object with a null key.
80      */
81     public ToscaEntityType() {
82         this(new PfConceptKey());
83     }
84
85     /**
86      * The Key Constructor creates a {@link ToscaEntityType} object with the given concept key.
87      *
88      * @param key the key
89      */
90     public ToscaEntityType(@NonNull final PfConceptKey key) {
91         this.key = key;
92     }
93
94     /**
95      * Copy constructor.
96      *
97      * @param copyConcept the concept to copy from
98      */
99     public ToscaEntityType(final ToscaEntityType copyConcept) {
100         super(copyConcept);
101     }
102
103     @Override
104     public List<PfKey> getKeys() {
105         final List<PfKey> keyList = getKey().getKeys();
106         if (derivedFrom != null) {
107             keyList.addAll(derivedFrom.getKeys());
108         }
109         return keyList;
110     }
111
112     @Override
113     public void clean() {
114         key.clean();
115
116         if (derivedFrom != null) {
117             derivedFrom.clean();
118         }
119
120         if (metadata != null) {
121             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
122                 metadataEntry.setValue(metadataEntry.getValue().trim());
123             }
124         }
125
126         description = (description != null ? description.trim() : null);
127     }
128
129     @Override
130     public PfValidationResult validate(PfValidationResult resultIn) {
131         PfValidationResult result = resultIn;
132
133         if (key.isNullKey()) {
134             result.addValidationMessage(
135                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
136         }
137
138         result = key.validate(result);
139
140         if (derivedFrom != null && derivedFrom.isNullKey()) {
141             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
142                     "derived from key is a null key"));
143         }
144
145         if (metadata != null) {
146             for (Entry<String, String> metadataEntry : metadata.entrySet()) {
147                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getKey())) {
148                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
149                             "property metadata key may not be null"));
150                 }
151                 if (!ParameterValidationUtils.validateStringParameter(metadataEntry.getValue())) {
152                     result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
153                             "property metadata value may not be null"));
154                 }
155             }
156         }
157
158         if (description != null && description.trim().length() == 0) {
159             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
160                     "property description may not be blank"));
161         }
162
163         return result;
164     }
165
166     @Override
167     public int compareTo(final PfConcept otherConcept) {
168         if (otherConcept == null) {
169             return -1;
170         }
171         if (this == otherConcept) {
172             return 0;
173         }
174         if (getClass() != otherConcept.getClass()) {
175             return this.hashCode() - otherConcept.hashCode();
176         }
177
178         final ToscaEntityType other = (ToscaEntityType) otherConcept;
179         if (!key.equals(other.key)) {
180             return key.compareTo(other.key);
181         }
182
183         int result = ObjectUtils.compare(derivedFrom, other.derivedFrom);
184         if (result != 0) {
185             return result;
186         }
187
188         result = PfUtils.compareObjects(metadata, other.metadata);
189         if (result != 0) {
190             return result;
191         }
192
193         return ObjectUtils.compare(description, other.description);
194     }
195
196     @Override
197     public PfConcept copyTo(@NonNull PfConcept target) {
198         final Object copyObject = target;
199         Assertions.instanceOf(copyObject, PfConcept.class);
200
201         final ToscaEntityType copy = ((ToscaEntityType) copyObject);
202         copy.setKey(new PfConceptKey(key));
203         copy.setDerivedFrom(derivedFrom != null ? new PfConceptKey(derivedFrom) : null);
204
205         if (metadata != null) {
206             final Map<String, String> newMatadata = new TreeMap<>();
207             for (final Entry<String, String> metadataEntry : metadata.entrySet()) {
208                 newMatadata.put(metadataEntry.getKey(), metadataEntry.getValue());
209             }
210             copy.setMetadata(newMatadata);
211         }
212
213         copy.setDescription(description);
214
215         return copy;
216     }
217 }