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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.config;
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;
30 * Singleton that loads and stores the Non Mano configuration
32 public class NonManoConfigurationManager {
33 private static NonManoConfigurationManager nonManoConfigurationManager = null;
34 private NonManoConfiguration nonManoConfiguration;
36 private NonManoConfigurationManager() {
41 * Loads the configuration yaml from the resources.
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");
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);
54 nonManoConfiguration = convertFromString(data, NonManoConfiguration.class);
57 public static NonManoConfigurationManager getInstance() {
58 if (nonManoConfigurationManager == null) {
59 nonManoConfigurationManager = new NonManoConfigurationManager();
62 return nonManoConfigurationManager;
65 public NonManoConfiguration getNonManoConfiguration() {
66 return nonManoConfiguration;
69 private <T> T convertFromString(final String yamlContents, final Class<T> className) {
71 return (new Yaml()).loadAs(yamlContents, className);
72 } catch (final Exception e) {
74 LoadConfigurationException(String.format("Failed to convert YAML %s to object.", yamlContents), e);