Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPolicy.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-2021, 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.Table;
34 import jakarta.ws.rs.core.Response;
35 import java.io.Serial;
36 import java.util.LinkedHashMap;
37 import java.util.List;
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
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.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.base.PfConcept;
47 import org.onap.policy.models.base.PfConceptKey;
48 import org.onap.policy.models.base.PfKey;
49 import org.onap.policy.models.base.PfModelRuntimeException;
50 import org.onap.policy.models.base.PfUtils;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52
53 /**
54  * Class to represent the policy in TOSCA definition.
55  *
56  * @author Chenfei Gao (cgao@research.att.com)
57  * @author Liam Fallon (liam.fallon@est.tech)
58  */
59 @Entity
60 @Table(name = "ToscaPolicy")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @Data
63 @EqualsAndHashCode(callSuper = true)
64 public class JpaToscaPolicy extends JpaToscaWithTypeAndStringProperties<ToscaPolicy> {
65     @Serial
66     private static final long serialVersionUID = 3265174757061982805L;
67     private static final String METADATA_METADATA_SET_NAME_TAG = "metadataSetName";
68     private static final String METADATA_METADATA_SET_VERSION_TAG = "metadataSetVersion";
69
70     // Tags for metadata
71     private static final String METADATA_POLICY_ID_TAG = "policy-id";
72     private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
73
74     private static final StandardCoder STANDARD_CODER = new StandardCoder();
75
76     @ElementCollection
77     @CollectionTable(joinColumns = {
78         @JoinColumn(name = "toscaPolicyName",    referencedColumnName = "name"),
79         @JoinColumn(name = "toscaPolicyVersion",    referencedColumnName = "version")
80     })
81     private List<@NotNull @Valid PfConceptKey> targets;
82
83     /**
84      * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
85      */
86     public JpaToscaPolicy() {
87         super();
88     }
89
90     /**
91      * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
92      *
93      * @param key the key
94      */
95     public JpaToscaPolicy(@NonNull final PfConceptKey key) {
96         super(key, new PfConceptKey());
97     }
98
99     /**
100      * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
101      *
102      * @param key the key
103      * @param type the type of the policy
104      */
105     public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
106         super(key, type);
107     }
108
109     /**
110      * Copy constructor.
111      *
112      * @param copyConcept the concept to copy from
113      */
114     public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
115         super(copyConcept);
116         this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
117     }
118
119     /**
120      * Authorative constructor.
121      *
122      * @param authorativeConcept the authorative concept to copy from
123      */
124     public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
125         super(new PfConceptKey());
126         this.fromAuthorative(authorativeConcept);
127     }
128
129     @Override
130     public ToscaPolicy toAuthorative() {
131         var toscaPolicy = new ToscaPolicy();
132         super.setToscaEntity(toscaPolicy);
133         super.toAuthorative();
134
135         return toscaPolicy;
136     }
137
138     @Override
139     public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
140         super.fromAuthorative(toscaPolicy);
141
142         // Add the property metadata if it doesn't exist already
143         if (toscaPolicy.getMetadata() == null) {
144             setMetadata(new LinkedHashMap<>());
145         }
146
147         // Add the policy name and version fields to the metadata
148         getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
149         getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion());
150
151         // Add metadataSet name and version to the metadata
152         if (getMetadata().containsKey(METADATA_METADATA_SET_NAME_TAG)
153                 && getMetadata().containsKey(METADATA_METADATA_SET_VERSION_TAG)) {
154             getMetadata().put(METADATA_METADATA_SET_NAME_TAG, getMetadata().get(METADATA_METADATA_SET_NAME_TAG)
155                     .replaceAll("^\"|\"$", ""));
156
157             getMetadata().put(METADATA_METADATA_SET_VERSION_TAG, getMetadata().get(METADATA_METADATA_SET_VERSION_TAG)
158                     .replaceAll("^\"|\"$", ""));
159         }
160     }
161
162     @Override
163     protected Object deserializePropertyValue(String propValue) {
164         try {
165             return STANDARD_CODER.decode(propValue, Object.class);
166         } catch (CoderException ce) {
167             String errorMessage = "error decoding property JSON value read from database: " + propValue;
168             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
169         }
170     }
171
172     @Override
173     protected String serializePropertyValue(Object propValue) {
174         try {
175             return STANDARD_CODER.encode(propValue);
176         } catch (CoderException ce) {
177             String errorMessage = "error encoding property JSON value for database: " + propValue;
178             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
179         }
180     }
181
182     @Override
183     public List<PfKey> getKeys() {
184         final List<PfKey> keyList = super.getKeys();
185
186         if (targets != null) {
187             keyList.addAll(targets);
188         }
189
190         return keyList;
191     }
192
193     @Override
194     public void clean() {
195         super.clean();
196
197         if (targets != null) {
198             for (PfConceptKey target : targets) {
199                 target.clean();
200             }
201         }
202     }
203
204     @Override
205     public BeanValidationResult validate(@NonNull String fieldName) {
206         BeanValidationResult result = super.validate(fieldName);
207
208         validateKeyVersionNotNull(result, "key", getKey());
209
210         return result;
211     }
212
213     @Override
214     public int compareTo(final PfConcept otherConcept) {
215         if (this == otherConcept) {
216             return 0;
217         }
218
219         int result = super.compareTo(otherConcept);
220         if (result != 0) {
221             return result;
222         }
223
224         final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
225
226         return PfUtils.compareCollections(targets, other.targets);
227     }
228 }