c84df9ca95ae92164c8418f7e00ad5eea245273d
[sdc.git] /
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(that.operationalScope, operationalScope)
310         && Objects.equals(startDate, that.startDate)
311         && Objects.equals(expiryDate, that.expiryDate)
312         && Objects.equals(thresholdValue, that.thresholdValue)
313         && Objects.equals(thresholdUnits, that.thresholdUnits)
314         && Objects.equals(increments, that.increments)
315         && Objects.equals(manufacturerReferenceNumber, that.manufacturerReferenceNumber);
316   }
317
318   @Override
319   public String toString() {
320     return "LicenseKeyGroupEntity{" + "vendorLicenseModelId='" + vendorLicenseModelId + '\''
321         + ", version=" + version
322         + ", id='" + id + '\''
323         + ", name='" + name + '\''
324         + ", description='" + description + '\''
325         + ", type=" + type
326         + ", operationalScope=" + operationalScope
327         + ", referencingFeatureGroups=" + referencingFeatureGroups
328         + ", versionUuId='" + versionUuId + '\''
329         + ", startDate=" + startDate
330         + ", expiryDate=" + expiryDate
331         + ", thresholdValue='" + thresholdValue + '\''
332         + ", thresholdUnits='" + thresholdUnits + '\''
333         + ", increments='" + increments + '\''
334         + '}';
335   }
336
337   /**
338    * Gets operational scope for artifact.
339    *
340    * @return the operational scope for artifact
341    */
342   public OperationalScopeForXml getOperationalScopeForArtifact() {
343     OperationalScopeForXml obj = new OperationalScopeForXml();
344     if (operationalScope != null) {
345       if (operationalScope.getResults().size() > 0) {
346         obj.setValue(operationalScope.getResults());
347       }
348     }
349     return obj;
350   }
351
352   /**
353    * Gets version for artifact.
354    *
355    * @return version in format suitable for artifact
356    */
357   public String getVersionForArtifact() {
358     return version.toString();
359   }
360
361   /**
362    * Gets type for artifact.
363    *
364    * @return the type for artifact
365    */
366   public LicenseKeyTypeForXml getTypeForArtifact() {
367     LicenseKeyTypeForXml typeXml = new LicenseKeyTypeForXml();
368     if (type != null) {
369       typeXml.setValue(type.toString());
370     } else {
371       typeXml.setValue(null);
372     }
373     return typeXml;
374   }
375
376   //Defined and used only for License Artifcat XMLs
377   public String getManufacturerReferenceNumber() {
378     return manufacturerReferenceNumber;
379   }
380
381   public void setManufacturerReferenceNumber(String manufacturerReferenceNumber) {
382     this.manufacturerReferenceNumber = 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
402
403 }