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