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;
74 @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
75 private String svgRepresentation;
78 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loopTemplate", orphanRemoval = true)
80 private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
83 @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
84 @JoinColumn(name = "service_uuid")
85 private Service modelService;
88 @Column(name = "maximum_instances_allowed")
89 private Integer maximumInstancesAllowed;
96 public String getName() {
103 * @param name the name to set
105 public void setName(String name) {
112 * @return the blueprint
114 public String getBlueprint() {
119 * dcaeBlueprintId getter.
121 * @return the dcaeBlueprintId
123 public String getDcaeBlueprintId() {
124 return dcaeBlueprintId;
128 * dcaeBlueprintId setter.
130 * @param dcaeBlueprintId the dcaeBlueprintId to set
132 public void setDcaeBlueprintId(String dcaeBlueprintId) {
133 this.dcaeBlueprintId = dcaeBlueprintId;
139 * @param blueprint the blueprint to set
141 public void setBlueprint(String blueprint) {
142 this.blueprint = blueprint;
146 * svgRepresentation getter.
148 * @return the svgRepresentation
150 public String getSvgRepresentation() {
151 return svgRepresentation;
155 * svgRepresentation setter.
157 * @param svgRepresentation the svgRepresentation to set
159 public void setSvgRepresentation(String svgRepresentation) {
160 this.svgRepresentation = svgRepresentation;
164 * loopElementModelsUsed getter.
166 * @return the loopElementModelsUsed
168 public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
169 return loopElementModelsUsed;
173 * maximumInstancesAllowed getter.
175 * @return the maximumInstancesAllowed
177 public Integer getMaximumInstancesAllowed() {
178 return maximumInstancesAllowed;
182 * maximumInstancesAllowed setter.
184 * @param maximumInstancesAllowed the maximumInstancesAllowed to set
186 public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
187 this.maximumInstancesAllowed = maximumInstancesAllowed;
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.
194 * @param loopElementModels The loopElementModel set to add
196 public void addLoopElementModels(Set<LoopElementModel> loopElementModels) {
197 for (LoopElementModel loopElementModel : loopElementModels) {
198 addLoopElementModel(loopElementModel);
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.
206 * @param loopElementModel The loopElementModel to add
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);
216 * Add a loopElement model to the current template, the flow order must be
217 * specified manually.
219 * @param loopElementModel The loopElementModel to add
220 * @param listPosition The position in the flow
222 public void addLoopElementModel(LoopElementModel loopElementModel, Integer listPosition) {
223 LoopTemplateLoopElementModel jointEntry = new LoopTemplateLoopElementModel(this, loopElementModel,
225 this.loopElementModelsUsed.add(jointEntry);
226 loopElementModel.getUsedByLoopTemplates().add(jointEntry);
230 * modelService getter.
232 * @return the modelService
234 public Service getModelService() {
239 * modelService setter.
241 * @param modelService the modelService to set
243 public void setModelService(Service modelService) {
244 this.modelService = modelService;
248 * Default constructor for serialization.
250 public LoopTemplate() {
257 * @param name The loop template name id
258 * @param blueprint The blueprint containing all microservices (legacy
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
265 public LoopTemplate(String name, String blueprint, String svgRepresentation, Integer maxInstancesAllowed,
268 this.blueprint = blueprint;
269 this.svgRepresentation = svgRepresentation;
271 this.maximumInstancesAllowed = maxInstancesAllowed;
272 this.modelService = service;
276 public int hashCode() {
277 final int prime = 31;
279 result = prime * result + ((name == null) ? 0 : name.hashCode());
284 public boolean equals(Object obj) {
291 if (getClass() != obj.getClass()) {
294 LoopTemplate other = (LoopTemplate) obj;
296 if (other.name != null) {
299 } else if (!name.equals(other.name)) {
306 * Generate the loop template name.
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
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(" ", "");