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