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;
73 cascade = CascadeType.ALL,
74 fetch = FetchType.EAGER,
75 mappedBy = "loopTemplate",
78 private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
82 fetch = FetchType.EAGER,
83 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;
92 @Column(name = "unique_blueprint", columnDefinition = "boolean default false")
93 private boolean uniqueBlueprint;
96 * Type of Loop allowed to be created.
99 @Column(name = "allowed_loop_type")
100 @Convert(converter = LoopTypeConvertor.class)
101 private LoopType allowedLoopType = LoopType.CLOSED;
108 public String getName() {
115 * @param name the name to set
117 public void setName(String name) {
124 * @return the blueprint
126 public String getBlueprint() {
131 * dcaeBlueprintId getter.
133 * @return the dcaeBlueprintId
135 public String getDcaeBlueprintId() {
136 return dcaeBlueprintId;
140 * dcaeBlueprintId setter.
142 * @param dcaeBlueprintId the dcaeBlueprintId to set
144 public void setDcaeBlueprintId(String dcaeBlueprintId) {
145 this.dcaeBlueprintId = dcaeBlueprintId;
151 * @param blueprint the blueprint to set
153 public void setBlueprint(String blueprint) {
154 this.blueprint = blueprint;
155 if (blueprint == null) {
156 this.uniqueBlueprint = false;
158 this.uniqueBlueprint = true;
163 * loopElementModelsUsed getter.
165 * @return the loopElementModelsUsed
167 public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
168 return loopElementModelsUsed;
172 * maximumInstancesAllowed getter.
174 * @return the maximumInstancesAllowed
176 public Integer getMaximumInstancesAllowed() {
177 return maximumInstancesAllowed;
181 * maximumInstancesAllowed setter.
183 * @param maximumInstancesAllowed the maximumInstancesAllowed to set
185 public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
186 this.maximumInstancesAllowed = maximumInstancesAllowed;
190 * allowedLoopType getter.
192 * @return the allowedLoopType Type of Loop allowed to be created
194 public LoopType getAllowedLoopType() {
195 return allowedLoopType;
199 * allowedLoopType setter.
201 * @param allowedLoopType the allowedLoopType to set
203 public void setAllowedLoopType(LoopType allowedLoopType) {
204 this.allowedLoopType = allowedLoopType;
208 * Add list of loopElements to the current template, each loopElementModel is
209 * added at the end of the list so the flowOrder is computed automatically.
211 * @param loopElementModels The loopElementModel set to add
213 public void addLoopElementModels(Set<LoopElementModel> loopElementModels) {
214 for (LoopElementModel loopElementModel : loopElementModels) {
215 addLoopElementModel(loopElementModel);
220 * Add a loopElement to the current template, the loopElementModel is added at
221 * the end of the list so the flowOrder is computed automatically.
223 * @param loopElementModel The loopElementModel to add
225 public void addLoopElementModel(LoopElementModel loopElementModel) {
226 this.addLoopElementModel(loopElementModel,this.loopElementModelsUsed.size());
230 * Add a loopElement model to the current template, the flow order must be
231 * specified manually.
233 * @param loopElementModel The loopElementModel to add
234 * @param listPosition The position in the flow
236 public void addLoopElementModel(LoopElementModel loopElementModel, Integer listPosition) {
237 LoopTemplateLoopElementModel jointEntry =
238 new LoopTemplateLoopElementModel(this, loopElementModel, listPosition);
239 this.loopElementModelsUsed.add(jointEntry);
240 loopElementModel.getUsedByLoopTemplates().add(jointEntry);
244 * modelService getter.
246 * @return the modelService
248 public Service getModelService() {
253 * modelService setter.
255 * @param modelService the modelService to set
257 public void setModelService(Service modelService) {
258 this.modelService = modelService;
262 * uniqueBlueprint getter.
264 * @return the uniqueBlueprint
266 public boolean getUniqueBlueprint() {
267 return uniqueBlueprint;
271 * Default constructor for serialization.
273 public LoopTemplate() {
280 * @param name The loop template name id
281 * @param blueprint The blueprint containing all microservices (legacy
283 * @param maxInstancesAllowed The maximum number of instances that can be
284 * created from that template
285 * @param service The service associated to that loop template
287 public LoopTemplate(String name, String blueprint, Integer maxInstancesAllowed, Service service) {
289 this.setBlueprint(blueprint);
291 this.maximumInstancesAllowed = maxInstancesAllowed;
292 this.modelService = service;
296 public int hashCode() {
297 final int prime = 31;
299 result = prime * result + ((name == null) ? 0 : name.hashCode());
304 public boolean equals(Object obj) {
311 if (getClass() != obj.getClass()) {
314 LoopTemplate other = (LoopTemplate) obj;
316 if (other.name != null) {
319 } else if (!name.equals(other.name)) {
326 * Generate the loop template name.
328 * @param serviceName The service name
329 * @param serviceVersion The service version
330 * @param resourceName The resource name
331 * @param blueprintFileName The blueprint file name
332 * @return The generated loop template name
334 public static String generateLoopTemplateName(String serviceName, String serviceVersion,
335 String resourceName, String blueprintFileName) {
336 StringBuilder buffer = new StringBuilder("LOOP_TEMPLATE_").append(serviceName).append("_v")
337 .append(serviceVersion).append("_").append(resourceName).append("_")
338 .append(blueprintFileName.replaceAll(".yaml", ""));
339 return buffer.toString().replace('.', '_').replaceAll(" ", "");