summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/errormng/ErrorConfigurationLoader.java
blob: 2180ca538e86586a830752f428c54919c0374e4d (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package org.onap.sdc.dcae.errormng;

import org.apache.commons.lang.ArrayUtils;
import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.common.onaplog.OnapLoggerError;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.onap.sdc.common.onaplog.enums.LogLevel;

public class ErrorConfigurationLoader {

	private static ErrorConfigurationLoader instance;
	private String jettyBase;
	private ErrorConfiguration errorConfiguration = new ErrorConfiguration();
	private OnapLoggerError errLogger = OnapLoggerError.getInstance();
	private OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

	public ErrorConfigurationLoader(String sourcePath) {
		jettyBase = sourcePath;
		loadErrorConfiguration();
		instance = this;
	}

	private void loadErrorConfiguration(){

		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "ErrorConfigurationLoader: Trying to load error configuration");
		if (jettyBase == null) {
			String msg = "Couldn't resolve jetty.base environmental variable";
			errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
			throw new ExceptionInInitializerError (msg + ". Failed to load error configuration files... aborting");
		}

		String path = jettyBase + "/config/dcae-be";
		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "jetty.base={}", jettyBase);
		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Configuration Path={}", path);

		File dir = new File(path);
		File[] files = dir.listFiles(new FilenameFilter() {
			@Override public boolean accept(File dir, String name) {
				return name.equals("error-configuration.yaml");
			}
		});

		if (ArrayUtils.isEmpty(files)) {
			String msg = "No error configuration files found";
			errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
			throw new ExceptionInInitializerError (msg);
		}else if (files.length>1){
			String msg = "Multiple configuration files found. Make sure only one file exists. Path: "+ path;
			errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
			throw new ExceptionInInitializerError (msg);
		}
		else {
			debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Loading error configuration file: {}", files[0].getName());
			try {
				errorConfiguration = parseErrConfFileAndSaveToMap(files[0].getCanonicalPath());
//				convertToUsefulMaps(errorConfiguration);
			} catch (IOException e) {
				String msg = "Exception thrown while trying to read the error configuration file path. File="+files[0].getName();
				errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
				throw new ExceptionInInitializerError (msg);
			}
			if(errorConfiguration == null){
				String msg = "Error configuration file couldn't be parsed";
				errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
				throw new ExceptionInInitializerError (msg);
			}
			debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Error Configuration: {}", errorConfiguration.toString());
		}
	}
	

	private ErrorConfiguration parseErrConfFileAndSaveToMap(String fullFileName) {

		Yaml yaml = new Yaml();

		InputStream in = null;
		ErrorConfiguration errorConfiguration = null;
		try {

			File f = new File(fullFileName);
			if (false == f.exists()) {
				errLogger.log(LogLevel.ERROR, this.getClass().getName(), "The file {} cannot be found. Ignore reading configuration.", fullFileName);
				return null;
			}
			in = Files.newInputStream(Paths.get(fullFileName));

			errorConfiguration = yaml.loadAs(in, ErrorConfiguration.class);

		} catch (Exception e) {
			debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to convert yaml file {} to object. {}", fullFileName, e);
			return null;
		} 
		finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Failed to close input stream {}", e.getMessage());
				}
			}
		}

		return errorConfiguration;
	}

	ErrorConfiguration getErrorConfiguration() {
		return errorConfiguration;
	}

	public static ErrorConfigurationLoader getErrorConfigurationLoader() {
		return instance;
	}
}