19c62ab43c7b02f824d02cb18b1496365654b37c
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.versioning.dao.impl;
22
23 import com.datastax.driver.core.ColumnDefinitions;
24 import com.datastax.driver.core.ResultSet;
25 import com.datastax.driver.core.Row;
26 import org.openecomp.core.dao.UniqueValueDao;
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;
37
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.function.Function;
43 import java.util.stream.Collectors;
44
45 class VersionableEntityDaoCassandraImpl implements VersionableEntityDao {
46
47   private final UniqueValueUtil uniqueValueUtil;
48   private static final NoSqlDb noSqlDb = NoSqlDbFactory.getInstance().createInterface();
49   private static Logger Logger =
50       (Logger) LoggerFactory.getLogger(VersionableEntityDaoCassandraImpl.class);
51
52   public VersionableEntityDaoCassandraImpl(
53       UniqueValueDao uniqueValueDao) {
54     this.uniqueValueUtil = new UniqueValueUtil(uniqueValueDao);
55   }
56
57   private static String commaSeparatedQuestionMarks(int size) {
58     StringBuilder sb = new StringBuilder(size * 2 - 1);
59     for (int i = 0; i < size; i++) {
60       if (i > 0) {
61         sb.append(',');
62       }
63       sb.append('?');
64     }
65     return sb.toString();
66
67   }
68
69   @Override
70   public void initVersion(VersionableEntityMetadata metadata, String entityId, Version baseVersion,
71                           Version newVersion) {
72     ResultSet rows = loadVersionRows(metadata, entityId, baseVersion);
73     List<String> columnNames =
74         rows.getColumnDefinitions().asList().stream().map(ColumnDefinitions.Definition::getName)
75             .collect(Collectors.toList());
76
77     String insertCql = String.format("insert into %s (%s) values (%s)", metadata.getName(),
78         CommonMethods.listToSeparatedString(columnNames, ','),
79         commaSeparatedQuestionMarks(columnNames.size()));
80     Logger.debug("insertCql", insertCql);
81
82     for (Row row : rows) {
83       List<Object> columnValues = new ArrayList<>();
84       Map<String, Object> columnNameToValue = new HashMap<>();
85
86       for (String columnName : columnNames) {
87         if (metadata.getVersionIdentifierName().equals(columnName)) {
88           columnValues.add(newVersion);
89           columnNameToValue.put(columnName, newVersion.toString());
90         } else {
91           Object value = row.getObject(columnName);
92           columnValues.add(value);
93           columnNameToValue.put(columnName, value);
94         }
95       }
96
97       initRowUniqueValues(metadata.getUniqueValuesMetadata(), columnNameToValue);
98
99       noSqlDb.execute(insertCql, columnValues.toArray());
100     }
101   }
102
103   @Override
104   public void deleteVersion(VersionableEntityMetadata metadata, String entityId,
105                             Version versionToDelete, Version backToVersion) {
106     deleteRowsUniqueValues(metadata, entityId, versionToDelete);
107
108     String deleteCql = String.format("delete from %s where %s=? and %s=?", metadata.getName(),
109         metadata.getIdentifierName(), metadata.getVersionIdentifierName());
110     noSqlDb.execute(deleteCql, entityId, versionToDelete);
111   }
112
113   @Override
114   public void closeVersion(VersionableEntityMetadata versionableTableMetadata, String entityId,
115                            Version versionToClose) {
116     // redundant in cassandra impl.
117   }
118
119   private ResultSet loadVersionRows(VersionableEntityMetadata metadata, String entityId,
120                                     Version version) {
121     String selectCql = String.format("select * from %s where %s=? and %s=?", metadata.getName(),
122         metadata.getIdentifierName(), metadata.getVersionIdentifierName());
123     Logger.debug("selectCql", selectCql);
124     Logger.debug("entityId", entityId);
125     Logger.debug("version", version);
126
127     return noSqlDb.execute(selectCql, entityId, version);
128   }
129
130   private void initRowUniqueValues(List<UniqueValueMetadata> metadata,
131                                    Map<String, Object> columnNameToValue) {
132     for (UniqueValueMetadata uniqueMetadata : metadata) {
133       List<String> uniqueValueCombination = uniqueMetadata.getUniqueConstraintIdentifiers().stream()
134           .map(colName -> (String) columnNameToValue.get(colName)).collect(Collectors.toList());
135       uniqueValueUtil.createUniqueValue(uniqueMetadata.getType(),
136           uniqueValueCombination.toArray(new String[uniqueValueCombination.size()]));
137     }
138   }
139
140   private void deleteRowUniqueValues(List<UniqueValueMetadata> metadata,
141                                      Map<String, Object> columnNameToValue) {
142     for (UniqueValueMetadata uniqueMetadata : metadata) {
143       List<String> uniqueValueCombination = uniqueMetadata.getUniqueConstraintIdentifiers().stream()
144           .map(colName -> (String) columnNameToValue.get(colName)).collect(Collectors.toList());
145       uniqueValueUtil.deleteUniqueValue(uniqueMetadata.getType(),
146           uniqueValueCombination.toArray(new String[uniqueValueCombination.size()]));
147     }
148   }
149
150   private void deleteRowsUniqueValues(VersionableEntityMetadata metadata, String entityId,
151                                       Version version) {
152     if (metadata.getUniqueValuesMetadata().isEmpty()) {
153       return;
154     }
155     ResultSet rows = loadVersionRows(metadata, entityId, version);
156     List<String> columnNames =
157         rows.getColumnDefinitions().asList().stream().map(ColumnDefinitions.Definition::getName)
158             .collect(Collectors.toList());
159
160     for (Row row : rows) {
161       Map<String, Object> columnNameToValue =
162           columnNames.stream().filter(name -> row.getObject(name) != null).collect(Collectors
163               .toMap(Function.identity(),
164                   columnName -> metadata.getVersionIdentifierName().equals(columnName) ? version
165                       .toString() : row.getObject(columnName)));
166       deleteRowUniqueValues(metadata.getUniqueValuesMetadata(), columnNameToValue);
167     }
168   }
169 }