bae36299a32907c31117af3b5e34f82d81c93ffc
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaSchemaDefinition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2020 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.parameters.BeanValidationResult;
37 import org.onap.policy.common.utils.validation.Assertions;
38 import org.onap.policy.models.base.PfAuthorative;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.base.Validated;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaSchemaDefinition;
45
46 /**
47  * Class to represent the EntrySchema of list/map property in TOSCA definition.
48  *
49  * @author Chenfei Gao (cgao@research.att.com)
50  * @author Liam Fallon (liam.fallon@est.tech)
51  */
52 @Data
53 @EqualsAndHashCode(callSuper = false)
54 @NoArgsConstructor
55 public class JpaToscaSchemaDefinition extends Validated
56         implements PfAuthorative<ToscaSchemaDefinition>, Serializable, Comparable<JpaToscaSchemaDefinition> {
57
58     private static final long serialVersionUID = 3645882081163287058L;
59
60     @Column
61     private PfConceptKey type;
62
63     @Column
64     private String description;
65
66     @ElementCollection
67     private List<JpaToscaConstraint> constraints;
68
69     /**
70      * The full constructor creates a {@link JpaToscaSchemaDefinition} object with mandatory fields.
71      *
72      * @param type the type of the entry schema
73      */
74     public JpaToscaSchemaDefinition(@NonNull final PfConceptKey type) {
75         this.type = type;
76     }
77
78     /**
79      * Copy constructor.
80      *
81      * @param copyConcept the concept to copy from
82      */
83     public JpaToscaSchemaDefinition(@NonNull final JpaToscaSchemaDefinition copyConcept) {
84         copyConcept.copyTo(this);
85     }
86
87     /**
88      * Authorative constructor.
89      *
90      * @param authorativeConcept the authorative concept to copy from
91      */
92     public JpaToscaSchemaDefinition(final ToscaSchemaDefinition authorativeConcept) {
93         this.fromAuthorative(authorativeConcept);
94     }
95
96     @Override
97     public ToscaSchemaDefinition toAuthorative() {
98         ToscaSchemaDefinition toscaEntrySchema = new ToscaSchemaDefinition();
99
100         toscaEntrySchema.setType(type.getName());
101         toscaEntrySchema.setTypeVersion(type.getVersion());
102
103         toscaEntrySchema.setDescription(description);
104
105         if (constraints != null) {
106             List<ToscaConstraint> toscaConstraints = new ArrayList<>();
107
108             for (JpaToscaConstraint constraint : constraints) {
109                 toscaConstraints.add(constraint.toAuthorative());
110             }
111
112             toscaEntrySchema.setConstraints(toscaConstraints);
113         }
114
115         return toscaEntrySchema;
116     }
117
118     @Override
119     public void fromAuthorative(final ToscaSchemaDefinition toscaEntrySchema) {
120         if (toscaEntrySchema.getTypeVersion() != null) {
121             type = new PfConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion());
122         } else {
123             type = new PfConceptKey(toscaEntrySchema.getType(), PfKey.NULL_KEY_VERSION);
124         }
125
126         description = toscaEntrySchema.getDescription();
127
128         if (toscaEntrySchema.getConstraints() != null) {
129             constraints = new ArrayList<>();
130
131             for (ToscaConstraint toscaConstraint : toscaEntrySchema.getConstraints()) {
132                 constraints.add(JpaToscaConstraint.newInstance(toscaConstraint));
133             }
134         }
135     }
136
137     public List<PfKey> getKeys() {
138         return type.getKeys();
139     }
140
141     public void clean() {
142         type.clean();
143         description = (description != null ? description.trim() : null);
144     }
145
146     @Override
147     public BeanValidationResult validate(@NonNull String fieldName) {
148         BeanValidationResult result = new BeanValidationResult(fieldName, this);
149
150         result.addResult(validateKeyNotNull("type", type));
151         result.addResult(validateNotBlank("description", description, false));
152
153         validateList(result, "constraints", constraints, Validated::validateNotNull);
154
155         return result;
156     }
157
158     @Override
159     public int compareTo(final JpaToscaSchemaDefinition other) {
160         if (other == null) {
161             return -1;
162         }
163         if (this == other) {
164             return 0;
165         }
166
167         int result = ObjectUtils.compare(description, other.description);
168         if (result != 0) {
169             return result;
170         }
171
172         return PfUtils.compareCollections(constraints, other.constraints);
173     }
174
175     /**
176      * Copy this entry schema to another.
177      *
178      * @param target the other schemaa
179      * @return the copied concept
180      */
181     public JpaToscaSchemaDefinition copyTo(@NonNull final JpaToscaSchemaDefinition target) {
182         Assertions.instanceOf(target, JpaToscaSchemaDefinition.class);
183
184         final JpaToscaSchemaDefinition copy = (target);
185         copy.setType(new PfConceptKey(type));
186         copy.setDescription(description);
187
188         if (constraints != null) {
189             final List<JpaToscaConstraint> newConstraints = new ArrayList<>();
190             for (final JpaToscaConstraint constraint : constraints) {
191                 newConstraints.add(constraint); // Constraints are immutable
192             }
193             copy.setConstraints(newConstraints);
194         }
195
196         return copy;
197     }
198 }