bcb63a2f1a1407cb6cc5af17019f632eb0bdcceb
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022,2024 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.models.acm.document.concepts;
22
23 import java.io.Serial;
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.LinkedHashSet;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30 import lombok.Data;
31 import lombok.EqualsAndHashCode;
32 import lombok.NoArgsConstructor;
33 import lombok.NonNull;
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.onap.policy.clamp.models.acm.document.base.DocConceptKey;
36 import org.onap.policy.clamp.models.acm.document.base.DocUtil;
37 import org.onap.policy.common.parameters.BeanValidationResult;
38 import org.onap.policy.common.parameters.annotations.NotBlank;
39 import org.onap.policy.common.parameters.annotations.NotNull;
40 import org.onap.policy.common.parameters.annotations.Valid;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties;
43 import org.onap.policy.models.tosca.utils.ToscaUtils;
44
45 @Data
46 @EqualsAndHashCode(callSuper = true)
47 @NoArgsConstructor
48 public class DocToscaWithToscaProperties<T extends ToscaWithToscaProperties> extends DocToscaEntity<T> {
49
50     @Serial
51     private static final long serialVersionUID = 1L;
52
53     @SuppressWarnings("squid:S1948")
54     private Map<@NotNull @NotBlank String, @NotNull @Valid DocToscaProperty> properties;
55
56     /**
57      * Copy constructor.
58      *
59      * @param copyConcept the concept to copy from
60      */
61     public DocToscaWithToscaProperties(DocToscaWithToscaProperties<T> copyConcept) {
62         super(copyConcept);
63         this.properties = PfUtils.mapMap(copyConcept.properties, DocToscaProperty::new);
64     }
65
66     @Override
67     public T toAuthorative() {
68         var tosca = super.toAuthorative();
69         tosca.setProperties(PfUtils.mapMap(properties, DocToscaProperty::toAuthorative));
70         return tosca;
71     }
72
73     /**
74      * Validates the fields of the object, including its key.
75      *
76      * @param fieldName name of the field containing this
77      * @return the result, or {@code null}
78      */
79     protected BeanValidationResult validateWithKey(@NonNull String fieldName) {
80         var result = super.validate(fieldName);
81
82         validateKeyVersionNotNull(result, "key", getConceptKey());
83
84         return result;
85     }
86
87     @Override
88     public void fromAuthorative(T authorativeConcept) {
89         super.fromAuthorative(authorativeConcept);
90
91         // Set properties
92         if (authorativeConcept.getProperties() != null) {
93             properties = new LinkedHashMap<>();
94             for (var toscaPropertyEntry : authorativeConcept.getProperties().entrySet()) {
95                 var jpaProperty = new DocToscaProperty(toscaPropertyEntry.getValue());
96                 jpaProperty.setName(toscaPropertyEntry.getKey());
97                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
98             }
99         }
100     }
101
102     /**
103      * Get the referenced data types.
104      *
105      * @return the referenced data types
106      */
107     public Collection<DocConceptKey> getReferencedDataTypes() {
108         if (properties == null) {
109             return CollectionUtils.emptyCollection();
110         }
111
112         Set<DocConceptKey> referencedDataTypes = new LinkedHashSet<>();
113
114         for (var property : properties.values()) {
115             referencedDataTypes.add(property.getTypeDocConceptKey());
116
117             if (property.getEntrySchema() != null) {
118                 referencedDataTypes.add(property.getEntrySchema().getTypeDocConceptKey());
119             }
120         }
121
122         var set = ToscaUtils.getPredefinedDataTypes().stream().map(DocConceptKey::new).collect(Collectors.toSet());
123         referencedDataTypes.removeAll(set);
124         return referencedDataTypes;
125     }
126
127     @Override
128     public int compareTo(final DocToscaEntity<T> otherConcept) {
129         if (this == otherConcept) {
130             return 0;
131         }
132
133         int result = super.compareTo(otherConcept);
134         if (result != 0) {
135             return result;
136         }
137
138         final var other = (DocToscaWithToscaProperties<T>) otherConcept;
139
140         return DocUtil.compareMaps(properties, other.properties);
141     }
142 }