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;
67 private static final String METADATA_POLICY_ID_TAG = "policy-id";
68 private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
70 private static final StandardCoder STANDARD_CODER = new StandardCoder();
73 @CollectionTable(joinColumns = {
74 @JoinColumn(name = "toscaPolicyName", referencedColumnName = "name"),
75 @JoinColumn(name = "toscaPolicyVersion", referencedColumnName = "version")
77 private List<@NotNull @Valid PfConceptKey> targets;
80 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
82 public JpaToscaPolicy() {
87 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
91 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
92 super(key, new PfConceptKey());
96 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
99 * @param type the type of the policy
101 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
108 * @param copyConcept the concept to copy from
110 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
112 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
116 * Authorative constructor.
118 * @param authorativeConcept the authorative concept to copy from
120 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
121 super(new PfConceptKey());
122 this.fromAuthorative(authorativeConcept);
126 public ToscaPolicy toAuthorative() {
127 var toscaPolicy = new ToscaPolicy();
128 super.setToscaEntity(toscaPolicy);
129 super.toAuthorative();
135 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
136 super.fromAuthorative(toscaPolicy);
138 // Add the property metadata if it doesn't exist already
139 if (toscaPolicy.getMetadata() == null) {
140 setMetadata(new LinkedHashMap<>());
143 // Add the policy name and version fields to the metadata
144 getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
145 getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion());
149 protected Object deserializePropertyValue(String propValue) {
151 return STANDARD_CODER.decode(propValue, Object.class);
152 } catch (CoderException ce) {
153 String errorMessage = "error decoding property JSON value read from database: " + propValue;
154 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
159 protected String serializePropertyValue(Object propValue) {
161 return STANDARD_CODER.encode(propValue);
162 } catch (CoderException ce) {
163 String errorMessage = "error encoding property JSON value for database: " + propValue;
164 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
169 public List<PfKey> getKeys() {
170 final List<PfKey> keyList = super.getKeys();
172 if (targets != null) {
173 keyList.addAll(targets);
180 public void clean() {
183 if (targets != null) {
184 for (PfConceptKey target : targets) {
191 public BeanValidationResult validate(String fieldName) {
192 BeanValidationResult result = super.validate(fieldName);
194 validateKeyVersionNotNull(result, "key", getKey());
200 public int compareTo(final PfConcept otherConcept) {
201 if (this == otherConcept) {
205 int result = super.compareTo(otherConcept);
210 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
212 return PfUtils.compareCollections(targets, other.targets);