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