Get policy in CsarInstaller
[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.annotations.Expose;
27
28 import java.io.Serializable;
29 import java.util.HashSet;
30 import java.util.Set;
31
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;
39
40 import org.onap.clamp.loop.common.AuditEntity;
41 import org.onap.clamp.util.SemanticVersioning;
42
43 /**
44  * This class represents the policy model tosca revision that we can have to a
45  * specific microservice.
46  */
47 @Entity
48 @Table(name = "policy_models")
49 @IdClass(PolicyModelId.class)
50 public class PolicyModel extends AuditEntity implements Serializable, Comparable<PolicyModel> {
51
52     /**
53      * The serial version id.
54      */
55     private static final long serialVersionUID = -286522705701376645L;
56
57     /**
58      * This variable is used to store the type mentioned in the micro-service
59      * blueprint.
60      */
61     @Id
62     @Expose
63     @Column(nullable = false, name = "policy_model_type")
64     private String policyModelType;
65
66     /**
67      * Semantic versioning on policy side.
68      */
69     @Id
70     @Expose
71     @Column(name = "version")
72     private String version;
73
74     @Column(columnDefinition = "MEDIUMTEXT", name = "policy_tosca")
75     private String policyModelTosca;
76
77     @Expose
78     @Column(name = "policy_acronym")
79     private String policyAcronym;
80
81     @ManyToMany(mappedBy = "policyModels", fetch = FetchType.EAGER)
82     private Set<LoopElementModel> usedByElementModels = new HashSet<>();
83
84     /**
85      * usedByElementModels getter.
86      * 
87      * @return the usedByElementModels
88      */
89     public Set<LoopElementModel> getUsedByElementModels() {
90         return usedByElementModels;
91     }
92
93     /**
94      * policyModelTosca getter.
95      * 
96      * @return the policyModelTosca
97      */
98     public String getPolicyModelTosca() {
99         return policyModelTosca;
100     }
101
102     /**
103      * policyModelTosca setter.
104      * 
105      * @param policyModelTosca the policyModelTosca to set
106      */
107     public void setPolicyModelTosca(String policyModelTosca) {
108         this.policyModelTosca = policyModelTosca;
109     }
110
111     /**
112      * policyModelType getter.
113      * 
114      * @return the modelType
115      */
116     public String getPolicyModelType() {
117         return policyModelType;
118     }
119
120     /**
121      * policyModelType setter.
122      * 
123      * @param modelType the modelType to set
124      */
125     public void setPolicyModelType(String modelType) {
126         this.policyModelType = modelType;
127     }
128
129     /**
130      * version getter.
131      * 
132      * @return the version
133      */
134     public String getVersion() {
135         return version;
136     }
137
138     /**
139      * version setter.
140      * 
141      * @param version the version to set
142      */
143     public void setVersion(String version) {
144         // Try to convert it before
145         this.version = version;
146     }
147
148     /**
149      * policyAcronym getter.
150      * 
151      * @return the policyAcronym value
152      */
153     public String getPolicyAcronym() {
154         return policyAcronym;
155     }
156
157     /**
158      * policyAcronym setter.
159      * 
160      * @param policyAcronym The policyAcronym to set
161      */
162     public void setPolicyAcronym(String policyAcronym) {
163         this.policyAcronym = policyAcronym;
164     }
165
166     /**
167      * Default constructor for serialization.
168      */
169     public PolicyModel() {
170     }
171
172     /**
173      * Constructor.
174      * 
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)
179      */
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;
185     }
186
187     @Override
188     public int hashCode() {
189         final int prime = 31;
190         int result = 1;
191         result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
192         result = prime * result + ((version == null) ? 0 : version.hashCode());
193         return result;
194     }
195
196     @Override
197     public boolean equals(Object obj) {
198         if (this == obj) {
199             return true;
200         }
201         if (obj == null) {
202             return false;
203         }
204         if (getClass() != obj.getClass()) {
205             return false;
206         }
207         PolicyModel other = (PolicyModel) obj;
208         if (policyModelType == null) {
209             if (other.policyModelType != null) {
210                 return false;
211             }
212         } else if (!policyModelType.equals(other.policyModelType)) {
213             return false;
214         }
215         if (version == null) {
216             if (other.version != null) {
217                 return false;
218             }
219         } else if (!version.equals(other.version)) {
220             return false;
221         }
222         return true;
223     }
224
225     @Override
226     public int compareTo(PolicyModel arg0) {
227
228         if (this.getPolicyModelType().equals(arg0.getPolicyModelType())) {
229             // Reverse it, so that by default we have the latest in they are same model type
230             return SemanticVersioning.compare(arg0.getVersion(), this.version);
231         } else {
232             return this.getPolicyModelType().compareTo(arg0.getPolicyModelType());
233         }
234
235     }
236 }