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.impl;
23 import fj.data.Either;
24 import org.elasticsearch.index.query.BoolQueryBuilder;
25 import org.elasticsearch.index.query.QueryBuilder;
26 import org.elasticsearch.index.query.QueryBuilders;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
30 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
31 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
32 import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent;
33 import org.openecomp.sdc.common.datastructure.AuditingFieldsKey;
34 import org.openecomp.sdc.common.datastructure.ESTimeBasedEvent;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.springframework.stereotype.Component;
38 import javax.annotation.PostConstruct;
40 import java.util.Map.Entry;
42 @Component("auditingDao")
43 public class AuditingDao extends ESTimeBasedDao {
45 private static final String FAILED_TO_GET_DISTRIBUTION_STATUSES_FOR_ACTION = "failed to get distribution statuses for action {}";
46 private static final String GET_SERVICE_DISTRIBUTION_STATUSES_LIST = "Get Service DistributionStatuses List";
47 private static final String SERVICE_INSTANCE_ID_FIELD = AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID
48 .getDisplayName(); // "serviceInstanceId";
49 private static final String STATUS_FIELD = AuditingFieldsKey.AUDIT_STATUS.getDisplayName(); // "status";
50 private static final String ACTION_FIELD = AuditingFieldsKey.AUDIT_ACTION.getDisplayName(); // "action";
51 private static final String DISTRIBUTION_ID_FIELD = AuditingFieldsKey.AUDIT_DISTRIBUTION_ID.getDisplayName(); // "distributionId";
52 private static Logger log = Logger.getLogger(AuditingDao.class.getName());
53 public static final String AUDITING_INDEX = "auditingevents";
57 AuditingActionEnum[] values = AuditingActionEnum.values();
58 for (AuditingActionEnum value : values) {
59 typesToClasses.put(value.getAuditingEsType(), ESTimeBasedEvent.class);
64 public String getIndexPrefix() {
65 return AUDITING_INDEX;
68 public ActionStatus addRecord(Map<AuditingFieldsKey, Object> params, String type) {
70 // TODO rhalili - remove? check debugEnabled?
71 Map<String, Object> displayFields = new HashMap<>();
72 StringBuilder sb = new StringBuilder();
73 for (Entry<AuditingFieldsKey, Object> entry : params.entrySet()) {
74 displayFields.put(entry.getKey().getDisplayName(), entry.getValue());
75 sb.append(entry.getKey().getDisplayName()).append(" = ").append(entry.getValue()).append(",");
79 // String type = clazz.getSimpleName().toLowerCase();
80 AuditingGenericEvent auditingGenericEvent = new AuditingGenericEvent();
81 populateCommonFields(params, auditingGenericEvent);
82 auditingGenericEvent.getFields().putAll(displayFields);
84 log.debug("Auditing: Persisting object of type {}, fields: {}", type, sb.toString());
86 return write(type, auditingGenericEvent);
89 public ActionStatus addRecord(AuditingGenericEvent auditEvent, String type) {
91 log.debug("Auditing: Persisting object of type {}, fields: {}", type, auditEvent.getAction());
92 // auditEvent.fillFields();
93 return write(type, auditEvent);
96 public Either<List<ESTimeBasedEvent>, ActionStatus> getListOfDistributionStatuses(String did) {
98 QueryBuilder componentNameMatch = QueryBuilders.matchQuery(DISTRIBUTION_ID_FIELD, did);
99 QueryBuilder componentVersionMatch = QueryBuilders.matchQuery(ACTION_FIELD,
100 AuditingActionEnum.DISTRIBUTION_STATUS.getName());
101 QueryBuilder remainingElementQueryBuilder = QueryBuilders.boolQuery().must(componentNameMatch)
102 .must(componentVersionMatch);
103 List<ESTimeBasedEvent> remainingElements = null;
105 remainingElements = customFindEvent(AuditingTypesConstants.DISTRIBUTION_STATUS_EVENT_TYPE,
106 remainingElementQueryBuilder, null);
107 } catch (Exception e) {
108 BeEcompErrorManager.getInstance().logBeDaoSystemError("Get DistributionStatuses List");
109 log.debug("failed to get distribution statuses for ", e);
110 return Either.right(ActionStatus.GENERAL_ERROR);
112 if (remainingElements != null && !remainingElements.isEmpty()) {
113 return Either.left(remainingElements);
115 log.debug("not found distribution statuses for did {}", did);
116 remainingElements = new ArrayList<>();
117 return Either.left(remainingElements);
122 public Either<List<ESTimeBasedEvent>, ActionStatus> getServiceDistributionStatusesList(String serviceInstanceId) {
124 List<ESTimeBasedEvent> resList = new ArrayList<>();
125 QueryBuilder componentNameMatch = QueryBuilders.matchQuery(SERVICE_INSTANCE_ID_FIELD, serviceInstanceId);
126 QueryBuilder componentVersionMatch = QueryBuilders.matchQuery(ACTION_FIELD,
127 AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName());
128 QueryBuilder remainingElementQueryBuilder = QueryBuilders.boolQuery().must(componentNameMatch)
129 .must(componentVersionMatch);
130 List<ESTimeBasedEvent> remainingElements = null;
132 remainingElements = customFindEvent(AuditingTypesConstants.RESOURCE_ADMIN_EVENT_TYPE,
133 remainingElementQueryBuilder, null);
134 } catch (Exception e) {
135 BeEcompErrorManager.getInstance().logBeDaoSystemError(GET_SERVICE_DISTRIBUTION_STATUSES_LIST);
136 log.debug(FAILED_TO_GET_DISTRIBUTION_STATUSES_FOR_ACTION,
137 AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName(), e);
138 return Either.right(ActionStatus.GENERAL_ERROR);
140 if (remainingElements != null && !remainingElements.isEmpty()) {
141 resList.addAll(remainingElements);
144 componentVersionMatch = QueryBuilders.matchQuery(ACTION_FIELD,
145 AuditingActionEnum.DISTRIBUTION_DEPLOY.getName());
146 remainingElementQueryBuilder = QueryBuilders.boolQuery().must(componentNameMatch).must(componentVersionMatch);
147 List<ESTimeBasedEvent> dResultElements = null;
149 dResultElements = customFindEvent(AuditingTypesConstants.DISTRIBUTION_DEPLOY_EVENT_TYPE,
150 remainingElementQueryBuilder, null);
151 } catch (Exception e) {
152 BeEcompErrorManager.getInstance().logBeDaoSystemError(GET_SERVICE_DISTRIBUTION_STATUSES_LIST);
153 log.debug("failed to get distribution statuses for action {}",
154 AuditingActionEnum.DISTRIBUTION_DEPLOY.getName(), e);
155 return Either.right(ActionStatus.GENERAL_ERROR);
157 if (dResultElements != null && !dResultElements.isEmpty()) {
158 resList.addAll(dResultElements);
161 componentVersionMatch = QueryBuilders.matchQuery(ACTION_FIELD,
162 AuditingActionEnum.DISTRIBUTION_NOTIFY.getName());
163 remainingElementQueryBuilder = QueryBuilders.boolQuery().must(componentNameMatch).must(componentVersionMatch);
164 List<ESTimeBasedEvent> dNotifyElements = null;
166 dNotifyElements = customFindEvent(AuditingTypesConstants.DISTRIBUTION_NOTIFICATION_EVENT_TYPE,
167 remainingElementQueryBuilder, null);
168 } catch (Exception e) {
169 BeEcompErrorManager.getInstance().logBeDaoSystemError(GET_SERVICE_DISTRIBUTION_STATUSES_LIST);
170 log.debug("failed to get distribution statuses for action {}",
171 AuditingActionEnum.DISTRIBUTION_NOTIFY.getName(), e);
172 return Either.right(ActionStatus.GENERAL_ERROR);
174 if (remainingElements != null && !remainingElements.isEmpty()) {
175 resList.addAll(dNotifyElements);
178 return Either.left(resList);
182 public Either<List<ESTimeBasedEvent>, ActionStatus> getFilteredResourceAdminAuditingEvents(
183 Map<AuditingFieldsKey, Object> filterMap) {
185 Iterator<Entry<AuditingFieldsKey, Object>> filterItr = filterMap.entrySet().iterator();
186 BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
187 while (filterItr.hasNext()) {
188 Entry<AuditingFieldsKey, Object> curr = filterItr.next();
189 boolQuery = boolQuery.must(QueryBuilders.termQuery(curr.getKey().getDisplayName(), curr.getValue()));
193 List<ESTimeBasedEvent> customFindEvent = customFindEvent(
194 ResourceAdminEvent.class.getSimpleName().toLowerCase(), boolQuery, null);
195 return Either.left(customFindEvent);
196 } catch (Exception e) {
197 log.debug("Failed to query AuditRecords in es");
198 return Either.right(ActionStatus.GENERAL_ERROR);
203 public Either<List<ESTimeBasedEvent>, ActionStatus> getListOfDistributionByAction(String did, String actionType,
204 String requestedStatus, Class<? extends AuditingGenericEvent> clazz) {
206 QueryBuilder distributionIdMatch = QueryBuilders.matchQuery(DISTRIBUTION_ID_FIELD, did);
207 QueryBuilder distributionActionMatch = QueryBuilders.matchQuery(ACTION_FIELD, actionType);
208 QueryBuilder remainingElementQueryBuilder;
210 if (requestedStatus != null && !requestedStatus.isEmpty()) {
211 QueryBuilder statusMatch = QueryBuilders.matchQuery(STATUS_FIELD, requestedStatus);
212 remainingElementQueryBuilder = QueryBuilders.boolQuery().must(distributionIdMatch)
213 .must(distributionActionMatch).must(statusMatch);
215 remainingElementQueryBuilder = QueryBuilders.boolQuery().must(distributionIdMatch)
216 .must(distributionActionMatch);
219 List<ESTimeBasedEvent> remainingElements = null;
221 remainingElements = customFindEvent(clazz.getSimpleName().toLowerCase(), remainingElementQueryBuilder,
223 } catch (Exception e) {
224 BeEcompErrorManager.getInstance().logBeDaoSystemError("Get DistributionStatuses List");
225 log.debug("failed to get distribution statuses for action {}", actionType, e);
226 return Either.right(ActionStatus.GENERAL_ERROR);
229 return Either.left(remainingElements);
233 private void populateCommonFields(Map<AuditingFieldsKey, Object> params,
234 AuditingGenericEvent timeBasedIndexedData) {
235 String dateStr = (String) params.get(AuditingFieldsKey.AUDIT_TIMESTAMP);
236 if (dateStr != null) {
237 timeBasedIndexedData.setTimestamp(dateStr);
239 timeBasedIndexedData.setAction((String) params.get(AuditingFieldsKey.AUDIT_ACTION));
240 Object statusObj = params.get(AuditingFieldsKey.AUDIT_STATUS);
241 // For BC. status was Integer and is String
242 if (statusObj != null) {
243 timeBasedIndexedData.setStatus(String.valueOf(statusObj));
245 timeBasedIndexedData.setStatus(null);
247 // timeBasedIndexedData.setStatus((String)params.get(AuditingFieldsKey.AUDIT_STATUS));
248 timeBasedIndexedData.setDesc((String) params.get(AuditingFieldsKey.AUDIT_DESC));
250 .setServiceInstanceId((String) params.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID));
251 timeBasedIndexedData.setRequestId((String) params.get(AuditingFieldsKey.AUDIT_REQUEST_ID));