2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorlicense.dao.impl;
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;
42 import java.util.Arrays;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.HashSet;
49 public class FeatureGroupCassandraDaoImpl extends CassandraBaseDao<FeatureGroupEntity>
50 implements FeatureGroupDao {
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);
60 private static Set<String> emptyIfNull(Set<String> set) {
61 return set == null ? new HashSet<>() : set;
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());
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"))));
76 VersioningManagerFactory.getInstance().createInterface()
77 .register(versionableEntityType, metadata);
81 protected Mapper<FeatureGroupEntity> getMapper() {
86 protected Object[] getKeys(FeatureGroupEntity entity) {
87 return new Object[]{entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
92 public long count(FeatureGroupEntity entity) {
93 return accessor.countByVlmVersion(entity.getVendorLicenseModelId(),
94 versionMapper.toUDT(entity.getVersion())).one().getLong("count");
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(
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()),
118 public Collection<FeatureGroupEntity> list(FeatureGroupEntity entity) {
119 return accessor.listByVlmVersion(entity.getVendorLicenseModelId(),
120 versionMapper.toUDT(entity.getVersion())).all();
124 public void deleteAll(FeatureGroupEntity entity) {
125 accessor.deleteByVlmVersion(entity.getVendorLicenseModelId(),
126 versionMapper.toUDT(entity.getVersion())).all();
130 public void addReferencingLicenseAgreement(FeatureGroupEntity entity, String licenseAgreementId) {
131 accessor.addReferencingLicenseAgreements(CommonMethods.toSingleElementSet(licenseAgreementId),
132 entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
136 public void removeReferencingLicenseAgreement(FeatureGroupEntity entity,
137 String licenseAgreementId) {
139 .removeReferencingLicenseAgreements(CommonMethods.toSingleElementSet(licenseAgreementId),
140 entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
145 public void removeEntitlementPool(FeatureGroupEntity entity, String entitlementPoolId) {
146 accessor.removeEntitlementPools(CommonMethods.toSingleElementSet(entitlementPoolId),
147 entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
151 public void removeLicenseKeyGroup(FeatureGroupEntity entity, String licenseKeyGroupId) {
152 accessor.removeLicenseKeyGroup(CommonMethods.toSingleElementSet(licenseKeyGroupId),
153 entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
157 interface FeatureGroupAccessor {
159 @Query("select * from feature_group where vlm_id=? AND version=?")
160 Result<FeatureGroupEntity> listByVlmVersion(String vendorLicenseModelId, UDTValue version);
162 @Query("select count(1) from feature_group where vlm_id=? AND version=?")
163 ResultSet countByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
165 @Query("delete from feature_group where vlm_id=? AND version=?")
166 ResultSet deleteByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
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,
174 Set<String> addedEntitlementPools,
175 Set<String> removedEntitlementPools,
176 Set<String> addedLicenseKeyGroups,
177 Set<String> removedLicenseKeyGroups,
178 String vendorLicenseModelId, UDTValue version,
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,
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,
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);
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);