Modify the Ui
[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",nullable = false)
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         if (this.policyAcronym == null) {
184             this.policyAcronym = createDefaultPolicyAcronym(policyType);
185         }
186     }
187
188     /**
189      * Constructor with acronym generated by default from policyType.
190      * 
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
194      */
195     public PolicyModel(String policyType, String policyModelTosca, String version) {
196         this(policyType, policyModelTosca, version, null);
197     }
198
199     public static String createDefaultPolicyAcronym(String policyType) {
200         String[] policyNameArray = policyType.split("\\.");
201         return policyNameArray[policyNameArray.length - 1];
202     }
203
204     @Override
205     public int hashCode() {
206         final int prime = 31;
207         int result = 1;
208         result = prime * result + ((policyModelType == null) ? 0 : policyModelType.hashCode());
209         result = prime * result + ((version == null) ? 0 : version.hashCode());
210         return result;
211     }
212
213     @Override
214     public boolean equals(Object obj) {
215         if (this == obj) {
216             return true;
217         }
218         if (obj == null) {
219             return false;
220         }
221         if (getClass() != obj.getClass()) {
222             return false;
223         }
224         PolicyModel other = (PolicyModel) obj;
225         if (policyModelType == null) {
226             if (other.policyModelType != null) {
227                 return false;
228             }
229         } else if (!policyModelType.equals(other.policyModelType)) {
230             return false;
231         }
232         if (version == null) {
233             if (other.version != null) {
234                 return false;
235             }
236         } else if (!version.equals(other.version)) {
237             return false;
238         }
239         return true;
240     }
241
242     @Override
243     public int compareTo(PolicyModel arg0) {
244
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);
248         } else {
249             return this.getPolicyModelType().compareTo(arg0.getPolicyModelType());
250         }
251
252     }
253 }