Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicyType.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, 2023 Nordix Foundation.
7  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * SPDX-License-Identifier: Apache-2.0
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.policy.models.tosca.simple.concepts;
26
27 import jakarta.persistence.CollectionTable;
28 import jakarta.persistence.ElementCollection;
29 import jakarta.persistence.Entity;
30 import jakarta.persistence.Inheritance;
31 import jakarta.persistence.InheritanceType;
32 import jakarta.persistence.JoinColumn;
33 import jakarta.persistence.Lob;
34 import jakarta.persistence.Table;
35 import java.io.Serial;
36 import java.util.List;
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import lombok.NoArgsConstructor;
40 import lombok.NonNull;
41 import org.onap.policy.common.parameters.BeanValidationResult;
42 import org.onap.policy.common.parameters.annotations.NotNull;
43 import org.onap.policy.common.parameters.annotations.Valid;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfUtils;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49
50 /**
51  * Class to represent the policy type in TOSCA definition.
52  *
53  * @author Chenfei Gao (cgao@research.att.com)
54  * @author Liam Fallon (liam.fallon@est.tech)
55  */
56
57 @Entity
58 @Table(name = "ToscaPolicyType")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @Data
61 @EqualsAndHashCode(callSuper = true)
62 @NoArgsConstructor
63 public class JpaToscaPolicyType extends JpaToscaWithToscaProperties<ToscaPolicyType> {
64     @Serial
65     private static final long serialVersionUID = -563659852901842616L;
66
67     @ElementCollection
68     @CollectionTable(joinColumns = {
69         @JoinColumn(name = "toscaPolicyTypeName",    referencedColumnName = "name"),
70         @JoinColumn(name = "toscaPolicyTypeVersion",    referencedColumnName = "version")
71     })
72     private List<@NotNull @Valid PfConceptKey> targets;
73
74     @ElementCollection
75     @Lob
76     private List<@NotNull @Valid JpaToscaTrigger> triggers;
77
78     /**
79      * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
80      *
81      * @param key the key
82      */
83     public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
84         super(key);
85     }
86
87     /**
88      * Copy constructor.
89      *
90      * @param copyConcept the concept to copy from
91      */
92     public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
93         super(copyConcept);
94         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
95         this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
96     }
97
98     /**
99      * Authorative constructor.
100      *
101      * @param authorativeConcept the authorative concept to copy from
102      */
103     public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
104         super(authorativeConcept);
105     }
106
107     @Override
108     public ToscaPolicyType toAuthorative() {
109         var toscaPolicyType = new ToscaPolicyType();
110         super.setToscaEntity(toscaPolicyType);
111         super.toAuthorative();
112
113         // TODO need to copy targets & triggers?
114
115         return toscaPolicyType;
116     }
117
118     @Override
119     public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
120         super.fromAuthorative(toscaPolicyType);
121
122         // TODO need to copy targets & triggers?
123     }
124
125     @Override
126     public List<PfKey> getKeys() {
127         final List<PfKey> keyList = super.getKeys();
128
129         if (targets != null) {
130             keyList.addAll(targets);
131         }
132
133         if (triggers != null) {
134             for (JpaToscaTrigger trigger : triggers) {
135                 keyList.addAll(trigger.getKeys());
136             }
137         }
138
139         return keyList;
140     }
141
142     @Override
143     public void clean() {
144         super.clean();
145
146         if (targets != null) {
147             for (PfConceptKey target : targets) {
148                 target.clean();
149             }
150         }
151
152         if (triggers != null) {
153             for (JpaToscaTrigger trigger : triggers) {
154                 trigger.clean();
155             }
156         }
157     }
158
159     @Override
160     public BeanValidationResult validate(@NonNull String fieldName) {
161         return validateWithKey(fieldName);
162     }
163
164     @Override
165     public int compareTo(final PfConcept otherConcept) {
166         if (this == otherConcept) {
167             return 0;
168         }
169
170         int result = super.compareTo(otherConcept);
171         if (result != 0) {
172             return result;
173         }
174
175         final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
176
177         result = PfUtils.compareCollections(targets, other.targets);
178         if (result != 0) {
179             return result;
180         }
181
182         return PfUtils.compareCollections(triggers, other.triggers);
183     }
184 }