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