2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2016 - 2017 ONAP
 
   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.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.ccsdk.sli.adaptors.ansible.impl;
 
  24 import java.io.FileInputStream;
 
  25 import java.io.IOException;
 
  26 import java.io.InputStream;
 
  27 import java.util.Optional;
 
  28 import java.util.Properties;
 
  29 import java.util.Vector;
 
  30 import org.onap.ccsdk.sli.adaptors.ansible.AnsibleAdapterPropertiesProvider;
 
  31 import org.onap.ccsdk.sli.core.sli.ConfigurationException;
 
  32 import org.onap.ccsdk.sli.core.utils.JREFileResolver;
 
  33 import org.onap.ccsdk.sli.core.utils.KarafRootFileResolver;
 
  34 import org.onap.ccsdk.sli.core.utils.PropertiesFileResolver;
 
  35 import org.onap.ccsdk.sli.core.utils.common.CoreDefaultFileResolver;
 
  36 import org.onap.ccsdk.sli.core.utils.common.SdncConfigEnvVarFileResolver;
 
  37 import org.slf4j.Logger;
 
  38 import org.slf4j.LoggerFactory;
 
  41  * Responsible for determining the properties file to use and instantiating the
 
  42  * <code>SqlResource</code> Service. The priority for properties file
 
  43  * resolution is as follows:
 
  46  * <li>A directory identified by the system environment variable
 
  47  * <code>SDNC_CONFIG_DIR</code></li>
 
  48  * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
 
  49  * <li>A directory identified by the JRE argument
 
  50  * <code>sql-resource.properties</code></li>
 
  51  * <li>A <code>sql-resource.properties</code> file located in the karaf root
 
  55 public class AnsibleAdapterPropertiesProviderImpl implements AnsibleAdapterPropertiesProvider {
 
  57     private static final Logger LOG = LoggerFactory.getLogger(AnsibleAdapterPropertiesProviderImpl.class);
 
  60      * The name of the properties file for database configuration
 
  62     private static final String ANSIBLEADAPTER_PROP_FILE_NAME = "ansible-adapter.properties";
 
  65      * A prioritized list of strategies for resolving sql-resource properties files.
 
  67     private Vector<PropertiesFileResolver> ansibleAdapterPropertiesFileResolvers = new Vector<>();
 
  70      * The configuration properties for the db connection.
 
  72     private Properties properties;
 
  75      * Set up the prioritized list of strategies for resolving dblib properties
 
  78     public AnsibleAdapterPropertiesProviderImpl() {
 
  79         ansibleAdapterPropertiesFileResolvers
 
  80                 .add(new SdncConfigEnvVarFileResolver("Using property file (1) from environment variable"));
 
  81         ansibleAdapterPropertiesFileResolvers.add(new CoreDefaultFileResolver("Using property file (2) from default directory"));
 
  83         ansibleAdapterPropertiesFileResolvers.add(
 
  84                 new JREFileResolver("Using property file (3) from JRE argument", AnsibleAdapterPropertiesProviderImpl.class));
 
  85         ansibleAdapterPropertiesFileResolvers.add(new KarafRootFileResolver("Using property file (4) from karaf root", this));
 
  87         // determines properties file as according to the priority described in the
 
  88         // class header comment
 
  89         final File propertiesFile = determinePropertiesFile(this);
 
  90         if (propertiesFile != null) {
 
  91             try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) {
 
  92                 properties = new Properties();
 
  93                 properties.load(fileInputStream);
 
  94             } catch (final IOException e) {
 
  95                 LOG.error("Failed to load properties for file: {}", propertiesFile.toString(),
 
  96                         new ConfigurationException("Failed to load properties for file: " + propertiesFile.toString(),
 
 100             // Try to read properties as resource
 
 102             InputStream propStr = getClass().getResourceAsStream("/" + ANSIBLEADAPTER_PROP_FILE_NAME);
 
 103             if (propStr != null) {
 
 104                 properties = new Properties();
 
 106                     properties.load(propStr);
 
 108                 } catch (IOException e) {
 
 115         if (properties == null) {
 
 116             reportFailure("Missing configuration properties resource(3)", new ConfigurationException(
 
 117                     "Missing configuration properties resource(3): " + ANSIBLEADAPTER_PROP_FILE_NAME));
 
 122      * Extract svclogic config properties.
 
 124      * @return the svclogic config properties
 
 126     public Properties getProperties() {
 
 131      * Reports the method chosen for properties resolution to the
 
 132      * <code>Logger</code>.
 
 135      *            Some user friendly message
 
 136      * @param fileOptional
 
 137      *            The file location of the chosen properties file
 
 138      * @return the file location of the chosen properties file
 
 140     private static File reportSuccess(final String message, final Optional<File> fileOptional) {
 
 141         if (fileOptional.isPresent()) {
 
 142             final File file = fileOptional.get();
 
 143             LOG.info("{} {}", message, file.getPath());
 
 150      * Reports fatal errors. This is the case in which no properties file could be
 
 154      *            An appropriate fatal error message
 
 155      * @param configurationException
 
 156      *            An exception describing what went wrong during resolution
 
 158     private static void reportFailure(final String message, final ConfigurationException configurationException) {
 
 160         LOG.error("{}", message, configurationException);
 
 164      * Determines the sql-resource properties file to use based on the following priority:
 
 166      * <li>A directory identified by the system environment variable
 
 167      * <code>SDNC_CONFIG_DIR</code></li>
 
 168      * <li>The default directory <code>DEFAULT_DBLIB_PROP_DIR</code></li>
 
 169      * <li>A directory identified by the JRE argument
 
 170      * <code>sql-resource.properties</code></li>
 
 171      * <li>A <code>sql-resource.properties</code> file located in the karaf root
 
 175     File determinePropertiesFile(final AnsibleAdapterPropertiesProviderImpl resourceProvider) {
 
 177         for (final PropertiesFileResolver sliPropertiesFileResolver : ansibleAdapterPropertiesFileResolvers) {
 
 178             final Optional<File> fileOptional = sliPropertiesFileResolver.resolveFile(ANSIBLEADAPTER_PROP_FILE_NAME);
 
 179             if (fileOptional.isPresent()) {
 
 180                 return reportSuccess(sliPropertiesFileResolver.getSuccessfulResolutionMessage(), fileOptional);