[SDC] Onboarding 1710 rebase.
[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.openecomp.sdc.vendorlicense.dao.types.xml.LicenseKeyTypeForXml;
30 import org.openecomp.sdc.vendorlicense.dao.types.xml.LimitXml;
31 import org.openecomp.sdc.vendorlicense.dao.types.xml.LimitForXml;
32 import org.openecomp.sdc.versioning.dao.types.Version;
33 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
34
35 import java.util.Collection;
36 import java.util.HashSet;
37 import java.util.Objects;
38 import java.util.Set;
39
40 @Table(keyspace = "dox", name = "license_key_group")
41 public class LicenseKeyGroupEntity implements VersionableEntity {
42   private static final String ENTITY_TYPE = "License Key Group";
43
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 = "lkg_id")
52   private String id;
53   private String name;
54   private String description;
55   @Enumerated
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
65   private Collection<LimitEntity> limits;
66
67   public LicenseKeyGroupEntity() {
68   }
69
70   /**
71    * Instantiates a new License key group entity.
72    *
73    * @param vendorLicenseModelId the vendor license model id
74    * @param version              the version
75    * @param id                   the id
76    */
77   public LicenseKeyGroupEntity(String vendorLicenseModelId, Version version, String id) {
78     this.vendorLicenseModelId = vendorLicenseModelId;
79     this.version = version;
80     this.id = id;
81   }
82
83   @Override
84   public String getEntityType() {
85     return ENTITY_TYPE;
86   }
87
88   @Override
89   public String getFirstClassCitizenId() {
90     return getVendorLicenseModelId();
91   }
92
93   public String getId() {
94     return id;
95   }
96
97   public void setId(String id) {
98     this.id = id;
99   }
100
101   public Version getVersion() {
102     return version;
103   }
104
105   public void setVersion(Version version) {
106     this.version = version;
107   }
108
109   @Override
110   public String getVersionUuId() {
111     return versionUuId;
112   }
113
114   @Override
115   public void setVersionUuId(String uuId) {
116     versionUuId = uuId;
117   }
118
119   public String getVendorLicenseModelId() {
120     return vendorLicenseModelId;
121   }
122
123   public void setVendorLicenseModelId(String vendorLicenseModelId) {
124     this.vendorLicenseModelId = vendorLicenseModelId;
125   }
126
127   public String getName() {
128     return name;
129   }
130
131   public void setName(String name) {
132     this.name = name;
133   }
134
135   public String getDescription() {
136     return description;
137   }
138
139   public void setDescription(String description) {
140     this.description = description;
141   }
142
143   public LicenseKeyType getType() {
144     return type;
145   }
146
147   public void setType(LicenseKeyType type) {
148     this.type = type;
149   }
150
151   public MultiChoiceOrOther<OperationalScope> getOperationalScope() {
152     return operationalScope;
153   }
154
155   public void setOperationalScope(MultiChoiceOrOther<OperationalScope> operationalScope) {
156     if(operationalScope != null)  {
157       operationalScope.resolveEnum(OperationalScope.class);
158     }
159     this.operationalScope = operationalScope;
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 Collection<LimitEntity> getLimits() {
171     return limits;
172   }
173
174   public void setLimits(Collection<LimitEntity> limits) {
175     this.limits = limits;
176   }
177
178   public LimitForXml getSPLimits(){
179     if(limits != null){
180       Set<LimitXml> hs = new HashSet<>();
181       for(LimitEntity obj : limits){
182         if(obj.getType().equals(LimitType.ServiceProvider)){
183           LimitXml xmlObj = new LimitXml();
184           xmlObj.setDescription(obj.getDescription());
185           xmlObj.setMetric(obj.getMetric().toString());
186           xmlObj.setValues(obj.getValue()!=null?Integer.toString(obj.getValue()):null);
187           xmlObj.setUnit(obj.getUnit()!=null?Integer.toString(obj.getUnit()):null);
188           xmlObj.setAggregationFunction(obj.getAggregationFunction()!=null?obj.getAggregationFunction().name():null);
189           xmlObj.setTime(obj.getTime()!=null?obj.getTime().name():null);
190           hs.add(xmlObj);
191         }
192       }
193       LimitForXml spLimitForXml = new LimitForXml();
194       spLimitForXml.setLimits(hs);
195       return spLimitForXml;
196     }
197
198     return null;
199   }
200
201   public LimitForXml getVendorLimits(){
202     if(limits != null){
203       Set<LimitXml> hs = new HashSet<>();
204       for(LimitEntity obj : limits){
205         if(obj.getType().equals(LimitType.Vendor)){
206           LimitXml xmlObj = new LimitXml();
207           xmlObj.setDescription(obj.getDescription());
208           xmlObj.setMetric(obj.getMetric().toString());
209           xmlObj.setValues(obj.getValue()!=null?Integer.toString(obj.getValue()):null);
210           xmlObj.setUnit(obj.getUnit()!=null?Integer.toString(obj.getUnit()):null);
211           xmlObj.setAggregationFunction(obj.getAggregationFunction()!=null?obj.getAggregationFunction().name():null);
212           xmlObj.setTime(obj.getTime()!=null?obj.getTime().name():null);
213           hs.add(xmlObj);
214         }
215       }
216       LimitForXml vendorLimitForXml = new LimitForXml();
217       vendorLimitForXml.setLimits(hs);
218       return vendorLimitForXml;
219     }
220
221     return null;
222   }
223
224   @Override
225   public int hashCode() {
226     return Objects
227         .hash(vendorLicenseModelId, version, id, name, description, type, operationalScope,
228             referencingFeatureGroups);
229   }
230
231   @Override
232   public boolean equals(Object obj) {
233     if (this == obj) {
234       return true;
235     }
236     if (obj == null || getClass() != obj.getClass()) {
237       return false;
238     }
239     LicenseKeyGroupEntity that = (LicenseKeyGroupEntity) obj;
240     return Objects.equals(vendorLicenseModelId, that.vendorLicenseModelId)
241         && Objects.equals(id, that.id)
242         && Objects.equals(name, that.name)
243         && Objects.equals(description, that.description)
244         && type == that.type
245         && Objects.equals(operationalScope, that.operationalScope)
246         && Objects.equals(referencingFeatureGroups, that.referencingFeatureGroups);
247   }
248
249   @Override
250   public String toString() {
251     return "LicenseKeyGroupEntity{" + "vendorLicenseModelId='" + vendorLicenseModelId + '\''
252         + ", version=" + version
253         + ", id='" + id + '\''
254         + ", name='" + name + '\''
255         + ", description='" + description + '\''
256         + ", type=" + type
257         + ", operationalScope=" + operationalScope
258         + ", referencingFeatureGroups=" + referencingFeatureGroups
259         + ", versionUuId='" + versionUuId + '\''
260         + '}';
261   }
262
263   /**
264    * Gets operational scope for artifact.
265    *
266    * @return the operational scope for artifact
267    */
268   public Set<String> getOperationalScopeForArtifact() {
269     if (operationalScope != null) {
270       return operationalScope.getResults();
271     } else {
272       return null;
273     }
274   }
275
276   /**
277    * Gets version for artifact.
278    * @return version in format suitable for artifact
279    */
280   public String getVersionForArtifact() {
281     return version.toString();
282   }
283
284   /**
285    * Gets type for artifact.
286    *
287    * @return the type for artifact
288    */
289   public LicenseKeyTypeForXml getTypeForArtifact() {
290     LicenseKeyTypeForXml typeXml = new LicenseKeyTypeForXml();
291     if (type != null) {
292       typeXml.setValue(type.toString());
293     } else {
294       typeXml.setValue(null);
295     }
296     return typeXml;
297   }
298 }