1caf272202ad2b8a4764a0ab0fd5946f70cfb2f9
[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.JsonIgnore;
24 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import java.io.Serial;
27 import java.io.Serializable;
28 import java.util.List;
29 import lombok.Data;
30 import lombok.NoArgsConstructor;
31 import org.apache.commons.lang3.ObjectUtils;
32 import org.onap.policy.clamp.models.acm.document.base.DocConceptKey;
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.ToscaSchemaDefinition;
37
38 @Data
39 @NoArgsConstructor
40 @JsonIgnoreProperties({"typeDocConceptKey"})
41 public class DocToscaSchemaDefinition
42         implements PfAuthorative<ToscaSchemaDefinition>, Serializable, Comparable<DocToscaSchemaDefinition> {
43
44     @Serial
45     private static final long serialVersionUID = 1L;
46
47     private String name;
48     private String type;
49
50     @JsonProperty("type_version")
51     private String typeVersion;
52
53     private String description;
54     private List<DocToscaConstraint> constraints;
55
56     public DocToscaSchemaDefinition(ToscaSchemaDefinition toscaEntrySchema) {
57         fromAuthorative(toscaEntrySchema);
58     }
59
60     /**
61      * Copy constructor.
62      *
63      * @param copyConcept the concept to copy from
64      */
65     public DocToscaSchemaDefinition(final DocToscaSchemaDefinition copyConcept) {
66         this.name = copyConcept.name;
67         this.type = copyConcept.type;
68         this.typeVersion = copyConcept.typeVersion;
69         this.description = copyConcept.description;
70         this.constraints = PfUtils.mapList(copyConcept.constraints, DocToscaConstraint::new);
71     }
72
73     @Override
74     public ToscaSchemaDefinition toAuthorative() {
75         var toscaEntrySchema = new ToscaSchemaDefinition();
76
77         toscaEntrySchema.setName(name);
78         toscaEntrySchema.setType(type);
79         toscaEntrySchema.setTypeVersion(typeVersion);
80         toscaEntrySchema.setDescription(description);
81
82         if (constraints != null) {
83             toscaEntrySchema.setConstraints(PfUtils.mapList(constraints, DocToscaConstraint::toAuthorative));
84         }
85
86         return toscaEntrySchema;
87     }
88
89     @Override
90     public void fromAuthorative(ToscaSchemaDefinition toscaEntrySchema) {
91         name = toscaEntrySchema.getName();
92
93         var key = DocUtil.createDocConceptKey(toscaEntrySchema.getType(), toscaEntrySchema.getTypeVersion());
94         type = key.getName();
95         typeVersion = key.getVersion();
96
97         description = toscaEntrySchema.getDescription();
98
99         if (toscaEntrySchema.getConstraints() != null) {
100             constraints = PfUtils.mapList(toscaEntrySchema.getConstraints(), DocToscaConstraint::new);
101
102         }
103     }
104
105     @JsonIgnore
106     public DocConceptKey getTypeDocConceptKey() {
107         return new DocConceptKey(type, typeVersion);
108     }
109
110     @Override
111     public int compareTo(DocToscaSchemaDefinition other) {
112         if (other == null) {
113             return -1;
114         }
115         if (this == other) {
116             return 0;
117         }
118
119         int result = ObjectUtils.compare(description, other.description);
120         if (result != 0) {
121             return result;
122         }
123
124         return PfUtils.compareCollections(constraints, other.constraints);
125     }
126 }