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 * ================================================================================
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.ElementCollection;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Table;
33 import javax.ws.rs.core.Response;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.common.parameters.annotations.Valid;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfConceptKey;
44 import org.onap.policy.models.base.PfKey;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50 * Class to represent the policy in TOSCA definition.
52 * @author Chenfei Gao (cgao@research.att.com)
53 * @author Liam Fallon (liam.fallon@est.tech)
56 @Table(name = "ToscaPolicy")
57 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
59 @EqualsAndHashCode(callSuper = true)
60 public class JpaToscaPolicy extends JpaToscaWithTypeAndStringProperties<ToscaPolicy> {
61 private static final long serialVersionUID = 3265174757061982805L;
64 private static final String METADATA_POLICY_ID_TAG = "policy-id";
65 private static final String METADATA_POLICY_VERSION_TAG = "policy-version";
67 private static final StandardCoder STANDARD_CODER = new StandardCoder();
70 private List<@NotNull @Valid PfConceptKey> targets;
73 * The Default Constructor creates a {@link JpaToscaPolicy} object with a null key.
75 public JpaToscaPolicy() {
80 * The Key Constructor creates a {@link JpaToscaPolicy} object with the given concept key.
84 public JpaToscaPolicy(@NonNull final PfConceptKey key) {
85 super(key, new PfConceptKey());
89 * The full Constructor creates a {@link JpaToscaPolicy} object with all mandatory fields.
92 * @param type the type of the policy
94 public JpaToscaPolicy(@NonNull final PfConceptKey key, @NonNull final PfConceptKey type) {
101 * @param copyConcept the concept to copy from
103 public JpaToscaPolicy(@NonNull final JpaToscaPolicy copyConcept) {
105 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
109 * Authorative constructor.
111 * @param authorativeConcept the authorative concept to copy from
113 public JpaToscaPolicy(final ToscaPolicy authorativeConcept) {
114 super(new PfConceptKey());
115 this.fromAuthorative(authorativeConcept);
119 public ToscaPolicy toAuthorative() {
120 ToscaPolicy toscaPolicy = new ToscaPolicy();
121 super.setToscaEntity(toscaPolicy);
122 super.toAuthorative();
128 public void fromAuthorative(@NonNull final ToscaPolicy toscaPolicy) {
129 super.fromAuthorative(toscaPolicy);
131 // Add the property metadata if it doesn't exist already
132 if (toscaPolicy.getMetadata() == null) {
133 setMetadata(new LinkedHashMap<>());
136 // Add the policy name and version fields to the metadata
137 getMetadata().put(METADATA_POLICY_ID_TAG, getKey().getName());
138 getMetadata().put(METADATA_POLICY_VERSION_TAG, getKey().getVersion());
142 protected Object deserializePropertyValue(String propValue) {
144 return STANDARD_CODER.decode(propValue, Object.class);
145 } catch (CoderException ce) {
146 String errorMessage = "error decoding property JSON value read from database: " + propValue;
147 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
152 protected String serializePropertyValue(Object propValue) {
154 return STANDARD_CODER.encode(propValue);
155 } catch (CoderException ce) {
156 String errorMessage = "error encoding property JSON value for database: " + propValue;
157 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
162 public List<PfKey> getKeys() {
163 final List<PfKey> keyList = super.getKeys();
165 if (targets != null) {
166 keyList.addAll(targets);
173 public void clean() {
176 if (targets != null) {
177 for (PfConceptKey target : targets) {
184 public BeanValidationResult validate(String fieldName) {
185 BeanValidationResult result = super.validate(fieldName);
187 validateKeyVersionNotNull(result, "key", getKey());
193 public int compareTo(final PfConcept otherConcept) {
194 if (this == otherConcept) {
198 int result = super.compareTo(otherConcept);
203 final JpaToscaPolicy other = (JpaToscaPolicy) otherConcept;
205 return PfUtils.compareCollections(targets, other.targets);