c086c535a1811fb836308e77660590c35b75e434
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaDataType.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.util.ArrayList;
27 import java.util.List;
28 import javax.persistence.ElementCollection;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Table;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NoArgsConstructor;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.parameters.annotations.Valid;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
43
44 /**
45  * Class to represent custom data type in TOSCA definition.
46  *
47  * @author Chenfei Gao (cgao@research.att.com)
48  * @author Liam Fallon (liam.fallon@est.tech)
49  */
50 @Entity
51 @Table(name = "ToscaDataType")
52 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
53 @Data
54 @EqualsAndHashCode(callSuper = true)
55 @NoArgsConstructor
56 public class JpaToscaDataType extends JpaToscaWithToscaProperties<ToscaDataType> {
57     private static final long serialVersionUID = -3922690413436539164L;
58
59     @ElementCollection
60     private List<@NotNull @Valid JpaToscaConstraint> constraints;
61
62     /**
63      * The Key Constructor creates a {@link JpaToscaDataType} object with the given concept key.
64      *
65      * @param key the key
66      */
67     public JpaToscaDataType(@NonNull final PfConceptKey key) {
68         super(key);
69     }
70
71     /**
72      * Copy constructor.
73      *
74      * @param copyConcept the concept to copy from
75      */
76     public JpaToscaDataType(final JpaToscaDataType copyConcept) {
77         super(copyConcept);
78         // Constraints are immutable
79         this.constraints = (copyConcept.constraints != null ? new ArrayList<>(copyConcept.constraints) : null);
80     }
81
82     /**
83      * Authorative constructor.
84      *
85      * @param authorativeConcept the authorative concept to copy from
86      */
87     public JpaToscaDataType(final ToscaDataType authorativeConcept) {
88         super(authorativeConcept);
89     }
90
91     @Override
92     public ToscaDataType toAuthorative() {
93         var toscaDataType = new ToscaDataType();
94         super.setToscaEntity(toscaDataType);
95         super.toAuthorative();
96
97         toscaDataType.setConstraints(PfUtils.mapList(constraints, JpaToscaConstraint::toAuthorative));
98
99         return toscaDataType;
100     }
101
102     @Override
103     public void fromAuthorative(final ToscaDataType toscaDataType) {
104         super.fromAuthorative(toscaDataType);
105
106         constraints = PfUtils.mapList(toscaDataType.getConstraints(), JpaToscaConstraint::newInstance);
107     }
108
109     @Override
110     public int compareTo(final PfConcept otherConcept) {
111         if (this == otherConcept) {
112             return 0;
113         }
114
115         int result = super.compareTo(otherConcept);
116         if (result != 0) {
117             return result;
118         }
119
120         final JpaToscaDataType other = (JpaToscaDataType) otherConcept;
121
122         return PfUtils.compareCollections(constraints, other.constraints);
123     }
124 }