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.openecomp.sparky.synchronizer;
25 import static java.util.concurrent.CompletableFuture.supplyAsync;
28 import java.util.concurrent.ExecutorService;
30 import org.openecomp.cl.api.Logger;
31 import org.openecomp.cl.eelf.LoggerFactory;
32 import org.openecomp.cl.mdc.MdcContext;
33 import org.openecomp.sparky.dal.NetworkTransaction;
34 import org.openecomp.sparky.dal.rest.HttpMethod;
35 import org.openecomp.sparky.dal.rest.OperationResult;
36 import org.openecomp.sparky.logging.AaiUiMsgs;
37 import org.openecomp.sparky.synchronizer.entity.AggregationSuggestionEntity;
38 import org.openecomp.sparky.synchronizer.enumeration.OperationState;
39 import org.openecomp.sparky.synchronizer.enumeration.SynchronizerState;
40 import org.openecomp.sparky.synchronizer.task.PerformElasticSearchPut;
41 import org.openecomp.sparky.util.NodeUtils;
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();
127 supplyAsync(new PerformElasticSearchPut(jsonPayload, elasticPutTxn,
128 esDataProvider, contextMap), esPutExecutor).whenComplete((result, error) -> {
130 esWorkOnHand.decrementAndGet();
133 String message = "Aggregation suggestion entity sync UPDATE PUT error - "
134 + error.getLocalizedMessage();
135 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
137 updateElasticSearchCounters(result);
138 wasEsOperationSuccessful(result);
142 } catch (Exception exc) {
144 "Exception caught during aggregation suggestion entity sync PUT operation. Message - "
145 + exc.getLocalizedMessage();
146 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
150 private void wasEsOperationSuccessful(NetworkTransaction result) {
151 if (result != null) {
152 OperationResult opResult = result.getOperationResult();
154 if (!opResult.wasSuccessful()) {
155 shouldPerformRetry = true;
157 isSyncInProgress = false;
158 shouldPerformRetry = false;
164 public SynchronizerState getState() {
166 return SynchronizerState.PERFORMING_SYNCHRONIZATION;
169 return SynchronizerState.IDLE;
173 public String getStatReport(boolean shouldDisplayFinalReport) {
174 syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs;
175 return getStatReport(syncDurationInMs, shouldDisplayFinalReport);
179 public void shutdown() {
180 this.shutdownExecutors();