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