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