push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / dao / impl / FeatureGroupCassandraDaoImpl.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.impl;
22
23 import com.datastax.driver.core.ResultSet;
24 import com.datastax.driver.core.UDTValue;
25 import com.datastax.driver.mapping.Mapper;
26 import com.datastax.driver.mapping.Result;
27 import com.datastax.driver.mapping.UDTMapper;
28 import com.datastax.driver.mapping.annotations.Accessor;
29 import com.datastax.driver.mapping.annotations.Query;
30 import org.openecomp.core.dao.impl.CassandraBaseDao;
31 import org.openecomp.core.nosqldb.api.NoSqlDb;
32 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
33 import org.openecomp.core.utilities.CommonMethods;
34 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
35 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
36 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
37 import org.openecomp.sdc.versioning.VersioningManagerFactory;
38 import org.openecomp.sdc.versioning.dao.types.Version;
39 import org.openecomp.sdc.versioning.types.UniqueValueMetadata;
40 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
41
42 import java.util.Arrays;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.HashSet;
46 import java.util.Set;
47
48
49 public class FeatureGroupCassandraDaoImpl extends CassandraBaseDao<FeatureGroupEntity>
50     implements FeatureGroupDao {
51
52   private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
53   private static Mapper<FeatureGroupEntity> mapper =
54       noSqlDb.getMappingManager().mapper(FeatureGroupEntity.class);
55   private static FeatureGroupAccessor accessor =
56       noSqlDb.getMappingManager().createAccessor(FeatureGroupAccessor.class);
57   private static UDTMapper<Version> versionMapper =
58       noSqlDb.getMappingManager().udtMapper(Version.class);
59
60   private static Set<String> emptyIfNull(Set<String> set) {
61     return set == null ? new HashSet<>() : set;
62   }
63
64   @Override
65   public void registerVersioning(String versionableEntityType) {
66     VersionableEntityMetadata metadata = new VersionableEntityMetadata(
67         mapper.getTableMetadata().getName(),
68         mapper.getTableMetadata().getPartitionKey().get(0).getName(),
69         mapper.getTableMetadata().getPartitionKey().get(1).getName());
70
71     metadata.setUniqueValuesMetadata(Collections.singletonList(
72         new UniqueValueMetadata(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
73             Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
74                 mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
75
76     VersioningManagerFactory.getInstance().createInterface()
77         .register(versionableEntityType, metadata);
78   }
79
80   @Override
81   protected Mapper<FeatureGroupEntity> getMapper() {
82     return mapper;
83   }
84
85   @Override
86   protected Object[] getKeys(FeatureGroupEntity entity) {
87     return new Object[]{entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
88         entity.getId()};
89   }
90
91   @Override
92   public long count(FeatureGroupEntity entity) {
93     return accessor.countByVlmVersion(entity.getVendorLicenseModelId(),
94         versionMapper.toUDT(entity.getVersion())).one().getLong("count");
95   }
96
97   @Override
98   public void updateFeatureGroup(FeatureGroupEntity entity,
99                                  Set<String> addedEntitlementPools,
100                                  Set<String> removedEntitlementPools,
101                                  Set<String> addedLicenseKeyGroups,
102                                  Set<String> removedLicenseKeyGroups) {
103     accessor.updateColumnsAndDeltaFeatureGroupIds(
104         entity.getName(),
105         entity.getDescription(),
106         entity.getPartNumber(),
107         emptyIfNull(addedEntitlementPools),
108         emptyIfNull(removedEntitlementPools),
109         emptyIfNull(addedLicenseKeyGroups),
110         emptyIfNull(removedLicenseKeyGroups),
111         entity.getVendorLicenseModelId(),
112         versionMapper.toUDT(entity.getVersion()),
113         entity.getId()
114     );
115   }
116
117   @Override
118   public Collection<FeatureGroupEntity> list(FeatureGroupEntity entity) {
119     return accessor.listByVlmVersion(entity.getVendorLicenseModelId(),
120         versionMapper.toUDT(entity.getVersion())).all();
121   }
122
123   @Override
124   public void deleteAll(FeatureGroupEntity entity) {
125     accessor.deleteByVlmVersion(entity.getVendorLicenseModelId(),
126         versionMapper.toUDT(entity.getVersion())).all();
127   }
128
129   @Override
130   public void addReferencingLicenseAgreement(FeatureGroupEntity entity, String licenseAgreementId) {
131     accessor.addReferencingLicenseAgreements(CommonMethods.toSingleElementSet(licenseAgreementId),
132         entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
133   }
134
135   @Override
136   public void removeReferencingLicenseAgreement(FeatureGroupEntity entity,
137                                                 String licenseAgreementId) {
138     accessor
139         .removeReferencingLicenseAgreements(CommonMethods.toSingleElementSet(licenseAgreementId),
140             entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
141             entity.getId());
142   }
143
144   @Override
145   public void removeEntitlementPool(FeatureGroupEntity entity, String entitlementPoolId) {
146     accessor.removeEntitlementPools(CommonMethods.toSingleElementSet(entitlementPoolId),
147         entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
148   }
149
150   @Override
151   public void removeLicenseKeyGroup(FeatureGroupEntity entity, String licenseKeyGroupId) {
152     accessor.removeLicenseKeyGroup(CommonMethods.toSingleElementSet(licenseKeyGroupId),
153         entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
154   }
155
156   @Accessor
157   interface FeatureGroupAccessor {
158
159     @Query("select * from feature_group where vlm_id=? AND version=?")
160     Result<FeatureGroupEntity> listByVlmVersion(String vendorLicenseModelId, UDTValue version);
161
162     @Query("select count(1) from feature_group where vlm_id=? AND version=?")
163     ResultSet countByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
164
165     @Query("delete from feature_group where vlm_id=? AND version=?")
166     ResultSet deleteByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
167
168     @Query(
169         "update feature_group set name=?,description=?, part_num=?, ep_ids=ep_ids+ ?,"
170                 + "ep_ids=ep_ids-?, lkg_ids=lkg_ids+?,lkg_ids=lkg_ids-? WHERE "
171                 + "vlm_id=? AND version=? AND fg_id=?")
172     ResultSet updateColumnsAndDeltaFeatureGroupIds(String name, String description,
173                                                    String partNumber,
174                                                    Set<String> addedEntitlementPools,
175                                                    Set<String> removedEntitlementPools,
176                                                    Set<String> addedLicenseKeyGroups,
177                                                    Set<String> removedLicenseKeyGroups,
178                                                    String vendorLicenseModelId, UDTValue version,
179                                                    String id);
180
181     @Query(
182         "UPDATE feature_group SET ref_la_ids = ref_la_ids "
183                 + "+ ? WHERE vlm_id=? AND version=? AND fg_id=?")
184     ResultSet addReferencingLicenseAgreements(Set<String> licenseAgreementIds,
185                                               String vendorLicenseModelId, UDTValue version,
186                                               String id);
187
188     @Query(
189         "UPDATE feature_group SET ref_la_ids = ref_la_ids - ? "
190                + "WHERE vlm_id=? AND version=? AND fg_id=?")
191     ResultSet removeReferencingLicenseAgreements(Set<String> licenseAgreementIds,
192                                                  String vendorLicenseModelId, UDTValue version,
193                                                  String id);
194
195     @Query("UPDATE feature_group SET ep_ids = ep_ids - ? WHERE vlm_id=? AND version=? AND fg_id=?")
196     ResultSet removeEntitlementPools(Set<String> entitlementPoolIds, String vendorLicenseModelId,
197                                      UDTValue version, String id);
198
199     @Query(
200         "UPDATE feature_group SET lkg_ids = lkg_ids - ? WHERE vlm_id=? AND version=? AND fg_id=?")
201     ResultSet removeLicenseKeyGroup(Set<String> licenseKeyGroupIds, String vendorLicenseModelId,
202                                     UDTValue version, String id);
203
204   }
205 }
206