1 package org.onap.sdc.dcae.errormng;
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;
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;
15 import org.onap.sdc.common.onaplog.Enums.LogLevel;
17 public class ErrorConfigurationLoader {
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();
25 public ErrorConfigurationLoader(String sourcePath) {
26 jettyBase = sourcePath;
27 loadErrorConfiguration();
31 private void loadErrorConfiguration(){
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");
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);
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");
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);
61 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Loading error configuration file: {}", files[0].getName());
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);
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);
75 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Error Configuration: {}", errorConfiguration.toString());
80 private ErrorConfiguration parseErrConfFileAndSaveToMap(String fullFileName) {
82 Yaml yaml = new Yaml();
84 InputStream in = null;
85 ErrorConfiguration errorConfiguration = null;
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);
93 in = Files.newInputStream(Paths.get(fullFileName));
95 errorConfiguration = yaml.loadAs(in, ErrorConfiguration.class);
97 } catch (Exception e) {
98 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to convert yaml file {} to object. {}", fullFileName, e);
105 } catch (IOException e) {
106 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to close input stream {}", e.getMessage());
111 return errorConfiguration;
114 ErrorConfiguration getErrorConfiguration() {
115 return errorConfiguration;
118 public static ErrorConfigurationLoader getErrorConfigurationLoader() {