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 org.openecomp.cl.mdc.MdcContext;
32 import java.net.InetAddress;
33 import java.net.UnknownHostException;
35 import java.util.concurrent.ExecutorService;
37 import org.openecomp.cl.api.Logger;
38 import org.openecomp.cl.eelf.LoggerFactory;
39 import org.openecomp.sparky.dal.NetworkTransaction;
40 import org.openecomp.sparky.dal.rest.HttpMethod;
41 import org.openecomp.sparky.dal.rest.OperationResult;
42 import org.openecomp.sparky.logging.AaiUiMsgs;
43 import org.openecomp.sparky.synchronizer.entity.AggregationSuggestionEntity;
44 import org.openecomp.sparky.synchronizer.enumeration.OperationState;
45 import org.openecomp.sparky.synchronizer.enumeration.SynchronizerState;
46 import org.openecomp.sparky.synchronizer.task.PerformElasticSearchPut;
47 import org.openecomp.sparky.util.NodeUtils;
50 public class AggregationSuggestionSynchronizer extends AbstractEntitySynchronizer
51 implements IndexSynchronizer {
53 private static final Logger LOG =
54 LoggerFactory.getInstance().getLogger(AggregationSuggestionSynchronizer.class);
56 private boolean isSyncInProgress;
57 private boolean shouldPerformRetry;
58 private Map<String, String> contextMap;
59 protected ExecutorService esPutExecutor;
61 public AggregationSuggestionSynchronizer(String indexName) throws Exception {
62 super(LOG, "ASS-" + indexName.toUpperCase(), 2, 5, 5, indexName);
64 this.isSyncInProgress = false;
65 this.shouldPerformRetry = false;
66 this.synchronizerName = "Aggregation Suggestion Synchronizer";
67 this.contextMap = MDC.getCopyOfContextMap();
68 this.esPutExecutor = NodeUtils.createNamedExecutor("ASS-ES-PUT", 2, LOG);
72 protected boolean isSyncDone() {
73 int totalWorkOnHand = esWorkOnHand.get();
75 if (LOG.isDebugEnabled()) {
76 LOG.debug(AaiUiMsgs.DEBUG_GENERIC,
77 indexName + ", isSyncDone(), totalWorkOnHand = " + totalWorkOnHand);
80 if (totalWorkOnHand > 0 || !isSyncInProgress) {
88 public OperationState doSync() {
89 isSyncInProgress = true;
93 while (!isSyncDone()) {
95 if (shouldPerformRetry) {
99 } catch (Exception exc) {
100 // We don't care about this exception
104 return OperationState.OK;
107 private void syncEntity() {
108 String txnId = NodeUtils.getRandomTxnId();
109 MdcContext.initialize(txnId, "AggregationSuggestionSynchronizer", "", "Sync", "");
111 AggregationSuggestionEntity syncEntity = new AggregationSuggestionEntity();
112 syncEntity.deriveFields();
116 link = getElasticFullUrl("/" + syncEntity.getId(), getIndexName());
117 } catch (Exception exc) {
118 LOG.error(AaiUiMsgs.ES_LINK_UPSERT, exc.getLocalizedMessage());
122 String jsonPayload = null;
123 jsonPayload = syncEntity.getIndexDocumentJson();
124 if (link != null && jsonPayload != null) {
126 NetworkTransaction elasticPutTxn = new NetworkTransaction();
127 elasticPutTxn.setLink(link);
128 elasticPutTxn.setOperationType(HttpMethod.PUT);
130 esWorkOnHand.incrementAndGet();
131 final Map<String, String> contextMap = MDC.getCopyOfContextMap();
132 supplyAsync(new PerformElasticSearchPut(jsonPayload, elasticPutTxn,
133 esDataProvider, contextMap), esPutExecutor).whenComplete((result, error) -> {
135 esWorkOnHand.decrementAndGet();
138 String message = "Aggregation suggestion entity sync UPDATE PUT error - "
139 + error.getLocalizedMessage();
140 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
142 updateElasticSearchCounters(result);
143 wasEsOperationSuccessful(result);
147 } catch (Exception exc) {
149 "Exception caught during aggregation suggestion entity sync PUT operation. Message - "
150 + exc.getLocalizedMessage();
151 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
155 private void wasEsOperationSuccessful(NetworkTransaction result) {
156 if (result != null) {
157 OperationResult opResult = result.getOperationResult();
159 if (!opResult.wasSuccessful()) {
160 shouldPerformRetry = true;
162 isSyncInProgress = false;
163 shouldPerformRetry = false;
169 public SynchronizerState getState() {
171 return SynchronizerState.PERFORMING_SYNCHRONIZATION;
174 return SynchronizerState.IDLE;
178 public String getStatReport(boolean shouldDisplayFinalReport) {
179 return getStatReport(System.currentTimeMillis() - this.syncStartedTimeStampInMs,
180 shouldDisplayFinalReport);
184 public void shutdown() {
185 this.shutdownExecutors();