Added oparent to sdc main
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / simulator / tenant / OperationalEvnironmentImportHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.asdctool.simulator.tenant;
22
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;
30
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;
37
38 /**
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
43  * @author dr2032
44  *
45  */
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();
49         
50         private OperationalEvnironmentImportHandler() {
51                 
52         }
53         
54         public static void execute(String fileName) {
55                 try {
56                         List<OperationalEnvironment> beans = new CsvToBeanBuilder<OperationalEnvironment>(new FileReader(fileName))
57                                        .withType(OperationalEnvironment.class).build().parse();
58                         
59                         List<OperationalEnvironmentEntry> entries = map(beans);
60                         modifyDb(entries);
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);
65                 }
66         }
67         
68         private static List<OperationalEnvironmentEntry> map(List<OperationalEnvironment> beans) {
69                 return beans.stream()
70                                 .map(OperationalEvnironmentImportHandler::map)
71                                 .collect(Collectors.toList());
72                 
73         }
74         
75         private static OperationalEnvironmentEntry map(OperationalEnvironment perationalEnvironment) {
76                 OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
77                 
78                 entry.setEnvironmentId(perationalEnvironment.getEnvironmentId());
79                 entry.addDmaapUebAddress(perationalEnvironment.getDmaapUebAddress());
80                 entry.setEcompWorkloadContext(perationalEnvironment.getEcompWorkloadContext());
81                 entry.setIsProduction(perationalEnvironment.getIsProduction());
82                 
83                 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
84                 try {
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);
89                 }
90                 
91                 entry.setStatus(perationalEnvironment.getStatus());
92                 entry.setTenant(perationalEnvironment.getTenant());
93                 entry.setUebApikey(perationalEnvironment.getUebApikey());
94                 entry.setUebSecretKey(perationalEnvironment.getUebSecretKey());
95                 
96                 return entry;
97                 
98         }
99         
100         private static OperationalEnvironmentDao createDaoObj() {
101                 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ImportTableConfig.class);
102                 return (OperationalEnvironmentDao) context.getBean("operational-environment-dao");
103         }
104         
105         private static void modifyDb(List<OperationalEnvironmentEntry> environments) {
106                 OperationalEnvironmentDao daoObj = createDaoObj();
107                 
108                 daoObj.deleteAll();
109                 
110                 environments.forEach(daoObj::save);
111         }
112
113         public static String getTableName() {
114                 return TABLE_NAME;
115         }
116
117
118 }