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