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