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.asdctool.impl;
24 import com.carrotsearch.hppc.cursors.ObjectCursor;
25 import com.fasterxml.jackson.core.type.TypeReference;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import fj.data.Either;
28 import org.apache.commons.lang.SystemUtils;
29 import org.elasticsearch.action.search.SearchResponse;
30 import org.elasticsearch.cluster.metadata.IndexMetaData;
31 import org.elasticsearch.common.collect.ImmutableOpenMap;
32 import org.elasticsearch.common.settings.Settings;
33 import org.elasticsearch.common.unit.TimeValue;
34 import org.elasticsearch.index.query.QueryBuilder;
35 import org.elasticsearch.index.query.QueryBuilders;
36 import org.elasticsearch.search.SearchHit;
37 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
38 import org.openecomp.sdc.be.auditing.impl.AuditAuthRequestEventFactory;
39 import org.openecomp.sdc.be.auditing.impl.AuditConsumerEventFactory;
40 import org.openecomp.sdc.be.auditing.impl.AuditGetUebClusterEventFactory;
41 import org.openecomp.sdc.be.auditing.impl.category.AuditCategoryEventFactory;
42 import org.openecomp.sdc.be.auditing.impl.category.AuditGetCategoryHierarchyEventFactory;
43 import org.openecomp.sdc.be.auditing.impl.distribution.*;
44 import org.openecomp.sdc.be.auditing.impl.resourceadmin.AuditResourceAdminEventMigrationFactory;
45 import org.openecomp.sdc.be.auditing.impl.usersadmin.AuditGetUsersListEventFactory;
46 import org.openecomp.sdc.be.auditing.impl.usersadmin.AuditUserAccessEventFactory;
47 import org.openecomp.sdc.be.auditing.impl.usersadmin.AuditUserAdminEventFactory;
48 import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
49 import org.openecomp.sdc.be.dao.cassandra.AuditCassandraDao;
50 import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
51 import org.openecomp.sdc.be.dao.cassandra.schema.Table;
52 import org.openecomp.sdc.be.dao.es.ElasticSearchClient;
53 import org.openecomp.sdc.be.resources.data.ESArtifactData;
54 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
55 import org.openecomp.sdc.be.resources.data.auditing.AuditingGenericEvent;
56 import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants;
57 import org.openecomp.sdc.be.resources.data.auditing.model.*;
58 import org.openecomp.sdc.common.datastructure.AuditingFieldsKey;
59 import org.openecomp.sdc.common.log.wrappers.Logger;
60 import org.springframework.beans.factory.annotation.Autowired;
63 import java.net.MalformedURLException;
64 import java.net.URISyntaxException;
66 import java.nio.file.Files;
67 import java.nio.file.Paths;
68 import java.util.EnumMap;
72 * Created by mlando on 5/16/2016.
74 public class DataMigration {
76 private ObjectMapper jsonMapper = new ObjectMapper();
78 private static Logger log = Logger.getLogger(DataMigration.class.getName());
80 private ElasticSearchClient elasticSearchClient;
81 private AuditCassandraDao auditCassandraDao;
82 private ArtifactCassandraDao artifactCassandraDao;
85 public DataMigration(AuditCassandraDao auditCassandraDao,
86 ArtifactCassandraDao artifactCassandraDao) {
87 this.auditCassandraDao = auditCassandraDao;
88 this.artifactCassandraDao = artifactCassandraDao;
92 * the method exports and imports the records from ES to cassandra the flow
93 * will check to see if the files are not empty if the files are not empty
94 * the export will be skiped and the flow will use the existing files. the
95 * flow will check if the tables in cassandra are empty, if the tables are
96 * not empty the proces will stop and exit. if the tables are empty the
97 * method will import the records from the files. in case of a fail the flow
98 * will exit and clear all the Cassandra tables.
100 * @param appConfigDir
101 * the location of the dir in wich the output files will be
103 * @param exportFromEs
104 * should the es be exported again and overwrite the old export
105 * @param importToCassandra
106 * should we import the data into cassandra
107 * @return true in case the operation was successful.
109 public boolean migrateDataESToCassndra(String appConfigDir, boolean exportFromEs, boolean importToCassandra) {
110 if (!initEsClient()) {
113 Map<Table, File> files = createOutPutFiles(appConfigDir, exportFromEs);
117 if (exportFromEs && filesEmpty(files)) {
118 Map<Table, PrintWriter> printerWritersMap = createWriters(files);
119 if (printerWritersMap == null) {
123 ImmutableOpenMap<String, IndexMetaData> indexData = getIndexData();
124 for (ObjectCursor<String> key : indexData.keys()) {
125 if (("resources".equalsIgnoreCase(key.value) || key.value.startsWith("auditingevents"))
126 && !exportArtifacts(key.value, printerWritersMap)) {
131 if (elasticSearchClient != null) {
132 elasticSearchClient.close();
134 for (PrintWriter writer : printerWritersMap.values()) {
140 return !importToCassandra || importToCassndra(files);
143 private boolean initEsClient() {
144 String configHome = System.getProperty("config.home");
146 Settings settings = null;
148 if (SystemUtils.IS_OS_WINDOWS) {
149 url = new URL("file:///" + configHome + "/elasticsearch.yml");
151 url = new URL("file:" + configHome + "/elasticsearch.yml");
153 log.debug("URL {}", url);
154 settings = Settings.settingsBuilder().loadFromPath(Paths.get(url.toURI())).build();
155 } catch (MalformedURLException | URISyntaxException e1) {
156 log.error("Failed to create URL in order to load elasticsearch yml", e1);
160 this.elasticSearchClient = new ElasticSearchClient();
161 this.elasticSearchClient.setClusterName(settings.get("cluster.name"));
162 this.elasticSearchClient.setLocal(settings.get("elasticSearch.local"));
163 this.elasticSearchClient.setTransportClient(settings.get("elasticSearch.transportclient"));
165 elasticSearchClient.initialize();
166 } catch (URISyntaxException e) {
167 log.error(e.getMessage());
174 * the method clears all the cassandra tables
176 private void truncateCassandraTable() {
177 log.info("import failed. truncating Cassandra tables.");
178 artifactCassandraDao.deleteAllArtifacts();
179 auditCassandraDao.deleteAllAudit();
183 * the method imports the records from the files into cassandra
186 * a map of files holding
187 * @return true if the operation was successful
189 private boolean importToCassndra(Map<Table, File> files) {
190 log.info("starting to import date into Cassandra.");
191 if (!validtaTablsNotEmpty(files))
193 for (Table table : files.keySet()) {
194 log.info("importing recordes into {}", table.getTableDescription().getTableName());
195 if (!handleImport(files, table)) {
196 truncateCassandraTable();
200 log.info("finished to import date into Cassandra.");
204 private boolean validtaTablsNotEmpty(Map<Table, File> files) {
205 for (Table table : files.keySet()) {
206 Either<Boolean, CassandraOperationStatus> isTableEmptyRes = checkIfTableIsEmpty(table);
207 if (isTableEmptyRes.isRight() || !isTableEmptyRes.left().value()) {
208 log.error("Cassandra table {} is not empty operation aborted.",
209 table.getTableDescription().getTableName());
217 * the method retrieves the fields from the given map and generates
218 * corresponding audit event according to the table name
221 * the map from which we will retrieve the fields enum values
223 * the table we are going to store the record in.
224 * @return an AuditingGenericEvent event representing the audit record that is going to be
227 AuditingGenericEvent createAuditEvent(Map<AuditingFieldsKey, String> map, Table table) {
228 AuditEventFactory factory = null;
230 case USER_ADMIN_EVENT:
231 factory = getAuditUserAdminEventFactory(map);
233 case USER_ACCESS_EVENT:
234 factory = getAuditUserAccessEventFactory(map);
236 case RESOURCE_ADMIN_EVENT:
237 factory = getAuditResourceAdminEventMigrationFactory(map);
239 case DISTRIBUTION_DOWNLOAD_EVENT:
240 factory = getAuditDistributionDownloadEventFactory(map);
242 case DISTRIBUTION_ENGINE_EVENT:
243 factory = getAuditDistributionEngineEventMigrationFactory(map);
245 case DISTRIBUTION_NOTIFICATION_EVENT:
246 factory = getAuditDistributionNotificationEventFactory(map);
248 case DISTRIBUTION_STATUS_EVENT:
249 factory = getAuditDistributionStatusEventFactory(map);
251 case DISTRIBUTION_DEPLOY_EVENT:
252 factory = getAuditDistributionDeployEventFactory(map);
254 case DISTRIBUTION_GET_UEB_CLUSTER_EVENT:
255 factory = getAuditGetUebClusterEventFactory(map);
258 factory = getAuditAuthRequestEventFactory(map);
261 factory = getAuditConsumerEventFactory(map);
264 factory = getAuditCategoryEventFactory(map);
266 case GET_USERS_LIST_EVENT:
267 factory = getAuditGetUsersListEventFactory(map);
269 case GET_CATEGORY_HIERARCHY_EVENT:
270 factory = getAuditGetCategoryHierarchyEventFactory(map);
275 return factory != null ? factory.getDbEvent() : null;
278 private AuditEventFactory getAuditGetCategoryHierarchyEventFactory(Map<AuditingFieldsKey, String> map) {
279 return new AuditGetCategoryHierarchyEventFactory(
280 CommonAuditData.newBuilder()
281 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
282 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
283 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
284 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
286 map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID),
287 map.get(AuditingFieldsKey.AUDIT_DETAILS),
288 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
291 private AuditEventFactory getAuditGetUsersListEventFactory(Map<AuditingFieldsKey, String> map) {
292 return new AuditGetUsersListEventFactory(
293 CommonAuditData.newBuilder()
294 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
295 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
296 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
297 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
299 map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID),
300 map.get(AuditingFieldsKey.AUDIT_USER_DETAILS),
301 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
304 private AuditEventFactory getAuditCategoryEventFactory(Map<AuditingFieldsKey, String> map) {
305 return new AuditCategoryEventFactory(
306 AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)),
307 CommonAuditData.newBuilder()
308 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
309 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
310 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
311 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
313 map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID),
314 map.get(AuditingFieldsKey.AUDIT_CATEGORY_NAME),
315 map.get(AuditingFieldsKey.AUDIT_SUB_CATEGORY_NAME),
316 map.get(AuditingFieldsKey.AUDIT_GROUPING_NAME),
317 map.get(AuditingFieldsKey.AUDIT_RESOURCE_TYPE),
318 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
321 private AuditEventFactory getAuditUserAccessEventFactory(Map<AuditingFieldsKey, String> map) {
322 return new AuditUserAccessEventFactory(
323 CommonAuditData.newBuilder()
324 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
325 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
326 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
327 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
329 map.get(AuditingFieldsKey.AUDIT_USER_UID),
330 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
333 private AuditEventFactory getAuditUserAdminEventFactory(Map<AuditingFieldsKey, String> map) {
334 return new AuditUserAdminEventFactory(
335 AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)),
336 CommonAuditData.newBuilder()
337 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
338 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
339 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
340 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
342 map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID),
343 map.get(AuditingFieldsKey.AUDIT_USER_BEFORE),
344 map.get(AuditingFieldsKey.AUDIT_USER_AFTER),
345 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
348 private AuditEventFactory getAuditConsumerEventFactory(Map<AuditingFieldsKey, String> map) {
349 return new AuditConsumerEventFactory(
350 AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)),
351 CommonAuditData.newBuilder()
352 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
353 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
354 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
355 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
357 map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID),
358 map.get(AuditingFieldsKey.AUDIT_ECOMP_USER),
359 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
362 private AuditEventFactory getAuditAuthRequestEventFactory(Map<AuditingFieldsKey, String> map) {
363 return new AuditAuthRequestEventFactory(
364 CommonAuditData.newBuilder()
365 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
366 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
367 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
368 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
370 map.get(AuditingFieldsKey.AUDIT_USER_UID),
371 map.get(AuditingFieldsKey.AUDIT_AUTH_URL),
372 map.get(AuditingFieldsKey.AUDIT_AUTH_REALM),
373 map.get(AuditingFieldsKey.AUDIT_AUTH_STATUS),
374 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
377 private AuditEventFactory getAuditGetUebClusterEventFactory(Map<AuditingFieldsKey, String> map) {
378 return new AuditGetUebClusterEventFactory(
379 CommonAuditData.newBuilder()
380 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
381 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
382 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
383 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
385 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_CONSUMER_ID),
386 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
389 private AuditEventFactory getAuditResourceAdminEventMigrationFactory(Map<AuditingFieldsKey, String> map) {
390 return new AuditResourceAdminEventMigrationFactory(
391 AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)),
392 CommonAuditData.newBuilder()
393 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
394 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
395 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
396 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
398 new ResourceCommonInfo(map.get(AuditingFieldsKey.AUDIT_RESOURCE_NAME),
399 map.get(AuditingFieldsKey.AUDIT_RESOURCE_TYPE)),
400 ResourceVersionInfo.newBuilder()
401 .artifactUuid(map.get(AuditingFieldsKey.AUDIT_PREV_ARTIFACT_UUID))
402 .state(map.get(AuditingFieldsKey.AUDIT_RESOURCE_PREV_STATE))
403 .version(map.get(AuditingFieldsKey.AUDIT_RESOURCE_PREV_VERSION))
404 .distributionStatus(map.get(AuditingFieldsKey.AUDIT_RESOURCE_DPREV_STATUS))
406 ResourceVersionInfo.newBuilder()
407 .artifactUuid(map.get(AuditingFieldsKey.AUDIT_CURR_ARTIFACT_UUID))
408 .state(map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_STATE))
409 .version(map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_VERSION))
410 .distributionStatus(map.get(AuditingFieldsKey.AUDIT_RESOURCE_DCURR_STATUS))
412 map.get(AuditingFieldsKey.AUDIT_INVARIANT_UUID),
413 map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID),
414 map.get(AuditingFieldsKey.AUDIT_ARTIFACT_DATA),
415 map.get(AuditingFieldsKey.AUDIT_RESOURCE_COMMENT),
416 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ID),
417 map.get(AuditingFieldsKey.AUDIT_RESOURCE_TOSCA_NODE_TYPE),
418 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
421 private AuditEventFactory getAuditDistributionDownloadEventFactory(Map<AuditingFieldsKey, String> map) {
422 return new AuditDistributionDownloadEventFactory(
423 CommonAuditData.newBuilder()
424 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
425 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
426 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
427 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
429 new DistributionData(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_CONSUMER_ID),
430 map.get(AuditingFieldsKey.AUDIT_RESOURCE_URL)),
431 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
434 private AuditEventFactory getAuditDistributionEngineEventMigrationFactory(Map<AuditingFieldsKey, String> map) {
435 return new AuditDistributionEngineEventMigrationFactory(
436 AuditingActionEnum.fromName(map.get(AuditingFieldsKey.AUDIT_ACTION)),
437 CommonAuditData.newBuilder()
438 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
439 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
440 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
441 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
443 DistributionTopicData.newBuilder()
444 .notificationTopic(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_NOTIFICATION_TOPIC_NAME))
445 .statusTopic(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_STATUS_TOPIC_NAME))
447 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_CONSUMER_ID),
448 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_API_KEY),
449 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ENVRIONMENT_NAME),
450 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ROLE),
451 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
454 private AuditEventFactory getAuditDistributionDeployEventFactory(Map<AuditingFieldsKey, String> map) {
455 return new AuditDistributionDeployEventFactory(
456 CommonAuditData.newBuilder()
457 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
458 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
459 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
460 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
462 new ResourceCommonInfo(map.get(AuditingFieldsKey.AUDIT_RESOURCE_NAME),
463 map.get(AuditingFieldsKey.AUDIT_RESOURCE_TYPE)),
464 map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_VERSION),
465 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ID),
466 map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID),
467 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
470 private AuditEventFactory getAuditDistributionStatusEventFactory(Map<AuditingFieldsKey, String> map) {
471 return new AuditDistributionStatusEventFactory(
472 CommonAuditData.newBuilder()
473 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
474 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
475 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
476 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
478 new DistributionData(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_CONSUMER_ID),
479 map.get(AuditingFieldsKey.AUDIT_RESOURCE_URL)),
480 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ID),
481 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_TOPIC_NAME),
482 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_STATUS_TIME),
483 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
486 private AuditEventFactory getAuditDistributionNotificationEventFactory(Map<AuditingFieldsKey, String> map) {
487 return new AuditDistributionNotificationEventFactory(
488 CommonAuditData.newBuilder()
489 .description(map.get(AuditingFieldsKey.AUDIT_DESC))
490 .status(map.get(AuditingFieldsKey.AUDIT_STATUS))
491 .requestId(map.get(AuditingFieldsKey.AUDIT_REQUEST_ID))
492 .serviceInstanceId(map.get(AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID))
494 new ResourceCommonInfo(map.get(AuditingFieldsKey.AUDIT_RESOURCE_NAME),
495 map.get(AuditingFieldsKey.AUDIT_RESOURCE_TYPE)),
496 ResourceVersionInfo.newBuilder()
497 .state(map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_STATE))
498 .version(map.get(AuditingFieldsKey.AUDIT_RESOURCE_CURR_VERSION))
500 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ID),
501 map.get(AuditingFieldsKey.AUDIT_MODIFIER_UID),
502 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_TOPIC_NAME),
503 new OperationalEnvAuditData(map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_ENVIRONMENT_ID),
504 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_VNF_WORKLOAD_CONTEXT),
505 map.get(AuditingFieldsKey.AUDIT_DISTRIBUTION_TENANT)),
506 map.get(AuditingFieldsKey.AUDIT_TIMESTAMP));
512 * the method reads the content of the file intended for a given table, and
513 * sores them in cassandra
516 * a map of files from which the recordes will be retrieved.
518 * the name of the table we want to look up in the files and sore
519 * in Cassandra // * @param store the function to call when
520 * storing recordes in cassndra
521 * @return true if the operation was successful
523 private boolean handleImport(Map<Table, File> files, Table table) {
524 BufferedReader br = null;
526 br = new BufferedReader(new FileReader(files.get(table)));
528 while ((line = br.readLine()) != null) {
529 CassandraOperationStatus res = CassandraOperationStatus.GENERAL_ERROR;
530 if (Table.ARTIFACT.equals(table)) {
531 res = artifactCassandraDao.saveArtifact(jsonMapper.readValue(line, ESArtifactData.class));
534 AuditingGenericEvent recordForCassandra = createAuditRecordForCassandra(line, table);
535 if (recordForCassandra != null) {
536 res = auditCassandraDao.saveRecord(recordForCassandra);
539 if (!res.equals(CassandraOperationStatus.OK)) {
540 log.error("save recored to cassndra {} failed with status {} aborting.",
541 table.getTableDescription().getTableName(), res);
546 } catch (IOException e) {
547 log.error("failed to read file", e);
553 } catch (IOException e) {
554 log.error("failed to close file reader", e);
560 AuditingGenericEvent createAuditRecordForCassandra(String json, Table table) throws IOException{
561 return createAuditEvent(parseToMap(json), table);
564 private Map<AuditingFieldsKey, String> parseToMap(String json) throws IOException {
565 return jsonMapper.readValue(json, new TypeReference<Map<AuditingFieldsKey, String>>(){});
569 * the method checks if the given table is empty
572 * the name of the table we want to check
573 * @return true if the table is empty
575 private Either<Boolean, CassandraOperationStatus> checkIfTableIsEmpty(Table table) {
576 if (Table.ARTIFACT.equals(table)) {
577 return artifactCassandraDao.isTableEmpty(table.getTableDescription().getTableName());
579 return auditCassandraDao.isTableEmpty(table.getTableDescription().getTableName());
583 private boolean filesEmpty(Map<Table, File> files) {
584 for (Table table : files.keySet()) {
585 File file = files.get(table);
586 if (file.length() != 0) {
587 log.info("file:{} is not empty skipping export", table.getTableDescription().getTableName());
595 * the method reads the records from es index of audit's into a file as
599 * the name of the index we want
600 * @param printerWritersMap
601 * a map of the writers we use to write to a file.
602 * @return true in case the export was successful.
604 private boolean exportAudit(String value, Map<Table, PrintWriter> printerWritersMap) {
605 log.info("stratng to export audit data from es index{} to file.", value);
606 QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
607 SearchResponse scrollResp = elasticSearchClient.getClient().prepareSearch(value).setScroll(new TimeValue(60000))
608 .setQuery(queryBuilder).setSize(100).execute().actionGet();
610 for (SearchHit hit : scrollResp.getHits().getHits()) {
611 PrintWriter out = printerWritersMap.get(TypeToTableMapping.getTableByType(hit.getType()));
612 out.println(hit.getSourceAsString());
614 scrollResp = elasticSearchClient.getClient().prepareSearchScroll(scrollResp.getScrollId())
615 .setScroll(new TimeValue(60000)).execute().actionGet();
616 if (scrollResp.getHits().getHits().length == 0) {
622 log.info("export audit data from es to file. finished succsesfully");
627 * the method reads the records from es index of resources into a file as
631 * the name of the index we want to read
632 * @param printerWritersMap
633 * a map of the writers we use to write to a file.
634 * @return true in case the export was successful.
636 private boolean exportArtifacts(String index, Map<Table, PrintWriter> printerWritersMap) {
637 log.info("stratng to export artifact data from es to file.");
638 PrintWriter out = printerWritersMap.get(Table.ARTIFACT);
639 QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
640 SearchResponse scrollResp = elasticSearchClient.getClient().prepareSearch(index).setScroll(new TimeValue(60000))
641 .setQuery(queryBuilder).setSize(100).execute().actionGet();
643 for (SearchHit hit : scrollResp.getHits().getHits()) {
645 out.println(hit.getSourceAsString());
647 scrollResp = elasticSearchClient.getClient().prepareSearchScroll(scrollResp.getScrollId())
648 .setScroll(new TimeValue(60000)).execute().actionGet();
649 if (scrollResp.getHits().getHits().length == 0) {
655 log.info("export artifact data from es to file. finished succsesfully");
660 * the method retrieves all the indexes from elasticsearch
662 * @return a map of indexes and there metadata
664 private ImmutableOpenMap<String, IndexMetaData> getIndexData() {
665 return elasticSearchClient.getClient().admin().cluster().prepareState().get().getState().getMetaData()
670 * the method creates all the files and dir which holds them. in case the
671 * files exist they will not be created again.
673 * @param appConfigDir
674 * the base path under which the output dir will be created and
675 * the export result files the created filesa are named according
676 * to the name of the table into which it will be imported.
678 * if true all the export files will be recreated
679 * @returnthe returns a map of tables and the files representing them them
681 private Map<Table, File> createOutPutFiles(String appConfigDir, boolean exportToEs) {
682 Map<Table, File> result = new EnumMap<Table, File>(Table.class);
683 File outputDir = new File(appConfigDir + "/output/");
684 if (!createOutPutFolder(outputDir)) {
687 for (Table table : Table.values()) {
688 File file = new File(outputDir + "/" + table.getTableDescription().getTableName());
692 Files.delete(file.toPath());
694 } catch (IOException e) {
695 log.error("failed to delete output file {}", file.getAbsolutePath(), e);
698 file = new File(outputDir + "/" + table.getTableDescription().getTableName());
700 if (!file.exists()) {
702 file.createNewFile();
703 } catch (IOException e) {
704 log.error("failed to create output file {}", file.getAbsolutePath(), e);
708 result.put(table, file);
715 * the method create the writers to each file
718 * a map of the files according to table
719 * @return returns a map of writers according to table.
721 private Map<Table, PrintWriter> createWriters(Map<Table, File> files) {
722 Map<Table, PrintWriter> printerWritersMap = new EnumMap<>(Table.class);
724 for (Table table : files.keySet()) {
725 log.info("creating writer for {}", table);
726 File file = files.get(table);
727 try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)) )){
728 printerWritersMap.put(table, out);
729 log.info("creating writer for {} was successful", table);
730 } catch (IOException e) {
731 log.error("create writer to file failed",e);
735 return printerWritersMap;
739 * the method creates the output dir in case it does not exist
742 * the path under wich the directory will be created.
743 * @return true in case the create was succsesful or the dir already exists
745 private boolean createOutPutFolder(File outputDir) {
746 if (!outputDir.exists()) {
747 log.info("creating output dir {}", outputDir.getAbsolutePath());
749 Files.createDirectories(outputDir.toPath());
750 } catch (IOException e) {
751 log.error("failed to create output dir {}", outputDir.getAbsolutePath(), e);
758 public enum TypeToTableMapping {
759 USER_ADMIN_EVENT_TYPE(AuditingTypesConstants.USER_ADMIN_EVENT_TYPE,
760 Table.USER_ADMIN_EVENT), USER_ACCESS_EVENT_TYPE(AuditingTypesConstants.USER_ACCESS_EVENT_TYPE,
761 Table.USER_ACCESS_EVENT), RESOURCE_ADMIN_EVENT_TYPE(
762 AuditingTypesConstants.RESOURCE_ADMIN_EVENT_TYPE,
763 Table.RESOURCE_ADMIN_EVENT), DISTRIBUTION_DOWNLOAD_EVENT_TYPE(
764 AuditingTypesConstants.DISTRIBUTION_DOWNLOAD_EVENT_TYPE,
765 Table.DISTRIBUTION_DOWNLOAD_EVENT), DISTRIBUTION_ENGINE_EVENT_TYPE(
766 AuditingTypesConstants.DISTRIBUTION_ENGINE_EVENT_TYPE,
767 Table.DISTRIBUTION_ENGINE_EVENT), DISTRIBUTION_NOTIFICATION_EVENT_TYPE(
768 AuditingTypesConstants.DISTRIBUTION_NOTIFICATION_EVENT_TYPE,
769 Table.DISTRIBUTION_NOTIFICATION_EVENT), DISTRIBUTION_STATUS_EVENT_TYPE(
770 AuditingTypesConstants.DISTRIBUTION_STATUS_EVENT_TYPE,
771 Table.DISTRIBUTION_STATUS_EVENT), DISTRIBUTION_DEPLOY_EVENT_TYPE(
772 AuditingTypesConstants.DISTRIBUTION_DEPLOY_EVENT_TYPE,
773 Table.DISTRIBUTION_DEPLOY_EVENT), DISTRIBUTION_GET_UEB_CLUSTER_EVENT_TYPE(
774 AuditingTypesConstants.DISTRIBUTION_GET_UEB_CLUSTER_EVENT_TYPE,
775 Table.DISTRIBUTION_GET_UEB_CLUSTER_EVENT), AUTH_EVENT_TYPE(
776 AuditingTypesConstants.AUTH_EVENT_TYPE,
777 Table.AUTH_EVENT), CONSUMER_EVENT_TYPE(
778 AuditingTypesConstants.CONSUMER_EVENT_TYPE,
779 Table.CONSUMER_EVENT), CATEGORY_EVENT_TYPE(
780 AuditingTypesConstants.CATEGORY_EVENT_TYPE,
781 Table.CATEGORY_EVENT), GET_USERS_LIST_EVENT_TYPE(
782 AuditingTypesConstants.GET_USERS_LIST_EVENT_TYPE,
783 Table.GET_USERS_LIST_EVENT), GET_CATEGORY_HIERARCHY_EVENT_TYPE(
784 AuditingTypesConstants.GET_CATEGORY_HIERARCHY_EVENT_TYPE,
785 Table.GET_CATEGORY_HIERARCHY_EVENT);
790 TypeToTableMapping(String typeName, Table table) {
791 this.typeName = typeName;
795 public String getTypeName() {
799 public Table getTable() {
803 public static Table getTableByType(String type) {
804 for (TypeToTableMapping mapping : TypeToTableMapping.values()) {
805 if (mapping.getTypeName().equalsIgnoreCase(type)) {
806 return mapping.getTable();