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