bed773d0212f2a9a6b1d6f5e5af4d8980254585d
[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.fasterxml.jackson.annotation.JsonProperty;
24 import jakarta.validation.Valid;
25 import java.io.Serial;
26 import java.io.Serializable;
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import lombok.Data;
30 import lombok.EqualsAndHashCode;
31 import lombok.NoArgsConstructor;
32 import org.apache.commons.lang3.ObjectUtils;
33 import org.onap.policy.clamp.models.acm.document.base.DocUtil;
34 import org.onap.policy.models.base.PfAuthorative;
35 import org.onap.policy.models.base.PfUtils;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
37
38 @Data
39 @NoArgsConstructor
40 @EqualsAndHashCode
41 public class DocToscaTopologyTemplate
42         implements PfAuthorative<ToscaTopologyTemplate>, Serializable, Comparable<DocToscaTopologyTemplate> {
43
44     @Serial
45     private static final long serialVersionUID = 1L;
46
47     private String description;
48
49     private Map<String, @Valid DocToscaParameter> inputs;
50
51     @JsonProperty("node_templates")
52     private Map<String, @Valid DocToscaNodeTemplate> nodeTemplates;
53
54     private Map<String, @Valid DocToscaPolicy> policies;
55
56     /**
57      * Authorative constructor.
58      *
59      * @param authorativeConcept the authorative concept to copy from
60      */
61     public DocToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
62         this.fromAuthorative(authorativeConcept);
63     }
64
65     /**
66      * Copy constructor.
67      *
68      * @param copyConcept the concept to copy from
69      */
70     public DocToscaTopologyTemplate(final DocToscaTopologyTemplate copyConcept) {
71         this.description = copyConcept.description;
72         this.inputs = PfUtils.mapMap(copyConcept.inputs, DocToscaParameter::new);
73         this.nodeTemplates =
74                 PfUtils.mapMap(copyConcept.nodeTemplates, DocToscaNodeTemplate::new, new LinkedHashMap<>());
75         this.policies = PfUtils.mapMap(copyConcept.policies, DocToscaPolicy::new, new LinkedHashMap<>());
76     }
77
78     @Override
79     public ToscaTopologyTemplate toAuthorative() {
80         final var toscaTopologyTemplate = new ToscaTopologyTemplate();
81
82         toscaTopologyTemplate.setDescription(description);
83         toscaTopologyTemplate.setInputs(PfUtils.mapMap(inputs, DocToscaParameter::toAuthorative));
84         toscaTopologyTemplate.setNodeTemplates(DocUtil.docMapToMap(nodeTemplates, DocToscaNodeTemplate::toAuthorative));
85
86         toscaTopologyTemplate.setPolicies(DocUtil.docMapToList(policies, DocToscaPolicy::toAuthorative));
87
88         return toscaTopologyTemplate;
89     }
90
91     @Override
92     public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
93         description = toscaTopologyTemplate.getDescription();
94
95         if (toscaTopologyTemplate.getInputs() != null) {
96             inputs = PfUtils.mapMap(toscaTopologyTemplate.getInputs(), DocToscaParameter::new);
97             for (var entry : inputs.entrySet()) {
98                 if (entry.getValue().getName() == null) {
99                     entry.getValue().setName(entry.getKey());
100                 }
101             }
102         }
103
104         nodeTemplates = DocUtil.mapToDocMap(toscaTopologyTemplate.getNodeTemplates(), DocToscaNodeTemplate::new);
105
106         policies =
107                 DocUtil.listToDocMap(toscaTopologyTemplate.getPolicies(), DocToscaPolicy::new, new LinkedHashMap<>());
108     }
109
110     @Override
111     public int compareTo(DocToscaTopologyTemplate otherConcept) {
112         int result = compareToWithoutEntities(otherConcept);
113         if (result != 0) {
114             return result;
115         }
116
117         result = DocUtil.compareMaps(inputs, otherConcept.inputs);
118         if (result != 0) {
119             return result;
120         }
121
122         result = DocUtil.compareMaps(nodeTemplates, otherConcept.nodeTemplates);
123         if (result != 0) {
124             return result;
125         }
126         return DocUtil.compareMaps(policies, otherConcept.policies);
127     }
128
129     /**
130      * Compare this topology template to another topology template, ignoring contained entities.
131      *
132      * @param otherConcept the other topology template
133      * @return the result of the comparison
134      */
135     public int compareToWithoutEntities(final DocToscaTopologyTemplate otherConcept) {
136         if (otherConcept == null) {
137             return -1;
138         }
139         if (this == otherConcept) {
140             return 0;
141         }
142
143         return ObjectUtils.compare(description, otherConcept.description);
144     }
145 }