f2cf14cb7adbecb965cc5bc690e4e43c6b39b949
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaRequirement.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Requirement Model
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.tosca.simple.concepts;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import javax.persistence.Column;
29 import javax.persistence.ElementCollection;
30 import javax.persistence.Entity;
31 import javax.persistence.Inheritance;
32 import javax.persistence.InheritanceType;
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.ToscaRequirement;
44
45 /**
46  * Class to represent the requirement in TOSCA definition.
47  *
48  * @author Chenfei Gao (cgao@research.att.com)
49  * @author Liam Fallon (liam.fallon@est.tech)
50  */
51 @Entity
52 @Table(name = "ToscaRequirement")
53 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
54 @Data
55 @EqualsAndHashCode(callSuper = true)
56 public class JpaToscaRequirement extends JpaToscaWithTypeAndStringProperties<ToscaRequirement> {
57
58     private static final long serialVersionUID = 2785481541573683089L;
59     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
60     private static final Integer JPA_UNBOUNDED_VALUE = -1;
61     private static final YamlJsonTranslator YAML_TRANSLATOR = new YamlJsonTranslator();
62
63     @Column
64     private String capability;
65
66     @Column
67     private String node;
68
69     @Column
70     private String relationship;
71
72     @ElementCollection
73     private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
74
75     /**
76      * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
77      */
78     public JpaToscaRequirement() {
79         this(new PfConceptKey());
80     }
81
82     /**
83      * The Key Constructor creates a {@link JpaToscaRequirement} object with the given concept key.
84      *
85      * @param key the key
86      */
87     public JpaToscaRequirement(@NonNull final PfConceptKey key) {
88         super(key);
89     }
90
91     /**
92      * Copy constructor.
93      *
94      * @param copyConcept the concept to copy from
95      */
96     public JpaToscaRequirement(@NonNull final JpaToscaRequirement copyConcept) {
97         super(copyConcept);
98         this.capability = copyConcept.capability;
99         this.node = copyConcept.node;
100         this.relationship = copyConcept.relationship;
101         this.occurrences = new ArrayList<>(copyConcept.occurrences);
102     }
103
104     /**
105      * Authorative constructor.
106      *
107      * @param authorativeConcept the authorative concept to copy from
108      */
109     public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
110         super(authorativeConcept);
111     }
112
113     @Override
114     public ToscaRequirement toAuthorative() {
115         var toscaRequirement = new ToscaRequirement();
116         super.setToscaEntity(toscaRequirement);
117         super.toAuthorative();
118
119         toscaRequirement.setCapability(capability);
120         toscaRequirement.setNode(node);
121         toscaRequirement.setRelationship(relationship);
122
123         if (occurrences != null) {
124             List<Object> occurrencesList = new ArrayList<>(occurrences);
125             for (Integer occurrence : occurrences) {
126                 if (JPA_UNBOUNDED_VALUE.equals(occurrence)) {
127                     occurrencesList.add(AUTHORATIVE_UNBOUNDED_LITERAL);
128                 } else {
129                     occurrencesList.add(occurrence);
130                 }
131             }
132             toscaRequirement.setOccurrences(occurrencesList);
133         }
134
135         return toscaRequirement;
136     }
137
138     @Override
139     public void fromAuthorative(@NonNull final ToscaRequirement toscaRequirement) {
140         super.fromAuthorative(toscaRequirement);
141
142         capability = toscaRequirement.getCapability();
143         node = toscaRequirement.getNode();
144         relationship = toscaRequirement.getRelationship();
145
146         if (toscaRequirement.getOccurrences() != null) {
147             occurrences = new ArrayList<>();
148             for (Object occurrence : toscaRequirement.getOccurrences()) {
149                 if (occurrence.equals(AUTHORATIVE_UNBOUNDED_LITERAL)) {
150                     occurrences.add(JPA_UNBOUNDED_VALUE);
151                 } else {
152                     occurrences.add(((Number) occurrence).intValue());
153                 }
154             }
155         }
156     }
157
158     @Override
159     protected Object deserializePropertyValue(String propValue) {
160         return YAML_TRANSLATOR.fromYaml(propValue, Object.class);
161     }
162
163     @Override
164     protected String serializePropertyValue(Object propValue) {
165         return YAML_TRANSLATOR.toYaml(propValue);
166     }
167
168     @Override
169     public void clean() {
170         super.clean();
171
172         capability = capability.trim();
173         node = node.trim();
174         relationship = relationship.trim();
175     }
176
177     @Override
178     public int compareTo(final PfConcept otherConcept) {
179         if (this == otherConcept) {
180             return 0;
181         }
182
183         int result = super.compareTo(otherConcept);
184         if (result != 0) {
185             return result;
186         }
187
188         final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
189
190         result = capability.compareTo(other.capability);
191         if (result != 0) {
192             return result;
193         }
194
195         result = node.compareTo(other.node);
196         if (result != 0) {
197             return result;
198         }
199
200         result = relationship.compareTo(other.relationship);
201         if (result != 0) {
202             return result;
203         }
204
205         return PfUtils.compareCollections(occurrences, other.occurrences);
206     }
207 }