Modify the template model
[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
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      * @return the loopElementType
151      */
152     public String getLoopElementType() {
153         return loopElementType;
154     }
155
156     /**
157      * @param loopElementType the loopElementType to set
158      */
159     public void setLoopElementType(String loopElementType) {
160         this.loopElementType = loopElementType;
161     }
162
163     /**
164      * usedByLoopTemplates getter.
165      * 
166      * @return the usedByLoopTemplates
167      */
168     public Set<LoopTemplateLoopElementModel> getUsedByLoopTemplates() {
169         return usedByLoopTemplates;
170     }
171
172     /**
173      * Default constructor for serialization.
174      */
175     public LoopElementModel() {
176     }
177
178     /**
179      * Constructor.
180      * 
181      * @param name            The name id
182      * @param loopElementType The type of loop element
183      * @param blueprint       The blueprint defined for dcae that contains the
184      *                        policy type to use
185      */
186     public LoopElementModel(String name, String loopElementType, String blueprint) {
187         this.name = name;
188         this.loopElementType = loopElementType;
189         this.blueprint = blueprint;
190     }
191
192     @Override
193     public int hashCode() {
194         final int prime = 31;
195         int result = 1;
196         result = prime * result + ((name == null) ? 0 : name.hashCode());
197         return result;
198     }
199
200     @Override
201     public boolean equals(Object obj) {
202         if (this == obj) {
203             return true;
204         }
205         if (obj == null) {
206             return false;
207         }
208         if (getClass() != obj.getClass()) {
209             return false;
210         }
211         LoopElementModel other = (LoopElementModel) obj;
212         if (name == null) {
213             if (other.name != null) {
214                 return false;
215             }
216         } else if (!name.equals(other.name)) {
217             return false;
218         }
219         return true;
220     }
221
222 }