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