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