2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * SPDX-License-Identifier: Apache-2.0
22 * ============LICENSE_END=========================================================
25 package org.onap.policy.models.tosca.simple.concepts;
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;
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;
54 * Class to represent the policy in TOSCA definition.
56 * @author Chenfei Gao (cgao@research.att.com)
57 * @author Liam Fallon (liam.fallon@est.tech)
60 @Table(name = "ToscaPolicy")
61 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
63 @EqualsAndHashCode(callSuper = true)
64 public class JpaToscaPolicy extends JpaToscaWithTypeAndStringProperties<ToscaPolicy> {
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";
71 private static final String METADATA_POLICY_ID_TAG = "policy-id";
72 private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
74 private static final StandardCoder STANDARD_CODER = new StandardCoder();
77 @CollectionTable(joinColumns = {
78 @JoinColumn(name = "toscaPolicyName", referencedColumnName = "name"),
79 @JoinColumn(name = "toscaPolicyVersion", referencedColumnName = "version")
81 private List<@NotNull @Valid PfConceptKey> targets;
84 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
86 public JpaToscaPolicy() {
91 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
95 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
96 super(key, new PfConceptKey());
100 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
103 * @param type the type of the policy
105 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
112 * @param copyConcept the concept to copy from
114 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
116 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
120 * Authorative constructor.
122 * @param authorativeConcept the authorative concept to copy from
124 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
125 super(new PfConceptKey());
126 this.fromAuthorative(authorativeConcept);
130 public ToscaPolicy toAuthorative() {
131 var toscaPolicy = new ToscaPolicy();
132 super.setToscaEntity(toscaPolicy);
133 super.toAuthorative();
139 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
140 super.fromAuthorative(toscaPolicy);
142 // Add the property metadata if it doesn't exist already
143 if (toscaPolicy.getMetadata() == null) {
144 setMetadata(new LinkedHashMap<>());
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());
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("^\"|\"$", ""));
157 getMetadata().put(METADATA_METADATA_SET_VERSION_TAG, getMetadata().get(METADATA_METADATA_SET_VERSION_TAG)
158 .replaceAll("^\"|\"$", ""));
163 protected Object deserializePropertyValue(String propValue) {
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);
173 protected String serializePropertyValue(Object propValue) {
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);
183 public List<PfKey> getKeys() {
184 final List<PfKey> keyList = super.getKeys();
186 if (targets != null) {
187 keyList.addAll(targets);
194 public void clean() {
197 if (targets != null) {
198 for (PfConceptKey target : targets) {
205 public BeanValidationResult validate(@NonNull String fieldName) {
206 BeanValidationResult result = super.validate(fieldName);
208 validateKeyVersionNotNull(result, "key", getKey());
214 public int compareTo(final PfConcept otherConcept) {
215 if (this == otherConcept) {
219 int result = super.compareTo(otherConcept);
224 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
226 return PfUtils.compareCollections(targets, other.targets);