Add models imports endpoint and persistence structure
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / ToscaModelImportCassandraDao.java
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.dao.cassandra;
21
22 import static java.util.function.Predicate.not;
23
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;
40
41 @Component("tosca-model-import-cassandra-dao")
42 public class ToscaModelImportCassandraDao extends CassandraDao {
43
44     private static final Logger LOGGER = Logger.getLogger(ToscaModelImportCassandraDao.class.getName());
45
46     private ToscaImportByModelAccessor toscaImportByModelAccessor;
47     private Mapper<ToscaImportByModel> toscaImportByModelMapper;
48
49     @Autowired
50     public ToscaModelImportCassandraDao(final CassandraClient cassandraClient) {
51         super(cassandraClient);
52     }
53
54     /**
55      * For test purposes.
56      *
57      * @param toscaImportByModelAccessor the sdcartifact.tosca_import_by_model accessor
58      */
59     ToscaModelImportCassandraDao(final ToscaImportByModelAccessor toscaImportByModelAccessor,
60                                  final Mapper<ToscaImportByModel> toscaImportByModelMapper) {
61         super(null);
62         this.toscaImportByModelAccessor = toscaImportByModelAccessor;
63         this.toscaImportByModelMapper = toscaImportByModelMapper;
64     }
65
66     @PostConstruct
67     public void init() {
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");
71             return;
72         }
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());
78             throw exception;
79         }
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());
85     }
86
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());
95
96         importOfModelList.forEach(toscaImportByModelMapper::save);
97         removedImportList.forEach(toscaImportByModel ->
98             toscaImportByModelMapper.delete(toscaImportByModel.getModelId(), toscaImportByModel.getFullPath())
99         );
100     }
101
102     public List<ToscaImportByModel> findAllByModel(final String modelId) {
103         return toscaImportByModelAccessor.findAllByModel(modelId).all();
104     }
105
106 }