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