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;
31 import java.util.concurrent.ExecutorService;
33 import org.openecomp.cl.api.Logger;
34 import org.openecomp.cl.eelf.LoggerFactory;
35 import org.openecomp.cl.mdc.MdcContext;
36 import org.openecomp.sparky.dal.NetworkTransaction;
37 import org.openecomp.sparky.dal.rest.HttpMethod;
38 import org.openecomp.sparky.dal.rest.OperationResult;
39 import org.openecomp.sparky.logging.AaiUiMsgs;
40 import org.openecomp.sparky.synchronizer.entity.AggregationSuggestionEntity;
41 import org.openecomp.sparky.synchronizer.enumeration.OperationState;
42 import org.openecomp.sparky.synchronizer.enumeration.SynchronizerState;
43 import org.openecomp.sparky.synchronizer.task.PerformElasticSearchPut;
44 import org.openecomp.sparky.util.NodeUtils;
47 public class AggregationSuggestionSynchronizer extends AbstractEntitySynchronizer
48 implements IndexSynchronizer {
50 private static final Logger LOG =
51 LoggerFactory.getInstance().getLogger(AggregationSuggestionSynchronizer.class);
53 private boolean isSyncInProgress;
54 private boolean shouldPerformRetry;
55 private Map<String, String> contextMap;
56 protected ExecutorService esPutExecutor;
58 public AggregationSuggestionSynchronizer(String indexName) throws Exception {
59 super(LOG, "ASS-" + indexName.toUpperCase(), 2, 5, 5, indexName);
61 this.isSyncInProgress = false;
62 this.shouldPerformRetry = false;
63 this.synchronizerName = "Aggregation Suggestion Synchronizer";
64 this.contextMap = MDC.getCopyOfContextMap();
65 this.esPutExecutor = NodeUtils.createNamedExecutor("ASS-ES-PUT", 2, LOG);
69 protected boolean isSyncDone() {
70 int totalWorkOnHand = esWorkOnHand.get();
72 if (LOG.isDebugEnabled()) {
73 LOG.debug(AaiUiMsgs.DEBUG_GENERIC,
74 indexName + ", isSyncDone(), totalWorkOnHand = " + totalWorkOnHand);
77 if (totalWorkOnHand > 0 || !isSyncInProgress) {
85 public OperationState doSync() {
86 isSyncInProgress = true;
87 this.syncDurationInMs = -1;
88 syncStartedTimeStampInMs = System.currentTimeMillis();
91 while (!isSyncDone()) {
93 if (shouldPerformRetry) {
97 } catch (Exception exc) {
98 // We don't care about this exception
102 return OperationState.OK;
105 private void syncEntity() {
106 String txnId = NodeUtils.getRandomTxnId();
107 MdcContext.initialize(txnId, "AggregationSuggestionSynchronizer", "", "Sync", "");
109 AggregationSuggestionEntity syncEntity = new AggregationSuggestionEntity();
110 syncEntity.deriveFields();
114 link = getElasticFullUrl("/" + syncEntity.getId(), getIndexName());
115 } catch (Exception exc) {
116 LOG.error(AaiUiMsgs.ES_LINK_UPSERT, exc.getLocalizedMessage());
120 String jsonPayload = null;
121 jsonPayload = syncEntity.getIndexDocumentJson();
122 if (link != null && jsonPayload != null) {
124 NetworkTransaction elasticPutTxn = new NetworkTransaction();
125 elasticPutTxn.setLink(link);
126 elasticPutTxn.setOperationType(HttpMethod.PUT);
128 esWorkOnHand.incrementAndGet();
129 final Map<String, String> contextMap = MDC.getCopyOfContextMap();
130 supplyAsync(new PerformElasticSearchPut(jsonPayload, elasticPutTxn,
131 esDataProvider, contextMap), esPutExecutor).whenComplete((result, error) -> {
133 esWorkOnHand.decrementAndGet();
136 String message = "Aggregation suggestion entity sync UPDATE PUT error - "
137 + error.getLocalizedMessage();
138 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
140 updateElasticSearchCounters(result);
141 wasEsOperationSuccessful(result);
145 } catch (Exception exc) {
147 "Exception caught during aggregation suggestion entity sync PUT operation. Message - "
148 + exc.getLocalizedMessage();
149 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
153 private void wasEsOperationSuccessful(NetworkTransaction result) {
154 if (result != null) {
155 OperationResult opResult = result.getOperationResult();
157 if (!opResult.wasSuccessful()) {
158 shouldPerformRetry = true;
160 isSyncInProgress = false;
161 shouldPerformRetry = false;
167 public SynchronizerState getState() {
169 return SynchronizerState.PERFORMING_SYNCHRONIZATION;
172 return SynchronizerState.IDLE;
176 public String getStatReport(boolean shouldDisplayFinalReport) {
177 syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs;
178 return getStatReport(syncDurationInMs, shouldDisplayFinalReport);
182 public void shutdown() {
183 this.shutdownExecutors();