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