Increase junit coverage
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / aai / ActiveInventoryAdapter.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;
24
25 import java.io.IOException;
26 import java.net.URLEncoder;
27 import java.nio.ByteBuffer;
28 import java.util.List;
29 import java.util.NoSuchElementException;
30
31 import org.apache.http.client.utils.URIBuilder;
32 import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor;
33 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
34 import org.onap.aai.sparky.dal.aai.config.ActiveInventoryConfig;
35 import org.onap.aai.sparky.dal.aai.config.ActiveInventoryRestConfig;
36 import org.onap.aai.sparky.dal.aai.enums.RestAuthenticationMode;
37 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
38 import org.onap.aai.sparky.dal.rest.OperationResult;
39 import org.onap.aai.sparky.dal.rest.RestClientBuilder;
40 import org.onap.aai.sparky.dal.rest.RestfulDataAccessor;
41 import org.onap.aai.sparky.logging.AaiUiMsgs;
42 import org.onap.aai.sparky.security.SecurityContextFactory;
43 import org.onap.aai.sparky.util.NodeUtils;
44 import org.onap.aai.cl.api.Logger;
45 import org.onap.aai.cl.eelf.LoggerFactory;
46
47 import com.sun.jersey.api.client.Client;
48 import com.sun.jersey.api.client.WebResource.Builder;
49
50
51 /**
52  * The Class ActiveInventoryAdapter.
53  */
54
55 /**
56  * @author davea
57  *
58  */
59 public class ActiveInventoryAdapter extends RestfulDataAccessor
60     implements ActiveInventoryDataProvider {
61
62   private static final Logger LOG =
63       LoggerFactory.getInstance().getLogger(ActiveInventoryAdapter.class);
64   
65   private static final String HEADER_TRANS_ID = "X-TransactionId";
66   private static final String HEADER_FROM_APP_ID = "X-FromAppId";
67   private static final String HEADER_AUTHORIZATION = "Authorization";
68
69   private static final String TRANSACTION_ID_PREFIX = "txnId-";
70   private static final String UI_APP_NAME = "AAI-UI";
71   
72   
73   private ActiveInventoryConfig config;
74
75   /**
76    * Instantiates a new active inventory adapter.
77    *
78    * @param restClientBuilder the rest client builder
79    * @throws ElasticSearchOperationException the elastic search operation exception
80    * @throws IOException Signals that an I/O exception has occurred.
81    */
82   public ActiveInventoryAdapter(RestClientBuilder restClientBuilder)
83       throws ElasticSearchOperationException, IOException {
84     super(restClientBuilder);
85
86     try {
87       this.config = ActiveInventoryConfig.getConfig();
88     } catch (Exception exc) {
89       throw new ElasticSearchOperationException("Error getting active inventory configuration",
90           exc);
91     }
92
93     clientBuilder.setUseHttps(true);
94
95     clientBuilder.setValidateServerHostname(config.getAaiSslConfig().isValidateServerHostName());
96
97     SecurityContextFactory sslContextFactory = clientBuilder.getSslContextFactory();
98
99     sslContextFactory.setServerCertificationChainValidationEnabled(
100         config.getAaiSslConfig().isValidateServerCertificateChain());
101     
102     if (config.getAaiRestConfig().getAuthenticationMode() == RestAuthenticationMode.SSL_CERT) {
103       sslContextFactory.setClientCertFileName(config.getAaiSslConfig().getKeystoreFilename());
104       sslContextFactory.setClientCertPassword(config.getAaiSslConfig().getKeystorePassword());
105       sslContextFactory.setTrustStoreFileName(config.getAaiSslConfig().getTruststoreFilename());
106     }
107
108     clientBuilder.setConnectTimeoutInMs(config.getAaiRestConfig().getConnectTimeoutInMs());
109     clientBuilder.setReadTimeoutInMs(config.getAaiRestConfig().getReadTimeoutInMs());
110
111   }
112
113   /* (non-Javadoc)
114    * @see org.onap.aai.sparky.dal.rest.RestfulDataAccessor#setClientDefaults(com.sun.jersey.api.client.Client, java.lang.String, java.lang.String, java.lang.String)
115    */
116   @Override
117   protected Builder setClientDefaults(Client client, String url, String payloadContentType,
118       String acceptContentType) {
119     Builder builder = super.setClientDefaults(client, url, payloadContentType, acceptContentType);
120
121     builder = builder.header(HEADER_FROM_APP_ID, UI_APP_NAME);
122     byte bytes[] = new byte[6];
123     txnIdGenerator.nextBytes(bytes);
124     builder =
125         builder.header(HEADER_TRANS_ID, TRANSACTION_ID_PREFIX + ByteBuffer.wrap(bytes).getInt());
126
127     if (config.getAaiRestConfig().getAuthenticationMode() == RestAuthenticationMode.SSL_BASIC) {
128       builder = builder.header(HEADER_AUTHORIZATION,
129           config.getAaiSslConfig().getBasicAuthenticationCredentials());
130     }
131
132     return builder;
133   }
134
135   /**
136    * Gets the full url.
137    *
138    * @param resourceUrl the resource url
139    * @return the full url
140    * @throws Exception the exception
141    */
142   private String getFullUrl(String resourceUrl) throws Exception {
143     ActiveInventoryRestConfig aaiRestConfig = ActiveInventoryConfig.getConfig().getAaiRestConfig();
144     final String host = aaiRestConfig.getHost();
145     final String port = aaiRestConfig.getPort();
146     final String basePath = aaiRestConfig.getResourceBasePath();
147     return String.format("https://%s:%s%s%s", host, port, basePath, resourceUrl);
148   }
149   
150   public String getGenericQueryForSelfLink(String startNodeType, List<String> queryParams) throws Exception {
151     
152     URIBuilder urlBuilder = new URIBuilder(getFullUrl("/search/generic-query"));
153     
154     for( String queryParam : queryParams) {
155       urlBuilder.addParameter("key", queryParam);
156     }
157     
158     urlBuilder.addParameter("start-node-type", startNodeType);
159     urlBuilder.addParameter("include", startNodeType);
160     
161     final String constructedLink = urlBuilder.toString();
162     
163     // TODO: debug log for constructed link
164
165     return constructedLink;
166
167 }
168
169
170   /* (non-Javadoc)
171    * @see org.onap.aai.sparky.dal.aai.ActiveInventoryDataProvider#getSelfLinksByEntityType(java.lang.String)
172    */
173   @Override
174   public OperationResult getSelfLinksByEntityType(String entityType) throws Exception {
175
176     /*
177      * For this one, I want to dynamically construct the nodes-query for self-link discovery as a
178      * utility method that will use the OXM model entity data to drive the query as well.
179      */
180     
181     if (entityType == null) {
182       throw new NullPointerException(
183           "Failed to getSelfLinksByEntityType() because entityType is null");
184     }
185
186     OxmEntityDescriptor entityDescriptor =
187         OxmModelLoader.getInstance().getEntityDescriptor(entityType);
188
189     if (entityDescriptor == null) {
190       throw new NoSuchElementException("Failed to getSelfLinksByEntityType() because could"
191           + " not find entity descriptor from OXM with type = " + entityType);
192     }
193
194     String link = null;
195     final String primaryKeyStr =
196         NodeUtils.concatArray(entityDescriptor.getPrimaryKeyAttributeName(), "/");
197
198     link = getFullUrl("/search/nodes-query?search-node-type=" + entityType + "&filter="
199         + primaryKeyStr + ":EXISTS");
200
201
202
203     return doGet(link, "application/json");
204
205   }
206
207   /* (non-Javadoc)
208    * @see org.onap.aai.sparky.dal.aai.ActiveInventoryDataProvider#getSelfLinkForEntity(java.lang.String, java.lang.String, java.lang.String)
209    */
210   @Override
211   public OperationResult getSelfLinkForEntity(String entityType, String primaryKeyName,
212       String primaryKeyValue) throws Exception {
213
214     if (entityType == null) {
215       throw new NullPointerException("Failed to getSelfLinkForEntity() because entityType is null");
216     }
217
218     if (primaryKeyName == null) {
219       throw new NullPointerException(
220           "Failed to getSelfLinkForEntity() because primaryKeyName is null");
221     }
222
223     if (primaryKeyValue == null) {
224       throw new NullPointerException(
225           "Failed to getSelfLinkForEntity() because primaryKeyValue is null");
226     }
227
228
229     /*
230      * Try to protect ourselves from illegal URI formatting exceptions caused by characters that
231      * aren't natively supported in a URI, but can be escaped to make them legal.
232      */
233
234     String encodedEntityType = URLEncoder.encode(entityType, "UTF-8");
235     String encodedPrimaryKeyName = URLEncoder.encode(primaryKeyName, "UTF-8");
236     String encodedPrimaryKeyValue = URLEncoder.encode(primaryKeyValue, "UTF-8");
237
238     String link = null;
239
240     if ("service-instance".equals(entityType)) {
241
242       link = getFullUrl("/search/generic-query?key=" + encodedEntityType + "."
243           + encodedPrimaryKeyName + ":" + encodedPrimaryKeyValue + "&start-node-type="
244           + encodedEntityType + "&include=customer&depth=2");
245
246     } else {
247
248       link =
249           getFullUrl("/search/generic-query?key=" + encodedEntityType + "." + encodedPrimaryKeyName
250               + ":" + encodedPrimaryKeyValue + "&start-node-type=" + encodedEntityType);
251
252     }
253
254     return queryActiveInventoryWithRetries(link, "application/json",
255         this.config.getAaiRestConfig().getNumRequestRetries());
256
257   }
258
259
260   /**
261    * Our retry conditions should be very specific.
262    *
263    * @param r the r
264    * @return true, if successful
265    */
266   private boolean shouldRetryRequest(OperationResult r) {
267
268     if (r == null) {
269       return true;
270     }
271
272     int rc = r.getResultCode();
273
274     if (rc == 200) {
275       return false;
276     }
277
278     if (rc == 404) {
279       return false;
280     }
281
282     return true;
283
284   }
285
286   /**
287    * Query active inventory.
288    *
289    * @param url the url
290    * @param acceptContentType the accept content type
291    * @return the operation result
292    */
293   // package protected for test classes instead of private
294   OperationResult queryActiveInventory(String url, String acceptContentType) {
295     return doGet(url, acceptContentType);
296   }
297
298   /* (non-Javadoc)
299    * @see org.onap.aai.sparky.dal.aai.ActiveInventoryDataProvider#queryActiveInventoryWithRetries(java.lang.String, java.lang.String, int)
300    */
301   @Override
302   public OperationResult queryActiveInventoryWithRetries(String url, String responseType,
303       int numRetries) {
304
305     OperationResult result = null;
306
307     for (int x = 0; x < numRetries; x++) {
308
309       LOG.debug(AaiUiMsgs.QUERY_AAI_RETRY_SEQ, url, String.valueOf(x + 1));
310
311       result = queryActiveInventory(url, responseType);
312
313       /**
314        * Record number of times we have attempted the request to later summarize how many times we
315        * are generally retrying over thousands of messages in a sync.
316        * 
317        * If the number of retries is surprisingly high, then we need to understand why that is as
318        * the number of retries is also causing a heavier load on AAI beyond the throttling controls
319        * we already have in place in term of the transaction rate controller and number of
320        * parallelized threads per task processor.
321        */
322
323       result.setNumRequestRetries(x);
324
325       if (!shouldRetryRequest(result)) {
326
327         /*
328          * if (myConfig.getAaiRestConfig().isCacheEnabled()) {
329          * 
330          * CachedHttpRequest cachedRequest = new CachedHttpRequest();
331          * cachedRequest.setHttpRequestMethod("GET"); cachedRequest.setPayload("");
332          * cachedRequest.setPayloadMimeType(""); cachedRequest.setUrl(url);
333          * cachedRequest.setOperationType( TransactionStorageType.ACTIVE_INVENTORY_QUERY.getIndex()
334          * );
335          * 
336          * CachedHttpResponse cachedResponse = new CachedHttpResponse();
337          * cachedResponse.setPayload(result.getResult());
338          * cachedResponse.setPayloadMimeType("application/json");
339          * cachedResponse.setStatusCode(result.getResultCode());
340          * 
341          * CachedHttpTransaction txn = new CachedHttpTransaction(cachedRequest, cachedResponse);
342          * storageProvider.persistTransaction(txn);
343          * 
344          * }
345          */
346
347
348         result.setResolvedLinkFromServer(true);
349         LOG.debug(AaiUiMsgs.QUERY_AAI_RETRY_DONE_SEQ, url, String.valueOf(x + 1));
350
351         return result;
352       }
353
354       try {
355         /*
356          * Sleep between re-tries to be nice to the target system.
357          */
358         Thread.sleep(50);
359       } catch (InterruptedException exc) {
360         LOG.error(AaiUiMsgs.QUERY_AAI_WAIT_INTERRUPTION, exc.getLocalizedMessage());
361         break;
362       }
363       LOG.error(AaiUiMsgs.QUERY_AAI_RETRY_FAILURE_WITH_SEQ, url, String.valueOf(x + 1));
364     }
365
366
367     result.setResolvedLinkFailure(true);
368     LOG.info(AaiUiMsgs.QUERY_AAI_RETRY_MAXED_OUT, url);
369
370     return result;
371
372   }
373
374   /* (non-Javadoc)
375    * @see org.onap.aai.sparky.dal.rest.RestfulDataAccessor#shutdown()
376    */
377   @Override
378   public void shutdown() {
379     // TODO Auto-generated method stub
380
381     if (entityCache != null) {
382       entityCache.shutdown();
383     }
384
385   }
386
387
388 }