a7bbd9dd35602d4750c0331a7367b2c7b59ba866
[clamp.git] / src / main / java / org / onap / clamp / loop / template / LoopTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop.template;
25
26 import com.google.gson.annotations.Expose;
27 import java.io.Serializable;
28 import java.util.Set;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
31 import javax.persistence.CascadeType;
32 import javax.persistence.Column;
33 import javax.persistence.Convert;
34 import javax.persistence.Entity;
35 import javax.persistence.FetchType;
36 import javax.persistence.Id;
37 import javax.persistence.JoinColumn;
38 import javax.persistence.ManyToOne;
39 import javax.persistence.OneToMany;
40 import javax.persistence.Table;
41 import org.hibernate.annotations.SortNatural;
42 import org.onap.clamp.clds.util.drawing.SvgLoopGenerator;
43 import org.onap.clamp.loop.common.AuditEntity;
44 import org.onap.clamp.loop.service.Service;
45
46 @Entity
47 @Table(name = "loop_templates")
48 public class LoopTemplate extends AuditEntity implements Serializable {
49
50     /**
51      * The serial version id.
52      */
53     private static final long serialVersionUID = -286522707701388642L;
54
55     @Id
56     @Expose
57     @Column(nullable = false, name = "name", unique = true)
58     private String name;
59
60     @Expose
61     @Column(name = "dcae_blueprint_id")
62     private String dcaeBlueprintId;
63
64     /**
65      * This field is used when we have a blueprint defining all microservices. The
66      * other option would be to have independent blueprint for each microservices.
67      * In that case they are stored in each MicroServiceModel
68      */
69     @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
70     private String blueprint;
71
72     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
73     private String svgRepresentation;
74
75     @Expose
76     @OneToMany(
77             cascade = CascadeType.ALL,
78             fetch = FetchType.EAGER,
79             mappedBy = "loopTemplate",
80             orphanRemoval = true)
81     @SortNatural
82     private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
83
84     @Expose
85     @ManyToOne(
86             fetch = FetchType.EAGER,
87             cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
88     @JoinColumn(name = "service_uuid")
89     private Service modelService;
90
91     @Expose
92     @Column(name = "maximum_instances_allowed")
93     private Integer maximumInstancesAllowed;
94
95     @Expose
96     @Column(name = "unique_blueprint", columnDefinition = "boolean default false")
97     private boolean uniqueBlueprint;
98
99     /**
100      * Type of Loop allowed to be created.
101      */
102     @Expose
103     @Column(name = "allowed_loop_type")
104     @Convert(converter = LoopTypeConvertor.class)
105     private LoopType allowedLoopType = LoopType.CLOSED;
106
107     /**
108      * name getter.
109      *
110      * @return the name
111      */
112     public String getName() {
113         return name;
114     }
115
116     /**
117      * name setter.
118      *
119      * @param name the name to set
120      */
121     public void setName(String name) {
122         this.name = name;
123     }
124
125     /**
126      * blueprint getter.
127      *
128      * @return the blueprint
129      */
130     public String getBlueprint() {
131         return blueprint;
132     }
133
134     /**
135      * dcaeBlueprintId getter.
136      *
137      * @return the dcaeBlueprintId
138      */
139     public String getDcaeBlueprintId() {
140         return dcaeBlueprintId;
141     }
142
143     /**
144      * dcaeBlueprintId setter.
145      *
146      * @param dcaeBlueprintId the dcaeBlueprintId to set
147      */
148     public void setDcaeBlueprintId(String dcaeBlueprintId) {
149         this.dcaeBlueprintId = dcaeBlueprintId;
150     }
151
152     /**
153      * blueprint setter.
154      *
155      * @param blueprint the blueprint to set
156      */
157     public void setBlueprint(String blueprint) {
158         this.blueprint = blueprint;
159         if (blueprint == null) {
160             this.uniqueBlueprint = false;
161         } else {
162             this.uniqueBlueprint = true;
163         }
164     }
165
166     /**
167      * svgRepresentation getter.
168      *
169      * @return the svgRepresentation
170      */
171     public String getSvgRepresentation() {
172         return svgRepresentation;
173     }
174
175     /**
176      * svgRepresentation setter.
177      *
178      * @param svgRepresentation the svgRepresentation to set
179      */
180     public void setSvgRepresentation(String svgRepresentation) {
181         this.svgRepresentation = svgRepresentation;
182     }
183
184     /**
185      * loopElementModelsUsed getter.
186      *
187      * @return the loopElementModelsUsed
188      */
189     public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
190         return loopElementModelsUsed;
191     }
192
193     /**
194      * maximumInstancesAllowed getter.
195      *
196      * @return the maximumInstancesAllowed
197      */
198     public Integer getMaximumInstancesAllowed() {
199         return maximumInstancesAllowed;
200     }
201
202     /**
203      * maximumInstancesAllowed setter.
204      *
205      * @param maximumInstancesAllowed the maximumInstancesAllowed to set
206      */
207     public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
208         this.maximumInstancesAllowed = maximumInstancesAllowed;
209     }
210
211     /**
212      * allowedLoopType getter.
213      *
214      * @return the allowedLoopType Type of Loop allowed to be created
215      */
216     public LoopType getAllowedLoopType() {
217         return allowedLoopType;
218     }
219
220     /**
221      * allowedLoopType setter.
222      *
223      * @param allowedLoopType the allowedLoopType to set
224      */
225     public void setAllowedLoopType(LoopType allowedLoopType) {
226         this.allowedLoopType = allowedLoopType;
227     }
228
229     /**
230      * Add list of loopElements to the current template, each loopElementModel is
231      * added at the end of the list so the flowOrder is computed automatically.
232      *
233      * @param loopElementModels The loopElementModel set to add
234      */
235     public void addLoopElementModels(Set<LoopElementModel> loopElementModels) {
236         for (LoopElementModel loopElementModel : loopElementModels) {
237             addLoopElementModel(loopElementModel);
238         }
239     }
240
241     /**
242      * Add a loopElement to the current template, the loopElementModel is added at
243      * the end of the list so the flowOrder is computed automatically.
244      *
245      * @param loopElementModel The loopElementModel to add
246      */
247     public void addLoopElementModel(LoopElementModel loopElementModel) {
248         this.addLoopElementModel(loopElementModel,this.loopElementModelsUsed.size());
249     }
250
251     /**
252      * Add a loopElement model to the current template, the flow order must be
253      * specified manually.
254      *
255      * @param loopElementModel The loopElementModel to add
256      * @param listPosition     The position in the flow
257      */
258     public void addLoopElementModel(LoopElementModel loopElementModel, Integer listPosition) {
259         LoopTemplateLoopElementModel jointEntry =
260                 new LoopTemplateLoopElementModel(this, loopElementModel, listPosition);
261         this.loopElementModelsUsed.add(jointEntry);
262         loopElementModel.getUsedByLoopTemplates().add(jointEntry);
263         this.setSvgRepresentation(SvgLoopGenerator.getSvgImage(this));
264     }
265
266     /**
267      * modelService getter.
268      *
269      * @return the modelService
270      */
271     public Service getModelService() {
272         return modelService;
273     }
274
275     /**
276      * modelService setter.
277      *
278      * @param modelService the modelService to set
279      */
280     public void setModelService(Service modelService) {
281         this.modelService = modelService;
282     }
283
284     /**
285      * uniqueBlueprint getter.
286      *
287      * @return the uniqueBlueprint
288      */
289     public boolean getUniqueBlueprint() {
290         return uniqueBlueprint;
291     }
292
293     /**
294      * Default constructor for serialization.
295      */
296     public LoopTemplate() {
297
298     }
299
300     /**
301      * Constructor.
302      *
303      * @param name                The loop template name id
304      * @param blueprint           The blueprint containing all microservices (legacy
305      *                            case)
306      * @param svgRepresentation   The svg representation of that loop template
307      * @param maxInstancesAllowed The maximum number of instances that can be
308      *                            created from that template
309      * @param service             The service associated to that loop template
310      */
311     public LoopTemplate(String name, String blueprint, String svgRepresentation,
312                         Integer maxInstancesAllowed, Service service) {
313         this.name = name;
314         this.setBlueprint(blueprint);
315         this.svgRepresentation = svgRepresentation;
316
317         this.maximumInstancesAllowed = maxInstancesAllowed;
318         this.modelService = service;
319     }
320
321     @Override
322     public int hashCode() {
323         final int prime = 31;
324         int result = 1;
325         result = prime * result + ((name == null) ? 0 : name.hashCode());
326         return result;
327     }
328
329     @Override
330     public boolean equals(Object obj) {
331         if (this == obj) {
332             return true;
333         }
334         if (obj == null) {
335             return false;
336         }
337         if (getClass() != obj.getClass()) {
338             return false;
339         }
340         LoopTemplate other = (LoopTemplate) obj;
341         if (name == null) {
342             if (other.name != null) {
343                 return false;
344             }
345         } else if (!name.equals(other.name)) {
346             return false;
347         }
348         return true;
349     }
350
351     /**
352      * Generate the loop template name.
353      *
354      * @param serviceName       The service name
355      * @param serviceVersion    The service version
356      * @param resourceName      The resource name
357      * @param blueprintFileName The blueprint file name
358      * @return The generated loop template name
359      */
360     public static String generateLoopTemplateName(String serviceName, String serviceVersion,
361                                                   String resourceName, String blueprintFileName) {
362         StringBuilder buffer = new StringBuilder("LOOP_TEMPLATE_").append(serviceName).append("_v")
363                 .append(serviceVersion).append("_").append(resourceName).append("_")
364                 .append(blueprintFileName.replaceAll(".yaml", ""));
365         return buffer.toString().replace('.', '_').replaceAll(" ", "");
366     }
367 }