9507a9d2f0a6a7c0d7b5fe616ebc70d8eed49c1b
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaNodeTemplate.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.Collections;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import javax.persistence.CascadeType;
29 import javax.persistence.Column;
30 import javax.persistence.Entity;
31 import javax.persistence.FetchType;
32 import javax.persistence.Inheritance;
33 import javax.persistence.InheritanceType;
34 import javax.persistence.JoinColumn;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.ws.rs.core.Response;
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41 import org.apache.commons.lang3.ObjectUtils;
42 import org.onap.policy.common.parameters.annotations.NotBlank;
43 import org.onap.policy.common.parameters.annotations.NotNull;
44 import org.onap.policy.common.parameters.annotations.Valid;
45 import org.onap.policy.common.utils.coder.CoderException;
46 import org.onap.policy.common.utils.coder.StandardCoder;
47 import org.onap.policy.models.base.PfConcept;
48 import org.onap.policy.models.base.PfConceptKey;
49 import org.onap.policy.models.base.PfKey;
50 import org.onap.policy.models.base.PfModelRuntimeException;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
53
54 /**
55  * Class to represent the node template in TOSCA definition.
56  */
57 @Entity
58 @Table(name = "ToscaNodeTemplate")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @Data
61 @EqualsAndHashCode(callSuper = false)
62 public class JpaToscaNodeTemplate extends JpaToscaWithStringProperties<ToscaNodeTemplate> {
63     private static final long serialVersionUID = 1675770231921107988L;
64
65     private static final StandardCoder STANDARD_CODER = new StandardCoder();
66
67     @Column
68     @NotNull
69     @NotBlank
70     private String type;
71
72     // formatter:off
73     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
74     @JoinColumn(name = "requirementsName", referencedColumnName = "name")
75     @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
76     @Valid
77     private JpaToscaRequirements requirements;
78
79     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
80     @JoinColumn(name = "capabilitiesName", referencedColumnName = "name")
81     @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
82     @Valid
83     private JpaToscaCapabilityAssignments capabilities;
84     // @formatter:on
85
86     /**
87      * The Default Constructor creates a {@link JpaToscaNodeTemplate} object with a null key.
88      */
89     public JpaToscaNodeTemplate() {
90         this(new PfConceptKey());
91     }
92
93     /**
94      * The Key Constructor creates a {@link JpaToscaNodeTemplate} object with the given concept key.
95      *
96      * @param key the key
97      */
98     public JpaToscaNodeTemplate(@NonNull final PfConceptKey key) {
99         this(key, null);
100     }
101
102     /**
103      * Copy constructor.
104      *
105      * @param copyConcept the concept to copy from
106      */
107     public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
108         super(copyConcept);
109         this.type = copyConcept.type;
110         this.requirements =
111                 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
112         this.capabilities =
113                 (copyConcept.capabilities != null ? new JpaToscaCapabilityAssignments(copyConcept.capabilities) : null);
114     }
115
116     /**
117      * The Key Constructor creates a {@link JpaToscaParameter} object with the given concept key.
118      *
119      * @param key the key
120      * @param type the node template type
121      */
122     public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
123         super(key);
124         this.type = type;
125     }
126
127     /**
128      * Authorative constructor.
129      *
130      * @param authorativeConcept the authorative concept to copy from
131      */
132     public JpaToscaNodeTemplate(final ToscaNodeTemplate authorativeConcept) {
133         this.fromAuthorative(authorativeConcept);
134     }
135
136     @Override
137     public ToscaNodeTemplate toAuthorative() {
138         ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
139         super.setToscaEntity(toscaNodeTemplate);
140         super.toAuthorative();
141
142         toscaNodeTemplate.setType(type);
143
144         if (requirements != null) {
145             toscaNodeTemplate.setRequirements(requirements.toAuthorative());
146         }
147
148         if (capabilities != null) {
149             toscaNodeTemplate.setCapabilities(new LinkedHashMap<>());
150             List<Map<String, ToscaCapabilityAssignment>> capabilityAssignmentMapList = capabilities.toAuthorative();
151             for (Map<String, ToscaCapabilityAssignment> capabilityAssignmentMap : capabilityAssignmentMapList) {
152                 toscaNodeTemplate.getCapabilities().putAll(capabilityAssignmentMap);
153             }
154         }
155
156         return toscaNodeTemplate;
157     }
158
159     @Override
160     public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
161         super.fromAuthorative(toscaNodeTemplate);
162
163         type = toscaNodeTemplate.getType();
164
165         if (toscaNodeTemplate.getRequirements() != null) {
166             requirements = new JpaToscaRequirements();
167             requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
168         }
169
170         if (toscaNodeTemplate.getCapabilities() != null) {
171             capabilities = new JpaToscaCapabilityAssignments();
172             capabilities.fromAuthorative(Collections.singletonList(toscaNodeTemplate.getCapabilities()));
173         }
174     }
175
176     @Override
177     protected Object deserializePropertyValue(String propValue) {
178         try {
179             return STANDARD_CODER.decode(propValue, Object.class);
180         } catch (CoderException ce) {
181             String errorMessage = "error decoding property JSON value read from database: " + propValue;
182             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
183         }
184     }
185
186     @Override
187     protected String serializePropertyValue(Object propValue) {
188         try {
189             return STANDARD_CODER.encode(propValue);
190         } catch (CoderException ce) {
191             String errorMessage = "error encoding property JSON value for database: " + propValue;
192             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
193         }
194     }
195
196     @Override
197     public List<PfKey> getKeys() {
198         final List<PfKey> keyList = super.getKeys();
199
200         if (requirements != null) {
201             keyList.addAll(requirements.getKeys());
202         }
203
204         if (capabilities != null) {
205             keyList.addAll(capabilities.getKeys());
206         }
207
208         return keyList;
209     }
210
211     @Override
212     public void clean() {
213         super.clean();
214
215         type = type.trim();
216
217         if (requirements != null) {
218             requirements.clean();
219         }
220
221         if (capabilities != null) {
222             capabilities.clean();
223         }
224     }
225
226     @Override
227     public int compareTo(final PfConcept otherConcept) {
228         if (this == otherConcept) {
229             return 0;
230         }
231
232         int result = super.compareTo(otherConcept);
233         if (result != 0) {
234             return result;
235         }
236
237         final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
238
239         result = type.compareTo(other.type);
240         if (result != 0) {
241             return result;
242         }
243
244         result = ObjectUtils.compare(requirements, other.requirements);
245         if (result != 0) {
246             return result;
247         }
248
249         return ObjectUtils.compare(capabilities, other.capabilities);
250     }
251 }