Implement serialization/deserialization for TOSCA concepts
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / ToscaTopologyTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import java.util.List;
24
25 import javax.persistence.CascadeType;
26 import javax.persistence.Column;
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.OneToOne;
32 import javax.persistence.Table;
33
34 import lombok.Data;
35 import lombok.EqualsAndHashCode;
36 import lombok.NonNull;
37
38 import org.apache.commons.lang3.ObjectUtils;
39 import org.onap.policy.common.utils.validation.Assertions;
40 import org.onap.policy.models.base.PfConcept;
41 import org.onap.policy.models.base.PfKey;
42 import org.onap.policy.models.base.PfReferenceKey;
43 import org.onap.policy.models.base.PfValidationMessage;
44 import org.onap.policy.models.base.PfValidationResult;
45 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
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 ToscaTopologyTemplate extends PfConcept {
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(cascade = CascadeType.ALL)
70     private ToscaPolicies policies;
71
72     /**
73      * The Default Constructor creates a {@link ToscaTopologyTemplate} object with a null key.
74      */
75     public ToscaTopologyTemplate() {
76         this(new PfReferenceKey(ToscaServiceTemplate.DEFAULT_NAME, ToscaServiceTemplate.DEFAULT_VERSION,
77                 DEFAULT_LOCAL_NAME));
78     }
79
80     /**
81      * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept
82      * key.
83      *
84      * @param key the key
85      */
86     public ToscaTopologyTemplate(@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 ToscaTopologyTemplate(final ToscaTopologyTemplate copyConcept) {
96         super(copyConcept);
97     }
98
99     @Override
100     public List<PfKey> getKeys() {
101         final List<PfKey> keyList = getKey().getKeys();
102
103         if (policies != null) {
104             keyList.addAll(policies.getKeys());
105         }
106
107         return keyList;
108     }
109
110     @Override
111     public void clean() {
112         key.clean();
113
114         description = (description != null ? description.trim() : null);
115
116         if (policies != null) {
117             policies.clean();
118         }
119     }
120
121     @Override
122     public PfValidationResult validate(PfValidationResult resultIn) {
123         PfValidationResult result = resultIn;
124
125         if (key.isNullKey()) {
126             result.addValidationMessage(
127                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
128         }
129
130         result = key.validate(result);
131
132         if (description != null && description.trim().length() == 0) {
133             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
134                     "property description may not be blank"));
135         }
136
137         if (policies != null) {
138             result = policies.validate(result);
139         }
140
141         return result;
142     }
143
144     @Override
145     public int compareTo(final PfConcept otherConcept) {
146         if (otherConcept == null) {
147             return -1;
148         }
149         if (this == otherConcept) {
150             return 0;
151         }
152         if (getClass() != otherConcept.getClass()) {
153             return this.hashCode() - otherConcept.hashCode();
154         }
155
156         final ToscaTopologyTemplate other = (ToscaTopologyTemplate) otherConcept;
157         if (!key.equals(other.key)) {
158             return key.compareTo(other.key);
159         }
160
161         int result = ObjectUtils.compare(description, other.description);
162         if (result != 0) {
163             return result;
164         }
165
166         return ObjectUtils.compare(policies, other.policies);
167     }
168
169     @Override
170     public PfConcept copyTo(@NonNull PfConcept target) {
171         final Object copyObject = target;
172         Assertions.instanceOf(copyObject, PfConcept.class);
173
174         final ToscaTopologyTemplate copy = ((ToscaTopologyTemplate) copyObject);
175         copy.setKey(new PfReferenceKey(key));
176         copy.setDescription(description);
177         copy.setPolicies(policies != null ? new ToscaPolicies(policies) : null);
178
179         return copy;
180     }
181 }