Merge "Change the Csar installer"
[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     @Expose
74     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
75     private String svgRepresentation;
76
77     @Expose
78     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loopTemplate", orphanRemoval = true)
79     @SortNatural
80     private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
81
82     @Expose
83     @ManyToOne(fetch = FetchType.EAGER, 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     /**
92      * name getter.
93      * 
94      * @return the name
95      */
96     public String getName() {
97         return name;
98     }
99
100     /**
101      * name setter.
102      * 
103      * @param name the name to set
104      */
105     public void setName(String name) {
106         this.name = name;
107     }
108
109     /**
110      * blueprint getter.
111      * 
112      * @return the blueprint
113      */
114     public String getBlueprint() {
115         return blueprint;
116     }
117
118     /**
119      * dcaeBlueprintId getter.
120      * 
121      * @return the dcaeBlueprintId
122      */
123     public String getDcaeBlueprintId() {
124         return dcaeBlueprintId;
125     }
126
127     /**
128      * dcaeBlueprintId setter.
129      * 
130      * @param dcaeBlueprintId the dcaeBlueprintId to set
131      */
132     public void setDcaeBlueprintId(String dcaeBlueprintId) {
133         this.dcaeBlueprintId = dcaeBlueprintId;
134     }
135
136     /**
137      * blueprint setter.
138      * 
139      * @param blueprint the blueprint to set
140      */
141     public void setBlueprint(String blueprint) {
142         this.blueprint = blueprint;
143     }
144
145     /**
146      * svgRepresentation getter.
147      * 
148      * @return the svgRepresentation
149      */
150     public String getSvgRepresentation() {
151         return svgRepresentation;
152     }
153
154     /**
155      * svgRepresentation setter.
156      * 
157      * @param svgRepresentation the svgRepresentation to set
158      */
159     public void setSvgRepresentation(String svgRepresentation) {
160         this.svgRepresentation = svgRepresentation;
161     }
162
163     /**
164      * loopElementModelsUsed getter.
165      * 
166      * @return the loopElementModelsUsed
167      */
168     public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
169         return loopElementModelsUsed;
170     }
171
172     /**
173      * maximumInstancesAllowed getter.
174      * 
175      * @return the maximumInstancesAllowed
176      */
177     public Integer getMaximumInstancesAllowed() {
178         return maximumInstancesAllowed;
179     }
180
181     /**
182      * maximumInstancesAllowed setter.
183      * 
184      * @param maximumInstancesAllowed the maximumInstancesAllowed to set
185      */
186     public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
187         this.maximumInstancesAllowed = maximumInstancesAllowed;
188     }
189
190     /**
191      * Add list of loopElements to the current template, each loopElementModel is
192      * added at the end of the list so the flowOrder is computed automatically.
193      * 
194      * @param loopElementModels The loopElementModel set to add
195      */
196     public void addLoopElementModels(Set<LoopElementModel> loopElementModels) {
197         for (LoopElementModel loopElementModel : loopElementModels) {
198             addLoopElementModel(loopElementModel);
199         }
200     }
201
202     /**
203      * Add a loopElement to the current template, the loopElementModel is added at
204      * the end of the list so the flowOrder is computed automatically.
205      * 
206      * @param loopElementModel The loopElementModel to add
207      */
208     public void addLoopElementModel(LoopElementModel loopElementModel) {
209         LoopTemplateLoopElementModel jointEntry = new LoopTemplateLoopElementModel(this, loopElementModel,
210                 this.loopElementModelsUsed.size());
211         this.loopElementModelsUsed.add(jointEntry);
212         loopElementModel.getUsedByLoopTemplates().add(jointEntry);
213     }
214
215     /**
216      * Add a loopElement model to the current template, the flow order must be
217      * specified manually.
218      * 
219      * @param loopElementModel The loopElementModel to add
220      * @param listPosition     The position in the flow
221      */
222     public void addLoopElementModel(LoopElementModel loopElementModel, Integer listPosition) {
223         LoopTemplateLoopElementModel jointEntry = new LoopTemplateLoopElementModel(this, loopElementModel,
224                 listPosition);
225         this.loopElementModelsUsed.add(jointEntry);
226         loopElementModel.getUsedByLoopTemplates().add(jointEntry);
227     }
228
229     /**
230      * modelService getter.
231      * 
232      * @return the modelService
233      */
234     public Service getModelService() {
235         return modelService;
236     }
237
238     /**
239      * modelService setter.
240      * 
241      * @param modelService the modelService to set
242      */
243     public void setModelService(Service modelService) {
244         this.modelService = modelService;
245     }
246
247     /**
248      * Default constructor for serialization.
249      */
250     public LoopTemplate() {
251
252     }
253
254     /**
255      * Constructor.
256      * 
257      * @param name                The loop template name id
258      * @param blueprint           The blueprint containing all microservices (legacy
259      *                            case)
260      * @param svgRepresentation   The svg representation of that loop template
261      * @param maxInstancesAllowed The maximum number of instances that can be
262      *                            created from that template
263      * @param service             The service associated to that loop template
264      */
265     public LoopTemplate(String name, String blueprint, String svgRepresentation, Integer maxInstancesAllowed,
266             Service service) {
267         this.name = name;
268         this.blueprint = blueprint;
269         this.svgRepresentation = svgRepresentation;
270
271         this.maximumInstancesAllowed = maxInstancesAllowed;
272         this.modelService = service;
273     }
274
275     @Override
276     public int hashCode() {
277         final int prime = 31;
278         int result = 1;
279         result = prime * result + ((name == null) ? 0 : name.hashCode());
280         return result;
281     }
282
283     @Override
284     public boolean equals(Object obj) {
285         if (this == obj) {
286             return true;
287         }
288         if (obj == null) {
289             return false;
290         }
291         if (getClass() != obj.getClass()) {
292             return false;
293         }
294         LoopTemplate other = (LoopTemplate) obj;
295         if (name == null) {
296             if (other.name != null) {
297                 return false;
298             }
299         } else if (!name.equals(other.name)) {
300             return false;
301         }
302         return true;
303     }
304
305     /**
306      * Generate the loop template name.
307      *
308      * @param serviceName       The service name
309      * @param serviceVersion    The service version
310      * @param resourceName      The resource name
311      * @param blueprintFileName The blueprint file name
312      * @return The generated loop template name
313      */
314     public static String generateLoopTemplateName(String serviceName, String serviceVersion, String resourceName,
315             String blueprintFilename) {
316         StringBuilder buffer = new StringBuilder("LOOP_TEMPLATE_").append(serviceName).append("_v")
317                 .append(serviceVersion).append("_").append(resourceName).append("_")
318                 .append(blueprintFilename.replaceAll(".yaml", ""));
319         return buffer.toString().replace('.', '_').replaceAll(" ", "");
320     }
321 }