cbb2de7dbc7b54e37a4948353344f4f09d9b4baf
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / impl / FSConfigurationSource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.common.impl;
23
24 import java.util.Arrays;
25 import java.util.stream.Collectors;
26 import org.openecomp.sdc.common.api.ConfigurationListener;
27 import org.openecomp.sdc.common.api.ConfigurationSource;
28 import org.openecomp.sdc.common.api.Constants;
29 import org.openecomp.sdc.common.api.exception.LoadConfigurationException;
30 import org.openecomp.sdc.common.util.YamlToObjectConverter;
31 import org.openecomp.sdc.exception.YamlConversionException;
32
33 /**
34  * Read configuration from file system
35  *
36  * @author esofer
37  */
38 public class FSConfigurationSource implements ConfigurationSource {
39
40     private final YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
41     private final ConfigFileChangeListener changeListener;
42     private final String appConfigDir;
43
44     public FSConfigurationSource(ConfigFileChangeListener changeListener, String appConfigDir) {
45         super();
46         this.changeListener = changeListener;
47         this.appConfigDir = appConfigDir;
48     }
49
50     /**
51      * convert camel case string to list of words separated by "-" where each word is in lower case format. For example, MyClass will be calculated to
52      * be my-class.yaml .
53      *
54      * @param className
55      * @return file name based on the class name
56      */
57     static <T> String calculateFileName(Class<T> className) {
58         String[] words = className.getSimpleName().split("(?=\\p{Upper})");
59         return Arrays.stream(words).map(String::toLowerCase)
60             .collect(Collectors.collectingAndThen(Collectors.joining("-"), str -> str + Constants.YAML_SUFFIX));
61     }
62
63     /*
64      * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-".
65      *
66      * (non-Javadoc)
67      *
68      * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener)
69      */
70     public <T> T getAndWatchConfiguration(final Class<T> className, final ConfigurationListener configurationListener) {
71         final String configFileName = calculateFileName(className);
72         T object;
73         try {
74             object = yamlToObjectConverter.convert(this.appConfigDir, className, configFileName);
75         } catch (final YamlConversionException e) {
76             final String errorMsg = String.format("Could not load '%s' in '%s' for class '%s'", configFileName, appConfigDir, className);
77             throw new LoadConfigurationException(errorMsg, e);
78         }
79         if (configurationListener != null && changeListener != null) {
80             changeListener.register(configFileName, configurationListener);
81         }
82         return object;
83     }
84
85     public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
86         String configFileName = calculateFileName(className);
87         if (configurationListener != null) {
88             changeListener.register(configFileName, configurationListener);
89         }
90     }
91 }