07c84c66f6cba71161f4c9e0abfa483eba7d8be0
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.ccsdk.sli.core.slipluginutils;
23
24 import com.google.common.annotations.VisibleForTesting;
25 import com.google.common.base.Strings;
26 import java.io.File;
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;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
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.
42  */
43 public final class Dme2PropertiesProvider {
44
45     private static final Logger LOG = LoggerFactory.getLogger(Dme2PropertiesProvider.class);
46
47     /**
48      * The name of the environment variable to specify the configuration directory.
49      */
50     private static final String SDNC_ROOT_DIR_ENV_VAR_KEY = "SDNC_CONFIG_DIR";
51
52     /**
53      * the dme2 properties file name.
54      */
55     private static final String DME2_PROPERTIES_FILE_NAME = "dme2.properties";
56
57     /**
58      * the key for <code>proxyUrl</code>, which represents a CSV list of urls
59      */
60     static final String PROXY_URL_KEY = "proxyUrl";
61
62     /**
63      * indicates that proxy urls are separated by commas
64      */
65     private static final String PROXY_URLS_VALUE_SEPARATOR = ",";
66
67     /**
68      * the key for <code>aafUserName</code>
69      */
70     static final String AAF_USERNAME_KEY = "aafUserName";
71
72     /**
73      * the key for <code>aafPassword</code>
74      */
75     static final String AAF_PASSWORD_KEY = "aafPassword";
76
77     /**
78      * the key for <code>envContext</code>
79      */
80     static final String ENV_CONTEXT_KEY = "envContext";
81
82     /**
83      * the key for <code>routeOffer</code>
84      */
85     static final String ROUTE_OFFER_KEY = "routeOffer";
86
87     /**
88      * the key for <code>commonServiceVersion</code>
89      */
90     static final String COMMON_SERVICE_VERSION_KEY = "commonServiceVersion";
91
92     /**
93      * the key for <code>partner</code>
94      */
95     static final String PARTNER_KEY = "partner";
96
97     private Optional<String []> proxyUrls = Optional.empty();
98
99     private Optional<String> aafUsername = Optional.empty();
100
101     private Optional<String> aafPassword = Optional.empty();
102
103     private Optional<String> envContext = Optional.empty();
104
105     private Optional<String> routeOffer = Optional.empty();
106
107     private Optional<String> commonServiceVersion = Optional.empty();
108
109     private Optional<String> partner = Optional.empty();
110
111
112     /**
113      * Instantiates the properties provider, which involves loading the appropriate properties for dme2.
114      */
115     public Dme2PropertiesProvider() {
116         this(getDme2Path(SDNC_ROOT_DIR_ENV_VAR_KEY, DME2_PROPERTIES_FILE_NAME).toString());
117     }
118
119     /**
120      * Instantiates the properties provider, which involves loading the appropriate properties for dme2.
121      *
122      * @param dme2Path location of the dme2.properties file
123      */
124     @VisibleForTesting
125     Dme2PropertiesProvider(final String dme2Path) {
126         final Properties properties;
127         try {
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);
140         }
141     }
142
143     private static Path getDme2Path(final String sdncRootDirectory, final String dme2Filename) {
144         return Paths.get(sdncRootDirectory, dme2Filename);
145     }
146
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));
151         return properties;
152     }
153
154     private String getProxyUrl(final Properties properties) {
155         return properties.getProperty(PROXY_URL_KEY);
156     }
157
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));
162         }
163         return Optional.empty();
164     }
165
166     public Optional<String []> getProxyUrls() {
167         return this.proxyUrls;
168     }
169
170     private Optional<String> getAafUsername(final Properties properties) {
171         final String aafUsernameValue = properties.getProperty(AAF_USERNAME_KEY);
172         return Optional.ofNullable(aafUsernameValue);
173     }
174
175     Optional<String> getAafUsername() {
176         return this.aafUsername;
177     }
178
179     private Optional<String> getAafPassword(final Properties properties) {
180         final String aafPassword = properties.getProperty(AAF_PASSWORD_KEY);
181         return Optional.ofNullable(aafPassword);
182     }
183
184     Optional<String> getAafPassword() {
185         return this.aafPassword;
186     }
187
188     private Optional<String> getEnvContext(final Properties properties) {
189         final String envContext = properties.getProperty(ENV_CONTEXT_KEY);
190         return Optional.ofNullable(envContext);
191     }
192
193     Optional<String> getEnvContext() {
194         return this.envContext;
195     }
196
197     private Optional<String> getRouteOffer(final Properties properties) {
198         final String routeOffer = properties.getProperty(ROUTE_OFFER_KEY);
199         return Optional.ofNullable(routeOffer);
200     }
201
202     Optional<String> getRouteOffer() {
203         return this.routeOffer;
204     }
205
206     private Optional<String> getCommonServiceVersion(final Properties properties) {
207         final String commonServiceVersion = properties.getProperty(COMMON_SERVICE_VERSION_KEY);
208         return Optional.ofNullable(commonServiceVersion);
209     }
210
211     Optional<String> getCommonServiceVersion() {
212         return this.commonServiceVersion;
213     }
214
215     private Optional<String> getPartner(final Properties properties) {
216         final String partner = properties.getProperty(PARTNER_KEY);
217         return Optional.ofNullable(partner);
218     }
219
220     Optional<String> getPartner() {
221         return this.partner;
222     }
223 }