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