2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop.template;
26 import com.google.gson.annotations.Expose;
27 import java.io.Serializable;
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;
47 @Table(name = "loop_templates")
48 public class LoopTemplate extends AuditEntity implements Serializable {
51 * The serial version id.
53 private static final long serialVersionUID = -286522707701388642L;
57 @Column(nullable = false, name = "name", unique = true)
61 @Column(name = "dcae_blueprint_id")
62 private String dcaeBlueprintId;
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
69 @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
70 private String blueprint;
72 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
73 private String svgRepresentation;
77 cascade = CascadeType.ALL,
78 fetch = FetchType.EAGER,
79 mappedBy = "loopTemplate",
82 private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
86 fetch = FetchType.EAGER,
87 cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
88 @JoinColumn(name = "service_uuid")
89 private Service modelService;
92 @Column(name = "maximum_instances_allowed")
93 private Integer maximumInstancesAllowed;
96 @Column(name = "unique_blueprint", columnDefinition = "boolean default false")
97 private boolean uniqueBlueprint;
100 * Type of Loop allowed to be created.
103 @Column(name = "allowed_loop_type")
104 @Convert(converter = LoopTypeConvertor.class)
105 private LoopType allowedLoopType = LoopType.CLOSED;
112 public String getName() {
119 * @param name the name to set
121 public void setName(String name) {
128 * @return the blueprint
130 public String getBlueprint() {
135 * dcaeBlueprintId getter.
137 * @return the dcaeBlueprintId
139 public String getDcaeBlueprintId() {
140 return dcaeBlueprintId;
144 * dcaeBlueprintId setter.
146 * @param dcaeBlueprintId the dcaeBlueprintId to set
148 public void setDcaeBlueprintId(String dcaeBlueprintId) {
149 this.dcaeBlueprintId = dcaeBlueprintId;
155 * @param blueprint the blueprint to set
157 public void setBlueprint(String blueprint) {
158 this.blueprint = blueprint;
159 if (blueprint == null) {
160 this.uniqueBlueprint = false;
162 this.uniqueBlueprint = true;
167 * svgRepresentation getter.
169 * @return the svgRepresentation
171 public String getSvgRepresentation() {
172 return svgRepresentation;
176 * svgRepresentation setter.
178 * @param svgRepresentation the svgRepresentation to set
180 public void setSvgRepresentation(String svgRepresentation) {
181 this.svgRepresentation = svgRepresentation;
185 * loopElementModelsUsed getter.
187 * @return the loopElementModelsUsed
189 public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
190 return loopElementModelsUsed;
194 * maximumInstancesAllowed getter.
196 * @return the maximumInstancesAllowed
198 public Integer getMaximumInstancesAllowed() {
199 return maximumInstancesAllowed;
203 * maximumInstancesAllowed setter.
205 * @param maximumInstancesAllowed the maximumInstancesAllowed to set
207 public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
208 this.maximumInstancesAllowed = maximumInstancesAllowed;
212 * allowedLoopType getter.
214 * @return the allowedLoopType Type of Loop allowed to be created
216 public LoopType getAllowedLoopType() {
217 return allowedLoopType;
221 * allowedLoopType setter.
223 * @param allowedLoopType the allowedLoopType to set
225 public void setAllowedLoopType(LoopType allowedLoopType) {
226 this.allowedLoopType = allowedLoopType;
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.
233 * @param loopElementModels The loopElementModel set to add
235 public void addLoopElementModels(Set<LoopElementModel> loopElementModels) {
236 for (LoopElementModel loopElementModel : loopElementModels) {
237 addLoopElementModel(loopElementModel);
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.
245 * @param loopElementModel The loopElementModel to add
247 public void addLoopElementModel(LoopElementModel loopElementModel) {
248 this.addLoopElementModel(loopElementModel,this.loopElementModelsUsed.size());
252 * Add a loopElement model to the current template, the flow order must be
253 * specified manually.
255 * @param loopElementModel The loopElementModel to add
256 * @param listPosition The position in the flow
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));
267 * modelService getter.
269 * @return the modelService
271 public Service getModelService() {
276 * modelService setter.
278 * @param modelService the modelService to set
280 public void setModelService(Service modelService) {
281 this.modelService = modelService;
285 * uniqueBlueprint getter.
287 * @return the uniqueBlueprint
289 public boolean getUniqueBlueprint() {
290 return uniqueBlueprint;
294 * Default constructor for serialization.
296 public LoopTemplate() {
303 * @param name The loop template name id
304 * @param blueprint The blueprint containing all microservices (legacy
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
311 public LoopTemplate(String name, String blueprint, String svgRepresentation,
312 Integer maxInstancesAllowed, Service service) {
314 this.setBlueprint(blueprint);
315 this.svgRepresentation = svgRepresentation;
317 this.maximumInstancesAllowed = maxInstancesAllowed;
318 this.modelService = service;
322 public int hashCode() {
323 final int prime = 31;
325 result = prime * result + ((name == null) ? 0 : name.hashCode());
330 public boolean equals(Object obj) {
337 if (getClass() != obj.getClass()) {
340 LoopTemplate other = (LoopTemplate) obj;
342 if (other.name != null) {
345 } else if (!name.equals(other.name)) {
352 * Generate the loop template name.
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
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(" ", "");