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