2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.tosca.simple.concepts;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import javax.persistence.AttributeOverride;
29 import javax.persistence.Column;
30 import javax.persistence.ElementCollection;
31 import javax.persistence.Entity;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.Table;
35 import javax.ws.rs.core.Response;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.onap.policy.common.parameters.BeanValidationResult;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.common.parameters.annotations.Valid;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfConceptKey;
46 import org.onap.policy.models.base.PfKey;
47 import org.onap.policy.models.base.PfModelRuntimeException;
48 import org.onap.policy.models.base.PfUtils;
49 import org.onap.policy.models.base.validation.annotations.VerifyKey;
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 JpaToscaWithStringProperties<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();
74 @AttributeOverride(name = "name", column = @Column(name = "type_name"))
75 @AttributeOverride(name = "version", column = @Column(name = "type_version"))
78 private PfConceptKey type;
81 private List<@NotNull @Valid PfConceptKey> targets;
85 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
87 public JpaToscaPolicy() {
88 this(new PfConceptKey());
92 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
96 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
97 this(key, new PfConceptKey());
101 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
104 * @param type the type of the policy
106 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
114 * @param copyConcept the concept to copy from
116 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
118 this.type = new PfConceptKey(copyConcept.type);
119 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
123 * Authorative constructor.
125 * @param authorativeConcept the authorative concept to copy from
127 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
128 super(new PfConceptKey());
129 type = new PfConceptKey();
130 this.fromAuthorative(authorativeConcept);
134 public ToscaPolicy toAuthorative() {
135 ToscaPolicy toscaPolicy = new ToscaPolicy();
136 super.setToscaEntity(toscaPolicy);
137 super.toAuthorative();
139 toscaPolicy.setType(type.getName());
141 if (!PfKey.NULL_KEY_VERSION.equals(type.getVersion())) {
142 toscaPolicy.setTypeVersion(type.getVersion());
144 toscaPolicy.setTypeVersion(null);
151 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
152 super.fromAuthorative(toscaPolicy);
154 if (toscaPolicy.getType() != null) {
155 type.setName(toscaPolicy.getType());
157 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
158 "PolicyType type not specified, the type of the PolicyType for this policy must be specified in "
162 if (toscaPolicy.getTypeVersion() != null) {
163 type.setVersion(toscaPolicy.getTypeVersion());
165 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
166 "PolicyType version not specified, the version of the PolicyType for this policy must be specified"
167 + " in the type_version field");
170 // Add the property metadata if it doesn't exist already
171 if (toscaPolicy.getMetadata() == null) {
172 setMetadata(new LinkedHashMap<>());
175 // Add the policy name and version fields to the metadata
176 getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
177 getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion());
181 protected Object deserializePropertyValue(String propValue) {
183 return STANDARD_CODER.decode(propValue, Object.class);
184 } catch (CoderException ce) {
185 String errorMessage = "error decoding property JSON value read from database: " + propValue;
186 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
191 protected String serializePropertyValue(Object propValue) {
193 return STANDARD_CODER.encode(propValue);
194 } catch (CoderException ce) {
195 String errorMessage = "error encoding property JSON value for database: " + propValue;
196 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
201 public List<PfKey> getKeys() {
202 final List<PfKey> keyList = super.getKeys();
204 keyList.addAll(type.getKeys());
206 if (targets != null) {
207 keyList.addAll(targets);
214 public void clean() {
219 if (targets != null) {
220 for (PfConceptKey target : targets) {
227 public BeanValidationResult validate(String fieldName) {
228 BeanValidationResult result = super.validate(fieldName);
230 validateKeyVersionNotNull(result, "key", getKey());
236 public int compareTo(final PfConcept otherConcept) {
237 if (this == otherConcept) {
241 int result = super.compareTo(otherConcept);
246 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
248 result = type.compareTo(other.type);
253 return PfUtils.compareCollections(targets, other.targets);