3e0ec281bcf4f2a942f962a5922b224788026ac0
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaCapabilityAssignment.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020-2021 Nordix Foundation.
4  * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import javax.persistence.ElementCollection;
29 import javax.persistence.Entity;
30 import javax.persistence.Inheritance;
31 import javax.persistence.InheritanceType;
32 import javax.persistence.Lob;
33 import javax.persistence.Table;
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
39 import org.onap.policy.models.base.PfConcept;
40 import org.onap.policy.models.base.PfConceptKey;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.base.validation.annotations.PfMin;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
44
45 /**
46  * Class to represent the parameter in TOSCA definition.
47  */
48 @Entity
49 @Table(name = "ToscaCapabilityAssignment")
50 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
51 @Data
52 @EqualsAndHashCode(callSuper = false)
53 public class JpaToscaCapabilityAssignment extends JpaToscaWithTypeAndStringProperties<ToscaCapabilityAssignment> {
54
55     private static final long serialVersionUID = 1675770231921107988L;
56
57     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
58     private static final Integer JPA_UNBOUNDED_VALUE = -1;
59
60     private static final YamlJsonTranslator YAML_JSON_TRANSLATOR = new YamlJsonTranslator();
61
62     @ElementCollection
63     @Lob
64     private Map<@NotNull String, @NotNull String> attributes;
65
66     @ElementCollection
67     private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
68
69     /**
70      * The Default Constructor creates a {@link JpaToscaCapabilityAssignment} object with a null key.
71      */
72     public JpaToscaCapabilityAssignment() {
73         this(new PfConceptKey());
74     }
75
76     /**
77      * The Key Constructor creates a {@link JpaToscaCapabilityAssignment} object with the given concept key.
78      *
79      * @param key the key
80      */
81     public JpaToscaCapabilityAssignment(@NonNull final PfConceptKey key) {
82         super(key);
83     }
84
85     /**
86      * Copy constructor.
87      *
88      * @param copyConcept the concept to copy from
89      */
90     public JpaToscaCapabilityAssignment(final JpaToscaCapabilityAssignment copyConcept) {
91         super(copyConcept);
92         this.attributes = copyConcept.attributes == null ? null : new LinkedHashMap<>(copyConcept.attributes);
93         this.occurrences = copyConcept.occurrences == null ? null : new ArrayList<>(copyConcept.occurrences);
94     }
95
96     /**
97      * Authorative constructor.
98      *
99      * @param authorativeConcept the authorative concept to copy from
100      */
101     public JpaToscaCapabilityAssignment(@NonNull final ToscaCapabilityAssignment authorativeConcept) {
102         super(authorativeConcept);
103     }
104
105     @Override
106     public ToscaCapabilityAssignment toAuthorative() {
107         var toscaCapabilityAssignment = new ToscaCapabilityAssignment();
108         super.setToscaEntity(toscaCapabilityAssignment);
109         super.toAuthorative();
110
111         toscaCapabilityAssignment.setAttributes(
112                 PfUtils.mapMap(attributes, attribute -> YAML_JSON_TRANSLATOR.fromYaml(attribute, Object.class)));
113
114         toscaCapabilityAssignment.setOccurrences(PfUtils.mapList(occurrences, occurrence -> {
115             if (occurrence.equals(JPA_UNBOUNDED_VALUE)) {
116                 return AUTHORATIVE_UNBOUNDED_LITERAL;
117             } else {
118                 return occurrence;
119             }
120         }));
121
122         return toscaCapabilityAssignment;
123     }
124
125     @Override
126     public void fromAuthorative(ToscaCapabilityAssignment toscaCapabilityAssignment) {
127         super.fromAuthorative(toscaCapabilityAssignment);
128
129         attributes = PfUtils.mapMap(toscaCapabilityAssignment.getAttributes(), YAML_JSON_TRANSLATOR::toYaml);
130
131         occurrences = PfUtils.mapList(toscaCapabilityAssignment.getOccurrences(), occurrence -> {
132             if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
133                 return JPA_UNBOUNDED_VALUE;
134             } else {
135                 return ((Number) occurrence).intValue();
136             }
137         });
138     }
139
140     @Override
141     protected Object deserializePropertyValue(String propValue) {
142         return YAML_JSON_TRANSLATOR.fromYaml(propValue, Object.class);
143     }
144
145     @Override
146     protected String serializePropertyValue(Object propValue) {
147         return YAML_JSON_TRANSLATOR.toYaml(propValue);
148     }
149
150     @Override
151     public void clean() {
152         super.clean();
153
154         attributes = PfUtils.mapMap(attributes, String::trim);
155     }
156
157     @Override
158     public int compareTo(final PfConcept otherConcept) {
159         if (this == otherConcept) {
160             return 0;
161         }
162
163         int result = super.compareTo(otherConcept);
164         if (result != 0) {
165             return result;
166         }
167
168         final JpaToscaCapabilityAssignment other = (JpaToscaCapabilityAssignment) otherConcept;
169
170         result = PfUtils.compareMaps(attributes, other.attributes);
171         if (result != 0) {
172             return result;
173         }
174
175         return PfUtils.compareCollections(occurrences, other.occurrences);
176     }
177 }