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;
28 import java.io.Serializable;
30 import java.util.SortedSet;
31 import java.util.TreeSet;
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;
43 import org.hibernate.annotations.SortNatural;
44 import org.onap.clamp.loop.common.AuditEntity;
45 import org.onap.clamp.loop.service.Service;
48 @Table(name = "loop_templates")
49 public class LoopTemplate extends AuditEntity implements Serializable {
52 * The serial version id.
54 private static final long serialVersionUID = -286522707701388642L;
58 @Column(nullable = false, name = "name", unique = true)
62 @Column(name = "dcae_blueprint_id")
63 private String dcaeBlueprintId;
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
70 @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
71 private String blueprint;
73 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
74 private String svgRepresentation;
77 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loopTemplate", orphanRemoval = true)
79 private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
82 @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
83 @JoinColumn(name = "service_uuid")
84 private Service modelService;
87 @Column(name = "maximum_instances_allowed")
88 private Integer maximumInstancesAllowed;
91 @Column(name = "unique_blueprint", columnDefinition = "boolean default false")
92 private boolean uniqueBlueprint;
99 public String getName() {
106 * @param name the name to set
108 public void setName(String name) {
115 * @return the blueprint
117 public String getBlueprint() {
122 * dcaeBlueprintId getter.
124 * @return the dcaeBlueprintId
126 public String getDcaeBlueprintId() {
127 return dcaeBlueprintId;
131 * dcaeBlueprintId setter.
133 * @param dcaeBlueprintId the dcaeBlueprintId to set
135 public void setDcaeBlueprintId(String dcaeBlueprintId) {
136 this.dcaeBlueprintId = dcaeBlueprintId;
142 * @param blueprint the blueprint to set
144 public void setBlueprint(String blueprint) {
145 this.blueprint = blueprint;
146 if (blueprint == null) {
147 this.uniqueBlueprint = false;
149 this.uniqueBlueprint = true;
154 * svgRepresentation getter.
156 * @return the svgRepresentation
158 public String getSvgRepresentation() {
159 return svgRepresentation;
163 * svgRepresentation setter.
165 * @param svgRepresentation the svgRepresentation to set
167 public void setSvgRepresentation(String svgRepresentation) {
168 this.svgRepresentation = svgRepresentation;
172 * loopElementModelsUsed getter.
174 * @return the loopElementModelsUsed
176 public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
177 return loopElementModelsUsed;
181 * maximumInstancesAllowed getter.
183 * @return the maximumInstancesAllowed
185 public Integer getMaximumInstancesAllowed() {
186 return maximumInstancesAllowed;
190 * maximumInstancesAllowed setter.
192 * @param maximumInstancesAllowed the maximumInstancesAllowed to set
194 public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
195 this.maximumInstancesAllowed = maximumInstancesAllowed;
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.
202 * @param loopElementModels The loopElementModel set to add
204 public void addLoopElementModels(Set<LoopElementModel> loopElementModels) {
205 for (LoopElementModel loopElementModel : loopElementModels) {
206 addLoopElementModel(loopElementModel);
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.
214 * @param loopElementModel The loopElementModel to add
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);
224 * Add a loopElement model to the current template, the flow order must be
225 * specified manually.
227 * @param loopElementModel The loopElementModel to add
228 * @param listPosition The position in the flow
230 public void addLoopElementModel(LoopElementModel loopElementModel, Integer listPosition) {
231 LoopTemplateLoopElementModel jointEntry = new LoopTemplateLoopElementModel(this, loopElementModel,
233 this.loopElementModelsUsed.add(jointEntry);
234 loopElementModel.getUsedByLoopTemplates().add(jointEntry);
238 * modelService getter.
240 * @return the modelService
242 public Service getModelService() {
247 * modelService setter.
249 * @param modelService the modelService to set
251 public void setModelService(Service modelService) {
252 this.modelService = modelService;
256 * uniqueBlueprint getter.
258 * @return the uniqueBlueprint
260 public boolean getUniqueBlueprint() {
261 return uniqueBlueprint;
265 * Default constructor for serialization.
267 public LoopTemplate() {
274 * @param name The loop template name id
275 * @param blueprint The blueprint containing all microservices (legacy
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
282 public LoopTemplate(String name, String blueprint, String svgRepresentation, Integer maxInstancesAllowed,
285 this.setBlueprint(blueprint);
286 this.svgRepresentation = svgRepresentation;
288 this.maximumInstancesAllowed = maxInstancesAllowed;
289 this.modelService = service;
293 public int hashCode() {
294 final int prime = 31;
296 result = prime * result + ((name == null) ? 0 : name.hashCode());
301 public boolean equals(Object obj) {
308 if (getClass() != obj.getClass()) {
311 LoopTemplate other = (LoopTemplate) obj;
313 if (other.name != null) {
316 } else if (!name.equals(other.name)) {
323 * Generate the loop template name.
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
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(" ", "");