DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / errormng / ErrorConfigurationLoader.java
1 package org.onap.sdc.dcae.errormng;
2
3 import org.apache.commons.lang.ArrayUtils;
4 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
5 import org.onap.sdc.common.onaplog.OnapLoggerError;
6 import org.yaml.snakeyaml.Yaml;
7
8 import java.io.File;
9 import java.io.FilenameFilter;
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.nio.file.Files;
13 import java.nio.file.Paths;
14
15 import org.onap.sdc.common.onaplog.Enums.LogLevel;
16
17 public class ErrorConfigurationLoader {
18
19         private static ErrorConfigurationLoader instance;
20         private String jettyBase;
21         private ErrorConfiguration errorConfiguration = new ErrorConfiguration();
22         private OnapLoggerError errLogger = OnapLoggerError.getInstance();
23         private OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
24
25         public ErrorConfigurationLoader(String sourcePath) {
26                 jettyBase = sourcePath;
27                 loadErrorConfiguration();
28                 instance = this;
29         }
30
31         private void loadErrorConfiguration(){
32
33                 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "ErrorConfigurationLoader: Trying to load error configuration");
34                 if (jettyBase == null) {
35                         String msg = "Couldn't resolve jetty.base environmental variable";
36                         errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
37                         throw new ExceptionInInitializerError (msg + ". Failed to load error configuration files... aborting");
38                 }
39
40                 String path = jettyBase + "/config/dcae-be";
41                 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "jetty.base={}", jettyBase);
42                 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Configuration Path={}", path);
43
44                 File dir = new File(path);
45                 File[] files = dir.listFiles(new FilenameFilter() {
46                         @Override public boolean accept(File dir, String name) {
47                                 return name.equals("error-configuration.yaml");
48                         }
49                 });
50
51                 if (ArrayUtils.isEmpty(files)) {
52                         String msg = "No error configuration files found";
53                         errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
54                         throw new ExceptionInInitializerError (msg);
55                 }else if (files.length>1){
56                         String msg = "Multiple configuration files found. Make sure only one file exists. Path: "+ path;
57                         errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
58                         throw new ExceptionInInitializerError (msg);
59                 }
60                 else {
61                         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Loading error configuration file: {}", files[0].getName());
62                         try {
63                                 errorConfiguration = parseErrConfFileAndSaveToMap(files[0].getCanonicalPath());
64 //                              convertToUsefulMaps(errorConfiguration);
65                         } catch (IOException e) {
66                                 String msg = "Exception thrown while trying to read the error configuration file path. File="+files[0].getName();
67                                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
68                                 throw new ExceptionInInitializerError (msg);
69                         }
70                         if(errorConfiguration == null){
71                                 String msg = "Error configuration file couldn't be parsed";
72                                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
73                                 throw new ExceptionInInitializerError (msg);
74                         }
75                         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Error Configuration: {}", errorConfiguration.toString());
76                 }
77         }
78         
79
80         private ErrorConfiguration parseErrConfFileAndSaveToMap(String fullFileName) {
81
82                 Yaml yaml = new Yaml();
83
84                 InputStream in = null;
85                 ErrorConfiguration errorConfiguration = null;
86                 try {
87
88                         File f = new File(fullFileName);
89                         if (false == f.exists()) {
90                                 errLogger.log(LogLevel.ERROR, this.getClass().getName(), "The file {} cannot be found. Ignore reading configuration.", fullFileName);
91                                 return null;
92                         }
93                         in = Files.newInputStream(Paths.get(fullFileName));
94
95                         errorConfiguration = yaml.loadAs(in, ErrorConfiguration.class);
96
97                 } catch (Exception e) {
98                         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to convert yaml file {} to object. {}", fullFileName, e);
99                         return null;
100                 } 
101                 finally {
102                         if (in != null) {
103                                 try {
104                                         in.close();
105                                 } catch (IOException e) {
106                                         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to close input stream {}", e.getMessage());
107                                 }
108                         }
109                 }
110
111                 return errorConfiguration;
112         }
113
114         ErrorConfiguration getErrorConfiguration() {
115                 return errorConfiguration;
116         }
117
118         public static ErrorConfigurationLoader getErrorConfigurationLoader() {
119                 return instance;
120         }
121 }