4796059833c270f978746897ca9badc1e15d932a
[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/operational/... model for a loop template.
47  * So it's an element in the flow (a box shown in the loop).
48  */
49
50 @Entity
51 @Table(name = "loop_element_models")
52 public class LoopElementModel extends AuditEntity implements Serializable {
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, it can be null if this is not a micro service model.
69      */
70     @Column(columnDefinition = "MEDIUMTEXT", name = "blueprint_yaml")
71     private String blueprint;
72
73     public static String MICRO_SERVICE_TYPE = "MICRO_SERVICE_TYPE";
74     public static String OPERATIONAL_POLICY_TYPE = "OPERATIONAL_POLICY_TYPE";
75     /**
76      * The type of element.
77      */
78     @Column(nullable = false, name = "loop_element_type")
79     private String loopElementType;
80
81     /**
82      * This variable is used to display the micro-service name in the SVG.
83      */
84     @Expose
85     @Column(name = "short_name")
86     private String shortName;
87
88     /**
89      * This variable is used to store the type mentioned in the micro-service
90      * blueprint.
91      */
92     @Expose
93     @ManyToMany(
94             fetch = FetchType.EAGER,
95             cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
96     @JoinTable(
97             name = "loopelementmodels_to_policymodels",
98             joinColumns = @JoinColumn(name = "loop_element_name", referencedColumnName = "name"),
99             inverseJoinColumns = {
100                     @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
101                     @JoinColumn(name = "policy_model_version", referencedColumnName = "version")})
102     @SortNatural
103     private SortedSet<PolicyModel> policyModels = new TreeSet<>();
104
105     @OneToMany(fetch = FetchType.LAZY, mappedBy = "loopElementModel", orphanRemoval = true)
106     private Set<LoopTemplateLoopElementModel> usedByLoopTemplates = new HashSet<>();
107
108     /**
109      * policyModels getter.
110      *
111      * @return the policyModel
112      */
113     public SortedSet<PolicyModel> getPolicyModels() {
114         return policyModels;
115     }
116
117     /**
118      * Method to add a new policyModel to the list.
119      *
120      * @param policyModel The policy model
121      */
122     public void addPolicyModel(PolicyModel policyModel) {
123         policyModels.add(policyModel);
124         policyModel.getUsedByElementModels().add(this);
125     }
126
127     /**
128      * name getter.
129      *
130      * @return the name
131      */
132     public String getName() {
133         return name;
134     }
135
136     /**
137      * name setter.
138      *
139      * @param name the name to set
140      */
141     public void setName(String name) {
142         this.name = name;
143     }
144
145     /**
146      * blueprint getter.
147      *
148      * @return the blueprint
149      */
150     public String getBlueprint() {
151         return blueprint;
152     }
153
154     /**
155      * blueprint setter.
156      *
157      * @param blueprint the blueprint to set
158      */
159     public void setBlueprint(String blueprint) {
160         this.blueprint = blueprint;
161     }
162
163     /**
164      * dcaeBlueprintId getter.
165      *
166      * @return the dcaeBlueprintId
167      */
168     public String getDcaeBlueprintId() {
169         return dcaeBlueprintId;
170     }
171
172     /**
173      * dcaeBlueprintId setter.
174      *
175      * @param dcaeBlueprintId the dcaeBlueprintId to set
176      */
177     public void setDcaeBlueprintId(String dcaeBlueprintId) {
178         this.dcaeBlueprintId = dcaeBlueprintId;
179     }
180
181     /**
182      * loopElementType getter.
183      *
184      * @return the loopElementType
185      */
186     public String getLoopElementType() {
187         return loopElementType;
188     }
189
190     /**
191      * loopElementType setter.
192      *
193      * @param loopElementType the loopElementType to set
194      */
195     public void setLoopElementType(String loopElementType) {
196         this.loopElementType = loopElementType;
197     }
198
199     /**
200      * shortName getter.
201      *
202      * @return the shortName
203      */
204     public String getShortName() {
205         return shortName;
206     }
207
208     /**
209      * @param shortName the shortName to set.
210      */
211     public void setShortName(String shortName) {
212         this.shortName = shortName;
213     }
214
215     /**
216      * usedByLoopTemplates getter.
217      *
218      * @return the usedByLoopTemplates
219      */
220     public Set<LoopTemplateLoopElementModel> getUsedByLoopTemplates() {
221         return usedByLoopTemplates;
222     }
223
224     /**
225      * Default constructor for serialization.
226      */
227     public LoopElementModel() {
228     }
229
230     /**
231      * Constructor.
232      *
233      * @param name            The name id
234      * @param loopElementType The type of loop element
235      * @param blueprint       The blueprint defined for dcae that contains the
236      *                        policy type to use
237      */
238     public LoopElementModel(String name, String loopElementType, String blueprint) {
239         this.name = name;
240         this.loopElementType = loopElementType;
241         this.blueprint = blueprint;
242     }
243
244     @Override
245     public int hashCode() {
246         final int prime = 31;
247         int result = 1;
248         result = prime * result + ((name == null) ? 0 : name.hashCode());
249         return result;
250     }
251
252     @Override
253     public boolean equals(Object obj) {
254         if (this == obj) {
255             return true;
256         }
257         if (obj == null) {
258             return false;
259         }
260         if (getClass() != obj.getClass()) {
261             return false;
262         }
263         LoopElementModel other = (LoopElementModel) obj;
264         if (name == null) {
265             if (other.name != null) {
266                 return false;
267             }
268         } else if (!name.equals(other.name)) {
269             return false;
270         }
271         return true;
272     }
273
274 }