2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sparky.autosuggestion.sync;
23 import static java.util.concurrent.CompletableFuture.supplyAsync;
26 import java.util.concurrent.ExecutorService;
28 import org.onap.aai.cl.api.Logger;
29 import org.onap.aai.cl.eelf.LoggerFactory;
30 import org.onap.aai.cl.mdc.MdcContext;
31 import org.onap.aai.restclient.client.OperationResult;
32 import org.onap.aai.sparky.dal.NetworkTransaction;
33 import org.onap.aai.sparky.dal.rest.HttpMethod;
34 import org.onap.aai.sparky.logging.AaiUiMsgs;
35 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
36 import org.onap.aai.sparky.sync.AbstractEntitySynchronizer;
37 import org.onap.aai.sparky.sync.IndexSynchronizer;
38 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
39 import org.onap.aai.sparky.sync.config.NetworkStatisticsConfig;
40 import org.onap.aai.sparky.sync.entity.AggregationSuggestionEntity;
41 import org.onap.aai.sparky.sync.enumeration.OperationState;
42 import org.onap.aai.sparky.sync.enumeration.SynchronizerState;
43 import org.onap.aai.sparky.sync.task.PerformElasticSearchPut;
44 import org.onap.aai.sparky.util.NodeUtils;
48 public class VnfAliasSuggestionSynchronizer extends AbstractEntitySynchronizer
49 implements IndexSynchronizer {
51 private static final Logger LOG =
52 LoggerFactory.getInstance().getLogger(VnfAliasSuggestionSynchronizer.class);
54 private boolean isSyncInProgress;
55 private boolean shouldPerformRetry;
56 private Map<String, String> contextMap;
57 protected ExecutorService esPutExecutor;
58 private FiltersConfig filtersConfig;
60 public VnfAliasSuggestionSynchronizer(ElasticSearchSchemaConfig schemaConfig,
61 int internalSyncWorkers, int aaiWorkers, int esWorkers, NetworkStatisticsConfig aaiStatConfig,
62 NetworkStatisticsConfig esStatConfig, FiltersConfig filtersConfig) throws Exception {
63 super(LOG, "VASS-" + schemaConfig.getIndexName().toUpperCase(), internalSyncWorkers, aaiWorkers,
64 esWorkers, schemaConfig.getIndexName(), aaiStatConfig, esStatConfig);
66 this.isSyncInProgress = false;
67 this.shouldPerformRetry = false;
68 this.synchronizerName = "VNFs Alias Suggestion Synchronizer";
69 this.contextMap = MDC.getCopyOfContextMap();
70 this.esPutExecutor = NodeUtils.createNamedExecutor("ASS-ES-PUT", 2, LOG);
71 this.filtersConfig = filtersConfig;
75 protected boolean isSyncDone() {
76 int totalWorkOnHand = esWorkOnHand.get();
78 if (LOG.isDebugEnabled()) {
79 LOG.debug(AaiUiMsgs.DEBUG_GENERIC,
80 indexName + ", isSyncDone(), totalWorkOnHand = " + totalWorkOnHand);
83 if (totalWorkOnHand > 0 || !isSyncInProgress) {
91 public OperationState doSync() {
92 isSyncInProgress = true;
93 this.syncDurationInMs = -1;
94 syncStartedTimeStampInMs = System.currentTimeMillis();
98 while (!isSyncDone()) {
100 if (shouldPerformRetry) {
104 } catch (Exception exc) {
105 // We don't care about this exception
109 return OperationState.OK;
112 private void syncEntity() {
113 String txnId = NodeUtils.getRandomTxnId();
114 MdcContext.initialize(txnId, synchronizerName, "", "Sync", "");
116 AggregationSuggestionEntity syncEntity = new AggregationSuggestionEntity(filtersConfig);
117 syncEntity.deriveFields();
118 syncEntity.initializeFilters();
122 link = elasticSearchAdapter.buildElasticSearchGetDocUrl(getIndexName(), syncEntity.getId());
123 } catch (Exception exc) {
124 LOG.error(AaiUiMsgs.ES_LINK_UPSERT, exc.getLocalizedMessage());
128 String jsonPayload = null;
129 jsonPayload = syncEntity.getAsJson();
130 if (link != null && jsonPayload != null) {
132 NetworkTransaction elasticPutTxn = new NetworkTransaction();
133 elasticPutTxn.setLink(link);
134 elasticPutTxn.setOperationType(HttpMethod.PUT);
136 esWorkOnHand.incrementAndGet();
137 final Map<String, String> contextMap = MDC.getCopyOfContextMap();
138 supplyAsync(new PerformElasticSearchPut(jsonPayload, elasticPutTxn,
139 elasticSearchAdapter, contextMap), esPutExecutor).whenComplete((result, error) -> {
141 esWorkOnHand.decrementAndGet();
144 String message = "Aggregation suggestion entity sync UPDATE PUT error - "
145 + error.getLocalizedMessage();
146 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
148 updateElasticSearchCounters(result);
149 wasEsOperationSuccessful(result);
153 } catch (Exception exc) {
155 "Exception caught during aggregation suggestion entity sync PUT operation. Message - "
156 + exc.getLocalizedMessage();
157 LOG.error(AaiUiMsgs.ES_AGGREGATION_SUGGESTION_ENTITY_SYNC_ERROR, message);
161 private void wasEsOperationSuccessful(NetworkTransaction result) {
162 if (result != null) {
163 OperationResult opResult = result.getOperationResult();
165 if (!opResult.wasSuccessful()) {
166 shouldPerformRetry = true;
168 isSyncInProgress = false;
169 shouldPerformRetry = false;
175 public SynchronizerState getState() {
177 return SynchronizerState.PERFORMING_SYNCHRONIZATION;
180 return SynchronizerState.IDLE;
184 public String getStatReport(boolean shouldDisplayFinalReport) {
185 syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs;
186 return getStatReport(syncDurationInMs, shouldDisplayFinalReport);
190 public void shutdown() {
191 this.shutdownExecutors();