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