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.versioning.dao.impl;
23 import com.datastax.driver.core.ColumnDefinitions;
24 import com.datastax.driver.core.ResultSet;
25 import com.datastax.driver.core.Row;
26 import com.datastax.driver.mapping.UDTMapper;
27 import org.openecomp.core.nosqldb.api.NoSqlDb;
28 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
29 import org.openecomp.core.util.UniqueValueUtil;
30 import org.openecomp.core.utilities.CommonMethods;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.versioning.dao.VersionableEntityDao;
34 import org.openecomp.sdc.versioning.dao.types.Version;
35 import org.openecomp.sdc.versioning.types.UniqueValueMetadata;
36 import org.openecomp.sdc.versioning.types.VersionableEntityMetadata;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
42 import java.util.function.Function;
43 import java.util.stream.Collectors;
45 class VersionableEntityDaoCassandraImpl implements VersionableEntityDao {
47 private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
48 private static Logger Logger =
49 (Logger) LoggerFactory.getLogger(VersionableEntityDaoCassandraImpl.class);
50 private static UDTMapper<Version> versionMapper =
51 noSqlDb.getMappingManager().udtMapper(Version.class);
53 private static String commaSeparatedQuestionMarks(int size) {
54 StringBuilder sb = new StringBuilder(size * 2 - 1);
55 for (int i = 0; i < size; i++) {
66 public void initVersion(VersionableEntityMetadata metadata, String entityId, Version baseVersion,
68 ResultSet rows = loadVersionRows(metadata, entityId, baseVersion);
69 List<String> columnNames =
70 rows.getColumnDefinitions().asList().stream().map(ColumnDefinitions.Definition::getName)
71 .collect(Collectors.toList());
73 String insertCql = String.format("insert into %s (%s) values (%s)", metadata.getName(),
74 CommonMethods.listToSeparatedString(columnNames, ','),
75 commaSeparatedQuestionMarks(columnNames.size()));
76 Logger.debug("insertCql", insertCql);
78 for (Row row : rows) {
79 List<Object> columnValues = new ArrayList<>();
80 Map<String, Object> columnNameToValue = new HashMap<>();
82 for (String columnName : columnNames) {
83 if (metadata.getVersionIdentifierName().equals(columnName)) {
84 columnValues.add(versionMapper.toUDT(newVersion));
85 columnNameToValue.put(columnName, newVersion.toString());
87 Object value = row.getObject(columnName);
88 columnValues.add(value);
89 columnNameToValue.put(columnName, value);
93 initRowUniqueValues(metadata.getUniqueValuesMetadata(), columnNameToValue);
95 noSqlDb.execute(insertCql, columnValues.toArray());
100 public void deleteVersion(VersionableEntityMetadata metadata, String entityId,
101 Version versionToDelete, Version backToVersion) {
102 deleteRowsUniqueValues(metadata, entityId, versionToDelete);
104 String deleteCql = String.format("delete from %s where %s=? and %s=?", metadata.getName(),
105 metadata.getIdentifierName(), metadata.getVersionIdentifierName());
106 noSqlDb.execute(deleteCql, entityId, versionMapper.toUDT(versionToDelete));
110 public void closeVersion(VersionableEntityMetadata versionableTableMetadata, String entityId,
111 Version versionToClose) {
112 // redundant in cassandra impl.
115 private ResultSet loadVersionRows(VersionableEntityMetadata metadata, String entityId,
117 String selectCql = String.format("select * from %s where %s=? and %s=?", metadata.getName(),
118 metadata.getIdentifierName(), metadata.getVersionIdentifierName());
119 Logger.debug("selectCql", selectCql);
120 Logger.debug("entityId", entityId);
121 Logger.debug("version", version);
123 return noSqlDb.execute(selectCql, entityId, versionMapper.toUDT(version));
126 private void initRowUniqueValues(List<UniqueValueMetadata> metadata,
127 Map<String, Object> columnNameToValue) {
128 for (UniqueValueMetadata uniqueMetadata : metadata) {
129 List<String> uniqueValueCombination = uniqueMetadata.getUniqueConstraintIdentifiers().stream()
130 .map(colName -> (String) columnNameToValue.get(colName)).collect(Collectors.toList());
131 UniqueValueUtil.createUniqueValue(uniqueMetadata.getType(),
132 uniqueValueCombination.toArray(new String[uniqueValueCombination.size()]));
136 private void deleteRowUniqueValues(List<UniqueValueMetadata> metadata,
137 Map<String, Object> columnNameToValue) {
138 for (UniqueValueMetadata uniqueMetadata : metadata) {
139 List<String> uniqueValueCombination = uniqueMetadata.getUniqueConstraintIdentifiers().stream()
140 .map(colName -> (String) columnNameToValue.get(colName)).collect(Collectors.toList());
141 UniqueValueUtil.deleteUniqueValue(uniqueMetadata.getType(),
142 uniqueValueCombination.toArray(new String[uniqueValueCombination.size()]));
146 private void deleteRowsUniqueValues(VersionableEntityMetadata metadata, String entityId,
148 if (metadata.getUniqueValuesMetadata().isEmpty()) {
151 ResultSet rows = loadVersionRows(metadata, entityId, version);
152 List<String> columnNames =
153 rows.getColumnDefinitions().asList().stream().map(ColumnDefinitions.Definition::getName)
154 .collect(Collectors.toList());
156 for (Row row : rows) {
157 Map<String, Object> columnNameToValue =
158 columnNames.stream().filter(name -> row.getObject(name) != null).collect(Collectors
159 .toMap(Function.identity(),
160 columnName -> metadata.getVersionIdentifierName().equals(columnName) ? version
161 .toString() : row.getObject(columnName)));
162 deleteRowUniqueValues(metadata.getUniqueValuesMetadata(), columnNameToValue);