2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.sli.adaptors.ansible.impl;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.Properties;
33 import org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdaptorPropertiesProvider;
34 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
35 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
36 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
37 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
38 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
39 import org.onap.ccsdk.sli.core.utils.common.EnvProperties;
40 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Responsible for determining the properties file to use and instantiating the
46 * <code>SqlResource</code> Service. The priority for properties file
47 * resolution is as follows:
50 * <li>A directory identified by the system environment variable
51 * <code>SDNC_CONFIG_DIR</code></li>
52 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
53 * <li>A directory identified by the JRE argument
54 * <code>sql-resource.properties</code></li>
55 * <li>A <code>sql-resource.properties</code> file located in the karaf root
59 public class AnsibleAdaptorPropertiesProviderImpl implements AnsibleAdaptorPropertiesProvider {
61 private static final Logger LOG = LoggerFactory.getLogger(AnsibleAdaptorPropertiesProviderImpl.class);
64 * The name of the properties file for database configuration
66 private static final String ANSIBLE_Adaptor_PROPERTIES = "ansible-adaptor.properties";
69 * A prioritized list of strategies for resolving sql-resource properties files.
71 private final List<PropertiesFileResolver> ansibleAdaptorPropertiesFileResolvers = new ArrayList<>();
74 * The configuration properties for the db connection.
76 private Properties properties;
79 * Set up the prioritized list of strategies for resolving dblib properties
82 public AnsibleAdaptorPropertiesProviderImpl() {
83 ansibleAdaptorPropertiesFileResolvers
84 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
85 ansibleAdaptorPropertiesFileResolvers
86 .add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
87 ansibleAdaptorPropertiesFileResolvers
88 .add(new JREFileResolver("Using property file (3) from JRE argument", AnsibleAdaptorPropertiesProviderImpl.class));
89 ansibleAdaptorPropertiesFileResolvers
90 .add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
92 // determines properties file as according to the priority described in the
93 // class header comment
94 final File propertiesFile = determinePropertiesFile();
95 if (propertiesFile != null) {
96 try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
97 properties = new EnvProperties();
98 properties.load(fileInputStream);
99 } catch (final IOException e) {
100 LOG.error("Failed to load properties for file: {}", propertiesFile,
101 new ConfigurationException("Failed to load properties for file: " + propertiesFile, e));
104 // Try to read properties as resource
105 InputStream propStr = getClass().getResourceAsStream("/" + ANSIBLE_Adaptor_PROPERTIES);
106 if (propStr != null) {
107 properties = new EnvProperties();
109 properties.load(propStr);
111 } catch (IOException e) {
116 if (properties == null) {
117 reportFailure(new ConfigurationException(
118 "Missing configuration properties resource(3): " + ANSIBLE_Adaptor_PROPERTIES));
119 LOG.info("Defaulting org.onap.appc.adaptor.ansible.clientType to TRUST_ALL");
120 properties = new Properties();
121 properties.setProperty("org.onap.appc.adaptor.ansible.clientType", "TRUST_ALL");
127 * Instantiates a new Ansible adaptor properties provider.
129 * @param configFilePath the config file path
131 public AnsibleAdaptorPropertiesProviderImpl(String configFilePath) {
132 properties = new EnvProperties();
134 properties.load(new FileInputStream(configFilePath));
135 } catch (IOException e) {
138 if (properties == null) {
139 reportFailure(new ConfigurationException(
140 "Missing configuration properties resource(3): " + ANSIBLE_Adaptor_PROPERTIES));
141 LOG.info("Defaulting org.onap.appc.adaptor.ansible.clientType to TRUST_ALL");
142 properties = new Properties();
143 properties.setProperty("org.onap.appc.adaptor.ansible.clientType", "TRUST_ALL");
149 * Reports the method chosen for properties resolution to the
150 * <code>Logger</code>.
152 * @param message Some user friendly message
153 * @param fileOptional The file location of the chosen properties file
155 * @return the file location of the chosen properties file
157 private static File reportSuccess(final String message, final Optional<File> fileOptional) {
158 if (fileOptional.isPresent()) {
159 final File file = fileOptional.get();
160 LOG.info("{} {}", message, file.getPath());
167 * Reports fatal errors. This is the case in which no properties file could be
170 * @param configurationException An exception describing what went wrong during resolution
172 private static void reportFailure(final ConfigurationException configurationException) {
173 LOG.error("{}", "Missing configuration properties resource(3)", configurationException);
177 * Extract svclogic config properties.
179 * @return the svclogic config properties
181 public Properties getProperties() {
186 * Determines the sql-resource properties file to use based on the following priority:
188 * <li>A directory identified by the system environment variable
189 * <code>SDNC_CONFIG_DIR</code></li>
190 * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
191 * <li>A directory identified by the JRE argument
192 * <code>sql-resource.properties</code></li>
193 * <li>A <code>sql-resource.properties</code> file located in the karaf root
197 File determinePropertiesFile() {
198 for (final PropertiesFileResolver propertiesFileResolver : ansibleAdaptorPropertiesFileResolvers) {
199 final Optional<File> fileOptional = propertiesFileResolver.resolveFile(ANSIBLE_Adaptor_PROPERTIES);
200 if (fileOptional.isPresent()) {
201 return reportSuccess(propertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);