3b53e1bc8a1d71c2d5bcb7610e8095e8fb3583f3
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.util.Collection;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedHashSet;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.stream.Collectors;
29 import lombok.Data;
30 import lombok.EqualsAndHashCode;
31 import lombok.NoArgsConstructor;
32 import lombok.NonNull;
33 import org.apache.commons.collections4.CollectionUtils;
34 import org.onap.policy.clamp.models.acm.document.base.DocConceptKey;
35 import org.onap.policy.common.parameters.BeanValidationResult;
36 import org.onap.policy.common.parameters.annotations.NotBlank;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.parameters.annotations.Valid;
39 import org.onap.policy.models.base.PfUtils;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties;
41 import org.onap.policy.models.tosca.utils.ToscaUtils;
42
43 @Data
44 @EqualsAndHashCode(callSuper = true)
45 @NoArgsConstructor
46 public class DocToscaWithToscaProperties<T extends ToscaWithToscaProperties> extends DocToscaEntity<T> {
47
48     private static final long serialVersionUID = 1L;
49
50     private Map<@NotNull @NotBlank String, @NotNull @Valid DocToscaProperty> properties;
51
52     /**
53      * Copy constructor.
54      *
55      * @param copyConcept the concept to copy from
56      */
57     public DocToscaWithToscaProperties(DocToscaWithToscaProperties<T> copyConcept) {
58         super(copyConcept);
59         this.properties = PfUtils.mapMap(copyConcept.properties, DocToscaProperty::new);
60     }
61
62     @Override
63     public T toAuthorative() {
64         var tosca = super.toAuthorative();
65         tosca.setProperties(PfUtils.mapMap(properties, DocToscaProperty::toAuthorative));
66         return tosca;
67     }
68
69     /**
70      * Validates the fields of the object, including its key.
71      *
72      * @param fieldName name of the field containing this
73      * @return the result, or {@code null}
74      */
75     protected BeanValidationResult validateWithKey(@NonNull String fieldName) {
76         var result = super.validate(fieldName);
77
78         validateKeyVersionNotNull(result, "key", getConceptKey());
79
80         return result;
81     }
82
83     @Override
84     public void fromAuthorative(T authorativeConcept) {
85         super.fromAuthorative(authorativeConcept);
86
87         // Set properties
88         if (authorativeConcept.getProperties() != null) {
89             properties = new LinkedHashMap<>();
90             for (var toscaPropertyEntry : authorativeConcept.getProperties().entrySet()) {
91                 var jpaProperty = new DocToscaProperty(toscaPropertyEntry.getValue());
92                 jpaProperty.setName(toscaPropertyEntry.getKey());
93                 properties.put(toscaPropertyEntry.getKey(), jpaProperty);
94             }
95         }
96     }
97
98     /**
99      * Get the referenced data types.
100      *
101      * @return the referenced data types
102      */
103     public Collection<DocConceptKey> getReferencedDataTypes() {
104         if (properties == null) {
105             return CollectionUtils.emptyCollection();
106         }
107
108         Set<DocConceptKey> referencedDataTypes = new LinkedHashSet<>();
109
110         for (var property : properties.values()) {
111             referencedDataTypes.add(property.getTypeDocConceptKey());
112
113             if (property.getEntrySchema() != null) {
114                 referencedDataTypes.add(property.getEntrySchema().getTypeDocConceptKey());
115             }
116         }
117
118         var set = ToscaUtils.getPredefinedDataTypes().stream().map(DocConceptKey::new).collect(Collectors.toSet());
119         referencedDataTypes.removeAll(set);
120         return referencedDataTypes;
121     }
122 }