Add template and tosca model entities and repositories
[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", nullable = false, 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<TemplateMicroServiceModel> microServiceModelUsed = 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      * microServiceModelUsed getter.
142      * 
143      * @return the microServiceModelUsed
144      */
145     public SortedSet<TemplateMicroServiceModel> getMicroServiceModelUsed() {
146         return microServiceModelUsed;
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 microService model to the current template, the microservice is added
169      * at the end of the list so the flowOrder is computed automatically.
170      * 
171      * @param microServiceModel The microserviceModel to add
172      */
173     public void addMicroServiceModel(MicroServiceModel microServiceModel) {
174         TemplateMicroServiceModel jointEntry = new TemplateMicroServiceModel(this, microServiceModel,
175                 this.microServiceModelUsed.size());
176         this.microServiceModelUsed.add(jointEntry);
177         microServiceModel.getUsedByLoopTemplates().add(jointEntry);
178     }
179
180     /**
181      * Add a microService model to the current template, the flow order must be
182      * specified manually.
183      * 
184      * @param microServiceModel The microserviceModel to add
185      * @param listPosition      The position in the flow
186      */
187     public void addMicroServiceModel(MicroServiceModel microServiceModel, Integer listPosition) {
188         TemplateMicroServiceModel jointEntry = new TemplateMicroServiceModel(this, microServiceModel, listPosition);
189         this.microServiceModelUsed.add(jointEntry);
190         microServiceModel.getUsedByLoopTemplates().add(jointEntry);
191     }
192
193     /**
194      * modelService getter.
195      * 
196      * @return the modelService
197      */
198     public Service getModelService() {
199         return modelService;
200     }
201
202     /**
203      * modelService setter.
204      * 
205      * @param modelService the modelService to set
206      */
207     public void setModelService(Service modelService) {
208         this.modelService = modelService;
209     }
210
211     /**
212      * Default constructor for serialization.
213      */
214     public LoopTemplate() {
215
216     }
217
218     /**
219      * Constructor.
220      * 
221      * @param name                The loop template name id
222      * @param blueprint           The blueprint containing all microservices (legacy
223      *                            case)
224      * @param svgRepresentation   The svg representation of that loop template
225      * @param maxInstancesAllowed The maximum number of instances that can be
226      *                            created from that template
227      * @param service             The service associated to that loop template
228      */
229     public LoopTemplate(String name, String blueprint, String svgRepresentation, Integer maxInstancesAllowed,
230             Service service) {
231         this.name = name;
232         this.blueprint = blueprint;
233         this.svgRepresentation = svgRepresentation;
234
235         this.maximumInstancesAllowed = maxInstancesAllowed;
236         this.modelService = service;
237     }
238
239     @Override
240     public int hashCode() {
241         final int prime = 31;
242         int result = 1;
243         result = prime * result + ((name == null) ? 0 : name.hashCode());
244         return result;
245     }
246
247     @Override
248     public boolean equals(Object obj) {
249         if (this == obj) {
250             return true;
251         }
252         if (obj == null) {
253             return false;
254         }
255         if (getClass() != obj.getClass()) {
256             return false;
257         }
258         LoopTemplate other = (LoopTemplate) obj;
259         if (name == null) {
260             if (other.name != null) {
261                 return false;
262             }
263         } else if (!name.equals(other.name)) {
264             return false;
265         }
266         return true;
267     }
268 }