2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.vendorlicense.dao.impl;
19 import com.datastax.driver.core.ResultSet;
20 import com.datastax.driver.core.UDTValue;
21 import com.datastax.driver.mapping.Mapper;
22 import com.datastax.driver.mapping.Result;
23 import com.datastax.driver.mapping.UDTMapper;
24 import com.datastax.driver.mapping.annotations.Accessor;
25 import com.datastax.driver.mapping.annotations.Query;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
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.LicenseKeyGroupDao;
36 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
37 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
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;
43 public class LicenseKeyGroupCassandraDaoImpl extends CassandraBaseDao<LicenseKeyGroupEntity>
44 implements LicenseKeyGroupDao {
45 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
46 private static Mapper<LicenseKeyGroupEntity> mapper =
47 noSqlDb.getMappingManager().mapper(LicenseKeyGroupEntity.class);
48 private static LicenseKeyGroupAccessor accessor =
49 noSqlDb.getMappingManager().createAccessor(LicenseKeyGroupAccessor.class);
50 private static UDTMapper<Version> versionMapper =
51 noSqlDb.getMappingManager().udtMapper(Version.class);
54 public void registerVersioning(String versionableEntityType) {
55 VersionableEntityMetadata metadata = new VersionableEntityMetadata(
56 mapper.getTableMetadata().getName(),
57 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
58 mapper.getTableMetadata().getPartitionKey().get(1).getName());
60 metadata.setUniqueValuesMetadata(Collections.singletonList(
61 new UniqueValueMetadata(VendorLicenseConstants.UniqueValues.LICENSE_KEY_GROUP_NAME,
62 Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
63 mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
65 ActionVersioningManagerFactory.getInstance().createInterface()
66 .register(versionableEntityType, metadata);
70 protected Mapper<LicenseKeyGroupEntity> getMapper() {
75 protected Object[] getKeys(LicenseKeyGroupEntity entity) {
76 return new Object[]{entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
81 public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity entity) {
82 return accessor.listByVlmVersion(entity.getVendorLicenseModelId(),
83 versionMapper.toUDT(entity.getVersion())).all();
87 public long count(LicenseKeyGroupEntity licenseKeyGroup) {
92 public void deleteAll(LicenseKeyGroupEntity entity) {
93 accessor.deleteByVlmVersion(entity.getVendorLicenseModelId(),
94 versionMapper.toUDT(entity.getVersion())).all();
98 public void addReferencingFeatureGroup(LicenseKeyGroupEntity entity, String featureGroupId) {
99 accessor.addReferencingFeatureGroups(CommonMethods.toSingleElementSet(featureGroupId),
100 entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
104 public void removeReferencingFeatureGroup(LicenseKeyGroupEntity entity, String featureGroupId) {
105 accessor.removeReferencingFeatureGroups(CommonMethods.toSingleElementSet(featureGroupId),
106 entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
110 interface LicenseKeyGroupAccessor {
111 @Query("select * from license_key_group where vlm_id=? and version=?")
112 Result<LicenseKeyGroupEntity> listByVlmVersion(String vendorLicenseModelId, UDTValue version);
114 @Query("delete from license_key_group where vlm_id=? and version=?")
115 Result<LicenseKeyGroupEntity> deleteByVlmVersion(String vendorLicenseModelId, UDTValue version);
118 "UPDATE license_key_group SET ref_fg_ids = ref_fg_ids + ? WHERE vlm_id=? AND version=? "
120 ResultSet addReferencingFeatureGroups(Set<String> referencingFeatureGroups,
121 String vendorLicenseModelId, UDTValue version, String id);
124 "UPDATE license_key_group SET ref_fg_ids = ref_fg_ids - ? WHERE vlm_id=? AND version=? "
126 ResultSet removeReferencingFeatureGroups(Set<String> referencingFeatureGroups,
127 String vendorLicenseModelId, UDTValue version,