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