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.LicenseAgreementDao;
36 import org.openecomp.sdc.vendorlicense.dao.types.ChoiceOrOther;
37 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
38 import org.openecomp.sdc.versioning.VersioningManagerFactory;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40 import org.openecomp.sdc.versioning.types.UniqueValueMetadata;
41 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.HashSet;
49 public class LicenseAgreementCassandraDaoImpl extends CassandraBaseDao<LicenseAgreementEntity>
50 implements LicenseAgreementDao {
51 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
52 private static Mapper<LicenseAgreementEntity> mapper =
53 noSqlDb.getMappingManager().mapper(LicenseAgreementEntity.class);
54 private static LicenseAgreementAccessor accessor =
55 noSqlDb.getMappingManager().createAccessor(LicenseAgreementAccessor.class);
56 private static UDTMapper<ChoiceOrOther> choiceOrOtherMapper =
57 noSqlDb.getMappingManager().udtMapper(ChoiceOrOther.class);
58 private static UDTMapper<Version> versionMapper =
59 noSqlDb.getMappingManager().udtMapper(Version.class);
61 private static Set<String> emptyIfNull(Set<String> set) {
62 return set == null ? new HashSet<>() : set;
66 public void registerVersioning(String versionableEntityType) {
67 VersionableEntityMetadata metadata = new VersionableEntityMetadata(
68 mapper.getTableMetadata().getName(),
69 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
70 mapper.getTableMetadata().getPartitionKey().get(1).getName());
72 metadata.setUniqueValuesMetadata(Collections.singletonList(
73 new UniqueValueMetadata(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
74 Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
75 mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
77 VersioningManagerFactory.getInstance().createInterface()
78 .register(versionableEntityType, metadata);
82 protected Mapper<LicenseAgreementEntity> getMapper() {
87 protected Object[] getKeys(LicenseAgreementEntity entity) {
88 return new Object[]{entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
93 public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity entity) {
94 return accessor.listByVlmVersion(entity.getVendorLicenseModelId(),
95 versionMapper.toUDT(entity.getVersion())).all();
99 public long count(LicenseAgreementEntity entity) {
100 return accessor.countByVlmVersion(entity.getVendorLicenseModelId(),
101 versionMapper.toUDT(entity.getVersion())).one().getLong("count");
105 public void deleteAll(LicenseAgreementEntity entity) {
106 accessor.deleteByVlmVersion(entity.getVendorLicenseModelId(),
107 versionMapper.toUDT(entity.getVersion())).all();
111 public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
112 accessor.removeFeatureGroup(CommonMethods.toSingleElementSet(featureGroupId),
113 licenseAgreement.getVendorLicenseModelId(),
114 versionMapper.toUDT(licenseAgreement.getVersion()), licenseAgreement.getId());
118 public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
119 Set<String> addedFeatureGroupIds,
120 Set<String> removedFeatureGroupIds) {
121 accessor.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement.getName(),
122 licenseAgreement.getDescription(),
123 licenseAgreement.getLicenseTerm() == null ? null
124 : choiceOrOtherMapper.toUDT(licenseAgreement.getLicenseTerm()),
125 licenseAgreement.getRequirementsAndConstrains(),
126 emptyIfNull(addedFeatureGroupIds),
127 emptyIfNull(removedFeatureGroupIds),
128 licenseAgreement.getVendorLicenseModelId(),
129 versionMapper.toUDT(licenseAgreement.getVersion()),
130 licenseAgreement.getId());
135 interface LicenseAgreementAccessor {
137 @Query("SELECT * FROM license_agreement WHERE vlm_id=? and version=?")
138 Result<LicenseAgreementEntity> listByVlmVersion(String vendorLicenseModelId, UDTValue version);
140 @Query("select count(1) from license_agreement where vlm_id=? AND version=?")
141 ResultSet countByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
143 @Query("delete from license_agreement where vlm_id=? AND version=?")
144 ResultSet deleteByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
147 "UPDATE license_agreement SET name=?, description=?, lic_term=?, req_const=?, "
148 + "fg_ids=fg_ids+?, fg_ids=fg_ids-? WHERE vlm_id=? AND version=? AND la_id=?")
149 ResultSet updateColumnsAndDeltaFeatureGroupIds(String name, String description,
150 UDTValue licenseTerm, String reqAndConst,
151 Set<String> addedFeatureGroupIds,
152 Set<String> removedFeatureGroupIds,
153 String vendorLicenseModelId, UDTValue version,
156 @Query("UPDATE license_agreement SET fg_ids=fg_ids-? WHERE vlm_id=? AND version=? AND la_id=?")
157 ResultSet removeFeatureGroup(Set<String> featureGroupIds, String vendorLicenseModelId,
158 UDTValue version, String id);