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