re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / cassandra / ComponentCassandraDao.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.be.dao.cassandra;
22
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;
34
35 import javax.annotation.PostConstruct;
36 import java.util.*;
37 import java.util.stream.Collectors;
38
39 @Component("component-cassandra-dao")
40 public class ComponentCassandraDao extends CassandraDao {
41
42         private static Logger logger = Logger.getLogger(ComponentCassandraDao.class.getName());
43
44         public final static Integer DEFAULT_FETCH_SIZE = 500;
45
46         private ComponentCacheAccessor componentCacheAccessor;
47
48         public ComponentCassandraDao() {
49                 super();
50
51         }
52
53         @PostConstruct
54         public void init() {
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");
63                         } else {
64                                 logger.info("** ComponentCassandraDao failed");
65                                 throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : "
66                                                 + result.right().value());
67                         }
68                 } else {
69                         logger.info("** Cassandra client isn't connected");
70                         logger.info("** ComponentCassandraDao created, but not connected");
71                 }
72         }
73
74         /**
75          * 
76          * @param ids
77          *            - list of components unique id
78          * @return
79          */
80         public Either<List<ComponentCacheData>, ActionStatus> getComponents(List<String> ids) {
81
82                 List<ComponentCacheData> components = new ArrayList<>();
83                 if (ids == null || ids.isEmpty()) {
84                         return Either.left(components);
85                 }
86
87                 try {
88
89                         Result<ComponentCacheData> events = componentCacheAccessor.getComponents(ids);
90                         if (events == null) {
91                                 logger.trace("not found component for ids list of in size {}", ids.size());
92                                 return Either.left(components);
93                         }
94                         events.all().forEach(event -> {
95                                 components.add(event);
96                                 logger.trace("Fetch component uid = {} isDirty = {}", event.getId(), event.getIsDirty());
97                         });
98
99                         logger.debug("Number of components to fetch was {}. Actually, {} components fetched", ids.size(),
100                                         components.size());
101
102                         return Either.left(components);
103                 } catch (Exception e) {
104                         BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache");
105
106                         logger.debug("failed to get components from cache", e);
107                         return Either.right(ActionStatus.GENERAL_ERROR);
108                 }
109         }
110
111         public Either<List<ComponentCacheData>, ActionStatus> getAllComponentIdTimeAndType() {
112                 try {
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);
118                         }
119                         events.all().forEach(event -> {
120                                 components.add(event);
121                                         logger.trace("Fetch component uid = {} isDirty = {}", event.getId(), event.getIsDirty());
122                         });
123
124                         logger.debug("Number of components fetched was {}.", components.size());
125
126                         return Either.left(components);
127                 } catch (Exception e) {
128                         BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache");
129
130                         logger.debug("failed to get components from cache", e);
131                         return Either.right(ActionStatus.GENERAL_ERROR);
132                 }
133         }
134
135         /**
136          * 
137          * @param id
138          *            - component unique id
139          * @return
140          */
141         public Either<ComponentCacheData, ActionStatus> getComponent(String id) {
142
143                 if (id == null) {
144                         return Either.right(ActionStatus.INVALID_CONTENT);
145                 }
146
147                 try {
148
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);
153                         }
154
155                         ComponentCacheData componentCacheData = events.one();
156                         if (componentCacheData != null) {
157                                 logger.debug("Component with id {} was found. isDirty={}.", componentCacheData.getId(),
158                                                 componentCacheData.getIsDirty());
159                         } else {
160                                 return Either.right(ActionStatus.RESOURCE_NOT_FOUND);
161                         }
162                         return Either.left(componentCacheData);
163
164                 } catch (Exception e) {
165                         BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentFromCache");
166
167                         logger.trace("Failed to get component from cache", e);
168                         return Either.right(ActionStatus.GENERAL_ERROR);
169                 }
170         }
171
172         public CassandraOperationStatus saveComponent(ComponentCacheData componentCacheData) {
173                 return client.save(componentCacheData, ComponentCacheData.class, manager);
174         }
175
176         /**
177          * the method checks if the given table is empty in the artifact keyspace
178          * 
179          * @param tableName
180          *            the name of the table we want to check
181          * @return true if the table is empty
182          */
183         public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
184                 return super.isTableEmpty(tableName);
185         }
186
187         /**
188          * 
189          * @param idToTimestampMap
190          *            - list of components unique id
191          * @return
192          */
193         public Either<ImmutablePair<List<ComponentCacheData>, Set<String>>, ActionStatus> getComponents(
194                         Map<String, Long> idToTimestampMap) {
195
196                 List<ComponentCacheData> components = new ArrayList<>();
197                 Set<String> notFetchedFromCache = new HashSet<>();
198                 ImmutablePair<List<ComponentCacheData>, Set<String>> result = new ImmutablePair<>(
199                 components, notFetchedFromCache);
200
201                 if (idToTimestampMap == null || idToTimestampMap.isEmpty()) {
202                         return Either.left(result);
203                 }
204
205                 try {
206
207                         Set<String> keySet = idToTimestampMap.keySet();
208                         List<String> ids = new ArrayList<>();
209                         ids.addAll(keySet);
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);
215                         }
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);
222                                 } else {
223                                         logger.trace(
224                                                         "Fetch and ignore component uid = {} from cache. Time requested is {} while timestamp in cache is {}",
225                                                         event.getId(), timeRequested, timeFromCache);
226                                 }
227                         });
228
229                         logger.debug("Number of components to fetch was {}. Actually, {} components fetched", ids.size(),
230                                         components.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
233                         // updated.
234                         Set<String> notFoundComponents = idToTimestampMap.keySet().stream()
235                                                              .filter(p -> !foundComponents.contains(p)).collect(Collectors.toSet());
236
237                         notFetchedFromCache.addAll(notFoundComponents);
238
239                         return Either.left(result);
240                 } catch (Exception e) {
241                         BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache");
242
243                         logger.debug("failed to get components from cache", e);
244                         return Either.right(ActionStatus.GENERAL_ERROR);
245                 }
246         }
247
248         public CassandraOperationStatus deleteComponent(String id) {
249                 return client.delete(id, ComponentCacheData.class, manager);
250         }
251
252 }