1eb3cac705e8e3fded9bc7063151a8261baa7861
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2026 OpenInfra Foundation Europe. All rights reserved.
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 com.google.gson.annotations.SerializedName;
24 import jakarta.validation.Valid;
25 import jakarta.validation.constraints.NotBlank;
26 import java.io.Serial;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import lombok.Data;
30 import lombok.EqualsAndHashCode;
31 import org.apache.commons.lang3.ObjectUtils;
32 import org.onap.policy.clamp.models.acm.document.base.DocUtil;
33 import org.onap.policy.models.base.PfKey;
34 import org.onap.policy.models.base.PfUtils;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
36
37 @Data
38 @EqualsAndHashCode(callSuper = true)
39 public class DocToscaServiceTemplate extends DocToscaEntity<ToscaServiceTemplate> {
40
41     @Serial
42     private static final long serialVersionUID = 1L;
43
44     public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_1_0";
45     public static final String DEFAULT_NAME = "tosca";
46     public static final String DEFAULT_VERSION = "1.0.0";
47
48     @SerializedName("tosca_definitions_version")
49     @NotBlank
50     private String toscaDefinitionsVersion;
51
52     @SerializedName("data_types")
53     private Map<String, @Valid DocToscaDataType> dataTypes;
54
55     @SerializedName("capability_types")
56     private Map<String, @Valid DocToscaCapabilityType> capabilityTypes;
57
58     @SerializedName("node_types")
59     private Map<String, @Valid DocToscaNodeType> nodeTypes;
60
61     @SerializedName("relationship_types")
62     private Map<String, @Valid DocToscaRelationshipType> relationshipTypes;
63
64     @SerializedName("policy_types")
65     private Map<String, @Valid DocToscaPolicyType> policyTypes;
66
67     @SerializedName("topology_template")
68     @Valid
69     private DocToscaTopologyTemplate toscaTopologyTemplate;
70
71     public DocToscaServiceTemplate(ToscaServiceTemplate authorativeConcept) {
72         this.fromAuthorative(authorativeConcept);
73     }
74
75     /**
76      * The Default Constructor creates a {@link DocToscaServiceTemplate} object with a null key.
77      */
78     public DocToscaServiceTemplate() {
79         super();
80         setName(DEFAULT_NAME);
81         setVersion(DEFAULT_VERSION);
82         setToscaDefinitionsVersion(DEFAULT_TOSCA_DEFINTIONS_VERISON);
83     }
84
85     /**
86      * Copy constructor.
87      *
88      * @param copyConcept the concept to copy from
89      */
90     public DocToscaServiceTemplate(final DocToscaServiceTemplate copyConcept) {
91         super(copyConcept);
92         this.toscaDefinitionsVersion = copyConcept.toscaDefinitionsVersion;
93         this.dataTypes = PfUtils.mapMap(copyConcept.dataTypes, DocToscaDataType::new, new LinkedHashMap<>());
94         this.capabilityTypes =
95                 PfUtils.mapMap(copyConcept.capabilityTypes, DocToscaCapabilityType::new, new LinkedHashMap<>());
96         this.nodeTypes = PfUtils.mapMap(copyConcept.nodeTypes, DocToscaNodeType::new, new LinkedHashMap<>());
97         this.relationshipTypes = PfUtils.mapMap(copyConcept.relationshipTypes, DocToscaRelationshipType::new,
98                 new LinkedHashMap<>());
99         this.policyTypes = PfUtils.mapMap(copyConcept.policyTypes, DocToscaPolicyType::new, new LinkedHashMap<>());
100         if (copyConcept.toscaTopologyTemplate != null) {
101             this.toscaTopologyTemplate = new DocToscaTopologyTemplate(copyConcept.toscaTopologyTemplate);
102         }
103     }
104
105     @Override
106     public ToscaServiceTemplate toAuthorative() {
107         final var toscaServiceTemplate = new ToscaServiceTemplate();
108         super.setToscaEntity(toscaServiceTemplate);
109         super.toAuthorative();
110
111         toscaServiceTemplate.setToscaDefinitionsVersion(toscaDefinitionsVersion);
112         toscaServiceTemplate.setDataTypes(DocUtil.docMapToMap(dataTypes, DocToscaDataType::toAuthorative));
113         toscaServiceTemplate
114                 .setCapabilityTypes(DocUtil.docMapToMap(capabilityTypes, DocToscaCapabilityType::toAuthorative));
115         toscaServiceTemplate
116                 .setRelationshipTypes(DocUtil.docMapToMap(relationshipTypes, DocToscaRelationshipType::toAuthorative));
117         toscaServiceTemplate.setNodeTypes(DocUtil.docMapToMap(nodeTypes, DocToscaNodeType::toAuthorative));
118         toscaServiceTemplate.setPolicyTypes(DocUtil.docMapToMap(policyTypes, DocToscaPolicyType::toAuthorative));
119         if (toscaTopologyTemplate != null) {
120             toscaServiceTemplate.setToscaTopologyTemplate(toscaTopologyTemplate.toAuthorative());
121         }
122
123         return toscaServiceTemplate;
124     }
125
126     @Override
127     public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
128         super.fromAuthorative(toscaServiceTemplate);
129         if (getVersion() == null || PfKey.NULL_KEY_VERSION.equals(getVersion())) {
130             setVersion(DEFAULT_VERSION);
131         }
132         if (getName() == null || PfKey.NULL_KEY_NAME.equals(getName())) {
133             setName(DEFAULT_NAME);
134         }
135
136         toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
137
138         dataTypes = DocUtil.mapToDocMap(toscaServiceTemplate.getDataTypes(), DocToscaDataType::new,
139                 new LinkedHashMap<>());
140
141         capabilityTypes = DocUtil.mapToDocMap(toscaServiceTemplate.getCapabilityTypes(), DocToscaCapabilityType::new,
142                 new LinkedHashMap<>());
143
144         relationshipTypes =
145                 DocUtil.mapToDocMap(toscaServiceTemplate.getRelationshipTypes(), DocToscaRelationshipType::new,
146                         new LinkedHashMap<>());
147
148         nodeTypes = DocUtil.mapToDocMap(toscaServiceTemplate.getNodeTypes(), DocToscaNodeType::new,
149                 new LinkedHashMap<>());
150
151         policyTypes = DocUtil.mapToDocMap(toscaServiceTemplate.getPolicyTypes(), DocToscaPolicyType::new,
152                 new LinkedHashMap<>());
153
154         if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
155             toscaTopologyTemplate = new DocToscaTopologyTemplate(toscaServiceTemplate.getToscaTopologyTemplate());
156         }
157     }
158
159     @Override
160     public int compareTo(DocToscaEntity<ToscaServiceTemplate> otherConcept) {
161         int result = compareToWithoutEntities(otherConcept);
162         if (result != 0) {
163             return result;
164         }
165
166         final var other = (DocToscaServiceTemplate) otherConcept;
167
168         result = DocUtil.compareMaps(dataTypes, other.dataTypes);
169         if (result != 0) {
170             return result;
171         }
172
173         result = DocUtil.compareMaps(capabilityTypes, other.capabilityTypes);
174         if (result != 0) {
175             return result;
176         }
177
178         result = DocUtil.compareMaps(relationshipTypes, other.relationshipTypes);
179         if (result != 0) {
180             return result;
181         }
182
183         result = DocUtil.compareMaps(nodeTypes, other.nodeTypes);
184         if (result != 0) {
185             return result;
186         }
187
188         result = DocUtil.compareMaps(policyTypes, other.policyTypes);
189         if (result != 0) {
190             return result;
191         }
192
193         return ObjectUtils.compare(toscaTopologyTemplate, other.toscaTopologyTemplate);
194     }
195
196     /**
197      * Compare this service template to another service template, ignoring contained entities.
198      *
199      * @param otherConcept the other topology template
200      * @return the result of the comparison
201      */
202     public int compareToWithoutEntities(final DocToscaEntity<ToscaServiceTemplate> otherConcept) {
203         int result = super.compareTo(otherConcept);
204         if (result != 0) {
205             return result;
206         }
207
208         final var other = (DocToscaServiceTemplate) otherConcept;
209         return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
210     }
211 }