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.be.dao.cassandra;
23 import com.datastax.driver.core.Session;
24 import com.datastax.driver.mapping.MappingManager;
25 import com.datastax.driver.mapping.Result;
26 import fj.data.Either;
27 import org.apache.commons.lang3.tuple.ImmutablePair;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.resources.data.ComponentCacheData;
31 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.springframework.stereotype.Component;
35 import javax.annotation.PostConstruct;
37 import java.util.stream.Collectors;
39 @Component("component-cassandra-dao")
40 public class ComponentCassandraDao extends CassandraDao {
42 private static Logger logger = Logger.getLogger(ComponentCassandraDao.class.getName());
44 public final static Integer DEFAULT_FETCH_SIZE = 500;
46 private ComponentCacheAccessor componentCacheAccessor;
48 public ComponentCassandraDao() {
55 String keyspace = AuditingTypesConstants.COMPONENT_KEYSPACE;
56 if (client.isConnected()) {
57 Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace);
58 if (result.isLeft()) {
59 session = result.left().value().left;
60 manager = result.left().value().right;
61 componentCacheAccessor = manager.createAccessor(ComponentCacheAccessor.class);
62 logger.info("** ComponentCassandraDao created");
64 logger.info("** ComponentCassandraDao failed");
65 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
66 + result.right().value());
69 logger.info("** Cassandra client isn't connected");
70 logger.info("** ComponentCassandraDao created, but not connected");
77 * - list of components unique id
80 public Either<List<ComponentCacheData>, ActionStatus> getComponents(List<String> ids) {
82 List<ComponentCacheData> components = new ArrayList<>();
83 if (ids == null || ids.isEmpty()) {
84 return Either.left(components);
89 Result<ComponentCacheData> events = componentCacheAccessor.getComponents(ids);
91 logger.trace("not found component for ids list of in size {}", ids.size());
92 return Either.left(components);
94 events.all().forEach(event -> {
95 components.add(event);
96 logger.trace("Fetch component uid = {} isDirty = {}", event.getId(), event.getIsDirty());
99 logger.debug("Number of components to fetch was {}. Actually, {} components fetched", ids.size(),
102 return Either.left(components);
103 } catch (Exception e) {
104 BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache");
106 logger.debug("failed to get components from cache", e);
107 return Either.right(ActionStatus.GENERAL_ERROR);
111 public Either<List<ComponentCacheData>, ActionStatus> getAllComponentIdTimeAndType() {
113 List<ComponentCacheData> components = new ArrayList<>();
114 Result<ComponentCacheData> events = componentCacheAccessor.getAllComponentIdTimeAndType();
115 if (events == null) {
116 logger.trace("no component found ");
117 return Either.left(components);
119 events.all().forEach(event -> {
120 components.add(event);
121 logger.trace("Fetch component uid = {} isDirty = {}", event.getId(), event.getIsDirty());
124 logger.debug("Number of components fetched was {}.", components.size());
126 return Either.left(components);
127 } catch (Exception e) {
128 BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache");
130 logger.debug("failed to get components from cache", e);
131 return Either.right(ActionStatus.GENERAL_ERROR);
138 * - component unique id
141 public Either<ComponentCacheData, ActionStatus> getComponent(String id) {
144 return Either.right(ActionStatus.INVALID_CONTENT);
149 Result<ComponentCacheData> events = componentCacheAccessor.getComponent(id);
150 if (events == null) {
151 logger.trace("not found component for id {}", id);
152 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
155 ComponentCacheData componentCacheData = events.one();
156 if (componentCacheData != null) {
157 logger.debug("Component with id {} was found. isDirty={}.", componentCacheData.getId(),
158 componentCacheData.getIsDirty());
160 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
162 return Either.left(componentCacheData);
164 } catch (Exception e) {
165 BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentFromCache");
167 logger.trace("Failed to get component from cache", e);
168 return Either.right(ActionStatus.GENERAL_ERROR);
172 public CassandraOperationStatus saveComponent(ComponentCacheData componentCacheData) {
173 return client.save(componentCacheData, ComponentCacheData.class, manager);
177 * the method checks if the given table is empty in the artifact keyspace
180 * the name of the table we want to check
181 * @return true if the table is empty
183 public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
184 return super.isTableEmpty(tableName);
189 * @param idToTimestampMap
190 * - list of components unique id
193 public Either<ImmutablePair<List<ComponentCacheData>, Set<String>>, ActionStatus> getComponents(
194 Map<String, Long> idToTimestampMap) {
196 List<ComponentCacheData> components = new ArrayList<>();
197 Set<String> notFetchedFromCache = new HashSet<>();
198 ImmutablePair<List<ComponentCacheData>, Set<String>> result = new ImmutablePair<>(
199 components, notFetchedFromCache);
201 if (idToTimestampMap == null || idToTimestampMap.isEmpty()) {
202 return Either.left(result);
207 Set<String> keySet = idToTimestampMap.keySet();
208 List<String> ids = new ArrayList<>();
210 Result<ComponentCacheData> events = componentCacheAccessor.getComponents(ids);
211 if (events == null) {
212 logger.trace("not found component for ids list of in size {}", ids.size());
213 notFetchedFromCache.addAll(idToTimestampMap.keySet());
214 return Either.left(result);
216 events.all().forEach(event -> {
217 long timeFromCache = event.getModificationTime().getTime();
218 long timeRequested = idToTimestampMap.get(event.getId());
219 if (timeFromCache == timeRequested) {
220 logger.trace("Fetch component uid = {} from cache", event.getId());
221 components.add(event);
224 "Fetch and ignore component uid = {} from cache. Time requested is {} while timestamp in cache is {}",
225 event.getId(), timeRequested, timeFromCache);
229 logger.debug("Number of components to fetch was {}. Actually, {} components fetched", ids.size(),
231 List<String> foundComponents = components.stream().map(ComponentCacheData::getId).collect(Collectors.toList());
232 // fetch all ids which was not found in cache/found in cache and not
234 Set<String> notFoundComponents = idToTimestampMap.keySet().stream()
235 .filter(p -> !foundComponents.contains(p)).collect(Collectors.toSet());
237 notFetchedFromCache.addAll(notFoundComponents);
239 return Either.left(result);
240 } catch (Exception e) {
241 BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache");
243 logger.debug("failed to get components from cache", e);
244 return Either.right(ActionStatus.GENERAL_ERROR);
248 public CassandraOperationStatus deleteComponent(String id) {
249 return client.delete(id, ComponentCacheData.class, manager);