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