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