00d58a822ed834d496b043b5276a417cabdcc112
[clamp.git] / src / main / java / org / onap / clamp / loop / template / PolicyModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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
32 import javax.persistence.Column;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.Id;
36 import javax.persistence.IdClass;
37 import javax.persistence.ManyToMany;
38 import javax.persistence.Table;
39
40 import org.onap.clamp.loop.common.AuditEntity;
41 import org.onap.clamp.util.SemanticVersioning;
42
43 /**
44  * This class represents the policy model tosca revision that we can have to a
45  * specific microservice.
46  */
47 @Entity
48 @Table(name = "policy_models")
49 @IdClass(PolicyModelId.class)
50 public class PolicyModel extends AuditEntity implements Serializable, Comparable<PolicyModel> {
51
52     /**
53      * The serial version id.
54      */
55     private static final long serialVersionUID = -286522705701376645L;
56
57     /**
58      * This variable is used to store the type mentioned in the micro-service
59      * blueprint.
60      */
61     @Id
62     @Expose
63     @Column(nullable = false, name = "policy_model_type")
64     private String policyModelType;
65
66     /**
67      * Semantic versioning on policy side.
68      */
69     @Id
70     @Expose
71     @Column(name = "version")
72     private String version;
73
74     @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca")
75     private String policyModelTosca;
76
77     @Expose
78     @Column(name = "policy_acronym")
79     private String policyAcronym;
80
81     @ManyToMany(mappedBy = "policyModels", fetch = FetchType.EAGER)
82     private Set<LoopElementModel> usedByElementModels = new HashSet<>();
83
84     /**
85      * @return the usedByElementModels
86      */
87     public Set<LoopElementModel> getUsedByElementModels() {
88         return usedByElementModels;
89     }
90
91     /**
92      * policyModelTosca getter.
93      * 
94      * @return the policyModelTosca
95      */
96     public String getPolicyModelTosca() {
97         return policyModelTosca;
98     }
99
100     /**
101      * policyModelTosca setter.
102      * 
103      * @param policyModelTosca the policyModelTosca to set
104      */
105     public void setPolicyModelTosca(String policyModelTosca) {
106         this.policyModelTosca = policyModelTosca;
107     }
108
109     /**
110      * policyModelType getter.
111      * 
112      * @return the modelType
113      */
114     public String getPolicyModelType() {
115         return policyModelType;
116     }
117
118     /**
119      * policyModelType setter.
120      * 
121      * @param modelType the modelType to set
122      */
123     public void setPolicyModelType(String modelType) {
124         this.policyModelType = modelType;
125     }
126
127     /**
128      * version getter.
129      * 
130      * @return the version
131      */
132     public String getVersion() {
133         return version;
134     }
135
136     /**
137      * version setter.
138      * 
139      * @param version the version to set
140      */
141     public void setVersion(String version) {
142         // Try to convert it before
143         this.version = version;
144     }
145
146     /**
147      * policyAcronym getter.
148      * 
149      * @return the policyAcronym value
150      */
151     public String getPolicyAcronym() {
152         return policyAcronym;
153     }
154
155     /**
156      * policyAcronym setter.
157      * 
158      * @param policyAcronym The policyAcronym to set
159      */
160     public void setPolicyAcronym(String policyAcronym) {
161         this.policyAcronym = policyAcronym;
162     }
163
164     /**
165      * Default constructor for serialization.
166      */
167     public PolicyModel() {
168     }
169
170     /**
171      * Constructor.
172      * 
173      * @param policyType       The policyType (referenced in the blueprint)
174      * @param policyModelTosca The policy tosca model in yaml
175      * @param version          the version like 1.0.0
176      * @param policyVariant    Subtype for policy if it exists (could be used by UI)
177      */
178     public PolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym) {
179         this.policyModelType = policyType;
180         this.policyModelTosca = policyModelTosca;
181         this.version = version;
182         this.policyAcronym = policyAcronym;
183     }
184
185     @Override
186     public int hashCode() {
187         final int prime = 31;
188         int result = 1;
189         result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
190         result = prime * result + ((version == null) ? 0 : version.hashCode());
191         return result;
192     }
193
194     @Override
195     public boolean equals(Object obj) {
196         if (this == obj) {
197             return true;
198         }
199         if (obj == null) {
200             return false;
201         }
202         if (getClass() != obj.getClass()) {
203             return false;
204         }
205         PolicyModel other = (PolicyModel) obj;
206         if (policyModelType == null) {
207             if (other.policyModelType != null) {
208                 return false;
209             }
210         } else if (!policyModelType.equals(other.policyModelType)) {
211             return false;
212         }
213         if (version == null) {
214             if (other.version != null) {
215                 return false;
216             }
217         } else if (!version.equals(other.version)) {
218             return false;
219         }
220         return true;
221     }
222
223     @Override
224     public int compareTo(PolicyModel arg0) {
225         // Reverse it, so that by default we have the latest
226         return SemanticVersioning.compare(arg0.getVersion(), this.version);
227     }
228 }