2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop.template;
26 import com.google.gson.JsonObject;
27 import com.google.gson.annotations.Expose;
28 import java.io.Serializable;
29 import java.util.HashSet;
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;
46 * This class represents the policy model tosca revision that we can have to a
47 * specific microservice.
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> {
56 * The serial version id.
58 private static final long serialVersionUID = -286522705701376645L;
61 * This variable is used to store the type mentioned in the micro-service
66 @Column(nullable = false, name = "policy_model_type")
67 private String policyModelType;
70 * Semantic versioning on policy side.
74 @Column(name = "version", nullable = false)
75 private String version;
77 @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca")
78 private String policyModelTosca;
81 @Column(name = "policy_acronym")
82 private String policyAcronym;
84 @ManyToMany(mappedBy = "policyModels", fetch = FetchType.EAGER)
85 private Set<LoopElementModel> usedByElementModels = new HashSet<>();
89 @Column(columnDefinition = "json", name = "policy_pdp_group")
90 private JsonObject policyPdpGroup;
93 * usedByElementModels getter.
95 * @return the usedByElementModels
97 public Set<LoopElementModel> getUsedByElementModels() {
98 return usedByElementModels;
102 * policyPdpGroup getter.
104 * @return the policyPdpGroup
106 public JsonObject getPolicyPdpGroup() {
107 return policyPdpGroup;
111 * policyPdpGroup setter.
113 * @param policyPdpGroup the policyPdpGroup to set
115 public void setPolicyPdpGroup(JsonObject policyPdpGroup) {
116 this.policyPdpGroup = policyPdpGroup;
120 * policyModelTosca getter.
122 * @return the policyModelTosca
124 public String getPolicyModelTosca() {
125 return policyModelTosca;
129 * policyModelTosca setter.
131 * @param policyModelTosca the policyModelTosca to set
133 public void setPolicyModelTosca(String policyModelTosca) {
134 this.policyModelTosca = policyModelTosca;
138 * policyModelType getter.
140 * @return the modelType
142 public String getPolicyModelType() {
143 return policyModelType;
147 * policyModelType setter.
149 * @param modelType the modelType to set
151 public void setPolicyModelType(String modelType) {
152 this.policyModelType = modelType;
158 * @return the version
160 public String getVersion() {
167 * @param version the version to set
169 public void setVersion(String version) {
170 // Try to convert it before
171 this.version = version;
175 * policyAcronym getter.
177 * @return the policyAcronym value
179 public String getPolicyAcronym() {
180 return policyAcronym;
184 * policyAcronym setter.
186 * @param policyAcronym The policyAcronym to set
188 public void setPolicyAcronym(String policyAcronym) {
189 this.policyAcronym = policyAcronym;
193 * Default constructor for serialization.
195 public PolicyModel() {
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)
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);
218 * Constructor with acronym generated by default from policyType.
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
224 public PolicyModel(String policyType, String policyModelTosca, String version) {
225 this(policyType, policyModelTosca, version, null);
228 public static String createDefaultPolicyAcronym(String policyType) {
229 String[] policyNameArray = policyType.split("\\.");
230 return policyNameArray[policyNameArray.length - 1];
234 public int hashCode() {
235 final int prime = 31;
237 result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
238 result = prime * result + ((version == null) ? 0 : version.hashCode());
243 public boolean equals(Object obj) {
250 if (getClass() != obj.getClass()) {
253 PolicyModel other = (PolicyModel) obj;
254 if (policyModelType == null) {
255 if (other.policyModelType != null) {
258 } else if (!policyModelType.equals(other.policyModelType)) {
261 if (version == null) {
262 if (other.version != null) {
265 } else if (!version.equals(other.version)) {
272 public int compareTo(PolicyModel arg0) {
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);
278 return this.getPolicyModelType().compareTo(arg0.getPolicyModelType());