dfdf7ed9b1b5ab2eaa1249666310d91d22c030a9
[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 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 java.util.List;
28 import javax.persistence.CollectionTable;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.Lob;
35 import javax.persistence.Table;
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import lombok.NoArgsConstructor;
39 import lombok.NonNull;
40 import org.onap.policy.common.parameters.BeanValidationResult;
41 import org.onap.policy.common.parameters.annotations.NotNull;
42 import org.onap.policy.common.parameters.annotations.Valid;
43 import org.onap.policy.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
48
49 /**
50  * Class to represent the policy type in TOSCA definition.
51  *
52  * @author Chenfei Gao (cgao@research.att.com)
53  * @author Liam Fallon (liam.fallon@est.tech)
54  */
55
56 @Entity
57 @Table(name = "ToscaPolicyType")
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @Data
60 @EqualsAndHashCode(callSuper = true)
61 @NoArgsConstructor
62 public class JpaToscaPolicyType extends JpaToscaWithToscaProperties<ToscaPolicyType> {
63     private static final long serialVersionUID = -563659852901842616L;
64
65     @ElementCollection
66     @CollectionTable(joinColumns = {
67         @JoinColumn(name = "toscaPolicyTypeName",    referencedColumnName = "name"),
68         @JoinColumn(name = "toscaPolicyTypeVersion",    referencedColumnName = "version")
69     })
70     private List<@NotNull @Valid PfConceptKey> targets;
71
72     @ElementCollection
73     @Lob
74     private List<@NotNull @Valid JpaToscaTrigger> triggers;
75
76     /**
77      * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
78      *
79      * @param key the key
80      */
81     public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
82         super(key);
83     }
84
85     /**
86      * Copy constructor.
87      *
88      * @param copyConcept the concept to copy from
89      */
90     public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
91         super(copyConcept);
92         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
93         this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
94     }
95
96     /**
97      * Authorative constructor.
98      *
99      * @param authorativeConcept the authorative concept to copy from
100      */
101     public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
102         super(authorativeConcept);
103     }
104
105     @Override
106     public ToscaPolicyType toAuthorative() {
107         var toscaPolicyType = new ToscaPolicyType();
108         super.setToscaEntity(toscaPolicyType);
109         super.toAuthorative();
110
111         // TODO need to copy targets & triggers?
112
113         return toscaPolicyType;
114     }
115
116     @Override
117     public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
118         super.fromAuthorative(toscaPolicyType);
119
120         // TODO need to copy targets & triggers?
121     }
122
123     @Override
124     public List<PfKey> getKeys() {
125         final List<PfKey> keyList = super.getKeys();
126
127         if (targets != null) {
128             keyList.addAll(targets);
129         }
130
131         if (triggers != null) {
132             for (JpaToscaTrigger trigger : triggers) {
133                 keyList.addAll(trigger.getKeys());
134             }
135         }
136
137         return keyList;
138     }
139
140     @Override
141     public void clean() {
142         super.clean();
143
144         if (targets != null) {
145             for (PfConceptKey target : targets) {
146                 target.clean();
147             }
148         }
149
150         if (triggers != null) {
151             for (JpaToscaTrigger trigger : triggers) {
152                 trigger.clean();
153             }
154         }
155     }
156
157     @Override
158     public BeanValidationResult validate(@NonNull String fieldName) {
159         return validateWithKey(fieldName);
160     }
161
162     @Override
163     public int compareTo(final PfConcept otherConcept) {
164         if (this == otherConcept) {
165             return 0;
166         }
167
168         int result = super.compareTo(otherConcept);
169         if (result != 0) {
170             return result;
171         }
172
173         final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
174
175         result = PfUtils.compareCollections(targets, other.targets);
176         if (result != 0) {
177             return result;
178         }
179
180         return PfUtils.compareCollections(triggers, other.triggers);
181     }
182 }