2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.ccsdk.sli.core.slipluginutils;
 
  24 import com.google.common.annotations.VisibleForTesting;
 
  25 import com.google.common.base.Strings;
 
  27 import java.io.FileNotFoundException;
 
  28 import java.io.FileReader;
 
  29 import java.io.IOException;
 
  30 import java.nio.file.Path;
 
  31 import java.nio.file.Paths;
 
  32 import java.util.Optional;
 
  33 import java.util.Properties;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  39  * Immutable properties container for dme2 properties.  Since the initial design decision was made to
 
  40  * utilize <code>Properties</code> instead of an OSGi <code>ManagedService</code>, it was decided
 
  41  * to make these properties immutable.
 
  43 public final class Dme2PropertiesProvider {
 
  45     private static final Logger LOG = LoggerFactory.getLogger(Dme2PropertiesProvider.class);
 
  48      * The name of the environment variable to specify the configuration directory.
 
  50     private static final String SDNC_ROOT_DIR_ENV_VAR_KEY = "SDNC_CONFIG_DIR";
 
  53      * the dme2 properties file name.
 
  55     private static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";
 
  58      * the key for <code>proxyUrl</code>, which represents a CSV list of urls
 
  60     static final String PROXY_URL_KEY = "proxyUrl";
 
  63      * indicates that proxy urls are separated by commas
 
  65     private static final String PROXY_URLS_VALUE_SEPARATOR = ",";
 
  68      * the key for <code>aafUserName</code>
 
  70     static final String AAF_USERNAME_KEY = "aafUserName";
 
  73      * the key for <code>aafPassword</code>
 
  75     static final String AAF_PASSWORD_KEY = "aafPassword";
 
  78      * the key for <code>envContext</code>
 
  80     static final String ENV_CONTEXT_KEY = "envContext";
 
  83      * the key for <code>routeOffer</code>
 
  85     static final String ROUTE_OFFER_KEY = "routeOffer";
 
  88      * the key for <code>commonServiceVersion</code>
 
  90     static final String COMMON_SERVICE_VERSION_KEY = "commonServiceVersion";
 
  93      * the key for <code>partner</code>
 
  95     static final String PARTNER_KEY = "partner";
 
  97     private Optional<String []> proxyUrls = Optional.empty();
 
  99     private Optional<String> aafUsername = Optional.empty();
 
 101     private Optional<String> aafPassword = Optional.empty();
 
 103     private Optional<String> envContext = Optional.empty();
 
 105     private Optional<String> routeOffer = Optional.empty();
 
 107     private Optional<String> commonServiceVersion = Optional.empty();
 
 109     private Optional<String> partner = Optional.empty();
 
 113      * Instantiates the properties provider, which involves loading the appropriate properties for dme2.
 
 115     public Dme2PropertiesProvider() {
 
 116         this(getDme2Path(SDNC_ROOT_DIR_ENV_VAR_KEY, DME2_PROPERTIES_FILE_NAME).toString());
 
 120      * Instantiates the properties provider, which involves loading the appropriate properties for dme2.
 
 122      * @param dme2Path location of the dme2.properties file
 
 125     Dme2PropertiesProvider(final String dme2Path) {
 
 126         final Properties properties;
 
 128             properties = getProperties(dme2Path);
 
 129             this.proxyUrls = getProxyUrls(properties);
 
 130             this.aafUsername = getAafUsername(properties);
 
 131             this.aafPassword = getAafPassword(properties);
 
 132             this.envContext = getEnvContext(properties);
 
 133             this.routeOffer = getRouteOffer(properties);
 
 134             this.commonServiceVersion = getCommonServiceVersion(properties);
 
 135             this.partner = getPartner(properties);
 
 136         } catch (final FileNotFoundException e) {
 
 137             LOG.error("dme2.properties file could not be found at path: {}", dme2Path, e);
 
 138         } catch (final IOException e) {
 
 139             LOG.error("fatal error reading dme2.properties at path: {}", dme2Path, e);
 
 143     private static Path getDme2Path(final String sdncRootDirectory, final String dme2Filename) {
 
 144         return Paths.get(sdncRootDirectory, dme2Filename);
 
 147     private static Properties getProperties(final String dme2Path) throws IOException {
 
 148         final File dme2File = new File(dme2Path);
 
 149         final Properties properties = new Properties();
 
 150         properties.load(new FileReader(dme2File));
 
 154     private String getProxyUrl(final Properties properties) {
 
 155         return properties.getProperty(PROXY_URL_KEY);
 
 158     private Optional<String []> getProxyUrls(final Properties properties) {
 
 159         final String proxyUrlsValue = getProxyUrl(properties);
 
 160         if (!Strings.isNullOrEmpty(proxyUrlsValue)) {
 
 161             return Optional.ofNullable(proxyUrlsValue.split(PROXY_URLS_VALUE_SEPARATOR));
 
 163         return Optional.empty();
 
 166     public Optional<String []> getProxyUrls() {
 
 167         return this.proxyUrls;
 
 170     private Optional<String> getAafUsername(final Properties properties) {
 
 171         final String aafUsernameValue = properties.getProperty(AAF_USERNAME_KEY);
 
 172         return Optional.ofNullable(aafUsernameValue);
 
 175     Optional<String> getAafUsername() {
 
 176         return this.aafUsername;
 
 179     private Optional<String> getAafPassword(final Properties properties) {
 
 180         final String aafPassword = properties.getProperty(AAF_PASSWORD_KEY);
 
 181         return Optional.ofNullable(aafPassword);
 
 184     Optional<String> getAafPassword() {
 
 185         return this.aafPassword;
 
 188     private Optional<String> getEnvContext(final Properties properties) {
 
 189         final String envContext = properties.getProperty(ENV_CONTEXT_KEY);
 
 190         return Optional.ofNullable(envContext);
 
 193     Optional<String> getEnvContext() {
 
 194         return this.envContext;
 
 197     private Optional<String> getRouteOffer(final Properties properties) {
 
 198         final String routeOffer = properties.getProperty(ROUTE_OFFER_KEY);
 
 199         return Optional.ofNullable(routeOffer);
 
 202     Optional<String> getRouteOffer() {
 
 203         return this.routeOffer;
 
 206     private Optional<String> getCommonServiceVersion(final Properties properties) {
 
 207         final String commonServiceVersion = properties.getProperty(COMMON_SERVICE_VERSION_KEY);
 
 208         return Optional.ofNullable(commonServiceVersion);
 
 211     Optional<String> getCommonServiceVersion() {
 
 212         return this.commonServiceVersion;
 
 215     private Optional<String> getPartner(final Properties properties) {
 
 216         final String partner = properties.getProperty(PARTNER_KEY);
 
 217         return Optional.ofNullable(partner);
 
 220     Optional<String> getPartner() {