4f6fdee0567d6edda6a024fd8fedc65b7c9999b9
[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 = RestAuthenticationMode.getRestAuthenticationMode(restProps.getProperty("authenticationMode", RestAuthenticationMode.SSL_CERT.getAuthenticationModeLabel()));
135
136     /*
137      * In any kind of error scenario, set the authentication mode to SSL_CERT as our default.
138      * This is an arbitrary default, but was chosen based on the way this code worked before
139      * introduction of the SSL Basic Auth settings.
140      */
141     if ( authenticationMode == RestAuthenticationMode.UNKNOWN_MODE) {
142       authenticationMode = RestAuthenticationMode.SSL_CERT;
143     }
144     
145   }
146
147   public RestAuthenticationMode getAuthenticationMode() {
148     return authenticationMode;
149   }
150
151   public void setAuthenticationMode(RestAuthenticationMode authenticationMode) {
152     this.authenticationMode = authenticationMode;
153   }
154
155   public int getNumCacheWorkers() {
156     return numCacheWorkers;
157   }
158
159   public void setNumCacheWorkers(int numCacheWorkers) {
160     this.numCacheWorkers = numCacheWorkers;
161   }
162
163   /**
164    * Should cache failures.
165    *
166    * @return true, if successful
167    */
168   public boolean shouldCacheFailures() {
169     return cacheFailures;
170   }
171
172   public void setShouldCacheFailures(boolean enabled) {
173     this.cacheFailures = enabled;
174   }
175
176   /**
177    * Checks if is shallow entity.
178    *
179    * @param entityType the entity type
180    * @return true, if is shallow entity
181    */
182   public boolean isShallowEntity(String entityType) {
183     if (entityType == null) {
184       return false;
185     }
186
187     for (String entity : shallowEntities) {
188       if (entityType.equalsIgnoreCase(entity)) {
189         return true;
190       }
191     }
192
193     return false;
194   }
195
196   public boolean isUseCacheOnly() {
197     return useCacheOnly;
198   }
199
200   public void setUseCacheOnly(boolean useCacheOnly) {
201     this.useCacheOnly = useCacheOnly;
202   }
203
204   public int getNumResolverWorkers() {
205     return numResolverWorkers;
206   }
207
208   public void setNumResolverWorkers(int numResolverWorkers) {
209     this.numResolverWorkers = numResolverWorkers;
210   }
211
212   public long getMaxTimeToLiveInMs() {
213     return maxTimeToLiveInMs;
214   }
215
216   public void setMaxTimeToLiveInMs(long maxTimeToLiveInMs) {
217     this.maxTimeToLiveInMs = maxTimeToLiveInMs;
218   }
219
220   public boolean isCacheEnabled() {
221     return cacheEnabled;
222   }
223
224   public void setCacheEnabled(boolean cacheEnabled) {
225     this.cacheEnabled = cacheEnabled;
226   }
227
228   public String getStorageFolderOverride() {
229     return storageFolderOverride;
230   }
231
232   public void setStorageFolderOverride(String storageFolderOverride) {
233     this.storageFolderOverride = storageFolderOverride;
234   }
235
236   public String getHost() {
237     return host;
238   }
239
240   public String getPort() {
241     return port;
242   }
243
244   public String getResourceBasePath() {
245     return resourceBasePath;
246   }
247
248   public void setHost(String host) {
249     this.host = host;
250   }
251
252   public void setPort(String port) {
253     this.port = port;
254   }
255
256   /* (non-Javadoc)
257    * @see java.lang.Object#toString()
258    */
259  
260  
261   public void setResourceBasePath(String resourceBasePath) {
262     this.resourceBasePath = resourceBasePath;
263   }
264
265   @Override
266   public String toString() {
267     return "ActiveInventoryRestConfig [host=" + host + ", port=" + port + ", connectTimeoutInMs="
268         + connectTimeoutInMs + ", readTimeoutInMs=" + readTimeoutInMs + ", numRequestRetries="
269         + numRequestRetries + ", numResolverWorkers=" + numResolverWorkers + ", useCacheOnly="
270         + useCacheOnly + ", cacheEnabled=" + cacheEnabled + ", cacheFailures=" + cacheFailures
271         + ", storageFolderOverride=" + storageFolderOverride + ", numCacheWorkers="
272         + numCacheWorkers + ", maxTimeToLiveInMs=" + maxTimeToLiveInMs + ", resourceBasePath="
273         + resourceBasePath + ", shallowEntities=" + shallowEntities + ", authenticationMode="
274         + authenticationMode + "]";
275   }
276
277   public int getConnectTimeoutInMs() {
278     return connectTimeoutInMs;
279   }
280
281   public void setConnectTimeoutInMs(int connectTimeoutInMs) {
282     this.connectTimeoutInMs = connectTimeoutInMs;
283   }
284
285   public int getReadTimeoutInMs() {
286     return readTimeoutInMs;
287   }
288
289   public void setReadTimeoutInMs(int readTimeoutInMs) {
290     this.readTimeoutInMs = readTimeoutInMs;
291   }
292
293   public int getNumRequestRetries() {
294     return numRequestRetries;
295   }
296
297   public void setNumRequestRetries(int numRequestRetries) {
298     this.numRequestRetries = numRequestRetries;
299   }
300
301 }