Entitlement Pool - Support Type Field
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-api / src / main / java / org / openecomp / sdc / vendorlicense / dao / types / EntitlementPoolEntity.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.dao.types;
22
23 import com.datastax.driver.mapping.annotations.ClusteringColumn;
24 import com.datastax.driver.mapping.annotations.Column;
25 import com.datastax.driver.mapping.annotations.Frozen;
26 import com.datastax.driver.mapping.annotations.PartitionKey;
27 import com.datastax.driver.mapping.annotations.Table;
28
29 import org.apache.commons.lang3.StringUtils;
30 import org.openecomp.sdc.vendorlicense.VendorLicenseUtil;
31 import org.openecomp.sdc.vendorlicense.dao.types.xml.LimitForXml;
32 import org.openecomp.sdc.vendorlicense.dao.types.xml.LimitXml;
33 import org.openecomp.sdc.vendorlicense.dao.types.xml.OperationalScopeForXml;
34 import org.openecomp.sdc.vendorlicense.dao.types.xml.ThresholdForXml;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
37
38 import java.util.Collection;
39 import java.util.HashSet;
40 import java.util.Objects;
41 import java.util.Set;
42
43 @Table(keyspace = "dox", name = "entitlement_pool")
44 public class EntitlementPoolEntity implements VersionableEntity {
45   private static final String ENTITY_TYPE = "Entitlement Pool";
46
47   @PartitionKey
48   @Column(name = "vlm_id")
49   private String vendorLicenseModelId;
50
51   @PartitionKey(value = 1)
52   @Frozen
53   private Version version;
54
55   @ClusteringColumn
56   @Column(name = "ep_id")
57   private String id;
58   private String name;
59   private String description;
60   private EntitlementPoolType type;
61
62   @Column(name = "threshold")
63   private Integer thresholdValue;
64
65   @Column(name = "threshold_unit")
66   private ThresholdUnit thresholdUnit;
67
68   private String increments;
69
70   @Column(name = "operational_scope")
71   @Frozen
72   private MultiChoiceOrOther<OperationalScope> operationalScope;
73
74   @Column(name = "ref_fg_ids")
75   private Set<String> referencingFeatureGroups = new HashSet<>();
76
77   @Column(name = "version_uuid")
78   private String versionUuId;
79
80
81   private String startDate;
82   private String expiryDate;
83
84   private Collection<LimitEntity> limits;
85
86   //Defined and used only for License Artifcat XMLs
87   private String manufacturerReferenceNumber;
88
89   /**
90    * Every entity class must have a default constructor according to
91    * <a href="http://docs.datastax.com/en/developer/java-driver/2.1/manual/object_mapper/creating/">
92    * Definition of mapped classes</a>.
93    */
94   public EntitlementPoolEntity() {
95     // Don't delete! Default constructor is required by DataStax driver
96   }
97
98   /**
99    * Instantiates a new Entitlement pool entity.
100    *
101    * @param vlmId   the vlm id
102    * @param version the version
103    * @param id      the id
104    */
105   public EntitlementPoolEntity(String vlmId, Version version, String id) {
106     this.vendorLicenseModelId = vlmId;
107     this.version = version;
108     this.id = id;
109   }
110
111   @Override
112   public String getEntityType() {
113     return ENTITY_TYPE;
114   }
115
116   @Override
117   public String getFirstClassCitizenId() {
118     return getVendorLicenseModelId();
119   }
120
121   @Override
122   public String getId() {
123     return id;
124   }
125
126   @Override
127   public void setId(String id) {
128     this.id = id;
129   }
130
131   @Override
132   public Version getVersion() {
133     return version;
134   }
135
136   @Override
137   public void setVersion(Version version) {
138     this.version = version;
139   }
140
141   @Override
142   public String getVersionUuId() {
143     return versionUuId;
144   }
145
146   @Override
147   public void setVersionUuId(String uuId) {
148     versionUuId = uuId;
149   }
150
151   public String getVendorLicenseModelId() {
152     return vendorLicenseModelId;
153   }
154
155   public void setVendorLicenseModelId(String vendorLicenseModelId) {
156     this.vendorLicenseModelId = vendorLicenseModelId;
157   }
158
159   public Set<String> getReferencingFeatureGroups() {
160     return referencingFeatureGroups;
161   }
162
163   public void setReferencingFeatureGroups(Set<String> referencingFeatureGroups) {
164     this.referencingFeatureGroups = referencingFeatureGroups;
165   }
166
167   public String getName() {
168     return name;
169   }
170
171   public void setName(String name) {
172     this.name = name;
173   }
174
175   public String getDescription() {
176     return description;
177   }
178
179   public void setDescription(String description) {
180     this.description = description;
181   }
182
183   public EntitlementPoolType getType() {
184     return type;
185   }
186
187   public void setType(EntitlementPoolType type) {
188     this.type = type;
189   }
190
191   public Integer getThresholdValue() {
192     return thresholdValue;
193   }
194
195   public void setThresholdValue(Integer thresholdValue) {
196     this.thresholdValue = thresholdValue;
197   }
198
199   public ThresholdUnit getThresholdUnit() {
200     return thresholdUnit;
201   }
202
203   public void setThresholdUnit(ThresholdUnit thresholdUnits) {
204     this.thresholdUnit = thresholdUnits;
205   }
206
207   public String getIncrements() {
208     return increments;
209   }
210
211   public void setIncrements(String increments) {
212     this.increments = increments;
213   }
214
215   public MultiChoiceOrOther<OperationalScope> getOperationalScope() {
216     return operationalScope;
217   }
218
219   public void setOperationalScope(MultiChoiceOrOther<OperationalScope> operationalScope) {
220     if (operationalScope != null) {
221       operationalScope.resolveEnum(OperationalScope.class);
222     }
223     this.operationalScope = operationalScope;
224   }
225
226   /**
227    * Gets threshold for artifact.
228    *
229    * @return the threshold for artifact
230    */
231   public ThresholdForXml getThresholdForArtifact() {
232     ThresholdForXml threshold = new ThresholdForXml();
233     threshold.setUnit(getThresholdUnit() == null ? null : getThresholdUnit().name());
234     threshold.setValue(getThresholdValue());
235     return threshold;
236   }
237
238   /**
239    * Gets version for artifact.
240    *
241    * @return version in format suitable for artifact
242    */
243   public String getVersionForArtifact() {
244     return version.toString();
245   }
246
247   public String getStartDate() {
248     return startDate;
249   }
250
251   public void setStartDate(String startDate) {
252     this.startDate = startDate;
253   }
254
255   public String getExpiryDate() {
256     return expiryDate;
257   }
258
259   public void setExpiryDate(String expiryDate) {
260     this.expiryDate = expiryDate;
261   }
262
263   public Collection<LimitEntity> getLimits() {
264     return limits;
265   }
266
267   public void setLimits(Collection<LimitEntity> limits) {
268     this.limits = limits;
269   }
270
271   public LimitForXml getSPLimits() {
272     if (limits != null) {
273       Set<LimitXml> hs = new HashSet<>();
274       for (LimitEntity obj : limits) {
275         if (obj.getType().equals(LimitType.ServiceProvider)) {
276           LimitXml xmlObj = new LimitXml();
277           xmlObj.setDescription(obj.getDescription());
278           xmlObj.setMetric(obj.getMetric());
279           xmlObj.setValues(obj.getValue());
280           xmlObj.setUnit(obj.getUnit());
281           xmlObj.setAggregationFunction(
282               obj.getAggregationFunction() != null ? obj.getAggregationFunction().name() : null);
283           xmlObj.setTime(obj.getTime());
284           hs.add(xmlObj);
285         }
286       }
287       LimitForXml spLimitForXml = new LimitForXml();
288       spLimitForXml.setLimits(hs);
289       return spLimitForXml;
290     }
291
292     return null;
293   }
294
295   public LimitForXml getVendorLimits() {
296     if (limits != null) {
297       Set<LimitXml> hs = new HashSet<>();
298       for (LimitEntity obj : limits) {
299         if (obj.getType().equals(LimitType.Vendor)) {
300           LimitXml xmlObj = new LimitXml();
301           xmlObj.setDescription(obj.getDescription());
302           xmlObj.setMetric(obj.getMetric());
303           xmlObj.setValues(obj.getValue());
304           xmlObj.setUnit(obj.getUnit());
305           xmlObj.setAggregationFunction(
306               obj.getAggregationFunction() != null ? obj.getAggregationFunction().name() : null);
307           xmlObj.setTime(obj.getTime());
308           hs.add(xmlObj);
309         }
310       }
311       LimitForXml vendorLimitForXml = new LimitForXml();
312       vendorLimitForXml.setLimits(hs);
313       return vendorLimitForXml;
314     }
315
316     return null;
317   }
318
319
320   @Override
321   public int hashCode() {
322     return Objects
323         .hash(vendorLicenseModelId, version, id, name, description, type, thresholdValue, thresholdUnit,
324             increments, operationalScope, referencingFeatureGroups, startDate, expiryDate);
325   }
326
327   @Override
328   public boolean equals(Object obj) {
329     if (this == obj) {
330       return true;
331     }
332     if (obj == null || getClass() != obj.getClass()) {
333       return false;
334     }
335     EntitlementPoolEntity that = (EntitlementPoolEntity) obj;
336     return Objects.equals(that.thresholdValue, thresholdValue)
337         && Objects.equals(vendorLicenseModelId, that.vendorLicenseModelId)
338         && Objects.equals(id, that.id)
339         && Objects.equals(name, that.name)
340         && Objects.equals(description, that.description)
341         && type == that.type
342         && Objects.equals(thresholdUnit, that.thresholdUnit)
343         && Objects.equals(increments, that.increments)
344         && Objects.equals(that.operationalScope, operationalScope)
345         && Objects.equals(startDate, that.startDate)
346         && Objects.equals(expiryDate, that.expiryDate)
347         && Objects.equals(manufacturerReferenceNumber, that.manufacturerReferenceNumber)
348         && Objects.equals(version, that.version);
349   }
350
351   @Override
352   public String toString() {
353     return "EntitlementPoolEntity{"
354         + "vendorLicenseModelId='" + vendorLicenseModelId + '\''
355         + ", version=" + version
356         + ", id='" + id + '\''
357         + ", name='" + name + '\''
358         + ", description='" + description + '\''
359         + ", type=" + type
360         + ", thresholdValue=" + thresholdValue
361         + ", thresholdUnit='" + thresholdUnit + '\''
362         + ", increments='" + increments + '\''
363         + ", operationalScope=" + operationalScope
364         + ", referencingFeatureGroups=" + referencingFeatureGroups
365         + ", version_uuid=" + versionUuId
366         + ", startDate=" + startDate
367         + ", expiryDate=" + expiryDate
368         + '}';
369   }
370
371   /**
372    * Gets operational scope for artifact.
373    *
374    * @return the operational scope for artifact
375    */
376   public OperationalScopeForXml getOperationalScopeForArtifact() {
377     OperationalScopeForXml obj = new OperationalScopeForXml();
378     if (operationalScope != null) {
379       if (operationalScope.getResults().size() > 0) {
380         obj.setValue(operationalScope.getResults());
381       }
382     }
383     return obj;
384   }
385
386   //Defined and used only for License Artifcat XMLs
387   public void setManufacturerReferenceNumber(String manufacturerReferenceNumber) {
388     this.manufacturerReferenceNumber = manufacturerReferenceNumber;
389   }
390
391   public String getManufacturerReferenceNumber() {
392     return manufacturerReferenceNumber;
393   }
394
395   public String getIsoFormatStartDate() {
396     String isoFormatStartDate = null;
397     if (!StringUtils.isEmpty(startDate)) {
398       isoFormatStartDate = VendorLicenseUtil.getIsoFormatDate(startDate);
399     }
400     return isoFormatStartDate;
401   }
402
403
404   public String getIsoFormatExpiryDate() {
405     String isoFormatExpDate = null;
406     if (!StringUtils.isEmpty(expiryDate)) {
407       isoFormatExpDate = VendorLicenseUtil.getIsoFormatDate(expiryDate);
408     }
409     return isoFormatExpDate;
410   }
411 }