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