2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2021 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 java.util.LinkedHashMap;
28 import java.util.List;
29 import javax.persistence.CollectionTable;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.JoinColumn;
35 import javax.persistence.Table;
36 import javax.ws.rs.core.Response;
38 import lombok.EqualsAndHashCode;
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.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.models.base.PfConcept;
46 import org.onap.policy.models.base.PfConceptKey;
47 import org.onap.policy.models.base.PfKey;
48 import org.onap.policy.models.base.PfModelRuntimeException;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53 * Class to represent the policy in TOSCA definition.
55 * @author Chenfei Gao (cgao@research.att.com)
56 * @author Liam Fallon (liam.fallon@est.tech)
59 @Table(name = "ToscaPolicy")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
62 @EqualsAndHashCode(callSuper = true)
63 public class JpaToscaPolicy extends JpaToscaWithTypeAndStringProperties<ToscaPolicy> {
64 private static final long serialVersionUID = 3265174757061982805L;
65 private static final String METADATA_METADATA_SET_NAME_TAG = "metadataSetName";
66 private static final String METADATA_METADATA_SET_VERSION_TAG = "metadataSetVersion";
69 private static final String METADATA_POLICY_ID_TAG = "policy-id";
70 private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
72 private static final StandardCoder STANDARD_CODER = new StandardCoder();
75 @CollectionTable(joinColumns = {
76 @JoinColumn(name = "toscaPolicyName", referencedColumnName = "name"),
77 @JoinColumn(name = "toscaPolicyVersion", referencedColumnName = "version")
79 private List<@NotNull @Valid PfConceptKey> targets;
82 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
84 public JpaToscaPolicy() {
89 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
93 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
94 super(key, new PfConceptKey());
98 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
101 * @param type the type of the policy
103 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
110 * @param copyConcept the concept to copy from
112 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
114 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
118 * Authorative constructor.
120 * @param authorativeConcept the authorative concept to copy from
122 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
123 super(new PfConceptKey());
124 this.fromAuthorative(authorativeConcept);
128 public ToscaPolicy toAuthorative() {
129 var toscaPolicy = new ToscaPolicy();
130 super.setToscaEntity(toscaPolicy);
131 super.toAuthorative();
137 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
138 super.fromAuthorative(toscaPolicy);
140 // Add the property metadata if it doesn't exist already
141 if (toscaPolicy.getMetadata() == null) {
142 setMetadata(new LinkedHashMap<>());
145 // Add the policy name and version fields to the metadata
146 getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
147 getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion());
149 // Add metadataSet name and version to the metadata
150 if (getMetadata().containsKey(METADATA_METADATA_SET_NAME_TAG)
151 && getMetadata().containsKey(METADATA_METADATA_SET_VERSION_TAG)) {
152 getMetadata().put(METADATA_METADATA_SET_NAME_TAG, getMetadata().get(METADATA_METADATA_SET_NAME_TAG)
153 .replaceAll("^\"|\"$", ""));
155 getMetadata().put(METADATA_METADATA_SET_VERSION_TAG, getMetadata().get(METADATA_METADATA_SET_VERSION_TAG)
156 .replaceAll("^\"|\"$", ""));
161 protected Object deserializePropertyValue(String propValue) {
163 return STANDARD_CODER.decode(propValue, Object.class);
164 } catch (CoderException ce) {
165 String errorMessage = "error decoding property JSON value read from database: " + propValue;
166 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
171 protected String serializePropertyValue(Object propValue) {
173 return STANDARD_CODER.encode(propValue);
174 } catch (CoderException ce) {
175 String errorMessage = "error encoding property JSON value for database: " + propValue;
176 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
181 public List<PfKey> getKeys() {
182 final List<PfKey> keyList = super.getKeys();
184 if (targets != null) {
185 keyList.addAll(targets);
192 public void clean() {
195 if (targets != null) {
196 for (PfConceptKey target : targets) {
203 public BeanValidationResult validate(String fieldName) {
204 BeanValidationResult result = super.validate(fieldName);
206 validateKeyVersionNotNull(result, "key", getKey());
212 public int compareTo(final PfConcept otherConcept) {
213 if (this == otherConcept) {
217 int result = super.compareTo(otherConcept);
222 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
224 return PfUtils.compareCollections(targets, other.targets);