Update UI to define Pdp Group
[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     @Expose
88     @Type(type = "json")
89     @Column(columnDefinition = "json", name = "policy_pdp_group")
90     private JsonObject policyPdpGroup;
91
92     /**
93      * usedByElementModels getter.
94      *
95      * @return the usedByElementModels
96      */
97     public Set<LoopElementModel> getUsedByElementModels() {
98         return usedByElementModels;
99     }
100
101     /**
102      * policyPdpGroup getter.
103      *
104      * @return the policyPdpGroup
105      */
106     public JsonObject getPolicyPdpGroup() {
107         return policyPdpGroup;
108     }
109
110     /**
111      * policyPdpGroup setter.
112      *
113      * @param policyPdpGroup the policyPdpGroup to set
114      */
115     public void setPolicyPdpGroup(JsonObject policyPdpGroup) {
116         this.policyPdpGroup = policyPdpGroup;
117     }
118
119     /**
120      * policyModelTosca getter.
121      *
122      * @return the policyModelTosca
123      */
124     public String getPolicyModelTosca() {
125         return policyModelTosca;
126     }
127
128     /**
129      * policyModelTosca setter.
130      *
131      * @param policyModelTosca the policyModelTosca to set
132      */
133     public void setPolicyModelTosca(String policyModelTosca) {
134         this.policyModelTosca = policyModelTosca;
135     }
136
137     /**
138      * policyModelType getter.
139      *
140      * @return the modelType
141      */
142     public String getPolicyModelType() {
143         return policyModelType;
144     }
145
146     /**
147      * policyModelType setter.
148      *
149      * @param modelType the modelType to set
150      */
151     public void setPolicyModelType(String modelType) {
152         this.policyModelType = modelType;
153     }
154
155     /**
156      * version getter.
157      *
158      * @return the version
159      */
160     public String getVersion() {
161         return version;
162     }
163
164     /**
165      * version setter.
166      *
167      * @param version the version to set
168      */
169     public void setVersion(String version) {
170         // Try to convert it before
171         this.version = version;
172     }
173
174     /**
175      * policyAcronym getter.
176      *
177      * @return the policyAcronym value
178      */
179     public String getPolicyAcronym() {
180         return policyAcronym;
181     }
182
183     /**
184      * policyAcronym setter.
185      *
186      * @param policyAcronym The policyAcronym to set
187      */
188     public void setPolicyAcronym(String policyAcronym) {
189         this.policyAcronym = policyAcronym;
190     }
191
192     /**
193      * Default constructor for serialization.
194      */
195     public PolicyModel() {
196     }
197
198     /**
199      * Constructor.
200      *
201      * @param policyType       The policyType (referenced in the blueprint
202      * @param policyModelTosca The policy tosca model in yaml
203      * @param version          the version like 1.0.0
204      * @param policyAcronym    Subtype for policy if it exists (could be used by UI)
205      */
206     public PolicyModel(String policyType, String policyModelTosca, String version,
207                        String policyAcronym) {
208         this.policyModelType = policyType;
209         this.policyModelTosca = policyModelTosca;
210         this.version = version;
211         this.policyAcronym = policyAcronym;
212         if (this.policyAcronym == null) {
213             this.policyAcronym = createDefaultPolicyAcronym(policyType);
214         }
215     }
216
217     /**
218      * Constructor with acronym generated by default from policyType.
219      *
220      * @param policyType       The policyType (referenced in the blueprint
221      * @param policyModelTosca The policy tosca model in yaml
222      * @param version          the version like 1.0.0
223      */
224     public PolicyModel(String policyType, String policyModelTosca, String version) {
225         this(policyType, policyModelTosca, version, null);
226     }
227
228     public static String createDefaultPolicyAcronym(String policyType) {
229         String[] policyNameArray = policyType.split("\\.");
230         return policyNameArray[policyNameArray.length - 1];
231     }
232
233     @Override
234     public int hashCode() {
235         final int prime = 31;
236         int result = 1;
237         result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
238         result = prime * result + ((version == null) ? 0 : version.hashCode());
239         return result;
240     }
241
242     @Override
243     public boolean equals(Object obj) {
244         if (this == obj) {
245             return true;
246         }
247         if (obj == null) {
248             return false;
249         }
250         if (getClass() != obj.getClass()) {
251             return false;
252         }
253         PolicyModel other = (PolicyModel) obj;
254         if (policyModelType == null) {
255             if (other.policyModelType != null) {
256                 return false;
257             }
258         } else if (!policyModelType.equals(other.policyModelType)) {
259             return false;
260         }
261         if (version == null) {
262             if (other.version != null) {
263                 return false;
264             }
265         } else if (!version.equals(other.version)) {
266             return false;
267         }
268         return true;
269     }
270
271     @Override
272     public int compareTo(PolicyModel arg0) {
273
274         if (this.getPolicyModelType().equals(arg0.getPolicyModelType())) {
275             // Reverse it, so that by default we have the latest in they are same model type
276             return SemanticVersioning.compare(arg0.getVersion(), this.version);
277         } else {
278             return this.getPolicyModelType().compareTo(arg0.getPolicyModelType());
279         }
280
281     }
282 }