2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.synchronizer;
25 import static java.util.concurrent.CompletableFuture.supplyAsync;
28 import java.util.concurrent.ExecutorService;
30 import org.onap.aai.sparky.dal.NetworkTransaction;
31 import org.onap.aai.sparky.dal.rest.HttpMethod;
32 import org.onap.aai.sparky.dal.rest.OperationResult;
33 import org.onap.aai.sparky.logging.AaiUiMsgs;
34 import org.onap.aai.sparky.synchronizer.entity.AggregationSuggestionEntity;
35 import org.onap.aai.sparky.synchronizer.enumeration.OperationState;
36 import org.onap.aai.sparky.synchronizer.enumeration.SynchronizerState;
37 import org.onap.aai.sparky.synchronizer.task.PerformElasticSearchPut;
38 import org.onap.aai.sparky.util.NodeUtils;
39 import org.onap.aai.cl.api.Logger;
40 import org.onap.aai.cl.eelf.LoggerFactory;
41 import org.onap.aai.cl.mdc.MdcContext;
44 public class AggregationSuggestionSynchronizer extends AbstractEntitySynchronizer
45 implements IndexSynchronizer {
47 private static final Logger LOG =
48 LoggerFactory.getInstance().getLogger(AggregationSuggestionSynchronizer.class);
50 private boolean isSyncInProgress;
51 private boolean shouldPerformRetry;
52 private Map<String, String> contextMap;
53 protected ExecutorService esPutExecutor;
55 public AggregationSuggestionSynchronizer(String indexName) throws Exception {
56 super(LOG, "ASS-" + indexName.toUpperCase(), 2, 5, 5, indexName);
58 this.isSyncInProgress = false;
59 this.shouldPerformRetry = false;
60 this.synchronizerName = "Aggregation Suggestion Synchronizer";
61 this.contextMap = MDC.getCopyOfContextMap();
62 this.esPutExecutor = NodeUtils.createNamedExecutor("ASS-ES-PUT", 2, LOG);
66 protected boolean isSyncDone() {
67 int totalWorkOnHand = esWorkOnHand.get();
69 if (LOG.isDebugEnabled()) {
70 LOG.debug(AaiUiMsgs.DEBUG_GENERIC,
71 indexName + ", isSyncDone(), totalWorkOnHand = " + totalWorkOnHand);
74 if (totalWorkOnHand > 0 || !isSyncInProgress) {
82 public OperationState doSync() {
83 isSyncInProgress = true;
84 this.syncDurationInMs = -1;
85 syncStartedTimeStampInMs = System.currentTimeMillis();
88 while (!isSyncDone()) {
90 if (shouldPerformRetry) {
94 } catch (Exception exc) {
95 // We don't care about this exception
99 return OperationState.OK;
102 private void syncEntity() {
103 String txnId = NodeUtils.getRandomTxnId();
104 MdcContext.initialize(txnId, "AggregationSuggestionSynchronizer", "", "Sync", "");
106 AggregationSuggestionEntity syncEntity = new AggregationSuggestionEntity();
107 syncEntity.deriveFields();
111 link = getElasticFullUrl("/" + syncEntity.getId(), getIndexName());
112 } catch (Exception exc) {
113 LOG.error(AaiUiMsgs.ES_LINK_UPSERT, exc.getLocalizedMessage());
117 String jsonPayload = null;
118 jsonPayload = syncEntity.getIndexDocumentJson();
119 if (link != null && jsonPayload != null) {
121 NetworkTransaction elasticPutTxn = new NetworkTransaction();
122 elasticPutTxn.setLink(link);
123 elasticPutTxn.setOperationType(HttpMethod.PUT);
125 esWorkOnHand.incrementAndGet();
126 final Map<String, String> contextMap = MDC.getCopyOfContextMap();
128 new PerformElasticSearchPut(jsonPayload, elasticPutTxn, esDataProvider, contextMap),
129 esPutExecutor).whenComplete((result, error) -> {
131 esWorkOnHand.decrementAndGet();
134 String message = "Aggregation suggestion entity sync UPDATE PUT error - "
135 + error.getLocalizedMessage();
136 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
138 updateElasticSearchCounters(result);
139 wasEsOperationSuccessful(result);
143 } catch (Exception exc) {
145 "Exception caught during aggregation suggestion entity sync PUT operation. Message - "
146 + exc.getLocalizedMessage();
147 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
151 private void wasEsOperationSuccessful(NetworkTransaction result) {
152 if (result != null) {
153 OperationResult opResult = result.getOperationResult();
155 if (!opResult.wasSuccessful()) {
156 shouldPerformRetry = true;
158 isSyncInProgress = false;
159 shouldPerformRetry = false;
165 public SynchronizerState getState() {
167 return SynchronizerState.PERFORMING_SYNCHRONIZATION;
170 return SynchronizerState.IDLE;
174 public String getStatReport(boolean shouldDisplayFinalReport) {
175 syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs;
176 return getStatReport(syncDurationInMs, shouldDisplayFinalReport);
180 public void shutdown() {
181 this.shutdownExecutors();