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 java.util.ArrayList;
24 import java.util.List;
26 import javax.annotation.PostConstruct;
28 import org.apache.commons.lang3.tuple.ImmutablePair;
29 import org.openecomp.sdc.be.config.BeEcompErrorManager;
30 import org.openecomp.sdc.be.dao.api.ActionStatus;
31 import org.openecomp.sdc.be.dao.cassandra.schema.Table;
32 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
33 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
34 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
35 import org.openecomp.sdc.be.resources.data.auditing.DistributionDeployEvent;
36 import org.openecomp.sdc.be.resources.data.auditing.DistributionNotificationEvent;
37 import org.openecomp.sdc.be.resources.data.auditing.DistributionStatusEvent;
38 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
39 import org.openecomp.sdc.common.config.EcompErrorName;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.stereotype.Component;
44 import com.datastax.driver.core.Session;
45 import com.datastax.driver.mapping.MappingManager;
46 import com.datastax.driver.mapping.Result;
48 import fj.data.Either;
50 @Component("audit-cassandra-dao")
51 public class AuditCassandraDao extends CassandraDao {
53 private AuditAccessor auditAccessor;
55 private static Logger logger = LoggerFactory.getLogger(AuditCassandraDao.class.getName());
57 public AuditCassandraDao() {
63 String keyspace = AuditingTypesConstants.AUDIT_KEYSPACE;
64 if (client.isConnected()) {
65 Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace);
66 if (result.isLeft()) {
67 session = result.left().value().left;
68 manager = result.left().value().right;
69 auditAccessor = manager.createAccessor(AuditAccessor.class);
70 logger.info("** AuditCassandraDao created");
72 logger.info("** AuditCassandraDao failed");
73 throw new RuntimeException(
74 "Audit keyspace [" + keyspace + "] failed to connect with error : " + result.right().value());
77 logger.info("** Cassandra client isn't connected");
78 logger.info("** AuditCassandraDao created, but not connected");
83 @SuppressWarnings("unchecked")
84 public <T extends AuditingGenericEvent> CassandraOperationStatus saveRecord(T entity) {
85 return client.save(entity, (Class<T>) entity.getClass(), manager);
93 public Either<List<DistributionStatusEvent>, ActionStatus> getListOfDistributionStatuses(String did) {
94 List<DistributionStatusEvent> remainingElements = new ArrayList<DistributionStatusEvent>();
97 Result<DistributionStatusEvent> events = auditAccessor.getListOfDistributionStatuses(did);
99 logger.debug("not found distribution statuses for did {}", did);
100 return Either.left(remainingElements);
102 events.all().forEach(event -> {
104 remainingElements.add(event);
105 logger.debug(event.toString());
107 return Either.left(remainingElements);
108 } catch (Exception e) {
109 BeEcompErrorManager.getInstance().logBeDaoSystemError("Get DistributionStatuses List");
110 logger.debug("failed to get distribution statuses for ", e);
111 return Either.right(ActionStatus.GENERAL_ERROR);
115 public Either<List<DistributionDeployEvent>, ActionStatus> getDistributionDeployByStatus(String did, String action,
117 List<DistributionDeployEvent> remainingElements = new ArrayList<DistributionDeployEvent>();
120 Result<DistributionDeployEvent> events = auditAccessor.getDistributionDeployByStatus(did, action, status);
121 if (events == null) {
122 logger.debug("not found distribution statuses for did {}", did);
123 return Either.left(remainingElements);
125 events.all().forEach(event -> {
127 remainingElements.add(event);
128 logger.debug(event.toString());
131 return Either.left(remainingElements);
132 } catch (Exception e) {
133 BeEcompErrorManager.getInstance().logBeDaoSystemError("get Distribution Deploy By Status");
134 logger.debug("failed to get distribution deploy by status for ", e);
135 return Either.right(ActionStatus.GENERAL_ERROR);
139 public Either<List<ResourceAdminEvent>, ActionStatus> getDistributionRequest(String did, String action) {
140 List<ResourceAdminEvent> remainingElements = new ArrayList<ResourceAdminEvent>();
143 Result<ResourceAdminEvent> events = auditAccessor.getDistributionRequest(did, action);
144 if (events == null) {
145 logger.debug("not found distribution requests for did {}", did);
146 return Either.left(remainingElements);
148 events.all().forEach(event -> {
150 remainingElements.add(event);
151 logger.debug(event.toString());
153 return Either.left(remainingElements);
154 } catch (Exception e) {
155 BeEcompErrorManager.getInstance().logBeDaoSystemError("get Distribution request");
156 logger.debug("failed to get distribution request for ", e);
157 return Either.right(ActionStatus.GENERAL_ERROR);
161 public Either<List<DistributionNotificationEvent>, ActionStatus> getDistributionNotify(String did, String action) {
162 List<DistributionNotificationEvent> remainingElements = new ArrayList<DistributionNotificationEvent>();
165 Result<DistributionNotificationEvent> events = auditAccessor.getDistributionNotify(did, action);
166 if (events == null) {
167 logger.debug("not found distribution notify for did {}", did);
168 return Either.left(remainingElements);
170 events.all().forEach(event -> {
172 remainingElements.add(event);
173 logger.debug(event.toString());
176 return Either.left(remainingElements);
177 } catch (Exception e) {
178 BeEcompErrorManager.getInstance().logBeDaoSystemError("get Distribution notify");
179 logger.debug("failed to get distribution notify for ", e);
180 return Either.right(ActionStatus.GENERAL_ERROR);
184 public Either<List<ResourceAdminEvent>, ActionStatus> getByServiceInstanceId(String serviceInstanceId) {
185 List<ResourceAdminEvent> remainingElements = new ArrayList<ResourceAdminEvent>();
188 Result<ResourceAdminEvent> events = auditAccessor.getByServiceInstanceId(serviceInstanceId);
189 if (events == null) {
190 logger.debug("not found audit records for serviceInstanceId {}", serviceInstanceId);
191 return Either.left(remainingElements);
193 events.all().forEach(event -> {
195 remainingElements.add(event);
196 logger.debug(event.toString());
198 return Either.left(remainingElements);
199 } catch (Exception e) {
200 BeEcompErrorManager.getInstance().logBeDaoSystemError("get Distribution notify");
201 logger.debug("failed to get distribution notify for ", e);
202 return Either.right(ActionStatus.GENERAL_ERROR);
208 * @param serviceInstanceId
211 public Either<List<? extends AuditingGenericEvent>, ActionStatus> getServiceDistributionStatusesList(
212 String serviceInstanceId) {
214 List<AuditingGenericEvent> resList = new ArrayList<>();
217 Result<ResourceAdminEvent> resourceAdminEvents = auditAccessor
218 .getServiceDistributionStatus(serviceInstanceId);
220 if (resourceAdminEvents != null) {
221 resourceAdminEvents.all().forEach(event -> {
224 logger.debug(event.toString());
228 } catch (Exception e) {
229 BeEcompErrorManager.getInstance().logBeDaoSystemError("Get Service DistributionStatuses List");
230 logger.debug("failed to get distribution statuses for action {}",
231 AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName(), e);
232 return Either.right(ActionStatus.GENERAL_ERROR);
235 Result<DistributionDeployEvent> distDeployEvents = auditAccessor
236 .getServiceDistributionDeploy(serviceInstanceId);
237 if (distDeployEvents != null) {
238 distDeployEvents.all().forEach(event -> {
241 logger.debug(event.toString());
244 } catch (Exception e) {
245 BeEcompErrorManager.getInstance().logBeDaoSystemError("Get Service DistributionStatuses List");
246 logger.debug("failed to get distribution statuses for action {}",
247 AuditingActionEnum.DISTRIBUTION_DEPLOY.getName(), e);
248 return Either.right(ActionStatus.GENERAL_ERROR);
251 Result<DistributionNotificationEvent> distNotifEvent = auditAccessor
252 .getServiceDistributionNotify(serviceInstanceId);
253 if (distNotifEvent != null) {
254 distNotifEvent.all().forEach(event -> {
257 logger.debug(event.toString());
260 } catch (Exception e) {
261 BeEcompErrorManager.getInstance().logBeDaoSystemError("Get Service DistributionStatuses List");
262 logger.debug("failed to get distribution statuses for action {}",
263 AuditingActionEnum.DISTRIBUTION_NOTIFY.getName(), e);
264 return Either.right(ActionStatus.GENERAL_ERROR);
267 return Either.left(resList);
270 public Either<List<ResourceAdminEvent>, ActionStatus> getAuditByServiceIdAndPrevVersion(String serviceInstanceId,
271 String prevVersion) {
272 List<ResourceAdminEvent> remainingElements = new ArrayList<ResourceAdminEvent>();
275 Result<ResourceAdminEvent> events = auditAccessor.getAuditByServiceIdAndPrevVersion(serviceInstanceId,
277 if (events == null) {
278 logger.debug("not found audit records for serviceInstanceId {} andprevVersion {}", serviceInstanceId,
280 return Either.left(remainingElements);
282 events.all().forEach(event -> {
284 remainingElements.add(event);
285 logger.debug(event.toString());
287 return Either.left(remainingElements);
288 } catch (Exception e) {
289 BeEcompErrorManager.getInstance().logBeDaoSystemError("get Audit By ServiceId And PrevVersion");
290 logger.debug("failed to getAuditByServiceIdAndPrevVersion ", e);
291 return Either.right(ActionStatus.GENERAL_ERROR);
295 public Either<List<ResourceAdminEvent>, ActionStatus> getAuditByServiceIdAndCurrVersion(String serviceInstanceId,
296 String currVersion) {
297 List<ResourceAdminEvent> remainingElements = new ArrayList<ResourceAdminEvent>();
300 Result<ResourceAdminEvent> events = auditAccessor.getAuditByServiceIdAndCurrVersion(serviceInstanceId,
302 if (events == null) {
303 logger.debug("not found audit records for serviceInstanceId {} andprevVersion {}", serviceInstanceId,
305 return Either.left(remainingElements);
307 events.all().forEach(event -> {
309 remainingElements.add(event);
310 logger.debug(event.toString());
312 return Either.left(remainingElements);
313 } catch (Exception e) {
314 BeEcompErrorManager.getInstance().logBeDaoSystemError("get Audit By ServiceId And CurrVersion");
316 logger.debug("failed to getAuditByServiceIdAndPrevVersion ", e);
317 return Either.right(ActionStatus.GENERAL_ERROR);
322 * the method checks if the given table is empty in the audit keyspace
325 * the name of the table we want to check
326 * @return true if the table is empty
328 public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) {
329 return super.isTableEmpty(tableName);
333 * ---------for use in JUnit only--------------- the method deletes all the
334 * tables in the audit keyspace
336 * @return the status of the last failed operation or ok if all the deletes
339 public CassandraOperationStatus deleteAllAudit() {
340 logger.info("cleaning all audit tables.");
341 String query = "truncate " + AuditingTypesConstants.AUDIT_KEYSPACE + ".";
343 for (Table table : Table.values()) {
344 if (table.getTableDescription().getKeyspace().equals(AuditingTypesConstants.AUDIT_KEYSPACE)) {
345 logger.debug("clean Audit table:{}", table.getTableDescription().getTableName());
346 session.execute(query + table.getTableDescription().getTableName() + ";");
347 logger.debug("clean Audit table:{} was succsesfull", table.getTableDescription().getTableName());
350 } catch (Exception e) {
351 logger.error("Failed to clean Audit", e);
352 return CassandraOperationStatus.GENERAL_ERROR;
354 logger.info("clean all audit finished succsesfully.");
355 return CassandraOperationStatus.OK;