2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.LinkedHashSet;
27 import java.util.List;
29 import java.util.Map.Entry;
31 import javax.persistence.ElementCollection;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Lob;
36 import javax.persistence.Table;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40 import org.apache.commons.collections4.CollectionUtils;
41 import org.onap.policy.common.parameters.BeanValidationResult;
42 import org.onap.policy.common.parameters.annotations.Entries;
43 import org.onap.policy.common.parameters.annotations.Items;
44 import org.onap.policy.common.parameters.annotations.NotNull;
45 import org.onap.policy.common.parameters.annotations.Valid;
46 import org.onap.policy.models.base.PfAuthorative;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfReferenceKey;
51 import org.onap.policy.models.base.PfUtils;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType;
54 import org.onap.policy.models.tosca.utils.ToscaUtils;
57 * Class to represent the relationship type in TOSCA definition.
61 @Table(name = "ToscaRelationshipType")
62 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
64 @EqualsAndHashCode(callSuper = true)
65 public class JpaToscaRelationshipType extends JpaToscaEntityType<ToscaRelationshipType>
66 implements PfAuthorative<ToscaRelationshipType> {
67 private static final long serialVersionUID = -563659852901842616L;
71 @Entries(key = @Items(notNull = {@NotNull}), value = @Items(notNull = {@NotNull}, valid = {@Valid}))
72 private Map<String, JpaToscaProperty> properties;
75 * The Default Constructor creates a {@link JpaToscaRelationshipType} object with a null key.
77 public JpaToscaRelationshipType() {
78 this(new PfConceptKey());
82 * The Key Constructor creates a {@link JpaToscaRelationshipType} object with the given concept key.
86 public JpaToscaRelationshipType(@NonNull final PfConceptKey key) {
93 * @param copyConcept the concept to copy from
95 public JpaToscaRelationshipType(final JpaToscaRelationshipType copyConcept) {
97 this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
101 * Authorative constructor.
103 * @param authorativeConcept the authorative concept to copy from
105 public JpaToscaRelationshipType(final ToscaRelationshipType authorativeConcept) {
106 this.fromAuthorative(authorativeConcept);
110 public ToscaRelationshipType toAuthorative() {
111 ToscaRelationshipType toscaRelationshipType = new ToscaRelationshipType();
112 super.setToscaEntity(toscaRelationshipType);
113 super.toAuthorative();
115 toscaRelationshipType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
117 return toscaRelationshipType;
121 public void fromAuthorative(final ToscaRelationshipType toscaRelationshipType) {
122 super.fromAuthorative(toscaRelationshipType);
125 if (toscaRelationshipType.getProperties() != null) {
126 properties = new LinkedHashMap<>();
127 for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaRelationshipType.getProperties().entrySet()) {
128 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
129 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
130 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
136 public List<PfKey> getKeys() {
137 final List<PfKey> keyList = super.getKeys();
139 if (properties != null) {
140 for (JpaToscaProperty property : properties.values()) {
141 keyList.addAll(property.getKeys());
149 public void clean() {
152 if (properties != null) {
153 for (JpaToscaProperty property : properties.values()) {
160 public BeanValidationResult validate(String fieldName) {
161 BeanValidationResult result = super.validate(fieldName);
163 result.addResult(validateKeyVersionNotNull("key", getKey()));
169 public int compareTo(final PfConcept otherConcept) {
170 if (otherConcept == null) {
173 if (this == otherConcept) {
176 if (getClass() != otherConcept.getClass()) {
177 return getClass().getName().compareTo(otherConcept.getClass().getName());
180 final JpaToscaRelationshipType other = (JpaToscaRelationshipType) otherConcept;
181 int result = super.compareTo(other);
186 return PfUtils.compareMaps(properties, other.properties);
190 * Get the data types referenced in a relationship type.
192 * @return the data types referenced in a relationship type
194 public Collection<PfConceptKey> getReferencedDataTypes() {
195 if (properties == null) {
196 return CollectionUtils.emptyCollection();
199 Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
201 for (JpaToscaProperty property : properties.values()) {
202 referencedDataTypes.add(property.getType());
204 if (property.getEntrySchema() != null) {
205 referencedDataTypes.add(property.getEntrySchema().getType());
209 referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
211 return referencedDataTypes;