Merge "Allow storing database password in environment variables"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTopologyTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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
26 import javax.persistence.CascadeType;
27 import javax.persistence.Column;
28 import javax.persistence.EmbeddedId;
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
38 import lombok.Data;
39 import lombok.EqualsAndHashCode;
40 import lombok.NonNull;
41
42 import org.apache.commons.lang3.ObjectUtils;
43 import org.onap.policy.models.base.PfAuthorative;
44 import org.onap.policy.models.base.PfConcept;
45 import org.onap.policy.models.base.PfKey;
46 import org.onap.policy.models.base.PfReferenceKey;
47 import org.onap.policy.models.base.PfValidationMessage;
48 import org.onap.policy.models.base.PfValidationResult;
49 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
51
52 /**
53  * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
54  * implemented.
55  *
56  * @author Liam Fallon (liam.fallon@est.tech)
57  */
58 @Entity
59 @Table(name = "ToscaTopologyTemplate")
60 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
61 @Data
62 @EqualsAndHashCode(callSuper = false)
63 public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative<ToscaTopologyTemplate> {
64     private static final long serialVersionUID = 8969698734673232603L;
65
66     public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
67
68     @EmbeddedId
69     private PfReferenceKey key;
70
71     @Column(name = "description")
72     private String description;
73
74     // @formatter:off
75     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
76     @JoinColumns(
77             {
78                 @JoinColumn(name = "policyName",    referencedColumnName = "name"),
79                 @JoinColumn(name = "policyVersion", referencedColumnName = "version")
80             }
81         )
82     // @formatter:on
83     private JpaToscaPolicies policies;
84
85     /**
86      * The Default Constructor creates a {@link JpaToscaTopologyTemplate} object with a null key.
87      */
88     public JpaToscaTopologyTemplate() {
89         this(new PfReferenceKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION,
90                 DEFAULT_LOCAL_NAME));
91     }
92
93     /**
94      * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key.
95      *
96      * @param key the key
97      */
98     public JpaToscaTopologyTemplate(@NonNull final PfReferenceKey key) {
99         this.key = key;
100     }
101
102     /**
103      * Copy constructor.
104      *
105      * @param copyConcept the concept to copy from
106      */
107     public JpaToscaTopologyTemplate(final JpaToscaTopologyTemplate copyConcept) {
108         super(copyConcept);
109         this.key = new PfReferenceKey(copyConcept.key);
110         this.description = copyConcept.description;
111         this.policies = (copyConcept.policies != null ? new JpaToscaPolicies(copyConcept.policies) : null);
112     }
113
114     /**
115      * Authorative constructor.
116      *
117      * @param authorativeConcept the authorative concept to copy from
118      */
119     public JpaToscaTopologyTemplate(final ToscaTopologyTemplate authorativeConcept) {
120         this.fromAuthorative(authorativeConcept);
121     }
122
123     @Override
124     public ToscaTopologyTemplate toAuthorative() {
125         final ToscaTopologyTemplate toscaTopologyTemplate = new ToscaTopologyTemplate();
126
127         toscaTopologyTemplate.setDescription(description);
128
129         if (policies != null) {
130             toscaTopologyTemplate.setPolicies(policies.toAuthorative());
131         }
132
133         return toscaTopologyTemplate;
134     }
135
136     @Override
137     public void fromAuthorative(ToscaTopologyTemplate toscaTopologyTemplate) {
138         description = toscaTopologyTemplate.getDescription();
139
140         if (toscaTopologyTemplate.getPolicies() != null) {
141             policies = new JpaToscaPolicies();
142             policies.fromAuthorative(toscaTopologyTemplate.getPolicies());
143         }
144     }
145
146     @Override
147     public List<PfKey> getKeys() {
148         final List<PfKey> keyList = getKey().getKeys();
149
150         if (policies != null) {
151             keyList.addAll(policies.getKeys());
152         }
153
154         return keyList;
155     }
156
157     @Override
158     public void clean() {
159         key.clean();
160
161         description = (description != null ? description.trim() : null);
162
163         if (policies != null) {
164             policies.clean();
165         }
166     }
167
168     @Override
169     public PfValidationResult validate(PfValidationResult resultIn) {
170         PfValidationResult result = resultIn;
171
172         if (key.isNullKey()) {
173             result.addValidationMessage(
174                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
175         }
176
177         result = key.validate(result);
178
179         if (description != null && description.trim().length() == 0) {
180             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
181                     "property description may not be blank"));
182         }
183
184         if (policies != null) {
185             result = policies.validate(result);
186         }
187
188         return result;
189     }
190
191     @Override
192     public int compareTo(final PfConcept otherConcept) {
193         int result = compareToWithoutEntities(otherConcept);
194         if (result != 0) {
195             return result;
196         }
197
198         return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies);
199     }
200
201     /**
202      * Compare this topology template to another topology template, ignoring contained entitites.
203      *
204      * @param otherConcept the other topology template
205      * @return the result of the comparison
206      */
207     public int compareToWithoutEntities(final PfConcept otherConcept) {
208         if (otherConcept == null) {
209             return -1;
210         }
211         if (this == otherConcept) {
212             return 0;
213         }
214         if (getClass() != otherConcept.getClass()) {
215             return getClass().getName().compareTo(otherConcept.getClass().getName());
216         }
217
218         final JpaToscaTopologyTemplate other = (JpaToscaTopologyTemplate) otherConcept;
219         if (!key.equals(other.key)) {
220             return key.compareTo(other.key);
221         }
222
223         return ObjectUtils.compare(description, other.description);
224     }
225 }