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