2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.synchronizer;
28 import static java.util.concurrent.CompletableFuture.supplyAsync;
30 import java.io.IOException;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33 import java.sql.Timestamp;
34 import java.text.SimpleDateFormat;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Deque;
38 import java.util.EnumSet;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.List;
43 import java.util.Map.Entry;
45 import java.util.concurrent.ConcurrentHashMap;
46 import java.util.concurrent.ConcurrentLinkedDeque;
47 import java.util.concurrent.ExecutorService;
48 import java.util.concurrent.atomic.AtomicInteger;
49 import java.util.function.Supplier;
51 import javax.json.Json;
53 import org.openecomp.cl.api.Logger;
54 import org.openecomp.cl.eelf.LoggerFactory;
55 import org.openecomp.sparky.config.oxm.OxmEntityDescriptor;
56 import org.openecomp.sparky.dal.NetworkTransaction;
57 import org.openecomp.sparky.dal.elasticsearch.config.ElasticSearchConfig;
58 import org.openecomp.sparky.dal.rest.HttpMethod;
59 import org.openecomp.sparky.dal.rest.OperationResult;
60 import org.openecomp.sparky.logging.AaiUiMsgs;
61 import org.openecomp.sparky.synchronizer.config.SynchronizerConfiguration;
62 import org.openecomp.sparky.synchronizer.entity.AggregationEntity;
63 import org.openecomp.sparky.synchronizer.entity.MergableEntity;
64 import org.openecomp.sparky.synchronizer.entity.SelfLinkDescriptor;
65 import org.openecomp.sparky.synchronizer.enumeration.OperationState;
66 import org.openecomp.sparky.synchronizer.enumeration.SynchronizerState;
67 import org.openecomp.sparky.synchronizer.task.PerformActiveInventoryRetrieval;
68 import org.openecomp.sparky.synchronizer.task.PerformElasticSearchPut;
69 import org.openecomp.sparky.synchronizer.task.PerformElasticSearchRetrieval;
70 import org.openecomp.sparky.synchronizer.task.PerformElasticSearchUpdate;
71 import org.openecomp.sparky.util.NodeUtils;
74 import org.openecomp.cl.mdc.MdcContext;
75 import com.fasterxml.jackson.core.JsonProcessingException;
76 import com.fasterxml.jackson.databind.JsonNode;
77 import com.fasterxml.jackson.databind.ObjectReader;
78 import com.fasterxml.jackson.databind.node.ArrayNode;
81 * The Class AutosuggestionSynchronizer.
83 public class AggregationSynchronizer extends AbstractEntitySynchronizer
84 implements IndexSynchronizer {
87 * The Class RetryAggregationEntitySyncContainer.
89 private class RetryAggregationEntitySyncContainer {
90 NetworkTransaction txn;
94 * Instantiates a new retry aggregation entity sync container.
99 public RetryAggregationEntitySyncContainer(NetworkTransaction txn, AggregationEntity ae) {
104 public NetworkTransaction getNetworkTransaction() {
108 public AggregationEntity getAggregationEntity() {
113 private static final Logger LOG =
114 LoggerFactory.getInstance().getLogger(AggregationSynchronizer.class);
115 private static final String INSERTION_DATE_TIME_FORMAT = "yyyyMMdd'T'HHmmssZ";
117 private boolean allWorkEnumerated;
118 private Deque<SelfLinkDescriptor> selflinks;
119 private Deque<RetryAggregationEntitySyncContainer> retryQueue;
120 private Map<String, Integer> retryLimitTracker;
121 protected ExecutorService esPutExecutor;
122 private ConcurrentHashMap<String, AtomicInteger> entityCounters;
123 private boolean syncInProgress;
124 private Map<String, String> contextMap;
125 private String entityType;
128 * Instantiates a new entity aggregation synchronizer.
130 * @param indexName the index name
131 * @throws Exception the exception
133 public AggregationSynchronizer(String entityType, String indexName) throws Exception {
134 super(LOG, "AGGES-" + indexName.toUpperCase(), 2, 5, 5, indexName); // multiple Autosuggestion
135 // Entity Synchronizer will
136 // run for different indices
138 this.entityType = entityType;
139 this.allWorkEnumerated = false;
140 this.entityCounters = new ConcurrentHashMap<String, AtomicInteger>();
141 this.synchronizerName = "Entity Aggregation Synchronizer";
142 this.enabledStatFlags = EnumSet.of(StatFlag.AAI_REST_STATS, StatFlag.ES_REST_STATS);
143 this.syncInProgress = false;
144 this.allWorkEnumerated = false;
145 this.selflinks = new ConcurrentLinkedDeque<SelfLinkDescriptor>();
146 this.retryQueue = new ConcurrentLinkedDeque<RetryAggregationEntitySyncContainer>();
147 this.retryLimitTracker = new ConcurrentHashMap<String, Integer>();
149 this.esPutExecutor = NodeUtils.createNamedExecutor("AGGES-ES-PUT", 1, LOG);
150 Map<String, OxmEntityDescriptor> descriptor = new HashMap<String, OxmEntityDescriptor>();
151 descriptor.put(entityType, oxmModelLoader.getEntityDescriptors().get(entityType));
152 this.aaiEntityStats.initializeCountersFromOxmEntityDescriptors(
154 this.esEntityStats.initializeCountersFromOxmEntityDescriptors(
156 this.contextMap = MDC.getCopyOfContextMap();
160 * Collect all the work.
162 * @return the operation state
164 private OperationState collectAllTheWork() {
165 final Map<String, String> contextMap = MDC.getCopyOfContextMap();
166 final String entity = this.getEntityType();
169 aaiWorkOnHand.set(1);
171 supplyAsync(new Supplier<Void>() {
175 MDC.setContextMap(contextMap);
176 OperationResult typeLinksResult = null;
178 typeLinksResult = aaiDataProvider.getSelfLinksByEntityType(entity);
179 aaiWorkOnHand.decrementAndGet();
180 processEntityTypeSelfLinks(typeLinksResult);
181 } catch (Exception exc) {
182 // TODO -> LOG, what should be logged here?
188 }, aaiExecutor).whenComplete((result, error) -> {
191 LOG.error(AaiUiMsgs.ERROR_GENERIC,
192 "An error occurred getting data from AAI. Error = " + error.getMessage());
196 while (aaiWorkOnHand.get() != 0) {
198 if (LOG.isDebugEnabled()) {
199 LOG.debug(AaiUiMsgs.WAIT_FOR_ALL_SELFLINKS_TO_BE_COLLECTED);
205 aaiWorkOnHand.set(selflinks.size());
206 allWorkEnumerated = true;
209 while (!isSyncDone()) {
215 * Make sure we don't hang on to retries that failed which could cause issues during future
218 retryLimitTracker.clear();
220 } catch (Exception exc) {
221 // TODO -> LOG, waht should be logged here?
224 return OperationState.OK;
229 * Perform retry sync.
231 private void performRetrySync() {
232 while (retryQueue.peek() != null) {
234 RetryAggregationEntitySyncContainer rsc = retryQueue.poll();
237 AggregationEntity ae = rsc.getAggregationEntity();
238 NetworkTransaction txn = rsc.getNetworkTransaction();
243 * In this retry flow the se object has already derived its fields
245 link = getElasticFullUrl("/" + ae.getId(), getIndexName());
246 } catch (Exception exc) {
247 LOG.error(AaiUiMsgs.ES_FAILED_TO_CONSTRUCT_URI, exc.getLocalizedMessage());
251 NetworkTransaction retryTransaction = new NetworkTransaction();
252 retryTransaction.setLink(link);
253 retryTransaction.setEntityType(txn.getEntityType());
254 retryTransaction.setDescriptor(txn.getDescriptor());
255 retryTransaction.setOperationType(HttpMethod.GET);
258 * IMPORTANT - DO NOT incrementAndGet the esWorkOnHand as this is a retry flow! We already
259 * called incrementAndGet when queuing the failed PUT!
262 supplyAsync(new PerformElasticSearchRetrieval(retryTransaction, esDataProvider),
263 esExecutor).whenComplete((result, error) -> {
265 esWorkOnHand.decrementAndGet();
268 LOG.error(AaiUiMsgs.ES_RETRIEVAL_FAILED_RESYNC, error.getLocalizedMessage());
270 updateElasticSearchCounters(result);
271 performDocumentUpsert(result, ae);
281 * Perform document upsert.
283 * @param esGetTxn the es get txn
286 protected void performDocumentUpsert(NetworkTransaction esGetTxn, AggregationEntity ae) {
290 * As part of the response processing we need to do the following:
291 * <li>1. Extract the version (if present), it will be the ETAG when we use the
292 * Search-Abstraction-Service
293 * <li>2. Spawn next task which is to do the PUT operation into elastic with or with the version
295 * <li>a) if version is null or RC=404, then standard put, no _update with version tag
296 * <li>b) if version != null, do PUT with _update?version= versionNumber in the URI to elastic
302 link = getElasticFullUrl("/" + ae.getId(), getIndexName());
303 } catch (Exception exc) {
304 LOG.error(AaiUiMsgs.ES_LINK_UPSERT, exc.getLocalizedMessage());
308 String versionNumber = null;
309 boolean wasEntryDiscovered = false;
310 if (esGetTxn.getOperationResult().getResultCode() == 404) {
311 LOG.info(AaiUiMsgs.ES_SIMPLE_PUT, ae.getEntityPrimaryKeyValue());
312 } else if (esGetTxn.getOperationResult().getResultCode() == 200) {
313 wasEntryDiscovered = true;
315 versionNumber = NodeUtils.extractFieldValueFromObject(
316 NodeUtils.convertJsonStrToJsonNode(esGetTxn.getOperationResult().getResult()),
318 } catch (IOException exc) {
320 "Error extracting version number from response, aborting aggregation entity sync of "
321 + ae.getEntityPrimaryKeyValue() + ". Error - " + exc.getLocalizedMessage();
322 LOG.error(AaiUiMsgs.ERROR_EXTRACTING_FROM_RESPONSE, message);
327 * Not being a 200 does not mean a failure. eg 201 is returned for created. TODO -> Should we
330 LOG.error(AaiUiMsgs.ES_OPERATION_RETURN_CODE,
331 String.valueOf(esGetTxn.getOperationResult().getResultCode()));
336 String jsonPayload = null;
337 if (wasEntryDiscovered) {
339 ArrayList<JsonNode> sourceObject = new ArrayList<JsonNode>();
340 NodeUtils.extractObjectsByKey(
341 NodeUtils.convertJsonStrToJsonNode(esGetTxn.getOperationResult().getResult()),
342 "_source", sourceObject);
344 if (!sourceObject.isEmpty()) {
345 String responseSource = NodeUtils.convertObjectToJson(sourceObject.get(0), false);
346 MergableEntity me = mapper.readValue(responseSource, MergableEntity.class);
347 ObjectReader updater = mapper.readerForUpdating(me);
348 MergableEntity merged = updater.readValue(ae.getIndexDocumentJson());
349 jsonPayload = mapper.writeValueAsString(merged);
351 } catch (IOException exc) {
353 "Error extracting source value from response, aborting aggregation entity sync of "
354 + ae.getEntityPrimaryKeyValue() + ". Error - " + exc.getLocalizedMessage();
355 LOG.error(AaiUiMsgs.ERROR_EXTRACTING_FROM_RESPONSE, message);
359 jsonPayload = ae.getIndexDocumentJson();
362 if (wasEntryDiscovered) {
363 if (versionNumber != null && jsonPayload != null) {
365 String requestPayload = esDataProvider.buildBulkImportOperationRequest(getIndexName(),
366 ElasticSearchConfig.getConfig().getType(), ae.getId(), versionNumber, jsonPayload);
368 NetworkTransaction transactionTracker = new NetworkTransaction();
369 transactionTracker.setEntityType(esGetTxn.getEntityType());
370 transactionTracker.setDescriptor(esGetTxn.getDescriptor());
371 transactionTracker.setOperationType(HttpMethod.PUT);
373 esWorkOnHand.incrementAndGet();
374 supplyAsync(new PerformElasticSearchUpdate(ElasticSearchConfig.getConfig().getBulkUrl(),
375 requestPayload, esDataProvider, transactionTracker), esPutExecutor)
376 .whenComplete((result, error) -> {
378 esWorkOnHand.decrementAndGet();
381 String message = "Aggregation entity sync UPDATE PUT error - "
382 + error.getLocalizedMessage();
383 LOG.error(AaiUiMsgs.ERROR_GENERIC, message);
385 updateElasticSearchCounters(result);
386 processStoreDocumentResult(result, esGetTxn, ae);
392 if (link != null && jsonPayload != null) {
394 NetworkTransaction updateElasticTxn = new NetworkTransaction();
395 updateElasticTxn.setLink(link);
396 updateElasticTxn.setEntityType(esGetTxn.getEntityType());
397 updateElasticTxn.setDescriptor(esGetTxn.getDescriptor());
398 updateElasticTxn.setOperationType(HttpMethod.PUT);
400 esWorkOnHand.incrementAndGet();
401 supplyAsync(new PerformElasticSearchPut(jsonPayload, updateElasticTxn, esDataProvider),
402 esPutExecutor).whenComplete((result, error) -> {
404 esWorkOnHand.decrementAndGet();
408 "Aggregation entity sync UPDATE PUT error - " + error.getLocalizedMessage();
409 LOG.error(AaiUiMsgs.ERROR_GENERIC, message);
411 updateElasticSearchCounters(result);
412 processStoreDocumentResult(result, esGetTxn, ae);
417 } catch (Exception exc) {
418 String message = "Exception caught during aggregation entity sync PUT operation. Message - "
419 + exc.getLocalizedMessage();
420 LOG.error(AaiUiMsgs.ERROR_GENERIC, message);
425 * Should allow retry.
428 * @return true, if successful
430 private boolean shouldAllowRetry(String id) {
431 boolean isRetryAllowed = true;
432 if (retryLimitTracker.get(id) != null) {
433 Integer currentCount = retryLimitTracker.get(id);
434 if (currentCount.intValue() >= RETRY_COUNT_PER_ENTITY_LIMIT.intValue()) {
435 isRetryAllowed = false;
436 String message = "Aggregation entity re-sync limit reached for " + id
437 + ", re-sync will no longer be attempted for this entity";
438 LOG.error(AaiUiMsgs.ERROR_GENERIC, message);
440 Integer newCount = new Integer(currentCount.intValue() + 1);
441 retryLimitTracker.put(id, newCount);
444 Integer firstRetryCount = new Integer(1);
445 retryLimitTracker.put(id, firstRetryCount);
448 return isRetryAllowed;
452 * Process store document result.
454 * @param esPutResult the es put result
455 * @param esGetResult the es get result
458 private void processStoreDocumentResult(NetworkTransaction esPutResult,
459 NetworkTransaction esGetResult, AggregationEntity ae) {
461 OperationResult or = esPutResult.getOperationResult();
463 if (!or.wasSuccessful()) {
464 if (or.getResultCode() == VERSION_CONFLICT_EXCEPTION_CODE) {
466 if (shouldAllowRetry(ae.getId())) {
467 esWorkOnHand.incrementAndGet();
469 RetryAggregationEntitySyncContainer rsc =
470 new RetryAggregationEntitySyncContainer(esGetResult, ae);
471 retryQueue.push(rsc);
473 String message = "Store document failed during aggregation entity synchronization"
474 + " due to version conflict. Entity will be re-synced.";
475 LOG.warn(AaiUiMsgs.ERROR_GENERIC, message);
479 "Store document failed during aggregation entity synchronization with result code "
480 + or.getResultCode() + " and result message " + or.getResult();
481 LOG.error(AaiUiMsgs.ERROR_GENERIC, message);
489 private void syncEntityTypes() {
491 while (selflinks.peek() != null) {
493 SelfLinkDescriptor linkDescriptor = selflinks.poll();
494 aaiWorkOnHand.decrementAndGet();
496 OxmEntityDescriptor descriptor = null;
498 if (linkDescriptor.getSelfLink() != null && linkDescriptor.getEntityType() != null) {
500 descriptor = oxmModelLoader.getEntityDescriptor(linkDescriptor.getEntityType());
502 if (descriptor == null) {
503 LOG.error(AaiUiMsgs.MISSING_ENTITY_DESCRIPTOR, linkDescriptor.getEntityType());
504 // go to next element in iterator
508 NetworkTransaction txn = new NetworkTransaction();
509 txn.setDescriptor(descriptor);
510 txn.setLink(linkDescriptor.getSelfLink());
511 txn.setOperationType(HttpMethod.GET);
512 txn.setEntityType(linkDescriptor.getEntityType());
514 aaiWorkOnHand.incrementAndGet();
516 supplyAsync(new PerformActiveInventoryRetrieval(txn, aaiDataProvider), aaiExecutor)
517 .whenComplete((result, error) -> {
519 aaiWorkOnHand.decrementAndGet();
522 LOG.error(AaiUiMsgs.AAI_RETRIEVAL_FAILED_GENERIC, error.getLocalizedMessage());
524 if (result == null) {
525 LOG.error(AaiUiMsgs.AAI_RETRIEVAL_FAILED_FOR_SELF_LINK,
526 linkDescriptor.getSelfLink());
528 updateActiveInventoryCounters(result);
529 fetchDocumentForUpsert(result);
540 * Fetch document for upsert.
544 private void fetchDocumentForUpsert(NetworkTransaction txn) {
546 if (!txn.getOperationResult().wasSuccessful()) {
547 String message = "Self link failure. Result - " + txn.getOperationResult().getResult();
548 LOG.error(AaiUiMsgs.ERROR_GENERIC, message);
553 final String jsonResult = txn.getOperationResult().getResult();
554 if (jsonResult != null && jsonResult.length() > 0) {
556 AggregationEntity ae = new AggregationEntity(oxmModelLoader);
557 ae.setLink( txn.getLink() );
558 populateAggregationEntityDocument(ae, jsonResult, txn.getDescriptor());
563 link = getElasticFullUrl("/" + ae.getId(), getIndexName());
564 } catch (Exception exc) {
565 LOG.error(AaiUiMsgs.ES_FAILED_TO_CONSTRUCT_QUERY, exc.getLocalizedMessage());
569 NetworkTransaction n2 = new NetworkTransaction();
571 n2.setEntityType(txn.getEntityType());
572 n2.setDescriptor(txn.getDescriptor());
573 n2.setOperationType(HttpMethod.GET);
575 esWorkOnHand.incrementAndGet();
577 supplyAsync(new PerformElasticSearchRetrieval(n2, esDataProvider), esExecutor)
578 .whenComplete((result, error) -> {
580 esWorkOnHand.decrementAndGet();
583 LOG.error(AaiUiMsgs.ES_RETRIEVAL_FAILED, error.getLocalizedMessage());
585 updateElasticSearchCounters(result);
586 performDocumentUpsert(result, ae);
592 } catch (JsonProcessingException exc) {
593 // TODO -> LOG, waht should be logged here?
594 } catch (IOException exc) {
595 // TODO -> LOG, waht should be logged here?
601 * Populate aggregation entity document.
604 * @param result the result
605 * @param resultDescriptor the result descriptor
606 * @throws JsonProcessingException the json processing exception
607 * @throws IOException Signals that an I/O exception has occurred.
609 protected void populateAggregationEntityDocument(AggregationEntity doc, String result,
610 OxmEntityDescriptor resultDescriptor) throws JsonProcessingException, IOException {
611 doc.setEntityType(resultDescriptor.getEntityName());
612 JsonNode entityNode = mapper.readTree(result);
613 Map<String, Object> map = mapper.convertValue(entityNode, Map.class);
614 doc.copyAttributeKeyValuePair(map);
618 * Process entity type self links.
620 * @param operationResult the operation result
622 private void processEntityTypeSelfLinks(OperationResult operationResult) {
624 JsonNode rootNode = null;
626 final String jsonResult = operationResult.getResult();
628 if (jsonResult != null && jsonResult.length() > 0 && operationResult.wasSuccessful()) {
631 rootNode = mapper.readTree(jsonResult);
632 } catch (IOException exc) {
634 "Could not deserialize JSON (representing operation result) as node tree. " +
635 "Operation result = " + jsonResult + ". " + exc.getLocalizedMessage();
636 LOG.error(AaiUiMsgs.JSON_PROCESSING_ERROR, message);
639 JsonNode resultData = rootNode.get("result-data");
640 ArrayNode resultDataArrayNode = null;
642 if (resultData.isArray()) {
643 resultDataArrayNode = (ArrayNode) resultData;
645 Iterator<JsonNode> elementIterator = resultDataArrayNode.elements();
646 JsonNode element = null;
648 while (elementIterator.hasNext()) {
649 element = elementIterator.next();
651 final String resourceType = NodeUtils.getNodeFieldAsText(element, "resource-type");
652 final String resourceLink = NodeUtils.getNodeFieldAsText(element, "resource-link");
654 OxmEntityDescriptor descriptor = null;
656 if (resourceType != null && resourceLink != null) {
658 descriptor = oxmModelLoader.getEntityDescriptor(resourceType);
660 if (descriptor == null) {
661 LOG.error(AaiUiMsgs.MISSING_ENTITY_DESCRIPTOR, resourceType);
662 // go to next element in iterator
666 selflinks.add(new SelfLinkDescriptor(resourceLink, SynchronizerConfiguration.NODES_ONLY_MODIFIER, resourceType));
679 * @see org.openecomp.sparky.synchronizer.IndexSynchronizer#doSync()
682 public OperationState doSync() {
683 String txnID = NodeUtils.getRandomTxnId();
684 MdcContext.initialize(txnID, "AggregationSynchronizer", "", "Sync", "");
686 return collectAllTheWork();
690 public SynchronizerState getState() {
693 return SynchronizerState.PERFORMING_SYNCHRONIZATION;
696 return SynchronizerState.IDLE;
703 * @see org.openecomp.sparky.synchronizer.IndexSynchronizer#getStatReport(boolean)
706 public String getStatReport(boolean showFinalReport) {
707 return getStatReport(System.currentTimeMillis() - this.syncStartedTimeStampInMs,
711 public String getEntityType() {
715 public void setEntityType(String entityType) {
716 this.entityType = entityType;
722 * @see org.openecomp.sparky.synchronizer.IndexSynchronizer#shutdown()
725 public void shutdown() {
726 this.shutdownExecutors();
730 protected boolean isSyncDone() {
732 int totalWorkOnHand = aaiWorkOnHand.get() + esWorkOnHand.get();
734 if (LOG.isDebugEnabled()) {
735 LOG.debug(AaiUiMsgs.DEBUG_GENERIC, indexName + ", isSyncDone(), totalWorkOnHand = "
736 + totalWorkOnHand + " all work enumerated = " + allWorkEnumerated);
739 if (totalWorkOnHand > 0 || !allWorkEnumerated) {
743 this.syncInProgress = false;
751 * @see org.openecomp.sparky.synchronizer.AbstractEntitySynchronizer#clearCache()
754 public void clearCache() {
756 if (syncInProgress) {
757 LOG.debug(AaiUiMsgs.DEBUG_GENERIC,
758 "Autosuggestion Entity Summarizer in progress, request to clear cache ignored");
763 this.resetCounters();
764 if (entityCounters != null) {
765 entityCounters.clear();
768 allWorkEnumerated = false;