552d9c15c6f28e82df212d13f103cff950cb77c4
[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  * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.tosca.simple.concepts;
24
25 import java.util.List;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import lombok.Data;
29 import lombok.EqualsAndHashCode;
30 import lombok.NonNull;
31 import org.apache.commons.lang3.StringUtils;
32 import org.onap.policy.common.parameters.annotations.NotNull;
33 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
34 import org.onap.policy.models.base.PfAuthorative;
35 import org.onap.policy.models.base.PfConcept;
36 import org.onap.policy.models.base.PfConceptKey;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.base.validation.annotations.VerifyKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaParameter;
41
42 /**
43  * Class to represent the parameter in TOSCA definition.
44  */
45 @Data
46 @EqualsAndHashCode(callSuper = false)
47 public class JpaToscaParameter extends PfConcept implements PfAuthorative<ToscaParameter> {
48     private static final long serialVersionUID = 1675770231921107988L;
49
50     @EmbeddedId
51     @VerifyKey
52     @NotNull
53     private PfReferenceKey key;
54
55     @Column
56     @VerifyKey
57     @NotNull
58     private PfConceptKey type;
59
60     @Column
61     private String value;
62
63     /**
64      * The Default Constructor creates a {@link JpaToscaParameter} object with a null key.
65      */
66     public JpaToscaParameter() {
67         this(new PfReferenceKey());
68     }
69
70     /**
71      * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
72      *
73      * @param key the key
74      */
75     public JpaToscaParameter(@NonNull final PfReferenceKey key) {
76         this(key, new PfConceptKey());
77     }
78
79     /**
80      * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
81      *
82      * @param key the key
83      * @param type the key of the parameter type
84      */
85     public JpaToscaParameter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
86         this.key = key;
87         this.type = type;
88     }
89
90     /**
91      * Copy constructor.
92      *
93      * @param copyConcept the concept to copy from
94      */
95     public JpaToscaParameter(final JpaToscaParameter copyConcept) {
96         super(copyConcept);
97         this.key = new PfReferenceKey(copyConcept.key);
98         this.type = new PfConceptKey(copyConcept.type);
99         this.value = copyConcept.value;
100     }
101
102     /**
103      * Authorative constructor.
104      *
105      * @param authorativeConcept the authorative concept to copy from
106      */
107     public JpaToscaParameter(final ToscaParameter authorativeConcept) {
108         this.fromAuthorative(authorativeConcept);
109     }
110
111     @Override
112     public ToscaParameter toAuthorative() {
113         var toscaParameter = new ToscaParameter();
114
115         toscaParameter.setName(key.getLocalName());
116
117         toscaParameter.setType(type.getName());
118         toscaParameter.setTypeVersion(type.getVersion());
119
120         if (!StringUtils.isBlank(value)) {
121             toscaParameter.setValue(new YamlJsonTranslator().fromYaml(value, Object.class));
122         }
123
124         return toscaParameter;
125     }
126
127     @Override
128     public void fromAuthorative(ToscaParameter toscaParameter) {
129         this.setKey(new PfReferenceKey());
130         getKey().setLocalName(toscaParameter.getName());
131
132         if (toscaParameter.getTypeVersion() != null) {
133             type = new PfConceptKey(toscaParameter.getType(), toscaParameter.getTypeVersion());
134         } else {
135             type = new PfConceptKey(toscaParameter.getType(), PfKey.NULL_KEY_VERSION);
136         }
137
138         value = new YamlJsonTranslator().toYaml(toscaParameter.getValue());
139     }
140
141     @Override
142     public List<PfKey> getKeys() {
143         final List<PfKey> keyList = getKey().getKeys();
144
145         keyList.addAll(type.getKeys());
146
147         return keyList;
148     }
149
150     @Override
151     public void clean() {
152         key.clean();
153
154         type.clean();
155
156         if (value != null) {
157             value = value.trim();
158         }
159     }
160
161     @Override
162     public int compareTo(final PfConcept otherConcept) {
163         if (otherConcept == null) {
164             return -1;
165         }
166         if (this == otherConcept) {
167             return 0;
168         }
169         if (getClass() != otherConcept.getClass()) {
170             return getClass().getName().compareTo(otherConcept.getClass().getName());
171         }
172
173         final JpaToscaParameter other = (JpaToscaParameter) otherConcept;
174         int result = key.compareTo(other.key);
175         if (result != 0) {
176             return result;
177         }
178
179         return value.compareTo(other.value);
180     }
181 }