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