Merge "Add a unit test"
[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     /**
67      * Here we store the blueprint coming from DCAE.
68      */
69     @Column(nullable = false, name = "blueprint_yaml")
70     private String blueprint;
71
72     /**
73      * The type of element.
74      */
75     @Column(nullable = false, name = "loop_element_type")
76     private String loopElementType;
77
78     /**
79      * This variable is used to store the type mentioned in the micro-service
80      * blueprint.
81      */
82     @Expose
83     @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
84     @JoinTable(name = "loopelementmodels_to_policymodels",
85             joinColumns = @JoinColumn(name = "loop_element_name", referencedColumnName = "name"),
86             inverseJoinColumns = { @JoinColumn(name = "policy_model_type", referencedColumnName = "policy_model_type"),
87                 @JoinColumn(name = "policy_model_version", referencedColumnName = "version") })
88     @SortNatural
89     private SortedSet<PolicyModel> policyModels = new TreeSet<>();
90
91     @OneToMany(fetch = FetchType.LAZY, mappedBy = "loopElementModel", orphanRemoval = true)
92     private Set<LoopTemplateLoopElementModel> usedByLoopTemplates = new HashSet<>();
93
94     /**
95      * policyModels getter.
96      * 
97      * @return the policyModel
98      */
99     public SortedSet<PolicyModel> getPolicyModels() {
100         return policyModels;
101     }
102
103     /**
104      * Method to add a new policyModel to the list.
105      * 
106      * @param policyModel The policy model
107      */
108     public void addPolicyModel(PolicyModel policyModel) {
109         policyModels.add(policyModel);
110         policyModel.getUsedByElementModels().add(this);
111     }
112
113     /**
114      * name getter.
115      * 
116      * @return the name
117      */
118     public String getName() {
119         return name;
120     }
121
122     /**
123      * name setter.
124      * 
125      * @param name the name to set
126      */
127     public void setName(String name) {
128         this.name = name;
129     }
130
131     /**
132      * blueprint getter.
133      * 
134      * @return the blueprint
135      */
136     public String getBlueprint() {
137         return blueprint;
138     }
139
140     /**
141      * blueprint setter.
142      * 
143      * @param blueprint the blueprint to set
144      */
145     public void setBlueprint(String blueprint) {
146         this.blueprint = blueprint;
147     }
148
149     /**
150      * loopElementType getter.
151      * 
152      * @return the loopElementType
153      */
154     public String getLoopElementType() {
155         return loopElementType;
156     }
157
158     /**
159      * loopElementType setter.
160      * 
161      * @param loopElementType the loopElementType to set
162      */
163     public void setLoopElementType(String loopElementType) {
164         this.loopElementType = loopElementType;
165     }
166
167     /**
168      * usedByLoopTemplates getter.
169      * 
170      * @return the usedByLoopTemplates
171      */
172     public Set<LoopTemplateLoopElementModel> getUsedByLoopTemplates() {
173         return usedByLoopTemplates;
174     }
175
176     /**
177      * Default constructor for serialization.
178      */
179     public LoopElementModel() {
180     }
181
182     /**
183      * Constructor.
184      * 
185      * @param name            The name id
186      * @param loopElementType The type of loop element
187      * @param blueprint       The blueprint defined for dcae that contains the
188      *                        policy type to use
189      */
190     public LoopElementModel(String name, String loopElementType, String blueprint) {
191         this.name = name;
192         this.loopElementType = loopElementType;
193         this.blueprint = blueprint;
194     }
195
196     @Override
197     public int hashCode() {
198         final int prime = 31;
199         int result = 1;
200         result = prime * result + ((name == null) ? 0 : name.hashCode());
201         return result;
202     }
203
204     @Override
205     public boolean equals(Object obj) {
206         if (this == obj) {
207             return true;
208         }
209         if (obj == null) {
210             return false;
211         }
212         if (getClass() != obj.getClass()) {
213             return false;
214         }
215         LoopElementModel other = (LoopElementModel) obj;
216         if (name == null) {
217             if (other.name != null) {
218                 return false;
219             }
220         } else if (!name.equals(other.name)) {
221             return false;
222         }
223         return true;
224     }
225
226 }