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.annotations.Expose;
27 import java.io.Serializable;
28 import java.util.HashSet;
30 import javax.persistence.Column;
31 import javax.persistence.Entity;
32 import javax.persistence.FetchType;
33 import javax.persistence.Id;
34 import javax.persistence.IdClass;
35 import javax.persistence.ManyToMany;
36 import javax.persistence.Table;
37 import org.onap.clamp.loop.common.AuditEntity;
38 import org.onap.clamp.util.SemanticVersioning;
41 * This class represents the policy model tosca revision that we can have to a
42 * specific microservice.
45 @Table(name = "policy_models")
46 @IdClass(PolicyModelId.class)
47 public class PolicyModel extends AuditEntity implements Serializable, Comparable<PolicyModel> {
50 * The serial version id.
52 private static final long serialVersionUID = -286522705701376645L;
55 * This variable is used to store the type mentioned in the micro-service
60 @Column(nullable = false, name = "policy_model_type")
61 private String policyModelType;
64 * Semantic versioning on policy side.
68 @Column(name = "version",nullable = false)
69 private String version;
71 @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca")
72 private String policyModelTosca;
75 @Column(name = "policy_acronym")
76 private String policyAcronym;
78 @ManyToMany(mappedBy = "policyModels", fetch = FetchType.EAGER)
79 private Set<LoopElementModel> usedByElementModels = new HashSet<>();
82 * usedByElementModels getter.
84 * @return the usedByElementModels
86 public Set<LoopElementModel> getUsedByElementModels() {
87 return usedByElementModels;
91 * policyModelTosca getter.
93 * @return the policyModelTosca
95 public String getPolicyModelTosca() {
96 return policyModelTosca;
100 * policyModelTosca setter.
102 * @param policyModelTosca the policyModelTosca to set
104 public void setPolicyModelTosca(String policyModelTosca) {
105 this.policyModelTosca = policyModelTosca;
109 * policyModelType getter.
111 * @return the modelType
113 public String getPolicyModelType() {
114 return policyModelType;
118 * policyModelType setter.
120 * @param modelType the modelType to set
122 public void setPolicyModelType(String modelType) {
123 this.policyModelType = modelType;
129 * @return the version
131 public String getVersion() {
138 * @param version the version to set
140 public void setVersion(String version) {
141 // Try to convert it before
142 this.version = version;
146 * policyAcronym getter.
148 * @return the policyAcronym value
150 public String getPolicyAcronym() {
151 return policyAcronym;
155 * policyAcronym setter.
157 * @param policyAcronym The policyAcronym to set
159 public void setPolicyAcronym(String policyAcronym) {
160 this.policyAcronym = policyAcronym;
164 * Default constructor for serialization.
166 public PolicyModel() {
172 * @param policyType The policyType (referenced in the blueprint
173 * @param policyModelTosca The policy tosca model in yaml
174 * @param version the version like 1.0.0
175 * @param policyAcronym Subtype for policy if it exists (could be used by UI)
177 public PolicyModel(String policyType, String policyModelTosca, String version,
178 String policyAcronym) {
179 this.policyModelType = policyType;
180 this.policyModelTosca = policyModelTosca;
181 this.version = version;
182 this.policyAcronym=policyAcronym;
183 if (this.policyAcronym == null) {
184 this.policyAcronym = createDefaultPolicyAcronym(policyType);
189 * Constructor with acronym generated by default from policyType.
191 * @param policyType The policyType (referenced in the blueprint
192 * @param policyModelTosca The policy tosca model in yaml
193 * @param version the version like 1.0.0
195 public PolicyModel(String policyType, String policyModelTosca, String version) {
196 this(policyType, policyModelTosca, version, null);
199 public static String createDefaultPolicyAcronym(String policyType) {
200 String[] policyNameArray = policyType.split("\\.");
201 return policyNameArray[policyNameArray.length - 1];
205 public int hashCode() {
206 final int prime = 31;
208 result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
209 result = prime * result + ((version == null) ? 0 : version.hashCode());
214 public boolean equals(Object obj) {
221 if (getClass() != obj.getClass()) {
224 PolicyModel other = (PolicyModel) obj;
225 if (policyModelType == null) {
226 if (other.policyModelType != null) {
229 } else if (!policyModelType.equals(other.policyModelType)) {
232 if (version == null) {
233 if (other.version != null) {
236 } else if (!version.equals(other.version)) {
243 public int compareTo(PolicyModel arg0) {
245 if (this.getPolicyModelType().equals(arg0.getPolicyModelType())) {
246 // Reverse it, so that by default we have the latest in they are same model type
247 return SemanticVersioning.compare(arg0.getVersion(), this.version);
249 return this.getPolicyModelType().compareTo(arg0.getPolicyModelType());