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