3c0de41d6f41b83f43e314b23e846cb388025f3c
[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.util.LinkedHashMap;
25 import java.util.Map;
26 import lombok.Data;
27 import lombok.EqualsAndHashCode;
28 import org.apache.commons.lang3.ObjectUtils;
29 import org.onap.policy.clamp.models.acm.document.base.DocUtil;
30 import org.onap.policy.common.parameters.annotations.NotBlank;
31 import org.onap.policy.common.parameters.annotations.NotNull;
32 import org.onap.policy.common.parameters.annotations.Valid;
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     private static final long serialVersionUID = 1L;
42
43     public static final String DEFAULT_TOSCA_DEFINTIONS_VERISON = "tosca_simple_yaml_1_1_0";
44     public static final String DEFAULT_NAME = "tosca";
45     public static final String DEFAULT_VERSION = "1.0.0";
46
47     @SerializedName("tosca_definitions_version")
48     @NotNull
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         toscaServiceTemplate.setToscaTopologyTemplate(toscaTopologyTemplate.toAuthorative());
120
121         return toscaServiceTemplate;
122     }
123
124     @Override
125     public void fromAuthorative(ToscaServiceTemplate toscaServiceTemplate) {
126         super.fromAuthorative(toscaServiceTemplate);
127         if (getVersion() == null || PfKey.NULL_KEY_VERSION.equals(getVersion())) {
128             setVersion(DEFAULT_VERSION);
129         }
130         if (getName() == null || PfKey.NULL_KEY_NAME.equals(getName())) {
131             setName(DEFAULT_NAME);
132         }
133
134         toscaDefinitionsVersion = toscaServiceTemplate.getToscaDefinitionsVersion();
135
136         dataTypes = DocUtil.mapToDocMap(toscaServiceTemplate.getDataTypes(), DocToscaDataType::new);
137
138         capabilityTypes = DocUtil.mapToDocMap(toscaServiceTemplate.getCapabilityTypes(), DocToscaCapabilityType::new);
139
140         relationshipTypes =
141                 DocUtil.mapToDocMap(toscaServiceTemplate.getRelationshipTypes(), DocToscaRelationshipType::new);
142
143         nodeTypes = DocUtil.mapToDocMap(toscaServiceTemplate.getNodeTypes(), DocToscaNodeType::new);
144
145         if (toscaServiceTemplate.getPolicyTypes() != null) {
146             policyTypes = DocUtil.mapToDocMap(toscaServiceTemplate.getPolicyTypes(), DocToscaPolicyType::new);
147         }
148
149         if (toscaServiceTemplate.getToscaTopologyTemplate() != null) {
150             toscaTopologyTemplate = new DocToscaTopologyTemplate(toscaServiceTemplate.getToscaTopologyTemplate());
151         }
152     }
153
154     @Override
155     public int compareTo(DocToscaEntity<ToscaServiceTemplate> otherConcept) {
156         int result = compareToWithoutEntities(otherConcept);
157         if (result != 0) {
158             return result;
159         }
160
161         final var other = (DocToscaServiceTemplate) otherConcept;
162
163         result = DocUtil.compareMaps(dataTypes, other.dataTypes);
164         if (result != 0) {
165             return result;
166         }
167
168         result = DocUtil.compareMaps(capabilityTypes, other.capabilityTypes);
169         if (result != 0) {
170             return result;
171         }
172
173         result = DocUtil.compareMaps(relationshipTypes, other.relationshipTypes);
174         if (result != 0) {
175             return result;
176         }
177
178         result = DocUtil.compareMaps(nodeTypes, other.nodeTypes);
179         if (result != 0) {
180             return result;
181         }
182
183         result = DocUtil.compareMaps(policyTypes, other.policyTypes);
184         if (result != 0) {
185             return result;
186         }
187
188         return ObjectUtils.compare(toscaTopologyTemplate, other.toscaTopologyTemplate);
189     }
190
191     /**
192      * Compare this service template to another service template, ignoring contained entitites.
193      *
194      * @param otherConcept the other topology template
195      * @return the result of the comparison
196      */
197     public int compareToWithoutEntities(final DocToscaEntity<ToscaServiceTemplate> otherConcept) {
198         if (otherConcept == null) {
199             return -1;
200         }
201         if (this == otherConcept) {
202             return 0;
203         }
204         if (getClass() != otherConcept.getClass()) {
205             return getClass().getName().compareTo(otherConcept.getClass().getName());
206         }
207
208         final var other = (DocToscaServiceTemplate) otherConcept;
209         if (!super.equals(other)) {
210             return super.compareTo(other);
211         }
212
213         return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion);
214     }
215 }