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