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