Use annotations on parameterized types
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaRelationshipType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Nordix Foundation.
4  * Modifications Copyright (C) 2020-2021 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Set;
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;
37 import lombok.Data;
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.NotNull;
43 import org.onap.policy.common.parameters.annotations.Valid;
44 import org.onap.policy.models.base.PfAuthorative;
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.PfReferenceKey;
49 import org.onap.policy.models.base.PfUtils;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaRelationshipType;
52 import org.onap.policy.models.tosca.utils.ToscaUtils;
53
54 /**
55  * Class to represent the relationship type in TOSCA definition.
56  */
57
58 @Entity
59 @Table(name = "ToscaRelationshipType")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @Data
62 @EqualsAndHashCode(callSuper = true)
63 public class JpaToscaRelationshipType extends JpaToscaEntityType<ToscaRelationshipType>
64         implements PfAuthorative<ToscaRelationshipType> {
65     private static final long serialVersionUID = -563659852901842616L;
66
67     @ElementCollection
68     @Lob
69     private Map<@NotNull String, @NotNull @Valid JpaToscaProperty> properties;
70
71     /**
72      * The Default Constructor creates a {@link JpaToscaRelationshipType} object with a null key.
73      */
74     public JpaToscaRelationshipType() {
75         this(new PfConceptKey());
76     }
77
78     /**
79      * The Key Constructor creates a {@link JpaToscaRelationshipType} object with the given concept key.
80      *
81      * @param key the key
82      */
83     public JpaToscaRelationshipType(@NonNull final PfConceptKey key) {
84         super(key);
85     }
86
87     /**
88      * Copy constructor.
89      *
90      * @param copyConcept the concept to copy from
91      */
92     public JpaToscaRelationshipType(final JpaToscaRelationshipType copyConcept) {
93         super(copyConcept);
94         this.properties = PfUtils.mapMap(copyConcept.properties, JpaToscaProperty::new);
95     }
96
97     /**
98      * Authorative constructor.
99      *
100      * @param authorativeConcept the authorative concept to copy from
101      */
102     public JpaToscaRelationshipType(final ToscaRelationshipType authorativeConcept) {
103         this.fromAuthorative(authorativeConcept);
104     }
105
106     @Override
107     public ToscaRelationshipType toAuthorative() {
108         ToscaRelationshipType toscaRelationshipType = new ToscaRelationshipType();
109         super.setToscaEntity(toscaRelationshipType);
110         super.toAuthorative();
111
112         toscaRelationshipType.setProperties(PfUtils.mapMap(properties, JpaToscaProperty::toAuthorative));
113
114         return toscaRelationshipType;
115     }
116
117     @Override
118     public void fromAuthorative(final ToscaRelationshipType toscaRelationshipType) {
119         super.fromAuthorative(toscaRelationshipType);
120
121         // Set properties
122         if (toscaRelationshipType.getProperties() != null) {
123             properties = new LinkedHashMap<>();
124             for (Entry<String, ToscaProperty> toscaPropertyEntry : toscaRelationshipType.getProperties().entrySet()) {
125                 JpaToscaProperty jpaProperty = new JpaToscaProperty(toscaPropertyEntry.getValue());
126                 jpaProperty.setKey(new PfReferenceKey(getKey(), toscaPropertyEntry.getKey()));
127                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
128             }
129         }
130     }
131
132     @Override
133     public List<PfKey> getKeys() {
134         final List<PfKey> keyList = super.getKeys();
135
136         if (properties != null) {
137             for (JpaToscaProperty property : properties.values()) {
138                 keyList.addAll(property.getKeys());
139             }
140         }
141
142         return keyList;
143     }
144
145     @Override
146     public void clean() {
147         super.clean();
148
149         if (properties != null) {
150             for (JpaToscaProperty property : properties.values()) {
151                 property.clean();
152             }
153         }
154     }
155
156     @Override
157     public BeanValidationResult validate(String fieldName) {
158         BeanValidationResult result = super.validate(fieldName);
159
160         validateKeyVersionNotNull(result, "key", getKey());
161
162         return result;
163     }
164
165     @Override
166     public int compareTo(final PfConcept otherConcept) {
167         if (otherConcept == null) {
168             return -1;
169         }
170         if (this == otherConcept) {
171             return 0;
172         }
173         if (getClass() != otherConcept.getClass()) {
174             return getClass().getName().compareTo(otherConcept.getClass().getName());
175         }
176
177         final JpaToscaRelationshipType other = (JpaToscaRelationshipType) otherConcept;
178         int result = super.compareTo(other);
179         if (result != 0) {
180             return result;
181         }
182
183         return PfUtils.compareMaps(properties, other.properties);
184     }
185
186     /**
187      * Get the data types referenced in a relationship type.
188      *
189      * @return the data types referenced in a relationship type
190      */
191     public Collection<PfConceptKey> getReferencedDataTypes() {
192         if (properties == null) {
193             return CollectionUtils.emptyCollection();
194         }
195
196         Set<PfConceptKey> referencedDataTypes = new LinkedHashSet<>();
197
198         for (JpaToscaProperty property : properties.values()) {
199             referencedDataTypes.add(property.getType());
200
201             if (property.getEntrySchema() != null) {
202                 referencedDataTypes.add(property.getEntrySchema().getType());
203             }
204         }
205
206         referencedDataTypes.removeAll(ToscaUtils.getPredefinedDataTypes());
207
208         return referencedDataTypes;
209     }
210 }