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;
50 public class LicenseAgreementCassandraDaoImpl extends CassandraBaseDao<LicenseAgreementEntity>
51 implements LicenseAgreementDao {
52 private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
53 private static Mapper<LicenseAgreementEntity> mapper =
54 noSqlDb.getMappingManager().mapper(LicenseAgreementEntity.class);
55 private static LicenseAgreementAccessor accessor =
56 noSqlDb.getMappingManager().createAccessor(LicenseAgreementAccessor.class);
57 private static UDTMapper<ChoiceOrOther> choiceOrOtherMapper =
58 noSqlDb.getMappingManager().udtMapper(ChoiceOrOther.class);
59 private static UDTMapper<Version> versionMapper =
60 noSqlDb.getMappingManager().udtMapper(Version.class);
62 private static Set<String> emptyIfNull(Set<String> set) {
63 return set == null ? new HashSet<>() : set;
67 public void registerVersioning(String versionableEntityType) {
68 VersionableEntityMetadata metadata = new VersionableEntityMetadata(
69 mapper.getTableMetadata().getName(),
70 mapper.getTableMetadata().getPartitionKey().get(0).getName(),
71 mapper.getTableMetadata().getPartitionKey().get(1).getName());
73 metadata.setUniqueValuesMetadata(Collections.singletonList(
74 new UniqueValueMetadata(VendorLicenseConstants.UniqueValues.LICENSE_AGREEMENT_NAME,
75 Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
76 mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
78 VersioningManagerFactory.getInstance().createInterface()
79 .register(versionableEntityType, metadata);
83 protected Mapper<LicenseAgreementEntity> getMapper() {
88 protected Object[] getKeys(LicenseAgreementEntity entity) {
89 return new Object[]{entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
94 public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity entity) {
95 return accessor.listByVlmVersion(entity.getVendorLicenseModelId(),
96 versionMapper.toUDT(entity.getVersion())).all();
100 public long count(LicenseAgreementEntity entity) {
101 return accessor.countByVlmVersion(entity.getVendorLicenseModelId(),
102 versionMapper.toUDT(entity.getVersion())).one().getLong("count");
106 public void deleteAll(LicenseAgreementEntity entity) {
107 accessor.deleteByVlmVersion(entity.getVendorLicenseModelId(),
108 versionMapper.toUDT(entity.getVersion())).all();
112 public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
113 accessor.removeFeatureGroup(CommonMethods.toSingleElementSet(featureGroupId),
114 licenseAgreement.getVendorLicenseModelId(),
115 versionMapper.toUDT(licenseAgreement.getVersion()), licenseAgreement.getId());
119 public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
120 Set<String> addedFeatureGroupIds,
121 Set<String> removedFeatureGroupIds) {
122 accessor.updateColumnsAndDeltaFeatureGroupIds(licenseAgreement.getName(),
123 licenseAgreement.getDescription(),
124 licenseAgreement.getLicenseTerm() == null ? null
125 : choiceOrOtherMapper.toUDT(licenseAgreement.getLicenseTerm()),
126 licenseAgreement.getRequirementsAndConstrains(),
127 emptyIfNull(addedFeatureGroupIds),
128 emptyIfNull(removedFeatureGroupIds),
129 licenseAgreement.getVendorLicenseModelId(),
130 versionMapper.toUDT(licenseAgreement.getVersion()),
131 licenseAgreement.getId());
136 interface LicenseAgreementAccessor {
138 @Query("SELECT * FROM license_agreement WHERE vlm_id=? and version=?")
139 Result<LicenseAgreementEntity> listByVlmVersion(String vendorLicenseModelId, UDTValue version);
141 @Query("select count(1) from license_agreement where vlm_id=? AND version=?")
142 ResultSet countByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
144 @Query("delete from license_agreement where vlm_id=? AND version=?")
145 ResultSet deleteByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
148 "UPDATE license_agreement SET name=?, description=?, lic_term=?, req_const=?, "
149 + "fg_ids=fg_ids+?, fg_ids=fg_ids-? WHERE vlm_id=? AND version=? AND la_id=?")
150 ResultSet updateColumnsAndDeltaFeatureGroupIds(String name, String description,
151 UDTValue licenseTerm, String reqAndConst,
152 Set<String> addedFeatureGroupIds,
153 Set<String> removedFeatureGroupIds,
154 String vendorLicenseModelId, UDTValue version,
157 @Query("UPDATE license_agreement SET fg_ids=fg_ids-? WHERE vlm_id=? AND version=? AND la_id=?")
158 ResultSet removeFeatureGroup(Set<String> featureGroupIds, String vendorLicenseModelId,
159 UDTValue version, String id);