66b05f65f84e776ba50bb0ddafbc6f7ced5baad2
[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.JsonObject;
27 import com.google.gson.annotations.Expose;
28 import java.io.Serializable;
29 import java.util.HashSet;
30 import java.util.Set;
31 import javax.persistence.Column;
32 import javax.persistence.Entity;
33 import javax.persistence.FetchType;
34 import javax.persistence.Id;
35 import javax.persistence.IdClass;
36 import javax.persistence.ManyToMany;
37 import javax.persistence.Table;
38 import org.hibernate.annotations.Type;
39 import org.hibernate.annotations.TypeDef;
40 import org.hibernate.annotations.TypeDefs;
41 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
42 import org.onap.clamp.loop.common.AuditEntity;
43 import org.onap.clamp.util.SemanticVersioning;
44
45 /**
46  * This class represents the policy model tosca revision that we can have to a
47  * specific microservice.
48  */
49 @Entity
50 @Table(name = "policy_models")
51 @IdClass(PolicyModelId.class)
52 @TypeDefs({@TypeDef(name = "json", typeClass = StringJsonUserType.class)})
53 public class PolicyModel extends AuditEntity implements Serializable, Comparable<PolicyModel> {
54
55     /**
56      * The serial version id.
57      */
58     private static final long serialVersionUID = -286522705701376645L;
59
60     /**
61      * This variable is used to store the type mentioned in the micro-service
62      * blueprint.
63      */
64     @Id
65     @Expose
66     @Column(nullable = false, name = "policy_model_type")
67     private String policyModelType;
68
69     /**
70      * Semantic versioning on policy side.
71      */
72     @Id
73     @Expose
74     @Column(name = "version", nullable = false)
75     private String version;
76
77     @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca")
78     private String policyModelTosca;
79
80     @Expose
81     @Column(name = "policy_acronym")
82     private String policyAcronym;
83
84     @ManyToMany(mappedBy = "policyModels", fetch = FetchType.EAGER)
85     private Set<LoopElementModel> usedByElementModels = new HashSet<>();
86
87     @Type(type = "json")
88     @Column(columnDefinition = "json", name = "policy_pdp_group")
89     private JsonObject policyPdpGroup;
90
91     /**
92      * usedByElementModels getter.
93      *
94      * @return the usedByElementModels
95      */
96     public Set<LoopElementModel> getUsedByElementModels() {
97         return usedByElementModels;
98     }
99
100     /**
101      * policyPdpGroup getter.
102      *
103      * @return the policyPdpGroup
104      */
105     public JsonObject getPolicyPdpGroup() {
106         return policyPdpGroup;
107     }
108
109     /**
110      * policyPdpGroup setter.
111      *
112      * @param policyPdpGroup the policyPdpGroup to set
113      */
114     public void setPolicyPdpGroup(JsonObject policyPdpGroup) {
115         this.policyPdpGroup = policyPdpGroup;
116     }
117
118     /**
119      * policyModelTosca getter.
120      *
121      * @return the policyModelTosca
122      */
123     public String getPolicyModelTosca() {
124         return policyModelTosca;
125     }
126
127     /**
128      * policyModelTosca setter.
129      *
130      * @param policyModelTosca the policyModelTosca to set
131      */
132     public void setPolicyModelTosca(String policyModelTosca) {
133         this.policyModelTosca = policyModelTosca;
134     }
135
136     /**
137      * policyModelType getter.
138      *
139      * @return the modelType
140      */
141     public String getPolicyModelType() {
142         return policyModelType;
143     }
144
145     /**
146      * policyModelType setter.
147      *
148      * @param modelType the modelType to set
149      */
150     public void setPolicyModelType(String modelType) {
151         this.policyModelType = modelType;
152     }
153
154     /**
155      * version getter.
156      *
157      * @return the version
158      */
159     public String getVersion() {
160         return version;
161     }
162
163     /**
164      * version setter.
165      *
166      * @param version the version to set
167      */
168     public void setVersion(String version) {
169         // Try to convert it before
170         this.version = version;
171     }
172
173     /**
174      * policyAcronym getter.
175      *
176      * @return the policyAcronym value
177      */
178     public String getPolicyAcronym() {
179         return policyAcronym;
180     }
181
182     /**
183      * policyAcronym setter.
184      *
185      * @param policyAcronym The policyAcronym to set
186      */
187     public void setPolicyAcronym(String policyAcronym) {
188         this.policyAcronym = policyAcronym;
189     }
190
191     /**
192      * Default constructor for serialization.
193      */
194     public PolicyModel() {
195     }
196
197     /**
198      * Constructor.
199      *
200      * @param policyType       The policyType (referenced in the blueprint
201      * @param policyModelTosca The policy tosca model in yaml
202      * @param version          the version like 1.0.0
203      * @param policyAcronym    Subtype for policy if it exists (could be used by UI)
204      */
205     public PolicyModel(String policyType, String policyModelTosca, String version,
206                        String policyAcronym) {
207         this.policyModelType = policyType;
208         this.policyModelTosca = policyModelTosca;
209         this.version = version;
210         this.policyAcronym = policyAcronym;
211         if (this.policyAcronym == null) {
212             this.policyAcronym = createDefaultPolicyAcronym(policyType);
213         }
214     }
215
216     /**
217      * Constructor with acronym generated by default from policyType.
218      *
219      * @param policyType       The policyType (referenced in the blueprint
220      * @param policyModelTosca The policy tosca model in yaml
221      * @param version          the version like 1.0.0
222      */
223     public PolicyModel(String policyType, String policyModelTosca, String version) {
224         this(policyType, policyModelTosca, version, null);
225     }
226
227     public static String createDefaultPolicyAcronym(String policyType) {
228         String[] policyNameArray = policyType.split("\\.");
229         return policyNameArray[policyNameArray.length - 1];
230     }
231
232     @Override
233     public int hashCode() {
234         final int prime = 31;
235         int result = 1;
236         result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
237         result = prime * result + ((version == null) ? 0 : version.hashCode());
238         return result;
239     }
240
241     @Override
242     public boolean equals(Object obj) {
243         if (this == obj) {
244             return true;
245         }
246         if (obj == null) {
247             return false;
248         }
249         if (getClass() != obj.getClass()) {
250             return false;
251         }
252         PolicyModel other = (PolicyModel) obj;
253         if (policyModelType == null) {
254             if (other.policyModelType != null) {
255                 return false;
256             }
257         } else if (!policyModelType.equals(other.policyModelType)) {
258             return false;
259         }
260         if (version == null) {
261             if (other.version != null) {
262                 return false;
263             }
264         } else if (!version.equals(other.version)) {
265             return false;
266         }
267         return true;
268     }
269
270     @Override
271     public int compareTo(PolicyModel arg0) {
272
273         if (this.getPolicyModelType().equals(arg0.getPolicyModelType())) {
274             // Reverse it, so that by default we have the latest in they are same model type
275             return SemanticVersioning.compare(arg0.getVersion(), this.version);
276         } else {
277             return this.getPolicyModelType().compareTo(arg0.getPolicyModelType());
278         }
279
280     }
281 }