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