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