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