3c804f3bfe1c172c670822892845d1afa6789c00
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorlicense.dao.impl;
18
19 import static org.openecomp.core.utilities.CommonMethods.toSingleElementSet;
20
21 import com.datastax.driver.core.ResultSet;
22 import com.datastax.driver.core.UDTValue;
23 import com.datastax.driver.mapping.Mapper;
24 import com.datastax.driver.mapping.Result;
25 import com.datastax.driver.mapping.UDTMapper;
26 import com.datastax.driver.mapping.annotations.Accessor;
27 import com.datastax.driver.mapping.annotations.Query;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.HashSet;
32 import java.util.Set;
33 import org.openecomp.core.dao.impl.CassandraBaseDao;
34 import org.openecomp.core.nosqldb.api.NoSqlDb;
35 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
36 import org.openecomp.core.utilities.CommonMethods;
37 import org.openecomp.sdc.vendorlicense.VendorLicenseConstants;
38 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
39 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
40 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.openecomp.sdc.versioning.types.UniqueValueMetadata;
43 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
44
45 public class FeatureGroupCassandraDaoImpl extends CassandraBaseDao<FeatureGroupEntity>
46     implements FeatureGroupDao {
47
48   private static NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
49   private static Mapper<FeatureGroupEntity> mapper =
50       noSqlDb.getMappingManager().mapper(FeatureGroupEntity.class);
51   private static FeatureGroupAccessor accessor =
52       noSqlDb.getMappingManager().createAccessor(FeatureGroupAccessor.class);
53   private static UDTMapper<Version> versionMapper =
54       noSqlDb.getMappingManager().udtMapper(Version.class);
55
56   private static Set<String> emptyIfNull(Set<String> set) {
57     return set == null ? new HashSet<>() : set;
58   }
59
60   @Override
61   public void registerVersioning(String versionableEntityType) {
62     VersionableEntityMetadata metadata = new VersionableEntityMetadata(
63         mapper.getTableMetadata().getName(),
64         mapper.getTableMetadata().getPartitionKey().get(0).getName(),
65         mapper.getTableMetadata().getPartitionKey().get(1).getName());
66
67     metadata.setUniqueValuesMetadata(Collections.singletonList(
68         new UniqueValueMetadata(VendorLicenseConstants.UniqueValues.FEATURE_GROUP_NAME,
69             Arrays.asList(mapper.getTableMetadata().getPartitionKey().get(0).getName(),
70                 mapper.getTableMetadata().getPartitionKey().get(1).getName(), "name"))));
71
72     ActionVersioningManagerFactory.getInstance().createInterface()
73         .register(versionableEntityType, metadata);
74   }
75
76   @Override
77   protected Mapper<FeatureGroupEntity> getMapper() {
78     return mapper;
79   }
80
81   @Override
82   protected Object[] getKeys(FeatureGroupEntity entity) {
83     return new Object[]{entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
84         entity.getId()};
85   }
86
87   @Override
88   public long count(FeatureGroupEntity entity) {
89     return accessor.countByVlmVersion(entity.getVendorLicenseModelId(),
90         versionMapper.toUDT(entity.getVersion())).one().getLong("count");
91   }
92
93   @Override
94   public void deleteAll(FeatureGroupEntity entity) {
95     accessor.deleteByVlmVersion(entity.getVendorLicenseModelId(),
96         versionMapper.toUDT(entity.getVersion())).all();
97   }
98
99   @Override
100   public void updateFeatureGroup(FeatureGroupEntity entity,
101                                  Set<String> addedEntitlementPools,
102                                  Set<String> removedEntitlementPools,
103                                  Set<String> addedLicenseKeyGroups,
104                                  Set<String> removedLicenseKeyGroups) {
105     accessor.updateColumnsAndDeltaFeatureGroupIds(
106         entity.getName(),
107         entity.getDescription(),
108         entity.getPartNumber(),
109         emptyIfNull(addedEntitlementPools),
110         emptyIfNull(removedEntitlementPools),
111         emptyIfNull(addedLicenseKeyGroups),
112         emptyIfNull(removedLicenseKeyGroups),
113         entity.getVendorLicenseModelId(),
114         versionMapper.toUDT(entity.getVersion()),
115         entity.getId()
116     );
117   }
118
119   @Override
120   public void addReferencingLicenseAgreement(FeatureGroupEntity entity, String licenseAgreementId) {
121     accessor.addReferencingLicenseAgreements(CommonMethods.toSingleElementSet(licenseAgreementId),
122         entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
123   }
124
125   @Override
126   public void removeReferencingLicenseAgreement(FeatureGroupEntity entity,
127                                                 String licenseAgreementId) {
128     accessor
129         .removeReferencingLicenseAgreements(CommonMethods.toSingleElementSet(licenseAgreementId),
130             entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()),
131             entity.getId());
132   }
133
134   @Override
135   public void removeEntitlementPool(FeatureGroupEntity entity, String entitlementPoolId) {
136     accessor.removeEntitlementPools(toSingleElementSet(entitlementPoolId),
137         entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
138   }
139
140   @Override
141   public void removeLicenseKeyGroup(FeatureGroupEntity entity, String licenseKeyGroupId) {
142     accessor.removeLicenseKeyGroup(toSingleElementSet(licenseKeyGroupId),
143         entity.getVendorLicenseModelId(), versionMapper.toUDT(entity.getVersion()), entity.getId());
144   }
145
146   @Override
147   public Collection<FeatureGroupEntity> list(FeatureGroupEntity entity) {
148     return accessor.listByVlmVersion(entity.getVendorLicenseModelId(),
149         versionMapper.toUDT(entity.getVersion())).all();
150   }
151
152   @Accessor
153   interface FeatureGroupAccessor {
154
155     @Query("select * from feature_group where vlm_id=? AND version=?")
156     Result<FeatureGroupEntity> listByVlmVersion(String vendorLicenseModelId, UDTValue version);
157
158     @Query("select count(1) from feature_group where vlm_id=? AND version=?")
159     ResultSet countByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
160
161     @Query("delete from feature_group where vlm_id=? AND version=?")
162     ResultSet deleteByVlmVersion(String vendorLicenseModelId, UDTValue vendorLicenseModelVersion);
163
164     @Query(
165         "update feature_group set name=?,description=?, part_num=?, ep_ids=ep_ids+ ?,"
166             + "ep_ids=ep_ids-?, lkg_ids=lkg_ids+?,lkg_ids=lkg_ids-? WHERE vlm_id=? AND version=? "
167             + "AND fg_id=?")
168     ResultSet updateColumnsAndDeltaFeatureGroupIds(String name, String description,
169         String partNumber,
170         Set<String> addedEntitlementPools,
171         Set<String> removedEntitlementPools,
172         Set<String> addedLicenseKeyGroups,
173         Set<String> removedLicenseKeyGroups,
174         String vendorLicenseModelId, UDTValue version,
175         String id);
176
177     @Query(
178         "UPDATE feature_group SET ref_la_ids = ref_la_ids + ? WHERE vlm_id=? AND version=? "
179             + "AND fg_id=?")
180     ResultSet addReferencingLicenseAgreements(Set<String> licenseAgreementIds,
181         String vendorLicenseModelId, UDTValue version,
182         String id);
183
184     @Query(
185         "UPDATE feature_group SET ref_la_ids = ref_la_ids - ? WHERE vlm_id=? AND version=? AND "
186             + "fg_id=?")
187     ResultSet removeReferencingLicenseAgreements(Set<String> licenseAgreementIds,
188         String vendorLicenseModelId, UDTValue version,
189         String id);
190
191     @Query("UPDATE feature_group SET ep_ids = ep_ids - ? WHERE vlm_id=? AND version=? AND fg_id=?")
192     ResultSet removeEntitlementPools(Set<String> entitlementPoolIds, String vendorLicenseModelId,
193         UDTValue version, String id);
194
195     @Query(
196         "UPDATE feature_group SET lkg_ids = lkg_ids - ? WHERE vlm_id=? AND version=? AND fg_id=?")
197     ResultSet removeLicenseKeyGroup(Set<String> licenseKeyGroupIds, String vendorLicenseModelId,
198         UDTValue version, String id);
199
200   }
201 }
202