re base code
[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
61   @Column(name = "threshold")
62   private Integer thresholdValue;
63
64   @Column(name = "threshold_unit")
65   private ThresholdUnit thresholdUnit;
66
67   private String increments;
68
69   @Column(name = "operational_scope")
70   @Frozen
71   private MultiChoiceOrOther<OperationalScope> operationalScope;
72
73   @Column(name = "ref_fg_ids")
74   private Set<String> referencingFeatureGroups = new HashSet<>();
75
76   @Column(name = "version_uuid")
77   private String versionUuId;
78
79
80   private String startDate;
81   private String expiryDate;
82
83   private Collection<LimitEntity> limits;
84
85   //Defined and used only for License Artifcat XMLs
86   private String manufacturerReferenceNumber;
87
88   /**
89    * Every entity class must have a default constructor according to
90    * <a href="http://docs.datastax.com/en/developer/java-driver/2.1/manual/object_mapper/creating/">
91    * Definition of mapped classes</a>.
92    */
93   public EntitlementPoolEntity() {
94     // Don't delete! Default constructor is required by DataStax driver
95   }
96
97   /**
98    * Instantiates a new Entitlement pool entity.
99    *
100    * @param vlmId   the vlm id
101    * @param version the version
102    * @param id      the id
103    */
104   public EntitlementPoolEntity(String vlmId, Version version, String id) {
105     this.vendorLicenseModelId = vlmId;
106     this.version = version;
107     this.id = id;
108   }
109
110   @Override
111   public String getEntityType() {
112     return ENTITY_TYPE;
113   }
114
115   @Override
116   public String getFirstClassCitizenId() {
117     return getVendorLicenseModelId();
118   }
119
120   @Override
121   public String getId() {
122     return id;
123   }
124
125   @Override
126   public void setId(String id) {
127     this.id = id;
128   }
129
130   @Override
131   public Version getVersion() {
132     return version;
133   }
134
135   @Override
136   public void setVersion(Version version) {
137     this.version = version;
138   }
139
140   @Override
141   public String getVersionUuId() {
142     return versionUuId;
143   }
144
145   @Override
146   public void setVersionUuId(String uuId) {
147     versionUuId = uuId;
148   }
149
150   public String getVendorLicenseModelId() {
151     return vendorLicenseModelId;
152   }
153
154   public void setVendorLicenseModelId(String vendorLicenseModelId) {
155     this.vendorLicenseModelId = vendorLicenseModelId;
156   }
157
158   public Set<String> getReferencingFeatureGroups() {
159     return referencingFeatureGroups;
160   }
161
162   public void setReferencingFeatureGroups(Set<String> referencingFeatureGroups) {
163     this.referencingFeatureGroups = referencingFeatureGroups;
164   }
165
166   public String getName() {
167     return name;
168   }
169
170   public void setName(String name) {
171     this.name = name;
172   }
173
174   public String getDescription() {
175     return description;
176   }
177
178   public void setDescription(String description) {
179     this.description = description;
180   }
181
182   public Integer getThresholdValue() {
183     return thresholdValue;
184   }
185
186   public void setThresholdValue(Integer thresholdValue) {
187     this.thresholdValue = thresholdValue;
188   }
189
190   public ThresholdUnit getThresholdUnit() {
191     return thresholdUnit;
192   }
193
194   public void setThresholdUnit(ThresholdUnit thresholdUnits) {
195     this.thresholdUnit = thresholdUnits;
196   }
197
198   public String getIncrements() {
199     return increments;
200   }
201
202   public void setIncrements(String increments) {
203     this.increments = increments;
204   }
205
206   public MultiChoiceOrOther<OperationalScope> getOperationalScope() {
207     return operationalScope;
208   }
209
210   public void setOperationalScope(MultiChoiceOrOther<OperationalScope> operationalScope) {
211     if (operationalScope != null) {
212       operationalScope.resolveEnum(OperationalScope.class);
213     }
214     this.operationalScope = operationalScope;
215   }
216
217   /**
218    * Gets threshold for artifact.
219    *
220    * @return the threshold for artifact
221    */
222   public ThresholdForXml getThresholdForArtifact() {
223     ThresholdForXml threshold = new ThresholdForXml();
224     threshold.setUnit(getThresholdUnit() == null ? null : getThresholdUnit().name());
225     threshold.setValue(getThresholdValue());
226     return threshold;
227   }
228
229   /**
230    * Gets version for artifact.
231    *
232    * @return version in format suitable for artifact
233    */
234   public String getVersionForArtifact() {
235     return version.toString();
236   }
237
238   public String getStartDate() {
239     return startDate;
240   }
241
242   public void setStartDate(String startDate) {
243     this.startDate = startDate;
244   }
245
246   public String getExpiryDate() {
247     return expiryDate;
248   }
249
250   public void setExpiryDate(String expiryDate) {
251     this.expiryDate = expiryDate;
252   }
253
254   public Collection<LimitEntity> getLimits() {
255     return limits;
256   }
257
258   public void setLimits(Collection<LimitEntity> limits) {
259     this.limits = limits;
260   }
261
262   public LimitForXml getSPLimits() {
263     if (limits != null) {
264       Set<LimitXml> hs = new HashSet<>();
265       for (LimitEntity obj : limits) {
266         if (obj.getType().equals(LimitType.ServiceProvider)) {
267           LimitXml xmlObj = new LimitXml();
268           xmlObj.setDescription(obj.getDescription());
269           xmlObj.setMetric(obj.getMetric());
270           xmlObj.setValues(obj.getValue());
271           xmlObj.setUnit(obj.getUnit());
272           xmlObj.setAggregationFunction(
273               obj.getAggregationFunction() != null ? obj.getAggregationFunction().name() : null);
274           xmlObj.setTime(obj.getTime());
275           hs.add(xmlObj);
276         }
277       }
278       LimitForXml spLimitForXml = new LimitForXml();
279       spLimitForXml.setLimits(hs);
280       return spLimitForXml;
281     }
282
283     return null;
284   }
285
286   public LimitForXml getVendorLimits() {
287     if (limits != null) {
288       Set<LimitXml> hs = new HashSet<>();
289       for (LimitEntity obj : limits) {
290         if (obj.getType().equals(LimitType.Vendor)) {
291           LimitXml xmlObj = new LimitXml();
292           xmlObj.setDescription(obj.getDescription());
293           xmlObj.setMetric(obj.getMetric());
294           xmlObj.setValues(obj.getValue());
295           xmlObj.setUnit(obj.getUnit());
296           xmlObj.setAggregationFunction(
297               obj.getAggregationFunction() != null ? obj.getAggregationFunction().name() : null);
298           xmlObj.setTime(obj.getTime());
299           hs.add(xmlObj);
300         }
301       }
302       LimitForXml vendorLimitForXml = new LimitForXml();
303       vendorLimitForXml.setLimits(hs);
304       return vendorLimitForXml;
305     }
306
307     return null;
308   }
309
310
311   @Override
312   public int hashCode() {
313     return Objects
314         .hash(vendorLicenseModelId, version, id, name, description, thresholdValue, thresholdUnit,
315             increments, operationalScope, referencingFeatureGroups, startDate, expiryDate);
316   }
317
318   @Override
319   public boolean equals(Object obj) {
320     if (this == obj) {
321       return true;
322     }
323     if (obj == null || getClass() != obj.getClass()) {
324       return false;
325     }
326     EntitlementPoolEntity that = (EntitlementPoolEntity) obj;
327     return Objects.equals(that.thresholdValue, thresholdValue)
328         && Objects.equals(vendorLicenseModelId, that.vendorLicenseModelId)
329         && Objects.equals(id, that.id)
330         && Objects.equals(name, that.name)
331         && Objects.equals(description, that.description)
332         && Objects.equals(thresholdUnit, that.thresholdUnit)
333         && Objects.equals(increments, that.increments)
334         && Objects.equals(operationalScope, that.operationalScope)
335         && Objects.equals(referencingFeatureGroups, that.referencingFeatureGroups)
336         && Objects.equals(startDate, that.startDate)
337         && Objects.equals(expiryDate, that.expiryDate)
338         && Objects.equals(manufacturerReferenceNumber, that.manufacturerReferenceNumber)
339         && Objects.equals(version, that.version);
340   }
341
342   @Override
343   public String toString() {
344     return "EntitlementPoolEntity{"
345         + "vendorLicenseModelId='" + vendorLicenseModelId + '\''
346         + ", version=" + version
347         + ", id='" + id + '\''
348         + ", name='" + name + '\''
349         + ", description='" + description + '\''
350         + ", thresholdValue=" + thresholdValue
351         + ", thresholdUnit='" + thresholdUnit + '\''
352         + ", increments='" + increments + '\''
353         + ", operationalScope=" + operationalScope
354         + ", referencingFeatureGroups=" + referencingFeatureGroups
355         + ", version_uuid=" + versionUuId
356         + ", startDate=" + startDate
357         + ", expiryDate=" + expiryDate
358         + '}';
359   }
360
361   /**
362    * Gets operational scope for artifact.
363    *
364    * @return the operational scope for artifact
365    */
366   public OperationalScopeForXml getOperationalScopeForArtifact() {
367     OperationalScopeForXml obj = new OperationalScopeForXml();
368     if (operationalScope != null) {
369       if (operationalScope.getResults().size() > 0) {
370         obj.setValue(operationalScope.getResults());
371       }
372     }
373     return obj;
374   }
375
376   //Defined and used only for License Artifcat XMLs
377   public void setManufacturerReferenceNumber(String manufacturerReferenceNumber) {
378     this.manufacturerReferenceNumber = manufacturerReferenceNumber;
379   }
380
381   public String getManufacturerReferenceNumber() {
382     return manufacturerReferenceNumber;
383   }
384
385   public String getIsoFormatStartDate() {
386     String isoFormatStartDate = null;
387     if (!StringUtils.isEmpty(startDate)) {
388       isoFormatStartDate = VendorLicenseUtil.getIsoFormatDate(startDate);
389     }
390     return isoFormatStartDate;
391   }
392
393
394   public String getIsoFormatExpiryDate() {
395     String isoFormatExpDate = null;
396     if (!StringUtils.isEmpty(expiryDate)) {
397       isoFormatExpDate = VendorLicenseUtil.getIsoFormatDate(expiryDate);
398     }
399     return isoFormatExpDate;
400   }
401 }