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.EntitlementPoolDao;
36 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
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;
47 public class EntitlementPoolCassandraDaoImpl extends CassandraBaseDao<EntitlementPoolEntity>
48 implements EntitlementPoolDao {
50 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
51 private static Mapper<EntitlementPoolEntity> mapper =
52 noSqlDb.getMappingManager().mapper(EntitlementPoolEntity.class);
53 private static EntitlementPoolAccessor accessor =
54 noSqlDb.getMappingManager().createAccessor(EntitlementPoolAccessor.class);
55 private static UDTMapper<Version> versionMapper =
56 noSqlDb.getMappingManager().udtMapper(Version.class);
59 public void registerVersioning(String versionableEntityType) {
60 VersionableEntityMetadata metadata = new VersionableEntityMetadata(
61 mapper.getTableMetadata().getName(),
62 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
63 mapper.getTableMetadata().getPartitionKey().get(1).getName());
65 metadata.setUniqueValuesMetadata(Collections.singletonList(
66 new UniqueValueMetadata(VendorLicenseConstants.UniqueValues.ENTITLEMENT_POOL_NAME,
67 Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
68 mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
70 VersioningManagerFactory.getInstance().createInterface()
71 .register(versionableEntityType, metadata);
75 protected Mapper<EntitlementPoolEntity> getMapper() {
80 protected Object[] getKeys(EntitlementPoolEntity entity) {
81 return new Object[]{entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
86 public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entity) {
87 return accessor.listByVlmVersion(entity.getVendorLicenseModelId(),
88 versionMapper.toUDT(entity.getVersion())).all();
92 public long count(EntitlementPoolEntity entity) {
93 return accessor.countByVlmVersion(entity.getVendorLicenseModelId(),
94 versionMapper.toUDT(entity.getVersion())).one().getLong("count");
98 public void deleteAll(EntitlementPoolEntity entity) {
99 accessor.deleteByVlmVersion(entity.getVendorLicenseModelId(),
100 versionMapper.toUDT(entity.getVersion())).all();
104 public void addReferencingFeatureGroup(EntitlementPoolEntity entity,
105 String referencingFeatureGroupId) {
107 .addReferencingFeatureGroups(CommonMethods.toSingleElementSet(referencingFeatureGroupId),
108 entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
113 public void removeReferencingFeatureGroup(EntitlementPoolEntity entity,
114 String referencingFeatureGroupId) {
116 .removeReferencingFeatureGroups(CommonMethods.toSingleElementSet(referencingFeatureGroupId),
117 entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
122 interface EntitlementPoolAccessor {
124 @Query("select * from entitlement_pool where vlm_id=? AND version=?")
125 Result<EntitlementPoolEntity> listByVlmVersion(String vendorLicenseModelId,
126 UDTValue vendorLicenseModelVersion);
128 @Query("delete from entitlement_pool where vlm_id=? AND version=?")
129 ResultSet deleteByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
131 @Query("select count(1) from entitlement_pool where vlm_id=? AND version=?")
132 ResultSet countByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
135 "UPDATE entitlement_pool SET ref_fg_ids = ref_fg_ids + ? WHERE vlm_id=? AND version=? "
137 ResultSet addReferencingFeatureGroups(Set<String> referencingFeatureGroups,
138 String vendorLicenseModelId,
139 UDTValue vendorLicenseModelVersion, String id);
142 "UPDATE entitlement_pool SET ref_fg_ids = ref_fg_ids - ? WHERE vlm_id=? AND version=? "
144 ResultSet removeReferencingFeatureGroups(Set<String> referencingFeatureGroups,
145 String vendorLicenseModelId,
146 UDTValue vendorLicenseModelVersion, String id);