Merge "Fix sonars in policy models"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaParameter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 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.List;
25 import javax.persistence.Column;
26 import javax.persistence.EmbeddedId;
27 import javax.persistence.Entity;
28 import javax.persistence.Inheritance;
29 import javax.persistence.InheritanceType;
30 import javax.persistence.Table;
31 import lombok.Data;
32 import lombok.EqualsAndHashCode;
33 import lombok.NonNull;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.policy.common.parameters.annotations.NotNull;
36 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
37 import org.onap.policy.models.base.PfAuthorative;
38 import org.onap.policy.models.base.PfConcept;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfReferenceKey;
42 import org.onap.policy.models.base.validation.annotations.VerifyKey;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
44
45 /**
46  * Class to represent the parameter in TOSCA definition.
47  */
48 @Entity
49 @Table(name = "ToscaParameter")
50 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
51 @Data
52 @EqualsAndHashCode(callSuper = false)
53 public class JpaToscaParameter extends PfConcept implements PfAuthorative<ToscaParameter> {
54     private static final long serialVersionUID = 1675770231921107988L;
55
56     @EmbeddedId
57     @VerifyKey
58     @NotNull
59     private PfReferenceKey key;
60
61     @Column
62     @VerifyKey
63     @NotNull
64     private PfConceptKey type;
65
66     @Column
67     private String value;
68
69     /**
70      * The Default Constructor creates a {@link JpaToscaParameter} object with a null key.
71      */
72     public JpaToscaParameter() {
73         this(new PfReferenceKey());
74     }
75
76     /**
77      * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
78      *
79      * @param key the key
80      */
81     public JpaToscaParameter(@NonNull final PfReferenceKey key) {
82         this(key, new PfConceptKey());
83     }
84
85     /**
86      * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
87      *
88      * @param key the key
89      * @param type the key of the parameter type
90      */
91     public JpaToscaParameter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
92         this.key = key;
93         this.type = type;
94     }
95
96     /**
97      * Copy constructor.
98      *
99      * @param copyConcept the concept to copy from
100      */
101     public JpaToscaParameter(final JpaToscaParameter copyConcept) {
102         super(copyConcept);
103         this.key = new PfReferenceKey(copyConcept.key);
104         this.type = new PfConceptKey(copyConcept.type);
105         this.value = copyConcept.value;
106     }
107
108     /**
109      * Authorative constructor.
110      *
111      * @param authorativeConcept the authorative concept to copy from
112      */
113     public JpaToscaParameter(final ToscaParameter authorativeConcept) {
114         this.fromAuthorative(authorativeConcept);
115     }
116
117     @Override
118     public ToscaParameter toAuthorative() {
119         var toscaParameter = new ToscaParameter();
120
121         toscaParameter.setName(key.getLocalName());
122
123         toscaParameter.setType(type.getName());
124         toscaParameter.setTypeVersion(type.getVersion());
125
126         if (!StringUtils.isBlank(value)) {
127             toscaParameter.setValue(new YamlJsonTranslator().fromYaml(value, Object.class));
128         }
129
130         return toscaParameter;
131     }
132
133     @Override
134     public void fromAuthorative(ToscaParameter toscaParameter) {
135         this.setKey(new PfReferenceKey());
136         getKey().setLocalName(toscaParameter.getName());
137
138         if (toscaParameter.getTypeVersion() != null) {
139             type = new PfConceptKey(toscaParameter.getType(), toscaParameter.getTypeVersion());
140         } else {
141             type = new PfConceptKey(toscaParameter.getType(), PfKey.NULL_KEY_VERSION);
142         }
143
144         value = new YamlJsonTranslator().toYaml(toscaParameter.getValue());
145     }
146
147     @Override
148     public List<PfKey> getKeys() {
149         final List<PfKey> keyList = getKey().getKeys();
150
151         keyList.addAll(type.getKeys());
152
153         return keyList;
154     }
155
156     @Override
157     public void clean() {
158         key.clean();
159
160         type.clean();
161
162         if (value != null) {
163             value = value.trim();
164         }
165     }
166
167     @Override
168     public int compareTo(final PfConcept otherConcept) {
169         if (otherConcept == null) {
170             return -1;
171         }
172         if (this == otherConcept) {
173             return 0;
174         }
175         if (getClass() != otherConcept.getClass()) {
176             return getClass().getName().compareTo(otherConcept.getClass().getName());
177         }
178
179         final JpaToscaParameter other = (JpaToscaParameter) otherConcept;
180         int result = key.compareTo(other.key);
181         if (result != 0) {
182             return result;
183         }
184
185         return value.compareTo(other.value);
186     }
187 }