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.loop.common.AuditEntity;
43 import org.onap.clamp.loop.service.Service;
46 @Table(name = "loop_templates")
47 public class LoopTemplate extends AuditEntity implements Serializable {
50 * The serial version id.
52 private static final long serialVersionUID = -286522707701388642L;
56 @Column(nullable = false, name = "name", unique = true)
60 @Column(name = "dcae_blueprint_id")
61 private String dcaeBlueprintId;
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
68 @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
69 private String blueprint;
71 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
72 private String svgRepresentation;
76 cascade = CascadeType.ALL,
77 fetch = FetchType.EAGER,
78 mappedBy = "loopTemplate",
81 private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
85 fetch = FetchType.EAGER,
86 cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
87 @JoinColumn(name = "service_uuid")
88 private Service modelService;
91 @Column(name = "maximum_instances_allowed")
92 private Integer maximumInstancesAllowed;
95 @Column(name = "unique_blueprint", columnDefinition = "boolean default false")
96 private boolean uniqueBlueprint;
99 * Type of Loop allowed to be created.
102 @Column(name = "allowed_loop_type")
103 @Convert(converter = LoopTypeConvertor.class)
104 private LoopType allowedLoopType = LoopType.CLOSED;
111 public String getName() {
118 * @param name the name to set
120 public void setName(String name) {
127 * @return the blueprint
129 public String getBlueprint() {
134 * dcaeBlueprintId getter.
136 * @return the dcaeBlueprintId
138 public String getDcaeBlueprintId() {
139 return dcaeBlueprintId;
143 * dcaeBlueprintId setter.
145 * @param dcaeBlueprintId the dcaeBlueprintId to set
147 public void setDcaeBlueprintId(String dcaeBlueprintId) {
148 this.dcaeBlueprintId = dcaeBlueprintId;
154 * @param blueprint the blueprint to set
156 public void setBlueprint(String blueprint) {
157 this.blueprint = blueprint;
158 if (blueprint == null) {
159 this.uniqueBlueprint = false;
161 this.uniqueBlueprint = true;
166 * svgRepresentation getter.
168 * @return the svgRepresentation
170 public String getSvgRepresentation() {
171 return svgRepresentation;
175 * svgRepresentation setter.
177 * @param svgRepresentation the svgRepresentation to set
179 public void setSvgRepresentation(String svgRepresentation) {
180 this.svgRepresentation = svgRepresentation;
184 * loopElementModelsUsed getter.
186 * @return the loopElementModelsUsed
188 public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
189 return loopElementModelsUsed;
193 * maximumInstancesAllowed getter.
195 * @return the maximumInstancesAllowed
197 public Integer getMaximumInstancesAllowed() {
198 return maximumInstancesAllowed;
202 * maximumInstancesAllowed setter.
204 * @param maximumInstancesAllowed the maximumInstancesAllowed to set
206 public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
207 this.maximumInstancesAllowed = maximumInstancesAllowed;
211 * allowedLoopType getter.
213 * @return the allowedLoopType Type of Loop allowed to be created
215 public LoopType getAllowedLoopType() {
216 return allowedLoopType;
220 * allowedLoopType setter.
222 * @param allowedLoopType the allowedLoopType to set
224 public void setAllowedLoopType(LoopType allowedLoopType) {
225 this.allowedLoopType = allowedLoopType;
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.
232 * @param loopElementModels The loopElementModel set to add
234 public void addLoopElementModels(Set<LoopElementModel> loopElementModels) {
235 for (LoopElementModel loopElementModel : loopElementModels) {
236 addLoopElementModel(loopElementModel);
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.
244 * @param loopElementModel The loopElementModel to add
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);
254 * Add a loopElement model to the current template, the flow order must be
255 * specified manually.
257 * @param loopElementModel The loopElementModel to add
258 * @param listPosition The position in the flow
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);
268 * modelService getter.
270 * @return the modelService
272 public Service getModelService() {
277 * modelService setter.
279 * @param modelService the modelService to set
281 public void setModelService(Service modelService) {
282 this.modelService = modelService;
286 * uniqueBlueprint getter.
288 * @return the uniqueBlueprint
290 public boolean getUniqueBlueprint() {
291 return uniqueBlueprint;
295 * Default constructor for serialization.
297 public LoopTemplate() {
304 * @param name The loop template name id
305 * @param blueprint The blueprint containing all microservices (legacy
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
312 public LoopTemplate(String name, String blueprint, String svgRepresentation,
313 Integer maxInstancesAllowed, Service service) {
315 this.setBlueprint(blueprint);
316 this.svgRepresentation = svgRepresentation;
318 this.maximumInstancesAllowed = maxInstancesAllowed;
319 this.modelService = service;
323 public int hashCode() {
324 final int prime = 31;
326 result = prime * result + ((name == null) ? 0 : name.hashCode());
331 public boolean equals(Object obj) {
338 if (getClass() != obj.getClass()) {
341 LoopTemplate other = (LoopTemplate) obj;
343 if (other.name != null) {
346 } else if (!name.equals(other.name)) {
353 * Generate the loop template name.
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
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(" ", "");