Remove sdc-tosca-parser dependency
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / config / NonManoConfigurationManager.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.config;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
25 import org.apache.commons.io.IOUtils;
26 import org.openecomp.sdc.be.config.exception.LoadConfigurationException;
27 import org.yaml.snakeyaml.Yaml;
28
29 /**
30  * Singleton that loads and stores the Non Mano configuration
31  */
32 public class NonManoConfigurationManager {
33     private static NonManoConfigurationManager nonManoConfigurationManager = null;
34     private NonManoConfiguration nonManoConfiguration;
35
36     private NonManoConfigurationManager() {
37         loadConfiguration();
38     }
39
40     /**
41      * Loads the configuration yaml from the resources.
42      */
43     private void loadConfiguration() {
44         final InputStream configYamlAsStream = getClass().getClassLoader().getResourceAsStream("config/nonManoConfig.yaml");
45         if (configYamlAsStream == null) {
46             throw new LoadConfigurationException("Expected non-mano configuration file 'config/nonManoConfig.yaml' not found in resources");
47         }
48         final String data;
49         try {
50             data = IOUtils.toString(configYamlAsStream, String.valueOf(StandardCharsets.UTF_8));
51         } catch (final IOException e) {
52             throw new LoadConfigurationException("Could not parse non-mano configuration file 'config/nonManoConfig.yaml' to string", e);
53         }
54         nonManoConfiguration = convertFromString(data, NonManoConfiguration.class);
55     }
56
57     public static NonManoConfigurationManager getInstance() {
58         if (nonManoConfigurationManager == null) {
59             nonManoConfigurationManager = new NonManoConfigurationManager();
60         }
61
62         return nonManoConfigurationManager;
63     }
64
65     public NonManoConfiguration getNonManoConfiguration() {
66         return nonManoConfiguration;
67     }
68
69     private <T> T convertFromString(final String yamlContents, final Class<T> className) {
70         try {
71             return (new Yaml()).loadAs(yamlContents, className);
72         } catch (final Exception e) {
73             throw new
74                 LoadConfigurationException(String.format("Failed to convert YAML %s to object.", yamlContents), e);
75         }
76     }
77
78 }