Create SVG in UI
[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.loop.common.AuditEntity;
43 import org.onap.clamp.loop.service.Service;
44
45 @Entity
46 @Table(name = "loop_templates")
47 public class LoopTemplate extends AuditEntity implements Serializable {
48
49     /**
50      * The serial version id.
51      */
52     private static final long serialVersionUID = -286522707701388642L;
53
54     @Id
55     @Expose
56     @Column(nullable = false, name = "name", unique = true)
57     private String name;
58
59     @Expose
60     @Column(name = "dcae_blueprint_id")
61     private String dcaeBlueprintId;
62
63     /**
64      * This field is used when we have a blueprint defining all microservices. The
65      * other option would be to have independent blueprint for each microservices.
66      * In that case they are stored in each MicroServiceModel
67      */
68     @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
69     private String blueprint;
70
71     @Expose
72     @OneToMany(
73             cascade = CascadeType.ALL,
74             fetch = FetchType.EAGER,
75             mappedBy = "loopTemplate",
76             orphanRemoval = true)
77     @SortNatural
78     private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
79
80     @Expose
81     @ManyToOne(
82             fetch = FetchType.EAGER,
83             cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
84     @JoinColumn(name = "service_uuid")
85     private Service modelService;
86
87     @Expose
88     @Column(name = "maximum_instances_allowed")
89     private Integer maximumInstancesAllowed;
90
91     @Expose
92     @Column(name = "unique_blueprint", columnDefinition = "boolean default false")
93     private boolean uniqueBlueprint;
94
95     /**
96      * Type of Loop allowed to be created.
97      */
98     @Expose
99     @Column(name = "allowed_loop_type")
100     @Convert(converter = LoopTypeConvertor.class)
101     private LoopType allowedLoopType = LoopType.CLOSED;
102
103     /**
104      * name getter.
105      *
106      * @return the name
107      */
108     public String getName() {
109         return name;
110     }
111
112     /**
113      * name setter.
114      *
115      * @param name the name to set
116      */
117     public void setName(String name) {
118         this.name = name;
119     }
120
121     /**
122      * blueprint getter.
123      *
124      * @return the blueprint
125      */
126     public String getBlueprint() {
127         return blueprint;
128     }
129
130     /**
131      * dcaeBlueprintId getter.
132      *
133      * @return the dcaeBlueprintId
134      */
135     public String getDcaeBlueprintId() {
136         return dcaeBlueprintId;
137     }
138
139     /**
140      * dcaeBlueprintId setter.
141      *
142      * @param dcaeBlueprintId the dcaeBlueprintId to set
143      */
144     public void setDcaeBlueprintId(String dcaeBlueprintId) {
145         this.dcaeBlueprintId = dcaeBlueprintId;
146     }
147
148     /**
149      * blueprint setter.
150      *
151      * @param blueprint the blueprint to set
152      */
153     public void setBlueprint(String blueprint) {
154         this.blueprint = blueprint;
155         if (blueprint == null) {
156             this.uniqueBlueprint = false;
157         } else {
158             this.uniqueBlueprint = true;
159         }
160     }
161
162     /**
163      * loopElementModelsUsed getter.
164      *
165      * @return the loopElementModelsUsed
166      */
167     public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
168         return loopElementModelsUsed;
169     }
170
171     /**
172      * maximumInstancesAllowed getter.
173      *
174      * @return the maximumInstancesAllowed
175      */
176     public Integer getMaximumInstancesAllowed() {
177         return maximumInstancesAllowed;
178     }
179
180     /**
181      * maximumInstancesAllowed setter.
182      *
183      * @param maximumInstancesAllowed the maximumInstancesAllowed to set
184      */
185     public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
186         this.maximumInstancesAllowed = maximumInstancesAllowed;
187     }
188
189     /**
190      * allowedLoopType getter.
191      *
192      * @return the allowedLoopType Type of Loop allowed to be created
193      */
194     public LoopType getAllowedLoopType() {
195         return allowedLoopType;
196     }
197
198     /**
199      * allowedLoopType setter.
200      *
201      * @param allowedLoopType the allowedLoopType to set
202      */
203     public void setAllowedLoopType(LoopType allowedLoopType) {
204         this.allowedLoopType = allowedLoopType;
205     }
206
207     /**
208      * Add list of loopElements to the current template, each loopElementModel is
209      * added at the end of the list so the flowOrder is computed automatically.
210      *
211      * @param loopElementModels The loopElementModel set to add
212      */
213     public void addLoopElementModels(Set<LoopElementModel> loopElementModels) {
214         for (LoopElementModel loopElementModel : loopElementModels) {
215             addLoopElementModel(loopElementModel);
216         }
217     }
218
219     /**
220      * Add a loopElement to the current template, the loopElementModel is added at
221      * the end of the list so the flowOrder is computed automatically.
222      *
223      * @param loopElementModel The loopElementModel to add
224      */
225     public void addLoopElementModel(LoopElementModel loopElementModel) {
226         this.addLoopElementModel(loopElementModel,this.loopElementModelsUsed.size());
227     }
228
229     /**
230      * Add a loopElement model to the current template, the flow order must be
231      * specified manually.
232      *
233      * @param loopElementModel The loopElementModel to add
234      * @param listPosition     The position in the flow
235      */
236     public void addLoopElementModel(LoopElementModel loopElementModel, Integer listPosition) {
237         LoopTemplateLoopElementModel jointEntry =
238                 new LoopTemplateLoopElementModel(this, loopElementModel, listPosition);
239         this.loopElementModelsUsed.add(jointEntry);
240         loopElementModel.getUsedByLoopTemplates().add(jointEntry);
241     }
242
243     /**
244      * modelService getter.
245      *
246      * @return the modelService
247      */
248     public Service getModelService() {
249         return modelService;
250     }
251
252     /**
253      * modelService setter.
254      *
255      * @param modelService the modelService to set
256      */
257     public void setModelService(Service modelService) {
258         this.modelService = modelService;
259     }
260
261     /**
262      * uniqueBlueprint getter.
263      *
264      * @return the uniqueBlueprint
265      */
266     public boolean getUniqueBlueprint() {
267         return uniqueBlueprint;
268     }
269
270     /**
271      * Default constructor for serialization.
272      */
273     public LoopTemplate() {
274
275     }
276
277     /**
278      * Constructor.
279      *
280      * @param name                The loop template name id
281      * @param blueprint           The blueprint containing all microservices (legacy
282      *                            case)
283      * @param maxInstancesAllowed The maximum number of instances that can be
284      *                            created from that template
285      * @param service             The service associated to that loop template
286      */
287     public LoopTemplate(String name, String blueprint, Integer maxInstancesAllowed, Service service) {
288         this.name = name;
289         this.setBlueprint(blueprint);
290
291         this.maximumInstancesAllowed = maxInstancesAllowed;
292         this.modelService = service;
293     }
294
295     @Override
296     public int hashCode() {
297         final int prime = 31;
298         int result = 1;
299         result = prime * result + ((name == null) ? 0 : name.hashCode());
300         return result;
301     }
302
303     @Override
304     public boolean equals(Object obj) {
305         if (this == obj) {
306             return true;
307         }
308         if (obj == null) {
309             return false;
310         }
311         if (getClass() != obj.getClass()) {
312             return false;
313         }
314         LoopTemplate other = (LoopTemplate) obj;
315         if (name == null) {
316             if (other.name != null) {
317                 return false;
318             }
319         } else if (!name.equals(other.name)) {
320             return false;
321         }
322         return true;
323     }
324
325     /**
326      * Generate the loop template name.
327      *
328      * @param serviceName       The service name
329      * @param serviceVersion    The service version
330      * @param resourceName      The resource name
331      * @param blueprintFileName The blueprint file name
332      * @return The generated loop template name
333      */
334     public static String generateLoopTemplateName(String serviceName, String serviceVersion,
335                                                   String resourceName, String blueprintFileName) {
336         StringBuilder buffer = new StringBuilder("LOOP_TEMPLATE_").append(serviceName).append("_v")
337                 .append(serviceVersion).append("_").append(resourceName).append("_")
338                 .append(blueprintFileName.replaceAll(".yaml", ""));
339         return buffer.toString().replace('.', '_').replaceAll(" ", "");
340     }
341 }