Changes include Metadata support, Upload tosca policy model and Loop Template
[clamp.git] / src / main / java / org / onap / clamp / loop / template / LoopElementModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 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 import java.io.Serializable;
28 import java.util.HashSet;
29 import java.util.Set;
30 import java.util.SortedSet;
31 import java.util.TreeSet;
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.JoinTable;
39 import javax.persistence.ManyToMany;
40 import javax.persistence.OneToMany;
41 import javax.persistence.Table;
42 import org.hibernate.annotations.SortNatural;
43 import org.onap.clamp.loop.common.AuditEntity;
44
45 /**
46  * This class represents a micro service model for a loop template.
47  */
48
49 @Entity
50 @Table(name = "loop_element_models")
51 public class LoopElementModel extends AuditEntity implements Serializable {
52     public static final String DEFAULT_GROUP_NAME = "DEFAULT";
53     /**
54      * The serial version id.
55      */
56     private static final long serialVersionUID = -286522707701376645L;
57
58     @Id
59     @Expose
60     @Column(nullable = false, name = "name", unique = true)
61     private String name;
62
63     @Expose
64     @Column(name = "dcae_blueprint_id")
65     private String dcaeBlueprintId;
66
67     /**
68      * Here we store the blueprint coming from DCAE.
69      */
70     @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
71     private String blueprint;
72
73     /**
74      * The type of element.
75      */
76     @Column(nullable = false, name = "loop_element_type")
77     private String loopElementType;
78
79     /**
80      * This variable is used to display the micro-service name in the SVG.
81      */
82     @Expose
83     @Column(name = "short_name")
84     private String shortName;
85
86     /**
87      * This variable is used to store the type mentioned in the micro-service
88      * blueprint.
89      */
90     @Expose
91     @ManyToMany(
92         fetch = FetchType.EAGER,
93         cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
94     @JoinTable(
95         name = "loopelementmodels_to_policymodels",
96         joinColumns = @JoinColumn(name = "loop_element_name", referencedColumnName = "name"),
97         inverseJoinColumns = {
98             @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
99             @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
100     @SortNatural
101     private SortedSet<PolicyModel> policyModels = new TreeSet<>();
102
103     @OneToMany(fetch = FetchType.LAZY, mappedBy = "loopElementModel", orphanRemoval = true)
104     private Set<LoopTemplateLoopElementModel> usedByLoopTemplates = new HashSet<>();
105
106     /**
107      * policyModels getter.
108      *
109      * @return the policyModel
110      */
111     public SortedSet<PolicyModel> getPolicyModels() {
112         return policyModels;
113     }
114
115     /**
116      * Method to add a new policyModel to the list.
117      *
118      * @param policyModel The policy model
119      */
120     public void addPolicyModel(PolicyModel policyModel) {
121         policyModels.add(policyModel);
122         policyModel.getUsedByElementModels().add(this);
123     }
124
125     /**
126      * name getter.
127      *
128      * @return the name
129      */
130     public String getName() {
131         return name;
132     }
133
134     /**
135      * name setter.
136      *
137      * @param name the name to set
138      */
139     public void setName(String name) {
140         this.name = name;
141     }
142
143     /**
144      * blueprint getter.
145      *
146      * @return the blueprint
147      */
148     public String getBlueprint() {
149         return blueprint;
150     }
151
152     /**
153      * blueprint setter.
154      *
155      * @param blueprint the blueprint to set
156      */
157     public void setBlueprint(String blueprint) {
158         this.blueprint = blueprint;
159     }
160
161     /**
162      * dcaeBlueprintId getter.
163      *
164      * @return the dcaeBlueprintId
165      */
166     public String getDcaeBlueprintId() {
167         return dcaeBlueprintId;
168     }
169
170     /**
171      * dcaeBlueprintId setter.
172      *
173      * @param dcaeBlueprintId the dcaeBlueprintId to set
174      */
175     public void setDcaeBlueprintId(String dcaeBlueprintId) {
176         this.dcaeBlueprintId = dcaeBlueprintId;
177     }
178
179     /**
180      * loopElementType getter.
181      *
182      * @return the loopElementType
183      */
184     public String getLoopElementType() {
185         return loopElementType;
186     }
187
188     /**
189      * loopElementType setter.
190      *
191      * @param loopElementType the loopElementType to set
192      */
193     public void setLoopElementType(String loopElementType) {
194         this.loopElementType = loopElementType;
195     }
196
197     /**
198      * shortName getter.
199      *
200      * @return the shortName
201      */
202     public String getShortName() {
203         return shortName;
204     }
205
206     /**
207      * @param shortName the shortName to set.
208      */
209     public void setShortName(String shortName) {
210         this.shortName = shortName;
211     }
212
213     /**
214      * usedByLoopTemplates getter.
215      *
216      * @return the usedByLoopTemplates
217      */
218     public Set<LoopTemplateLoopElementModel> getUsedByLoopTemplates() {
219         return usedByLoopTemplates;
220     }
221
222     /**
223      * Default constructor for serialization.
224      */
225     public LoopElementModel() {
226     }
227
228     /**
229      * Constructor.
230      *
231      * @param name The name id
232      * @param loopElementType The type of loop element
233      * @param blueprint The blueprint defined for dcae that contains the
234      *        policy type to use
235      */
236     public LoopElementModel(String name, String loopElementType, String blueprint) {
237         this.name = name;
238         this.loopElementType = loopElementType;
239         this.blueprint = blueprint;
240     }
241
242     @Override
243     public int hashCode() {
244         final int prime = 31;
245         int result = 1;
246         result = prime * result + ((name == null) ? 0 : name.hashCode());
247         return result;
248     }
249
250     @Override
251     public boolean equals(Object obj) {
252         if (this == obj) {
253             return true;
254         }
255         if (obj == null) {
256             return false;
257         }
258         if (getClass() != obj.getClass()) {
259             return false;
260         }
261         LoopElementModel other = (LoopElementModel) obj;
262         if (name == null) {
263             if (other.name != null) {
264                 return false;
265             }
266         } else if (!name.equals(other.name)) {
267             return false;
268         }
269         return true;
270     }
271
272 }