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.topology.sync;
23 import static java.util.concurrent.CompletableFuture.supplyAsync;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Deque;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.concurrent.ConcurrentLinkedDeque;
33 import java.util.function.Supplier;
35 import org.onap.aai.cl.api.Logger;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.cl.mdc.MdcContext;
38 import org.onap.aai.restclient.client.OperationResult;
39 import org.onap.aai.sparky.config.oxm.GeoEntityLookup;
40 import org.onap.aai.sparky.config.oxm.GeoOxmEntityDescriptor;
41 import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor;
42 import org.onap.aai.sparky.config.oxm.OxmEntityLookup;
43 import org.onap.aai.sparky.dal.NetworkTransaction;
44 import org.onap.aai.sparky.dal.rest.HttpMethod;
45 import org.onap.aai.sparky.inventory.entity.GeoIndexDocument;
46 import org.onap.aai.sparky.logging.AaiUiMsgs;
47 import org.onap.aai.sparky.sync.AbstractEntitySynchronizer;
48 import org.onap.aai.sparky.sync.IndexSynchronizer;
49 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
50 import org.onap.aai.sparky.sync.config.NetworkStatisticsConfig;
51 import org.onap.aai.sparky.sync.entity.SelfLinkDescriptor;
52 import org.onap.aai.sparky.sync.enumeration.OperationState;
53 import org.onap.aai.sparky.sync.enumeration.SynchronizerState;
54 import org.onap.aai.sparky.sync.task.PerformActiveInventoryRetrieval;
55 import org.onap.aai.sparky.sync.task.StoreDocumentTask;
56 import org.onap.aai.sparky.util.NodeUtils;
59 import com.fasterxml.jackson.core.JsonProcessingException;
60 import com.fasterxml.jackson.databind.JsonNode;
61 import com.fasterxml.jackson.databind.node.ArrayNode;
65 * The Class GeoSynchronizer.
67 public class GeoSynchronizer extends AbstractEntitySynchronizer implements IndexSynchronizer {
69 private static final Logger LOG = LoggerFactory.getInstance().getLogger(GeoSynchronizer.class);
71 private boolean allWorkEnumerated;
72 private Deque<SelfLinkDescriptor> selflinks;
73 private GeoEntityLookup geoEntityLookup;
74 private OxmEntityLookup oxmEntityLookup;
76 private Map<String, GeoOxmEntityDescriptor> geoDescriptorMap = null;
79 * Instantiates a new geo synchronizer.
81 * @param indexName the index name
82 * @throws Exception the exception
84 public GeoSynchronizer(ElasticSearchSchemaConfig schemaConfig, int internalSyncWorkers,
85 int aaiWorkers, int esWorkers, NetworkStatisticsConfig aaiStatConfig,
86 NetworkStatisticsConfig esStatConfig, GeoEntityLookup geoEntityLookup,
87 OxmEntityLookup oxmEntityLookup) throws Exception {
89 super(LOG, "GEO", internalSyncWorkers, aaiWorkers, esWorkers, schemaConfig.getIndexName(),aaiStatConfig, esStatConfig);
90 this.geoEntityLookup = geoEntityLookup;
91 this.oxmEntityLookup = oxmEntityLookup;
92 this.allWorkEnumerated = false;
93 this.selflinks = new ConcurrentLinkedDeque<SelfLinkDescriptor>();
94 this.synchronizerName = "Geo Synchronizer";
95 this.geoDescriptorMap = geoEntityLookup.getGeoEntityDescriptors();
96 this.aaiEntityStats.intializeEntityCounters(geoDescriptorMap.keySet());
97 this.esEntityStats.intializeEntityCounters(geoDescriptorMap.keySet());
98 this.syncDurationInMs = -1;
103 * @see org.openecomp.sparky.synchronizer.IndexSynchronizer#doSync()
106 public OperationState doSync() {
107 this.syncDurationInMs = -1;
109 setShouldSkipSync(false);
110 allWorkEnumerated = false;
111 syncStartedTimeStampInMs = System.currentTimeMillis();
112 String txnID = NodeUtils.getRandomTxnId();
113 MdcContext.initialize(txnID, "GeoSynchronizer", "", "Sync", "");
116 return OperationState.OK;
121 * Collect all the work.
123 * @return the operation state
125 public OperationState collectAllTheWork() {
126 final Map<String,String> contextMap = MDC.getCopyOfContextMap();
128 if (geoDescriptorMap.isEmpty()) {
129 setShouldSkipSync(true);
130 LOG.error(AaiUiMsgs.OXM_FAILED_RETRIEVAL, "geo entities");
131 return OperationState.ERROR;
134 Collection<String> syncTypes = geoDescriptorMap.keySet();
139 * launch a parallel async thread to process the documents for each entity-type (to max the of
140 * the configured executor anyway)
143 aaiWorkOnHand.set(syncTypes.size());
145 for (String key : syncTypes) {
147 supplyAsync(new Supplier<Void>() {
151 MDC.setContextMap(contextMap);
152 OperationResult typeLinksResult = null;
154 typeLinksResult = aaiAdapter.getSelfLinksByEntityType(key);
155 aaiWorkOnHand.decrementAndGet();
156 processEntityTypeSelfLinks(typeLinksResult);
157 } catch (Exception exc) {
158 LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, exc);
164 }, aaiExecutor).whenComplete((result, error) -> {
167 LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, error.getMessage());
173 while (aaiWorkOnHand.get() != 0) {
175 if (LOG.isDebugEnabled()) {
176 LOG.debug(AaiUiMsgs.WAIT_FOR_ALL_SELFLINKS_TO_BE_COLLECTED);
182 aaiWorkOnHand.set(selflinks.size());
183 allWorkEnumerated = true;
186 } catch (Exception exc) {
187 LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, exc);
189 return OperationState.OK;
195 private void syncEntityTypes() {
197 while (selflinks.peek() != null) {
199 SelfLinkDescriptor linkDescriptor = selflinks.poll();
200 aaiWorkOnHand.decrementAndGet();
202 OxmEntityDescriptor descriptor = null;
204 if (linkDescriptor.getSelfLink() != null && linkDescriptor.getEntityType() != null) {
206 descriptor = oxmEntityLookup.getEntityDescriptors().get(linkDescriptor.getEntityType());
208 if (descriptor == null) {
209 LOG.error(AaiUiMsgs.MISSING_ENTITY_DESCRIPTOR, linkDescriptor.getEntityType());
210 // go to next element in iterator
214 NetworkTransaction txn = new NetworkTransaction();
215 txn.setDescriptor(descriptor);
216 txn.setLink(linkDescriptor.getSelfLink());
217 txn.setOperationType(HttpMethod.GET);
218 txn.setEntityType(linkDescriptor.getEntityType());
220 aaiWorkOnHand.incrementAndGet();
222 supplyAsync(new PerformActiveInventoryRetrieval(txn, aaiAdapter), aaiExecutor)
223 .whenComplete((result, error) -> {
225 aaiWorkOnHand.decrementAndGet();
228 LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, error.getMessage());
230 if (result == null) {
231 LOG.error(AaiUiMsgs.SELF_LINK_GET_NO_RESPONSE, linkDescriptor.getSelfLink());
233 processEntityTypeSelfLinkResult(result);
242 * Process entity type self links.
244 * @param operationResult the operation result
246 private void processEntityTypeSelfLinks(OperationResult operationResult) {
248 JsonNode rootNode = null;
250 final String jsonResult = operationResult.getResult();
252 if (jsonResult != null && jsonResult.length() > 0 && operationResult.wasSuccessful()) {
255 rootNode = mapper.readTree(jsonResult);
256 } catch (IOException exc) {
257 LOG.error(AaiUiMsgs.ERROR_GENERIC, exc);
260 JsonNode resultData = rootNode.get("result-data");
261 ArrayNode resultDataArrayNode = null;
263 if (resultData.isArray()) {
264 resultDataArrayNode = (ArrayNode) resultData;
266 Iterator<JsonNode> elementIterator = resultDataArrayNode.elements();
267 JsonNode element = null;
269 while (elementIterator.hasNext()) {
270 element = elementIterator.next();
272 final String resourceType = NodeUtils.getNodeFieldAsText(element, "resource-type");
273 final String resourceLink = NodeUtils.getNodeFieldAsText(element, "resource-link");
275 if (resourceType != null && resourceLink != null) {
277 if (geoDescriptorMap.containsKey(resourceType)) {
278 selflinks.add(new SelfLinkDescriptor(resourceLink + "?nodes-only", resourceType));
280 LOG.error(AaiUiMsgs.MISSING_ENTITY_DESCRIPTOR, resourceType);
281 // go to next element in iterator
293 * Process entity type self link result.
297 private void processEntityTypeSelfLinkResult(NetworkTransaction txn) {
299 updateActiveInventoryCounters(txn);
301 if (!txn.getOperationResult().wasSuccessful()) {
305 GeoOxmEntityDescriptor descriptor = geoDescriptorMap.get(txn.getEntityType());
307 if ( descriptor == null ) {
312 if (descriptor.hasGeoEntity()) {
314 GeoIndexDocument geoDoc = new GeoIndexDocument();
316 final String jsonResult = txn.getOperationResult().getResult();
318 if (jsonResult != null && jsonResult.length() > 0) {
320 populateGeoDocument(geoDoc, jsonResult, txn.getDescriptor(), txn.getLink());
322 if (!geoDoc.isValidGeoDocument()) {
324 LOG.info(AaiUiMsgs.GEO_SYNC_IGNORING_ENTITY, geoDoc.getEntityType(), geoDoc.toString());
330 link = elasticSearchAdapter.buildElasticSearchGetDocUrl(getIndexName(), geoDoc.getId());
331 } catch (Exception exc) {
332 LOG.error(AaiUiMsgs.ES_FAILED_TO_CONSTRUCT_URI, exc);
337 NetworkTransaction n2 = new NetworkTransaction();
339 n2.setEntityType(txn.getEntityType());
340 n2.setDescriptor(txn.getDescriptor());
341 n2.setOperationType(HttpMethod.PUT);
343 esWorkOnHand.incrementAndGet();
345 supplyAsync(new StoreDocumentTask(geoDoc, n2, elasticSearchAdapter), esExecutor)
346 .whenComplete((result, error) -> {
348 esWorkOnHand.decrementAndGet();
351 LOG.error(AaiUiMsgs.ES_STORE_FAILURE, error.getMessage());
353 updateElasticSearchCounters(result);
354 processStoreDocumentResult(result);
361 } catch (JsonProcessingException exc) {
362 LOG.error(AaiUiMsgs.JSON_PROCESSING_ERROR, exc);
363 } catch (IOException exc) {
364 LOG.error(AaiUiMsgs.JSON_PROCESSING_ERROR, exc);
372 * Process store document result.
376 private void processStoreDocumentResult(NetworkTransaction txn) {
378 OperationResult or = txn.getOperationResult();
380 if (!or.wasSuccessful()) {
381 LOG.error(AaiUiMsgs.ES_STORE_FAILURE, or.toString());
383 * if(or.getResultCode() != 404 || (or.getResultCode() == 404 &&
384 * !synchronizerConfig.isResourceNotFoundErrorsSupressed())) { logger.error(
385 * "Skipping failed resource = " + "link" + " RC=[" + or.getResultCode() + "]. Message: " +
395 public SynchronizerState getState() {
398 return SynchronizerState.PERFORMING_SYNCHRONIZATION;
401 return SynchronizerState.IDLE;
406 * @see org.openecomp.sparky.synchronizer.IndexSynchronizer#getStatReport(boolean)
409 public String getStatReport(boolean showFinalReport) {
410 syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs;
411 return this.getStatReport(syncDurationInMs, showFinalReport);
415 * @see org.openecomp.sparky.synchronizer.IndexSynchronizer#shutdown()
418 public void shutdown() {
419 this.shutdownExecutors();
423 * Populate geo document.
426 * @param result the result
427 * @param resultDescriptor the result descriptor
428 * @param entityLink the entity link
429 * @throws JsonProcessingException the json processing exception
430 * @throws IOException Signals that an I/O exception has occurred.
432 protected void populateGeoDocument(GeoIndexDocument doc, String result,
433 OxmEntityDescriptor resultDescriptor, String entityLink)
434 throws JsonProcessingException, IOException {
436 doc.setSelfLink(entityLink);
437 doc.setEntityType(resultDescriptor.getEntityName());
439 JsonNode entityNode = mapper.readTree(result);
441 List<String> primaryKeyValues = new ArrayList<String>();
442 String pkeyValue = null;
444 for (String keyName : resultDescriptor.getPrimaryKeyAttributeNames()) {
445 pkeyValue = NodeUtils.getNodeFieldAsText(entityNode, keyName);
446 if (pkeyValue != null) {
447 primaryKeyValues.add(pkeyValue);
449 LOG.warn(AaiUiMsgs.ES_PKEYVALUE_NULL, resultDescriptor.getEntityName());
453 final String primaryCompositeKeyValue = NodeUtils.concatArray(primaryKeyValues, "/");
454 doc.setEntityPrimaryKeyValue(primaryCompositeKeyValue);
456 GeoOxmEntityDescriptor descriptor = geoDescriptorMap.get(resultDescriptor.getEntityName());
458 String geoLatKey = descriptor.getGeoLatName();
459 String geoLongKey = descriptor.getGeoLongName();
461 doc.setLatitude(NodeUtils.getNodeFieldAsText(entityNode, geoLatKey));
462 doc.setLongitude(NodeUtils.getNodeFieldAsText(entityNode, geoLongKey));
468 protected boolean isSyncDone() {
469 if (shouldSkipSync()) {
470 syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs;
474 int totalWorkOnHand = aaiWorkOnHand.get() + esWorkOnHand.get();
476 if (totalWorkOnHand > 0 || !allWorkEnumerated) {