5ed537b11cdf2cb1a3bd5ec7d0df8132f9958a9a
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / aai / config / ActiveInventoryRestConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.dal.aai.config;
24
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Properties;
28
29 import org.onap.aai.sparky.dal.aai.enums.RestAuthenticationMode;
30 import org.onap.aai.sparky.util.ConfigHelper;
31
32 /**
33  * The Class ActiveInventoryRestConfig.
34  */
35 public class ActiveInventoryRestConfig {
36
37   private String host;
38
39   /**
40    * @return the cacheFailures
41    */
42   public boolean isCacheFailures() {
43     return cacheFailures;
44   }
45
46   /**
47    * @param cacheFailures the cacheFailures to set
48    */
49   public void setCacheFailures(boolean cacheFailures) {
50     this.cacheFailures = cacheFailures;
51   }
52
53   /**
54    * @param shallowEntities the shallowEntities to set
55    */
56   public void setShallowEntities(List<String> shallowEntities) {
57     this.shallowEntities = shallowEntities;
58   }
59
60   private String port;
61
62   private int connectTimeoutInMs;
63
64   private int readTimeoutInMs;
65
66   private int numRequestRetries;
67
68   private int numResolverWorkers;
69
70   private boolean useCacheOnly;
71
72   private boolean cacheEnabled;
73
74   private boolean cacheFailures;
75
76   private String storageFolderOverride;
77
78   int numCacheWorkers;
79
80   private long maxTimeToLiveInMs;
81
82   private String resourceBasePath;
83
84   private List<String> shallowEntities;
85
86   private RestAuthenticationMode authenticationMode;
87
88   public List<String> getShallowEntities() {
89     return shallowEntities;
90   }
91
92   /**
93    * Instantiates a new active inventory rest config.
94    *
95    * @param props the props
96    */
97   public ActiveInventoryRestConfig(Properties props) {
98
99     if (props == null) {
100       return;
101     }
102
103     Properties restProps = ConfigHelper.getConfigWithPrefix("aai.rest", props);
104
105     resourceBasePath = restProps.getProperty("resourceBasePath", "/aai/v7");
106     host = restProps.getProperty("host", "localhost");
107     port = restProps.getProperty("port", "8443");
108     numRequestRetries = Integer.parseInt(restProps.getProperty("numRequestRetries", "5"));
109     numResolverWorkers = Integer.parseInt(restProps.getProperty("numResolverWorkers", "15"));
110
111     connectTimeoutInMs = Integer.parseInt(restProps.getProperty("connectTimeoutInMs", "5000"));
112     readTimeoutInMs = Integer.parseInt(restProps.getProperty("readTimeoutInMs", "10000"));
113
114     String shallowEntitiesProperty = restProps.getProperty("shallowEntities", "");
115     shallowEntities = Arrays.asList(shallowEntitiesProperty.split(","));
116
117     Properties cacheProps = ConfigHelper.getConfigWithPrefix("aai.rest.cache", props);
118     cacheEnabled = Boolean.parseBoolean(cacheProps.getProperty("enabled", "false"));
119     storageFolderOverride = cacheProps.getProperty("storageFolderOverride", null);
120     cacheFailures = Boolean.parseBoolean(cacheProps.getProperty("cacheFailures", "false"));
121     useCacheOnly = Boolean.parseBoolean(cacheProps.getProperty("useCacheOnly", "false"));
122     numCacheWorkers = Integer.parseInt(cacheProps.getProperty("numWorkers", "5"));
123
124
125     if (storageFolderOverride != null && storageFolderOverride.length() == 0) {
126       storageFolderOverride = null;
127     }
128     /*
129      * The expectation of this parameter is that if the value > 0, then the cached resources will be
130      * served back instead of dipping AAI/DataLayer as long as the current resource age from the
131      * cached instance is < maxTimeToLiveInMs.
132      */
133     maxTimeToLiveInMs = Long.parseLong(cacheProps.getProperty("maxTimeToLiveInMs", "-1"));
134     authenticationMode =
135         RestAuthenticationMode.getRestAuthenticationMode(restProps.getProperty("authenticationMode",
136             RestAuthenticationMode.SSL_CERT.getAuthenticationModeLabel()));
137
138     /*
139      * In any kind of error scenario, set the authentication mode to SSL_CERT as our default. This
140      * is an arbitrary default, but was chosen based on the way this code worked before introduction
141      * of the SSL Basic Auth settings.
142      */
143     if (authenticationMode == RestAuthenticationMode.UNKNOWN_MODE) {
144       authenticationMode = RestAuthenticationMode.SSL_CERT;
145     }
146
147   }
148
149   public RestAuthenticationMode getAuthenticationMode() {
150     return authenticationMode;
151   }
152
153   public void setAuthenticationMode(RestAuthenticationMode authenticationMode) {
154     this.authenticationMode = authenticationMode;
155   }
156
157   public int getNumCacheWorkers() {
158     return numCacheWorkers;
159   }
160
161   public void setNumCacheWorkers(int numCacheWorkers) {
162     this.numCacheWorkers = numCacheWorkers;
163   }
164
165   /**
166    * Should cache failures.
167    *
168    * @return true, if successful
169    */
170   public boolean shouldCacheFailures() {
171     return cacheFailures;
172   }
173
174   public void setShouldCacheFailures(boolean enabled) {
175     this.cacheFailures = enabled;
176   }
177
178   /**
179    * Checks if is shallow entity.
180    *
181    * @param entityType the entity type
182    * @return true, if is shallow entity
183    */
184   public boolean isShallowEntity(String entityType) {
185     if (entityType == null) {
186       return false;
187     }
188
189     for (String entity : shallowEntities) {
190       if (entityType.equalsIgnoreCase(entity)) {
191         return true;
192       }
193     }
194
195     return false;
196   }
197
198   public boolean isUseCacheOnly() {
199     return useCacheOnly;
200   }
201
202   public void setUseCacheOnly(boolean useCacheOnly) {
203     this.useCacheOnly = useCacheOnly;
204   }
205
206   public int getNumResolverWorkers() {
207     return numResolverWorkers;
208   }
209
210   public void setNumResolverWorkers(int numResolverWorkers) {
211     this.numResolverWorkers = numResolverWorkers;
212   }
213
214   public long getMaxTimeToLiveInMs() {
215     return maxTimeToLiveInMs;
216   }
217
218   public void setMaxTimeToLiveInMs(long maxTimeToLiveInMs) {
219     this.maxTimeToLiveInMs = maxTimeToLiveInMs;
220   }
221
222   public boolean isCacheEnabled() {
223     return cacheEnabled;
224   }
225
226   public void setCacheEnabled(boolean cacheEnabled) {
227     this.cacheEnabled = cacheEnabled;
228   }
229
230   public String getStorageFolderOverride() {
231     return storageFolderOverride;
232   }
233
234   public void setStorageFolderOverride(String storageFolderOverride) {
235     this.storageFolderOverride = storageFolderOverride;
236   }
237
238   public String getHost() {
239     return host;
240   }
241
242   public String getPort() {
243     return port;
244   }
245
246   public String getResourceBasePath() {
247     return resourceBasePath;
248   }
249
250   public void setHost(String host) {
251     this.host = host;
252   }
253
254   public void setPort(String port) {
255     this.port = port;
256   }
257
258   /*
259    * (non-Javadoc)
260    * 
261    * @see java.lang.Object#toString()
262    */
263
264
265   public void setResourceBasePath(String resourceBasePath) {
266     this.resourceBasePath = resourceBasePath;
267   }
268
269   @Override
270   public String toString() {
271     return "ActiveInventoryRestConfig [host=" + host + ", port=" + port + ", connectTimeoutInMs="
272         + connectTimeoutInMs + ", readTimeoutInMs=" + readTimeoutInMs + ", numRequestRetries="
273         + numRequestRetries + ", numResolverWorkers=" + numResolverWorkers + ", useCacheOnly="
274         + useCacheOnly + ", cacheEnabled=" + cacheEnabled + ", cacheFailures=" + cacheFailures
275         + ", storageFolderOverride=" + storageFolderOverride + ", numCacheWorkers="
276         + numCacheWorkers + ", maxTimeToLiveInMs=" + maxTimeToLiveInMs + ", resourceBasePath="
277         + resourceBasePath + ", shallowEntities=" + shallowEntities + ", authenticationMode="
278         + authenticationMode + "]";
279   }
280
281   public int getConnectTimeoutInMs() {
282     return connectTimeoutInMs;
283   }
284
285   public void setConnectTimeoutInMs(int connectTimeoutInMs) {
286     this.connectTimeoutInMs = connectTimeoutInMs;
287   }
288
289   public int getReadTimeoutInMs() {
290     return readTimeoutInMs;
291   }
292
293   public void setReadTimeoutInMs(int readTimeoutInMs) {
294     this.readTimeoutInMs = readTimeoutInMs;
295   }
296
297   public int getNumRequestRetries() {
298     return numRequestRetries;
299   }
300
301   public void setNumRequestRetries(int numRequestRetries) {
302     this.numRequestRetries = numRequestRetries;
303   }
304
305 }