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