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<>();
88 @Column(columnDefinition = "json", name = "policy_pdp_group")
89 private JsonObject policyPdpGroup;
92 * usedByElementModels getter.
94 * @return the usedByElementModels
96 public Set<LoopElementModel> getUsedByElementModels() {
97 return usedByElementModels;
101 * policyPdpGroup getter.
103 * @return the policyPdpGroup
105 public JsonObject getPolicyPdpGroup() {
106 return policyPdpGroup;
110 * policyPdpGroup setter.
112 * @param policyPdpGroup the policyPdpGroup to set
114 public void setPolicyPdpGroup(JsonObject policyPdpGroup) {
115 this.policyPdpGroup = policyPdpGroup;
119 * policyModelTosca getter.
121 * @return the policyModelTosca
123 public String getPolicyModelTosca() {
124 return policyModelTosca;
128 * policyModelTosca setter.
130 * @param policyModelTosca the policyModelTosca to set
132 public void setPolicyModelTosca(String policyModelTosca) {
133 this.policyModelTosca = policyModelTosca;
137 * policyModelType getter.
139 * @return the modelType
141 public String getPolicyModelType() {
142 return policyModelType;
146 * policyModelType setter.
148 * @param modelType the modelType to set
150 public void setPolicyModelType(String modelType) {
151 this.policyModelType = modelType;
157 * @return the version
159 public String getVersion() {
166 * @param version the version to set
168 public void setVersion(String version) {
169 // Try to convert it before
170 this.version = version;
174 * policyAcronym getter.
176 * @return the policyAcronym value
178 public String getPolicyAcronym() {
179 return policyAcronym;
183 * policyAcronym setter.
185 * @param policyAcronym The policyAcronym to set
187 public void setPolicyAcronym(String policyAcronym) {
188 this.policyAcronym = policyAcronym;
192 * Default constructor for serialization.
194 public PolicyModel() {
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)
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);
217 * Constructor with acronym generated by default from policyType.
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
223 public PolicyModel(String policyType, String policyModelTosca, String version) {
224 this(policyType, policyModelTosca, version, null);
227 public static String createDefaultPolicyAcronym(String policyType) {
228 String[] policyNameArray = policyType.split("\\.");
229 return policyNameArray[policyNameArray.length - 1];
233 public int hashCode() {
234 final int prime = 31;
236 result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
237 result = prime * result + ((version == null) ? 0 : version.hashCode());
242 public boolean equals(Object obj) {
249 if (getClass() != obj.getClass()) {
252 PolicyModel other = (PolicyModel) obj;
253 if (policyModelType == null) {
254 if (other.policyModelType != null) {
257 } else if (!policyModelType.equals(other.policyModelType)) {
260 if (version == null) {
261 if (other.version != null) {
264 } else if (!version.equals(other.version)) {
271 public int compareTo(PolicyModel arg0) {
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);
277 return this.getPolicyModelType().compareTo(arg0.getPolicyModelType());