2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.dao.cassandra;
22 import static java.util.function.Predicate.not;
24 import com.datastax.driver.core.Session;
25 import com.datastax.driver.mapping.Mapper;
26 import com.datastax.driver.mapping.MappingManager;
27 import fj.data.Either;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import javax.annotation.PostConstruct;
31 import org.apache.commons.lang3.tuple.ImmutablePair;
32 import org.openecomp.sdc.be.dao.api.exception.CassandraDaoInitException;
33 import org.openecomp.sdc.be.dao.api.exception.CassandraDaoInitExceptionProvider;
34 import org.openecomp.sdc.be.data.model.ToscaImportByModel;
35 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
36 import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
41 @Component("tosca-model-import-cassandra-dao")
42 public class ToscaModelImportCassandraDao extends CassandraDao {
44 private static final Logger LOGGER = Logger.getLogger(ToscaModelImportCassandraDao.class.getName());
46 private ToscaImportByModelAccessor toscaImportByModelAccessor;
47 private Mapper<ToscaImportByModel> toscaImportByModelMapper;
50 public ToscaModelImportCassandraDao(final CassandraClient cassandraClient) {
51 super(cassandraClient);
57 * @param toscaImportByModelAccessor the sdcartifact.tosca_import_by_model accessor
59 ToscaModelImportCassandraDao(final ToscaImportByModelAccessor toscaImportByModelAccessor,
60 final Mapper<ToscaImportByModel> toscaImportByModelMapper) {
62 this.toscaImportByModelAccessor = toscaImportByModelAccessor;
63 this.toscaImportByModelMapper = toscaImportByModelMapper;
68 final var keyspace = AuditingTypesConstants.ARTIFACT_KEYSPACE;
69 if (!client.isConnected()) {
70 LOGGER.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaModelImportCassandraDao.class.getName(), "Cassandra client isn't connected");
73 final Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> connectionResult = client.connect(keyspace);
74 if (connectionResult.isRight()) {
75 final CassandraDaoInitException exception =
76 CassandraDaoInitExceptionProvider.keySpaceConnectError(keyspace, connectionResult.right().value()).get();
77 LOGGER.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaModelImportCassandraDao.class.getName(), exception.getMessage());
80 session = connectionResult.left().value().getLeft();
81 manager = connectionResult.left().value().getRight();
82 toscaImportByModelMapper = manager.mapper(ToscaImportByModel.class);
83 toscaImportByModelAccessor = manager.createAccessor(ToscaImportByModelAccessor.class);
84 LOGGER.info("{} successfully initialized", ToscaModelImportCassandraDao.class.getName());
87 public void importAll(final String modelId, final List<ToscaImportByModel> toscaImportByModelList) {
88 final List<ToscaImportByModel> importOfModelList = toscaImportByModelList.stream()
89 .filter(toscaImportByModel -> modelId.equals(toscaImportByModel.getModelId()))
90 .collect(Collectors.toList());
91 final List<ToscaImportByModel> actualImportOfModelList = toscaImportByModelAccessor.findAllByModel(modelId).all();
92 final List<ToscaImportByModel> removedImportList = actualImportOfModelList.stream()
93 .filter(not(importOfModelList::contains))
94 .collect(Collectors.toList());
96 importOfModelList.forEach(toscaImportByModelMapper::save);
97 removedImportList.forEach(toscaImportByModel ->
98 toscaImportByModelMapper.delete(toscaImportByModel.getModelId(), toscaImportByModel.getFullPath())
102 public List<ToscaImportByModel> findAllByModel(final String modelId) {
103 return toscaImportByModelAccessor.findAllByModel(modelId).all();