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;
28 import java.io.Serializable;
29 import java.util.HashSet;
32 import javax.persistence.Column;
33 import javax.persistence.Entity;
34 import javax.persistence.FetchType;
35 import javax.persistence.Id;
36 import javax.persistence.IdClass;
37 import javax.persistence.ManyToMany;
38 import javax.persistence.Table;
40 import org.onap.clamp.loop.common.AuditEntity;
41 import org.onap.clamp.util.SemanticVersioning;
44 * This class represents the policy model tosca revision that we can have to a
45 * specific microservice.
48 @Table(name = "policy_models")
49 @IdClass(PolicyModelId.class)
50 public class PolicyModel extends AuditEntity implements Serializable, Comparable<PolicyModel> {
53 * The serial version id.
55 private static final long serialVersionUID = -286522705701376645L;
58 * This variable is used to store the type mentioned in the micro-service
63 @Column(nullable = false, name = "policy_model_type")
64 private String policyModelType;
67 * Semantic versioning on policy side.
71 @Column(name = "version")
72 private String version;
74 @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca")
75 private String policyModelTosca;
78 @Column(name = "policy_acronym")
79 private String policyAcronym;
81 @ManyToMany(mappedBy = "policyModels", fetch = FetchType.EAGER)
82 private Set<LoopElementModel> usedByElementModels = new HashSet<>();
85 * usedByElementModels getter.
87 * @return the usedByElementModels
89 public Set<LoopElementModel> getUsedByElementModels() {
90 return usedByElementModels;
94 * policyModelTosca getter.
96 * @return the policyModelTosca
98 public String getPolicyModelTosca() {
99 return policyModelTosca;
103 * policyModelTosca setter.
105 * @param policyModelTosca the policyModelTosca to set
107 public void setPolicyModelTosca(String policyModelTosca) {
108 this.policyModelTosca = policyModelTosca;
112 * policyModelType getter.
114 * @return the modelType
116 public String getPolicyModelType() {
117 return policyModelType;
121 * policyModelType setter.
123 * @param modelType the modelType to set
125 public void setPolicyModelType(String modelType) {
126 this.policyModelType = modelType;
132 * @return the version
134 public String getVersion() {
141 * @param version the version to set
143 public void setVersion(String version) {
144 // Try to convert it before
145 this.version = version;
149 * policyAcronym getter.
151 * @return the policyAcronym value
153 public String getPolicyAcronym() {
154 return policyAcronym;
158 * policyAcronym setter.
160 * @param policyAcronym The policyAcronym to set
162 public void setPolicyAcronym(String policyAcronym) {
163 this.policyAcronym = policyAcronym;
167 * Default constructor for serialization.
169 public PolicyModel() {
175 * @param policyType The policyType (referenced in the blueprint
176 * @param policyModelTosca The policy tosca model in yaml
177 * @param version the version like 1.0.0
178 * @param policyAcronym Subtype for policy if it exists (could be used by UI)
180 public PolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym) {
181 this.policyModelType = policyType;
182 this.policyModelTosca = policyModelTosca;
183 this.version = version;
184 this.policyAcronym = policyAcronym;
188 * Constructor with acronym generated by default from policyType.
190 * @param policyType The policyType (referenced in the blueprint
191 * @param policyModelTosca The policy tosca model in yaml
192 * @param version the version like 1.0.0
194 public PolicyModel(String policyType, String policyModelTosca, String version) {
195 this(policyType, policyModelTosca, version, createDefaultPolicyAcronym(policyType));
198 public static String createDefaultPolicyAcronym(String policyType) {
199 String[] policyNameArray = policyType.split("\\.");
200 return policyNameArray[policyNameArray.length - 1];
204 public int hashCode() {
205 final int prime = 31;
207 result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
208 result = prime * result + ((version == null) ? 0 : version.hashCode());
213 public boolean equals(Object obj) {
220 if (getClass() != obj.getClass()) {
223 PolicyModel other = (PolicyModel) obj;
224 if (policyModelType == null) {
225 if (other.policyModelType != null) {
228 } else if (!policyModelType.equals(other.policyModelType)) {
231 if (version == null) {
232 if (other.version != null) {
235 } else if (!version.equals(other.version)) {
242 public int compareTo(PolicyModel arg0) {
244 if (this.getPolicyModelType().equals(arg0.getPolicyModelType())) {
245 // Reverse it, so that by default we have the latest in they are same model type
246 return SemanticVersioning.compare(arg0.getVersion(), this.version);
248 return this.getPolicyModelType().compareTo(arg0.getPolicyModelType());