Update deploy-loop route
[clamp.git] / src / main / java / org / onap / clamp / loop / template / LoopTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop.template;
25
26 import com.google.gson.annotations.Expose;
27
28 import java.io.Serializable;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
31
32 import javax.persistence.CascadeType;
33 import javax.persistence.Column;
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
42 import org.hibernate.annotations.SortNatural;
43 import org.onap.clamp.loop.common.AuditEntity;
44 import org.onap.clamp.loop.service.Service;
45
46 @Entity
47 @Table(name = "loop_templates")
48 public class LoopTemplate extends AuditEntity implements Serializable {
49
50     /**
51      * The serial version id.
52      */
53     private static final long serialVersionUID = -286522707701388642L;
54
55     @Id
56     @Expose
57     @Column(nullable = false, name = "name", unique = true)
58     private String name;
59
60     /**
61      * This field is used when we have a blueprint defining all microservices. The
62      * other option would be to have independent blueprint for each microservices.
63      * In that case they are stored in each MicroServiceModel
64      */
65     @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
66     private String blueprint;
67
68     @Expose
69     @Column(columnDefinition = "MEDIUMTEXT", name = "svg_representation")
70     private String svgRepresentation;
71
72     @Expose
73     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "loopTemplate", orphanRemoval = true)
74     @SortNatural
75     private SortedSet<LoopTemplateLoopElementModel> loopElementModelsUsed = new TreeSet<>();
76
77     @Expose
78     @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
79     @JoinColumn(name = "service_uuid")
80     private Service modelService;
81
82     @Expose
83     @Column(name = "maximum_instances_allowed")
84     private Integer maximumInstancesAllowed;
85
86     /**
87      * name getter.
88      * 
89      * @return the name
90      */
91     public String getName() {
92         return name;
93     }
94
95     /**
96      * name setter.
97      * 
98      * @param name the name to set
99      */
100     public void setName(String name) {
101         this.name = name;
102     }
103
104     /**
105      * blueprint getter.
106      * 
107      * @return the blueprint
108      */
109     public String getBlueprint() {
110         return blueprint;
111     }
112
113     /**
114      * blueprint setter.
115      * 
116      * @param blueprint the blueprint to set
117      */
118     public void setBlueprint(String blueprint) {
119         this.blueprint = blueprint;
120     }
121
122     /**
123      * svgRepresentation getter.
124      * 
125      * @return the svgRepresentation
126      */
127     public String getSvgRepresentation() {
128         return svgRepresentation;
129     }
130
131     /**
132      * svgRepresentation setter.
133      * 
134      * @param svgRepresentation the svgRepresentation to set
135      */
136     public void setSvgRepresentation(String svgRepresentation) {
137         this.svgRepresentation = svgRepresentation;
138     }
139
140     /**
141      * loopElementModelsUsed getter.
142      * 
143      * @return the loopElementModelsUsed
144      */
145     public SortedSet<LoopTemplateLoopElementModel> getLoopElementModelsUsed() {
146         return loopElementModelsUsed;
147     }
148
149     /**
150      * maximumInstancesAllowed getter.
151      * 
152      * @return the maximumInstancesAllowed
153      */
154     public Integer getMaximumInstancesAllowed() {
155         return maximumInstancesAllowed;
156     }
157
158     /**
159      * maximumInstancesAllowed setter.
160      * 
161      * @param maximumInstancesAllowed the maximumInstancesAllowed to set
162      */
163     public void setMaximumInstancesAllowed(Integer maximumInstancesAllowed) {
164         this.maximumInstancesAllowed = maximumInstancesAllowed;
165     }
166
167     /**
168      * Add a loopElement to the current template, the loopElementModel is added at
169      * the end of the list so the flowOrder is computed automatically.
170      * 
171      * @param loopElementModel The loopElementModel to add
172      */
173     public void addLoopElementModel(LoopElementModel loopElementModel) {
174         LoopTemplateLoopElementModel jointEntry = new LoopTemplateLoopElementModel(this, loopElementModel,
175                 this.loopElementModelsUsed.size());
176         this.loopElementModelsUsed.add(jointEntry);
177         loopElementModel.getUsedByLoopTemplates().add(jointEntry);
178     }
179
180     /**
181      * Add a loopElement model to the current template, the flow order must be
182      * specified manually.
183      * 
184      * @param loopElementModel The loopElementModel to add
185      * @param listPosition     The position in the flow
186      */
187     public void addLoopElementModel(LoopElementModel loopElementModel, Integer listPosition) {
188         LoopTemplateLoopElementModel jointEntry = new LoopTemplateLoopElementModel(this, loopElementModel,
189                 listPosition);
190         this.loopElementModelsUsed.add(jointEntry);
191         loopElementModel.getUsedByLoopTemplates().add(jointEntry);
192     }
193
194     /**
195      * modelService getter.
196      * 
197      * @return the modelService
198      */
199     public Service getModelService() {
200         return modelService;
201     }
202
203     /**
204      * modelService setter.
205      * 
206      * @param modelService the modelService to set
207      */
208     public void setModelService(Service modelService) {
209         this.modelService = modelService;
210     }
211
212     /**
213      * Default constructor for serialization.
214      */
215     public LoopTemplate() {
216
217     }
218
219     /**
220      * Constructor.
221      * 
222      * @param name                The loop template name id
223      * @param blueprint           The blueprint containing all microservices (legacy
224      *                            case)
225      * @param svgRepresentation   The svg representation of that loop template
226      * @param maxInstancesAllowed The maximum number of instances that can be
227      *                            created from that template
228      * @param service             The service associated to that loop template
229      */
230     public LoopTemplate(String name, String blueprint, String svgRepresentation, Integer maxInstancesAllowed,
231             Service service) {
232         this.name = name;
233         this.blueprint = blueprint;
234         this.svgRepresentation = svgRepresentation;
235
236         this.maximumInstancesAllowed = maxInstancesAllowed;
237         this.modelService = service;
238     }
239
240     @Override
241     public int hashCode() {
242         final int prime = 31;
243         int result = 1;
244         result = prime * result + ((name == null) ? 0 : name.hashCode());
245         return result;
246     }
247
248     @Override
249     public boolean equals(Object obj) {
250         if (this == obj) {
251             return true;
252         }
253         if (obj == null) {
254             return false;
255         }
256         if (getClass() != obj.getClass()) {
257             return false;
258         }
259         LoopTemplate other = (LoopTemplate) obj;
260         if (name == null) {
261             if (other.name != null) {
262                 return false;
263             }
264         } else if (!name.equals(other.name)) {
265             return false;
266         }
267         return true;
268     }
269 }