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