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