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;
29 import java.util.HashSet;
31 import org.openecomp.core.dao.impl.CassandraBaseDao;
32 import org.openecomp.core.nosqldb.api.NoSqlDb;
33 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
34 import org.openecomp.core.utilities.CommonMethods;
35 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
36 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
37 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
38 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
39 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41 import org.openecomp.sdc.versioning.types.UniqueValueMetadata;
42 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
45 public class LicenseAgreementCassandraDaoImpl extends CassandraBaseDao<LicenseAgreementEntity>
46 implements LicenseAgreementDao {
47 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
48 private static Mapper<LicenseAgreementEntity> mapper =
49 noSqlDb.getMappingManager().mapper(LicenseAgreementEntity.class);
50 private static LicenseAgreementAccessor accessor =
51 noSqlDb.getMappingManager().createAccessor(LicenseAgreementAccessor.class);
52 private static UDTMapper<ChoiceOrOther> choiceOrOtherMapper =
53 noSqlDb.getMappingManager().udtMapper(ChoiceOrOther.class);
54 private static UDTMapper<Version> versionMapper =
55 noSqlDb.getMappingManager().udtMapper(Version.class);
57 private static Set<String> emptyIfNull(Set<String> set) {
58 return set == null ? new HashSet<>() : set;
62 public void registerVersioning(String versionableEntityType) {
63 VersionableEntityMetadata metadata = new VersionableEntityMetadata(
64 mapper.getTableMetadata().getName(),
65 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
66 mapper.getTableMetadata().getPartitionKey().get(1).getName());
68 metadata.setUniqueValuesMetadata(Collections.singletonList(
69 new UniqueValueMetadata(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
70 Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
71 mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
73 ActionVersioningManagerFactory.getInstance().createInterface()
74 .register(versionableEntityType, metadata);
78 protected Mapper<LicenseAgreementEntity> getMapper() {
83 protected Object[] getKeys(LicenseAgreementEntity entity) {
84 return new Object[]{entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
89 public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity entity) {
90 return accessor.listByVlmVersion(entity.getVendorLicenseModelId(),
91 versionMapper.toUDT(entity.getVersion())).all();
95 public long count(LicenseAgreementEntity entity) {
96 return accessor.countByVlmVersion(entity.getVendorLicenseModelId(),
97 versionMapper.toUDT(entity.getVersion())).one().getLong("count");
101 public void deleteAll(LicenseAgreementEntity entity) {
102 accessor.deleteByVlmVersion(entity.getVendorLicenseModelId(),
103 versionMapper.toUDT(entity.getVersion())).all();
107 public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
108 accessor.removeFeatureGroup(CommonMethods.toSingleElementSet(featureGroupId),
109 licenseAgreement.getVendorLicenseModelId(),
110 versionMapper.toUDT(licenseAgreement.getVersion()), licenseAgreement.getId());
114 public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
115 Set<String> addedFeatureGroupIds,
116 Set<String> removedFeatureGroupIds) {
117 accessor.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement.getName(),
118 licenseAgreement.getDescription(),
119 licenseAgreement.getLicenseTerm() == null ? null
120 : choiceOrOtherMapper.toUDT(licenseAgreement.getLicenseTerm()),
121 licenseAgreement.getRequirementsAndConstrains(),
122 emptyIfNull(addedFeatureGroupIds),
123 emptyIfNull(removedFeatureGroupIds),
124 licenseAgreement.getVendorLicenseModelId(),
125 versionMapper.toUDT(licenseAgreement.getVersion()),
126 licenseAgreement.getId());
131 interface LicenseAgreementAccessor {
133 @Query("SELECT * FROM license_agreement WHERE vlm_id=? and version=?")
134 Result<LicenseAgreementEntity> listByVlmVersion(String vendorLicenseModelId, UDTValue version);
136 @Query("select count(1) from license_agreement where vlm_id=? AND version=?")
137 ResultSet countByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
139 @Query("delete from license_agreement where vlm_id=? AND version=?")
140 ResultSet deleteByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
143 "UPDATE license_agreement SET name=?, description=?, lic_term=?, req_const=?, "
144 + "fg_ids=fg_ids+?, fg_ids=fg_ids-? WHERE vlm_id=? AND version=? AND la_id=?")
145 ResultSet updateColumnsAndDeltaFeatureGroupIds(String name, String description,
146 UDTValue licenseTerm, String reqAndConst,
147 Set<String> addedFeatureGroupIds,
148 Set<String> removedFeatureGroupIds,
149 String vendorLicenseModelId, UDTValue version,
152 @Query("UPDATE license_agreement SET fg_ids=fg_ids-? WHERE vlm_id=? AND version=? AND la_id=?")
153 ResultSet removeFeatureGroup(Set<String> featureGroupIds, String vendorLicenseModelId,
154 UDTValue version, String id);