Add collaboration feature
[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.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 MdcDataDebugMessage mdcDataDebugMessage = 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   public LicenseKeyGroupEntity() {
84   }
85
86   /**
87    * Instantiates a new License key group entity.
88    *
89    * @param vendorLicenseModelId the vendor license model id
90    * @param version              the version
91    * @param id                   the id
92    */
93   public LicenseKeyGroupEntity(String vendorLicenseModelId, Version version, String id) {
94     this.vendorLicenseModelId = vendorLicenseModelId;
95     this.version = version;
96     this.id = id;
97   }
98
99   @Override
100   public String getEntityType() {
101     return ENTITY_TYPE;
102   }
103
104   @Override
105   public String getFirstClassCitizenId() {
106     return getVendorLicenseModelId();
107   }
108
109   public String getId() {
110     return id;
111   }
112
113   public void setId(String id) {
114     this.id = id;
115   }
116
117   public Version getVersion() {
118     return version;
119   }
120
121   public void setVersion(Version version) {
122     this.version = version;
123   }
124
125   @Override
126   public String getVersionUuId() {
127     return versionUuId;
128   }
129
130   @Override
131   public void setVersionUuId(String uuId) {
132     versionUuId = uuId;
133   }
134
135   public String getVendorLicenseModelId() {
136     return vendorLicenseModelId;
137   }
138
139   public void setVendorLicenseModelId(String vendorLicenseModelId) {
140     this.vendorLicenseModelId = vendorLicenseModelId;
141   }
142
143   public String getName() {
144     return name;
145   }
146
147   public void setName(String name) {
148     this.name = name;
149   }
150
151   public String getDescription() {
152     return description;
153   }
154
155   public void setDescription(String description) {
156     this.description = description;
157   }
158
159   public LicenseKeyType getType() {
160     return type;
161   }
162
163   public void setType(LicenseKeyType type) {
164     this.type = type;
165   }
166
167   public MultiChoiceOrOther<OperationalScope> getOperationalScope() {
168     return operationalScope;
169   }
170
171   public void setOperationalScope(MultiChoiceOrOther<OperationalScope> operationalScope) {
172     if (operationalScope != null) {
173       operationalScope.resolveEnum(OperationalScope.class);
174     }
175     this.operationalScope = operationalScope;
176   }
177
178   public Set<String> getReferencingFeatureGroups() {
179     return referencingFeatureGroups;
180   }
181
182   public void setReferencingFeatureGroups(Set<String> referencingFeatureGroups) {
183     this.referencingFeatureGroups = referencingFeatureGroups;
184   }
185
186   public Integer getThresholdValue() {
187     return thresholdValue;
188   }
189
190   public void setThresholdValue(Integer thresholdValue) {
191     this.thresholdValue = thresholdValue;
192   }
193
194   public ThresholdUnit getThresholdUnits() {
195     return thresholdUnits;
196   }
197
198   public void setThresholdUnits(ThresholdUnit thresholdUnit) {
199     this.thresholdUnits = thresholdUnit;
200   }
201
202   public String getIncrements() {
203     return increments;
204   }
205
206   public void setIncrements(String increments) {
207     this.increments = increments;
208   }
209
210   public ThresholdForXml getThresholdForArtifact() {
211     ThresholdForXml threshold = new ThresholdForXml();
212     threshold.setUnit(getThresholdUnits() == null ? null : getThresholdUnits().name());
213     threshold.setValue(getThresholdValue());
214     return threshold;
215   }
216
217   public Collection<LimitEntity> getLimits() {
218     return limits;
219   }
220
221   public void setLimits(Collection<LimitEntity> limits) {
222     this.limits = limits;
223   }
224
225   public LimitForXml getSPLimits() {
226     if (limits != null) {
227       Set<LimitXml> hs = new HashSet<>();
228       for (LimitEntity obj : limits) {
229         if (obj.getType().equals(LimitType.ServiceProvider)) {
230           LimitXml xmlObj = new LimitXml();
231           xmlObj.setDescription(obj.getDescription());
232           xmlObj.setMetric(obj.getMetric());
233           xmlObj.setValues(obj.getValue());
234           xmlObj.setUnit(obj.getUnit());
235           xmlObj.setAggregationFunction(
236               obj.getAggregationFunction() != null ? obj.getAggregationFunction().name() : null);
237           xmlObj.setTime(obj.getTime());
238           hs.add(xmlObj);
239         }
240       }
241       LimitForXml spLimitForXml = new LimitForXml();
242       spLimitForXml.setLimits(hs);
243       return spLimitForXml;
244     }
245
246     return null;
247   }
248
249   public LimitForXml getVendorLimits() {
250     if (limits != null) {
251       Set<LimitXml> hs = new HashSet<>();
252       for (LimitEntity obj : limits) {
253         if (obj.getType().equals(LimitType.Vendor)) {
254           LimitXml xmlObj = new LimitXml();
255           xmlObj.setDescription(obj.getDescription());
256           xmlObj.setMetric(obj.getMetric());
257           xmlObj.setValues(obj.getValue());
258           xmlObj.setUnit(obj.getUnit());
259           xmlObj.setAggregationFunction(
260               obj.getAggregationFunction() != null ? obj.getAggregationFunction().name() : null);
261           xmlObj.setTime(obj.getTime());
262           hs.add(xmlObj);
263         }
264       }
265       LimitForXml vendorLimitForXml = new LimitForXml();
266       vendorLimitForXml.setLimits(hs);
267       return vendorLimitForXml;
268     }
269
270     return null;
271   }
272
273   public String getStartDate() {
274     return startDate;
275   }
276
277   public void setStartDate(String startDate) {
278     this.startDate = startDate;
279   }
280
281   public String getExpiryDate() {
282     return expiryDate;
283   }
284
285   public void setExpiryDate(String expiryDate) {
286     this.expiryDate = expiryDate;
287   }
288
289   @Override
290   public int hashCode() {
291     return Objects
292         .hash(vendorLicenseModelId, version, id, name, description, type, operationalScope,
293             referencingFeatureGroups, startDate, expiryDate,
294             thresholdValue, thresholdUnits, increments);
295   }
296
297   @Override
298   public boolean equals(Object obj) {
299     if (this == obj) {
300       return true;
301     }
302     if (obj == null || getClass() != obj.getClass()) {
303       return false;
304     }
305     LicenseKeyGroupEntity that = (LicenseKeyGroupEntity) obj;
306     return Objects.equals(vendorLicenseModelId, that.vendorLicenseModelId)
307         && Objects.equals(version, that.version)
308         && Objects.equals(id, that.id)
309         && Objects.equals(name, that.name)
310         && Objects.equals(description, that.description)
311         && type == that.type
312         && Objects.equals(operationalScope, that.operationalScope)
313         && Objects.equals(referencingFeatureGroups, that.referencingFeatureGroups)
314         && Objects.equals(startDate, that.startDate)
315         && Objects.equals(expiryDate, that.expiryDate)
316         && Objects.equals(thresholdValue, that.thresholdValue)
317         && Objects.equals(thresholdUnits, that.thresholdUnits)
318         && Objects.equals(increments, that.increments)
319         && Objects.equals(manufacturerReferenceNumber, that.manufacturerReferenceNumber);
320   }
321
322   @Override
323   public String toString() {
324     return "LicenseKeyGroupEntity{" + "vendorLicenseModelId='" + vendorLicenseModelId + '\''
325         + ", version=" + version
326         + ", id='" + id + '\''
327         + ", name='" + name + '\''
328         + ", description='" + description + '\''
329         + ", type=" + type
330         + ", operationalScope=" + operationalScope
331         + ", referencingFeatureGroups=" + referencingFeatureGroups
332         + ", versionUuId='" + versionUuId + '\''
333         + ", startDate=" + startDate
334         + ", expiryDate=" + expiryDate
335         + ", thresholdValue='" + thresholdValue + '\''
336         + ", thresholdUnits='" + thresholdUnits + '\''
337         + ", increments='" + increments + '\''
338         + '}';
339   }
340
341   /**
342    * Gets operational scope for artifact.
343    *
344    * @return the operational scope for artifact
345    */
346   public OperationalScopeForXml getOperationalScopeForArtifact() {
347     OperationalScopeForXml obj = new OperationalScopeForXml();
348     if (operationalScope != null) {
349       if (operationalScope.getResults().size() > 0) {
350         obj.setValue(operationalScope.getResults());
351       }
352     }
353     return obj;
354   }
355
356   /**
357    * Gets version for artifact.
358    *
359    * @return version in format suitable for artifact
360    */
361   public String getVersionForArtifact() {
362     return version.toString();
363   }
364
365   /**
366    * Gets type for artifact.
367    *
368    * @return the type for artifact
369    */
370   public LicenseKeyTypeForXml getTypeForArtifact() {
371     LicenseKeyTypeForXml typeXml = new LicenseKeyTypeForXml();
372     if (type != null) {
373       typeXml.setValue(type.toString());
374     } else {
375       typeXml.setValue(null);
376     }
377     return typeXml;
378   }
379
380   //Defined and used only for License Artifcat XMLs
381   public String getManufacturerReferenceNumber() {
382     return manufacturerReferenceNumber;
383   }
384
385   public void setManufacturerReferenceNumber(String manufacturerReferenceNumber) {
386     this.manufacturerReferenceNumber = manufacturerReferenceNumber;
387   }
388
389   public String getIsoFormatStartDate() {
390     mdcDataDebugMessage.debugEntryMessage("start date", startDate);
391     String isoFormatStartDate = null;
392     if (!StringUtils.isEmpty(startDate)) {
393       isoFormatStartDate = VendorLicenseUtil.getIsoFormatDate(startDate);
394       mdcDataDebugMessage.debugExitMessage("start date", "iso format start date", startDate,
395           isoFormatStartDate);
396     }
397     return isoFormatStartDate;
398   }
399
400
401   public String getIsoFormatExpiryDate() {
402     mdcDataDebugMessage.debugEntryMessage("expiry date", expiryDate);
403     String isoFormatExpDate = null;
404     if (!StringUtils.isEmpty(expiryDate)) {
405       isoFormatExpDate = VendorLicenseUtil.getIsoFormatDate(expiryDate);
406       mdcDataDebugMessage.debugExitMessage("expiry date", "iso format expiry date", expiryDate,
407           isoFormatExpDate);
408     }
409     return isoFormatExpDate;
410   }
411
412
413 }