JPA concepts for TOSCA
[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  * ================================================================================
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.models.tosca.simple.concepts;
22
23 import java.util.List;
24 import javax.persistence.Column;
25 import javax.persistence.EmbeddedId;
26 import javax.persistence.Entity;
27 import javax.persistence.Inheritance;
28 import javax.persistence.InheritanceType;
29 import javax.persistence.Table;
30 import lombok.Data;
31 import lombok.EqualsAndHashCode;
32 import lombok.NonNull;
33 import org.apache.commons.lang3.StringUtils;
34 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
35 import org.onap.policy.models.base.PfAuthorative;
36 import org.onap.policy.models.base.PfConcept;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.base.PfValidationMessage;
41 import org.onap.policy.models.base.PfValidationResult;
42 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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     private PfReferenceKey key;
58
59     @Column
60     private PfConceptKey type;
61
62     @Column
63     private String value;
64
65     /**
66      * The Default Constructor creates a {@link JpaToscaParameter} object with a null key.
67      */
68     public JpaToscaParameter() {
69         this(new PfReferenceKey());
70     }
71
72     /**
73      * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
74      *
75      * @param key the key
76      */
77     public JpaToscaParameter(@NonNull final PfReferenceKey key) {
78         this(key, new PfConceptKey());
79     }
80
81     /**
82      * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
83      *
84      * @param key the key
85      * @param type the key of the parameter type
86      */
87     public JpaToscaParameter(@NonNull final PfReferenceKey key, @NonNull final PfConceptKey type) {
88         this.key = key;
89         this.type = type;
90     }
91
92     /**
93      * Copy constructor.
94      *
95      * @param copyConcept the concept to copy from
96      */
97     public JpaToscaParameter(final JpaToscaParameter copyConcept) {
98         super(copyConcept);
99         this.key = new PfReferenceKey(copyConcept.key);
100         this.type = new PfConceptKey(copyConcept.type);
101         this.value = copyConcept.value;
102     }
103
104     /**
105      * Authorative constructor.
106      *
107      * @param authorativeConcept the authorative concept to copy from
108      */
109     public JpaToscaParameter(final ToscaParameter authorativeConcept) {
110         this.fromAuthorative(authorativeConcept);
111     }
112
113     @Override
114     public ToscaParameter toAuthorative() {
115         ToscaParameter toscaParameter = new ToscaParameter();
116
117         toscaParameter.setName(key.getLocalName());
118
119         toscaParameter.setType(type.getName());
120         toscaParameter.setTypeVersion(type.getVersion());
121
122         if (!StringUtils.isBlank(value)) {
123             toscaParameter.setValue(new YamlJsonTranslator().fromYaml(value, Object.class));
124         }
125
126         return toscaParameter;
127     }
128
129     @Override
130     public void fromAuthorative(ToscaParameter toscaParameter) {
131         this.setKey(new PfReferenceKey());
132         getKey().setLocalName(toscaParameter.getName());
133
134         if (toscaParameter.getTypeVersion() != null) {
135             type = new PfConceptKey(toscaParameter.getType(), toscaParameter.getTypeVersion());
136         } else {
137             type = new PfConceptKey(toscaParameter.getType(), PfKey.NULL_KEY_VERSION);
138         }
139
140         value = new YamlJsonTranslator().toYaml(toscaParameter.getValue());
141     }
142
143     @Override
144     public List<PfKey> getKeys() {
145         final List<PfKey> keyList = getKey().getKeys();
146
147         keyList.addAll(type.getKeys());
148
149         return keyList;
150     }
151
152     @Override
153     public void clean() {
154         key.clean();
155
156         type.clean();
157
158         if (value != null) {
159             value = value.trim();
160         }
161     }
162
163     @Override
164     public PfValidationResult validate(final PfValidationResult resultIn) {
165         PfValidationResult result = resultIn;
166
167         if (key.isNullKey()) {
168             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
169                     "paremeter key is a null key"));
170         }
171
172         result = key.validate(result);
173
174         if (type == null || type.isNullKey()) {
175             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
176                     "parameter type may not be null"));
177         }
178
179         return result;
180     }
181
182     @Override
183     public int compareTo(final PfConcept otherConcept) {
184         if (otherConcept == null) {
185             return -1;
186         }
187         if (this == otherConcept) {
188             return 0;
189         }
190         if (getClass() != otherConcept.getClass()) {
191             return getClass().getName().compareTo(otherConcept.getClass().getName());
192         }
193
194         final JpaToscaParameter other = (JpaToscaParameter) otherConcept;
195         int result = key.compareTo(other.key);
196         if (result != 0) {
197             return result;
198         }
199
200         return value.compareTo(other.value);
201     }
202 }