1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package org.openecomp.sdc.asdctool.simulator.tenant;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;
import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
import org.openecomp.sdc.be.dao.cassandra.schema.Table;
import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.opencsv.bean.CsvToBeanBuilder;
/**
* Imports CSV file into
* Example of line in the file
* 00002,135.42.43.45:5757,Context,FALSE,2017-10-11 12:02:01,INITIAL,personal tenant,abcd123456789,bbbbbbbbbbb
* Date format is fixed: yyyy-MM-dd HH:mm:ss
* @author dr2032
*
*/
public class OperationalEvnironmentImportHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(OperationalEvnironmentImportHandler.class);
private static final String TABLE_NAME = Table.SDC_OPERATIONAL_ENVIRONMENT.getTableDescription().getTableName();
private OperationalEvnironmentImportHandler() {
}
public static void execute(String fileName) {
try {
List<OperationalEnvironment> beans = new CsvToBeanBuilder<OperationalEnvironment>(new FileReader(fileName))
.withType(OperationalEnvironment.class).build().parse();
List<OperationalEnvironmentEntry> entries = map(beans);
modifyDb(entries);
LOGGER.info("File {} has been successfully imported into the [{}] table.", fileName, TABLE_NAME);
} catch (IllegalStateException | FileNotFoundException e) {
String errorMessage = String.format("Failed to import file: %s into the [%s] table ", fileName, TABLE_NAME);
LOGGER.error(errorMessage, e);
}
}
private static List<OperationalEnvironmentEntry> map(List<OperationalEnvironment> beans) {
return beans.stream()
.map(OperationalEvnironmentImportHandler::map)
.collect(Collectors.toList());
}
private static OperationalEnvironmentEntry map(OperationalEnvironment perationalEnvironment) {
OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
entry.setEnvironmentId(perationalEnvironment.getEnvironmentId());
entry.addDmaapUebAddress(perationalEnvironment.getDmaapUebAddress());
entry.setEcompWorkloadContext(perationalEnvironment.getEcompWorkloadContext());
entry.setIsProduction(perationalEnvironment.getIsProduction());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
entry.setLastModified(formatter.parse(perationalEnvironment.getLastModified()));
} catch (ParseException e) {
LOGGER.error("Faild to pase Date, expected format is [yyyy-MM-dd HH:mm:ss].", e);
throw new RuntimeException(e);
}
entry.setStatus(perationalEnvironment.getStatus());
entry.setTenant(perationalEnvironment.getTenant());
entry.setUebApikey(perationalEnvironment.getUebApikey());
entry.setUebSecretKey(perationalEnvironment.getUebSecretKey());
return entry;
}
private static OperationalEnvironmentDao createDaoObj() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ImportTableConfig.class);
return (OperationalEnvironmentDao) context.getBean("operational-environment-dao");
}
private static void modifyDb(List<OperationalEnvironmentEntry> environments) {
OperationalEnvironmentDao daoObj = createDaoObj();
daoObj.deleteAll();
environments.forEach(daoObj::save);
}
public static String getTableName() {
return TABLE_NAME;
}
}
|