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 * 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.List;
28 import javax.persistence.CollectionTable;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.Lob;
35 import javax.persistence.Table;
37 import lombok.EqualsAndHashCode;
38 import lombok.NoArgsConstructor;
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.models.base.PfConcept;
44 import org.onap.policy.models.base.PfConceptKey;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
50 * Class to represent the policy type in TOSCA definition.
52 * @author Chenfei Gao (cgao@research.att.com)
53 * @author Liam Fallon (liam.fallon@est.tech)
57 @Table(name = "ToscaPolicyType")
58 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @EqualsAndHashCode(callSuper = true)
62 public class JpaToscaPolicyType extends JpaToscaWithToscaProperties<ToscaPolicyType> {
63 private static final long serialVersionUID = -563659852901842616L;
66 @CollectionTable(joinColumns = {
67 @JoinColumn(name = "toscaPolicyTypeName", referencedColumnName = "name"),
68 @JoinColumn(name = "toscaPolicyTypeVersion", referencedColumnName = "version")
70 private List<@NotNull @Valid PfConceptKey> targets;
74 private List<@NotNull @Valid JpaToscaTrigger> triggers;
77 * The Key Constructor creates a {@link JpaToscaPolicyType} object with the given concept key.
81 public JpaToscaPolicyType(@NonNull final PfConceptKey key) {
88 * @param copyConcept the concept to copy from
90 public JpaToscaPolicyType(final JpaToscaPolicyType copyConcept) {
92 this.targets = PfUtils.mapList(copyConcept.targets, PfConceptKey::new);
93 this.triggers = PfUtils.mapList(copyConcept.triggers, JpaToscaTrigger::new);
97 * Authorative constructor.
99 * @param authorativeConcept the authorative concept to copy from
101 public JpaToscaPolicyType(final ToscaPolicyType authorativeConcept) {
102 super(authorativeConcept);
106 public ToscaPolicyType toAuthorative() {
107 var toscaPolicyType = new ToscaPolicyType();
108 super.setToscaEntity(toscaPolicyType);
109 super.toAuthorative();
111 // TODO need to copy targets & triggers?
113 return toscaPolicyType;
117 public void fromAuthorative(final ToscaPolicyType toscaPolicyType) {
118 super.fromAuthorative(toscaPolicyType);
120 // TODO need to copy targets & triggers?
124 public List<PfKey> getKeys() {
125 final List<PfKey> keyList = super.getKeys();
127 if (targets != null) {
128 keyList.addAll(targets);
131 if (triggers != null) {
132 for (JpaToscaTrigger trigger : triggers) {
133 keyList.addAll(trigger.getKeys());
141 public void clean() {
144 if (targets != null) {
145 for (PfConceptKey target : targets) {
150 if (triggers != null) {
151 for (JpaToscaTrigger trigger : triggers) {
158 public BeanValidationResult validate(@NonNull String fieldName) {
159 return validateWithKey(fieldName);
163 public int compareTo(final PfConcept otherConcept) {
164 if (this == otherConcept) {
168 int result = super.compareTo(otherConcept);
173 final JpaToscaPolicyType other = (JpaToscaPolicyType) otherConcept;
175 result = PfUtils.compareCollections(targets, other.targets);
180 return PfUtils.compareCollections(triggers, other.triggers);