2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.simulator.tenant;
23 import com.opencsv.bean.CsvToBeanBuilder;
24 import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
25 import org.openecomp.sdc.be.dao.cassandra.schema.Table;
26 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
33 import java.text.ParseException;
34 import java.text.SimpleDateFormat;
35 import java.util.List;
36 import java.util.stream.Collectors;
39 * Imports CSV file into
40 * Example of line in the file
41 * 00002,135.42.43.45:5757,Context,FALSE,2017-10-11 12:02:01,INITIAL,personal tenant,abcd123456789,bbbbbbbbbbb
42 * Date format is fixed: yyyy-MM-dd HH:mm:ss
46 public class OperationalEvnironmentImportHandler {
47 private static final Logger LOGGER = LoggerFactory.getLogger(OperationalEvnironmentImportHandler.class);
48 private static final String TABLE_NAME = Table.SDC_OPERATIONAL_ENVIRONMENT.getTableDescription().getTableName();
50 private OperationalEvnironmentImportHandler() {
54 public static void execute(String fileName) {
56 List<OperationalEnvironment> beans = new CsvToBeanBuilder<OperationalEnvironment>(new FileReader(fileName))
57 .withType(OperationalEnvironment.class).build().parse();
59 List<OperationalEnvironmentEntry> entries = map(beans);
61 LOGGER.info("File {} has been successfully imported into the [{}] table.", fileName, TABLE_NAME);
62 } catch (IllegalStateException | FileNotFoundException e) {
63 String errorMessage = String.format("Failed to import file: %s into the [%s] table ", fileName, TABLE_NAME);
64 LOGGER.error(errorMessage, e);
68 private static List<OperationalEnvironmentEntry> map(List<OperationalEnvironment> beans) {
70 .map(OperationalEvnironmentImportHandler::map)
71 .collect(Collectors.toList());
75 private static OperationalEnvironmentEntry map(OperationalEnvironment perationalEnvironment) {
76 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
78 entry.setEnvironmentId(perationalEnvironment.getEnvironmentId());
79 entry.addDmaapUebAddress(perationalEnvironment.getDmaapUebAddress());
80 entry.setEcompWorkloadContext(perationalEnvironment.getEcompWorkloadContext());
81 entry.setIsProduction(perationalEnvironment.getIsProduction());
83 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
85 entry.setLastModified(formatter.parse(perationalEnvironment.getLastModified()));
86 } catch (ParseException e) {
87 LOGGER.error("Faild to pase Date, expected format is [yyyy-MM-dd HH:mm:ss].", e);
88 throw new RuntimeException(e);
91 entry.setStatus(perationalEnvironment.getStatus());
92 entry.setTenant(perationalEnvironment.getTenant());
93 entry.setUebApikey(perationalEnvironment.getUebApikey());
94 entry.setUebSecretKey(perationalEnvironment.getUebSecretKey());
100 private static OperationalEnvironmentDao createDaoObj() {
101 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ImportTableConfig.class);
102 return (OperationalEnvironmentDao) context.getBean("operational-environment-dao");
105 private static void modifyDb(List<OperationalEnvironmentEntry> environments) {
106 OperationalEnvironmentDao daoObj = createDaoObj();
110 environments.forEach(daoObj::save);
113 public static String getTableName() {