diff options
Diffstat (limited to 'src')
16 files changed, 615 insertions, 1274 deletions
diff --git a/src/main/java/org/onap/aai/sparky/dal/elasticsearch/config/ElasticSearchConfig.java b/src/main/java/org/onap/aai/sparky/dal/elasticsearch/config/ElasticSearchConfig.java index 863fa4b..2fe6bb6 100644 --- a/src/main/java/org/onap/aai/sparky/dal/elasticsearch/config/ElasticSearchConfig.java +++ b/src/main/java/org/onap/aai/sparky/dal/elasticsearch/config/ElasticSearchConfig.java @@ -68,8 +68,6 @@ public class ElasticSearchConfig { private String auditIndexName; - private String topographicalSearchIndex; - private String entityCountHistoryIndex; private String autosuggestIndexname; @@ -185,8 +183,6 @@ public class ElasticSearchConfig { mappingsFileName = props.getProperty("elasticsearch.mappingsFileName"); settingsFileName = props.getProperty("elasticsearch.settingsFileName"); auditIndexName = props.getProperty("elasticsearch.auditIndexName", AUDIT_INDEX_NAME_DEFAULT); - topographicalSearchIndex = - props.getProperty("elasticsearch.topographicalIndexName", TOPOGRAPHICAL_INDEX_NAME_DEFAULT); entityCountHistoryIndex = props.getProperty("elasticsearch.entityCountHistoryIndexName", ENTITY_COUNT_HISTORY_INDEX_NAME_DEFAULT); entityCountHistoryMappingsFileName = @@ -290,14 +286,6 @@ public class ElasticSearchConfig { this.auditIndexName = auditIndexName; } - public String getTopographicalSearchIndex() { - return topographicalSearchIndex; - } - - public void setTopographicalSearchIndex(String topographicalSearchIndex) { - this.topographicalSearchIndex = topographicalSearchIndex; - } - public String getEntityCountHistoryIndex() { return entityCountHistoryIndex; } @@ -529,7 +517,7 @@ public class ElasticSearchConfig { + ", clusterName=" + clusterName + ", mappingsFileName=" + mappingsFileName + ", settingsFileName=" + settingsFileName + ", syncAdapterMaxConcurrentWorkers=" + syncAdapterMaxConcurrentWorkers + ", auditIndexName=" + auditIndexName - + ", topographicalSearchIndex=" + topographicalSearchIndex + ", entityCountHistoryIndex=" + + ", entityCountHistoryIndex=" + entityCountHistoryIndex + ", autosuggestIndexname=" + autosuggestIndexname + ", entityCountHistoryMappingsFileName=" + entityCountHistoryMappingsFileName + ", autoSuggestSettingsFileName=" + autoSuggestSettingsFileName diff --git a/src/main/java/org/onap/aai/sparky/inventory/servlet/GeoVisualizationServlet.java b/src/main/java/org/onap/aai/sparky/inventory/servlet/GeoVisualizationServlet.java deleted file mode 100644 index b0a0670..0000000 --- a/src/main/java/org/onap/aai/sparky/inventory/servlet/GeoVisualizationServlet.java +++ /dev/null @@ -1,220 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017 Amdocs - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ -package org.onap.aai.sparky.inventory.servlet; - -import org.onap.aai.cl.mdc.MdcContext; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import java.io.IOException; -import java.io.PrintWriter; -import java.nio.ByteBuffer; -import java.security.SecureRandom; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.onap.aai.cl.api.Logger; -import org.onap.aai.cl.eelf.LoggerFactory; -import org.json.JSONArray; -import org.json.JSONObject; -import org.onap.aai.sparky.dal.elasticsearch.SearchAdapter; -import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig; -import org.onap.aai.sparky.dal.rest.OperationResult; -import org.onap.aai.sparky.dal.rest.RestClientBuilder; -import org.onap.aai.sparky.logging.AaiUiMsgs; -import org.onap.aai.sparky.util.NodeUtils; -import org.onap.aai.sparky.util.ServletUtils; - -/** - * The Class GeoVisualizationServlet. - */ -public class GeoVisualizationServlet extends HttpServlet { - - private static final Logger LOG = - LoggerFactory.getInstance().getLogger(GeoVisualizationServlet.class); - - private static final long serialVersionUID = 1L; - - private SearchAdapter search = null; - private ElasticSearchConfig elasticConfig = null; - private ObjectMapper mapper; - - private static final String SEARCH_STRING = "_search"; - - private static final String SEARCH_PARAMETER = - "?filter_path=hits.hits._source&_source=location&size=5000&q=entityType:"; - - /** - * Instantiates a new geo visualization servlet. - * - * @throws ServletException the servlet exception - */ - public GeoVisualizationServlet() throws ServletException { - init(); - } - - /* (non-Javadoc) - * @see javax.servlet.GenericServlet#init() - */ - @Override - public void init() throws ServletException { - super.init(); - try { - if (elasticConfig == null) { - elasticConfig = ElasticSearchConfig.getConfig(); - } - if (search == null) { - search = new SearchAdapter(); - } - this.mapper = new ObjectMapper(); - } catch (Exception exc) { - new ServletException( - "Caught an exception while getting an instance of servlet configuration.", exc); - } - } - - public void setSearch(SearchAdapter search) { - this.search = search; - } - - public void setElasticConfig(ElasticSearchConfig elasticConfig) { - this.elasticConfig = elasticConfig; - } - - /* (non-Javadoc) - * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - String txnID = request.getHeader("X-TransactionId"); - if (txnID == null){ - txnID = NodeUtils.getRandomTxnId(); - } - - String partnerName = request.getHeader("X-FromAppId"); - if ( partnerName == null) - partnerName = "Browser"; - - MdcContext.initialize(txnID, "AAI-UI", "", partnerName, - request.getRemoteAddr()); - - OperationResult operationResult = null; - try { - operationResult = getGeoVisualizationResults(response, request.getParameter("entity")); - } catch (Exception exc) { - LOG.error(AaiUiMsgs.ERROR_PROCESSING_REQUEST, exc); - } - } - - /* (non-Javadoc) - * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException { - - } - - /** - * Gets the geo visualization results. - * - * @param response the response - * @param entityType the entity type - * @return the geo visualization results - * @throws Exception the exception - */ - protected OperationResult getGeoVisualizationResults(HttpServletResponse response, - String entityType) throws Exception { - OperationResult operationResult = new OperationResult(); - - String parameters = SEARCH_PARAMETER + entityType; - String requestString = String.format("/%s/%s/%s", elasticConfig.getTopographicalSearchIndex(), - SEARCH_STRING, parameters); - - try { - final String fullUrlStr = ServletUtils.getFullUrl(elasticConfig, requestString); - OperationResult opResult = ServletUtils.executeGetQuery(LOG, search, response, fullUrlStr); - - JSONObject finalOutputJson = formatOutput(opResult.getResult()); - - if (finalOutputJson != null) { - response.setContentType("application/json"); - PrintWriter out = response.getWriter(); - out.println(finalOutputJson); - out.close(); - } - - } catch (JsonProcessingException exc) { - ServletUtils.handleSearchServletErrors(LOG, "Unable to map JSONpayload", exc, response); - } - - return operationResult; - } - - /** - * Format output. - * - * @param results the results - * @return the JSON object - */ - private JSONObject formatOutput(String results) { - JsonNode resultNode = null; - JSONObject finalResult = new JSONObject(); - JSONArray entitiesArr = new JSONArray(); - - try { - resultNode = mapper.readTree(results); - - final JsonNode hitsNode = resultNode.get("hits").get("hits"); - if (hitsNode.isArray()) { - - for (final JsonNode arrayNode : hitsNode) { - JsonNode sourceNode = arrayNode.get("_source"); - if (sourceNode.get("location") != null) { - JsonNode locationNode = sourceNode.get("location"); - if (NodeUtils.isNumeric(locationNode.get("lon").asText()) - && NodeUtils.isNumeric(locationNode.get("lat").asText())) { - JSONObject location = new JSONObject(); - location.put("longitude", locationNode.get("lon").asText()); - location.put("latitude", locationNode.get("lat").asText()); - - entitiesArr.put(location); - } - - } - } - } - finalResult.put("plotPoints", entitiesArr); - - } catch (IOException exc) { - LOG.warn(AaiUiMsgs.ERROR_BUILDING_SEARCH_RESPONSE, exc.getLocalizedMessage()); - } - - return finalResult; - } -} diff --git a/src/main/java/org/onap/aai/sparky/synchronizer/GeoSynchronizer.java b/src/main/java/org/onap/aai/sparky/synchronizer/GeoSynchronizer.java deleted file mode 100644 index 0f3926d..0000000 --- a/src/main/java/org/onap/aai/sparky/synchronizer/GeoSynchronizer.java +++ /dev/null @@ -1,466 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017 Amdocs - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ -package org.onap.aai.sparky.synchronizer; - -import static java.util.concurrent.CompletableFuture.supplyAsync; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Deque; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentLinkedDeque; -import java.util.function.Supplier; - -import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor; -import org.onap.aai.sparky.dal.NetworkTransaction; -import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig; -import org.onap.aai.sparky.dal.rest.HttpMethod; -import org.onap.aai.sparky.dal.rest.OperationResult; -import org.onap.aai.sparky.inventory.entity.GeoIndexDocument; -import org.onap.aai.sparky.logging.AaiUiMsgs; -import org.onap.aai.sparky.synchronizer.entity.SelfLinkDescriptor; -import org.onap.aai.sparky.synchronizer.enumeration.OperationState; -import org.onap.aai.sparky.synchronizer.enumeration.SynchronizerState; -import org.onap.aai.sparky.synchronizer.task.PerformActiveInventoryRetrieval; -import org.onap.aai.sparky.synchronizer.task.StoreDocumentTask; -import org.onap.aai.sparky.util.NodeUtils; -import org.onap.aai.cl.api.Logger; -import org.onap.aai.cl.eelf.LoggerFactory; -import org.onap.aai.cl.mdc.MdcContext; -import org.slf4j.MDC; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; - - -/** - * The Class GeoSynchronizer. - */ -public class GeoSynchronizer extends AbstractEntitySynchronizer implements IndexSynchronizer { - - private static final Logger LOG = LoggerFactory.getInstance().getLogger(GeoSynchronizer.class); - - private boolean allWorkEnumerated; - private Deque<SelfLinkDescriptor> selflinks; - - private ElasticSearchConfig elasticConfig = null; - private Map<String, OxmEntityDescriptor> geoDescriptorMap = null; - - /** - * Instantiates a new geo synchronizer. - * - * @param indexName the index name - * @throws Exception the exception - */ - public GeoSynchronizer(String indexName) throws Exception { - - super(LOG, "GEO", 2, 5, 5, indexName); - this.allWorkEnumerated = false; - this.selflinks = new ConcurrentLinkedDeque<SelfLinkDescriptor>(); - this.synchronizerName = "Geo Synchronizer"; - this.geoDescriptorMap = oxmModelLoader.getGeoEntityDescriptors(); - this.aaiEntityStats.initializeCountersFromOxmEntityDescriptors(geoDescriptorMap); - this.esEntityStats.initializeCountersFromOxmEntityDescriptors(geoDescriptorMap); - this.syncDurationInMs = -1; - } - - - /* (non-Javadoc) - * @see org.onap.aai.sparky.synchronizer.IndexSynchronizer#doSync() - */ - @Override - public OperationState doSync() { - resetCounters(); - allWorkEnumerated = false; - syncStartedTimeStampInMs = System.currentTimeMillis(); - String txnID = NodeUtils.getRandomTxnId(); - MdcContext.initialize(txnID, "GeoSynchronizer", "", "Sync", ""); - - collectAllTheWork(); - return OperationState.OK; - } - - - /** - * Collect all the work. - * - * @return the operation state - */ - public OperationState collectAllTheWork() { - final Map<String,String> contextMap = MDC.getCopyOfContextMap(); - if (elasticConfig == null) { - try { - elasticConfig = ElasticSearchConfig.getConfig(); - } catch (Exception exc) { - LOG.error(AaiUiMsgs.CONFIGURATION_ERROR, "Search"); - } - } - - if (geoDescriptorMap.isEmpty()) { - setShouldSkipSync(true); - LOG.error(AaiUiMsgs.OXM_FAILED_RETRIEVAL, "geo entities"); - return OperationState.ERROR; - } - - Collection<String> syncTypes = geoDescriptorMap.keySet(); - - try { - - /* - * launch a parallel async thread to process the documents for each entity-type (to max the of - * the configured executor anyway) - */ - - aaiWorkOnHand.set(syncTypes.size()); - - for (String key : syncTypes) { - - supplyAsync(new Supplier<Void>() { - - @Override - public Void get() { - MDC.setContextMap(contextMap); - OperationResult typeLinksResult = null; - try { - typeLinksResult = aaiDataProvider.getSelfLinksByEntityType(key); - aaiWorkOnHand.decrementAndGet(); - processEntityTypeSelfLinks(typeLinksResult); - } catch (Exception exc) { - LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, exc); - } - - return null; - } - - }, aaiExecutor).whenComplete((result, error) -> { - - if (error != null) { - LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, error.getMessage()); - } - }); - - } - - while (aaiWorkOnHand.get() != 0) { - - if (LOG.isDebugEnabled()) { - LOG.debug(AaiUiMsgs.WAIT_FOR_ALL_SELFLINKS_TO_BE_COLLECTED); - } - - Thread.sleep(1000); - } - - aaiWorkOnHand.set(selflinks.size()); - allWorkEnumerated = true; - syncEntityTypes(); - - } catch (Exception exc) { - LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, exc); - } - return OperationState.OK; - } - - /** - * Sync entity types. - */ - private void syncEntityTypes() { - - while (selflinks.peek() != null) { - - SelfLinkDescriptor linkDescriptor = selflinks.poll(); - aaiWorkOnHand.decrementAndGet(); - - OxmEntityDescriptor descriptor = null; - - if (linkDescriptor.getSelfLink() != null && linkDescriptor.getEntityType() != null) { - - descriptor = oxmModelLoader.getEntityDescriptor(linkDescriptor.getEntityType()); - - if (descriptor == null) { - LOG.error(AaiUiMsgs.MISSING_ENTITY_DESCRIPTOR, linkDescriptor.getEntityType()); - // go to next element in iterator - continue; - } - - NetworkTransaction txn = new NetworkTransaction(); - txn.setDescriptor(descriptor); - txn.setLink(linkDescriptor.getSelfLink()); - txn.setOperationType(HttpMethod.GET); - txn.setEntityType(linkDescriptor.getEntityType()); - - aaiWorkOnHand.incrementAndGet(); - - supplyAsync(new PerformActiveInventoryRetrieval(txn, aaiDataProvider), aaiExecutor) - .whenComplete((result, error) -> { - - aaiWorkOnHand.decrementAndGet(); - - if (error != null) { - LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, error.getMessage()); - } else { - if (result == null) { - LOG.error(AaiUiMsgs.SELF_LINK_GET_NO_RESPONSE, linkDescriptor.getSelfLink()); - } else { - processEntityTypeSelfLinkResult(result); - } - } - }); - } - } - } - - /** - * Process entity type self links. - * - * @param operationResult the operation result - */ - private void processEntityTypeSelfLinks(OperationResult operationResult) { - - JsonNode rootNode = null; - - final String jsonResult = operationResult.getResult(); - - if (jsonResult != null && jsonResult.length() > 0 && operationResult.wasSuccessful()) { - - try { - rootNode = mapper.readTree(jsonResult); - } catch (IOException exc) { - LOG.error(AaiUiMsgs.ERROR_GENERIC, exc); - return; - } - - JsonNode resultData = rootNode.get("result-data"); - ArrayNode resultDataArrayNode = null; - - if (resultData.isArray()) { - resultDataArrayNode = (ArrayNode) resultData; - - Iterator<JsonNode> elementIterator = resultDataArrayNode.elements(); - JsonNode element = null; - - while (elementIterator.hasNext()) { - element = elementIterator.next(); - - final String resourceType = NodeUtils.getNodeFieldAsText(element, "resource-type"); - final String resourceLink = NodeUtils.getNodeFieldAsText(element, "resource-link"); - - if (resourceType != null && resourceLink != null) { - - if (geoDescriptorMap.containsKey(resourceType)) { - selflinks.add(new SelfLinkDescriptor(resourceLink + "?nodes-only", resourceType)); - } else { - LOG.error(AaiUiMsgs.MISSING_ENTITY_DESCRIPTOR, resourceType); - // go to next element in iterator - continue; - } - - } - } - } - } - - } - - /** - * Process entity type self link result. - * - * @param txn the txn - */ - private void processEntityTypeSelfLinkResult(NetworkTransaction txn) { - - updateActiveInventoryCounters(txn); - - if (!txn.getOperationResult().wasSuccessful()) { - return; - } - - try { - if (!(txn.getDescriptor().getGeoLatName().isEmpty() - && txn.getDescriptor().getGeoLongName().isEmpty())) { - - GeoIndexDocument geoDoc = new GeoIndexDocument(oxmModelLoader); - - final String jsonResult = txn.getOperationResult().getResult(); - - if (jsonResult != null && jsonResult.length() > 0) { - - populateGeoDocument(geoDoc, jsonResult, txn.getDescriptor(), txn.getLink()); - - if (!geoDoc.isValidGeoDocument()) { - - LOG.info(AaiUiMsgs.GEO_SYNC_IGNORING_ENTITY, geoDoc.getEntityType(), geoDoc.toString()); - - } else { - - String link = null; - try { - link = getElasticFullUrl("/" + geoDoc.getId(), getIndexName(), "default"); - } catch (Exception exc) { - LOG.error(AaiUiMsgs.ES_FAILED_TO_CONSTRUCT_URI, exc); - } - - if (link != null) { - - NetworkTransaction n2 = new NetworkTransaction(); - n2.setLink(link); - n2.setEntityType(txn.getEntityType()); - n2.setDescriptor(txn.getDescriptor()); - n2.setOperationType(HttpMethod.PUT); - - esWorkOnHand.incrementAndGet(); - - supplyAsync(new StoreDocumentTask(geoDoc, n2, esDataProvider), esExecutor) - .whenComplete((result, error) -> { - - esWorkOnHand.decrementAndGet(); - - if (error != null) { - LOG.error(AaiUiMsgs.ES_STORE_FAILURE, error.getMessage()); - } else { - updateElasticSearchCounters(result); - processStoreDocumentResult(result); - } - }); - } - } - } - } - } catch (JsonProcessingException exc) { - LOG.error(AaiUiMsgs.JSON_PROCESSING_ERROR, exc); - } catch (IOException exc) { - LOG.error(AaiUiMsgs.JSON_PROCESSING_ERROR, exc); - } - - return; - } - - - /** - * Process store document result. - * - * @param txn the txn - */ - private void processStoreDocumentResult(NetworkTransaction txn) { - - OperationResult or = txn.getOperationResult(); - - if (!or.wasSuccessful()) { - LOG.error(AaiUiMsgs.ES_STORE_FAILURE, or.toString()); - /* - * if(or.getResultCode() != 404 || (or.getResultCode() == 404 && - * !synchronizerConfig.isResourceNotFoundErrorsSupressed())) { logger.error( - * "Skipping failed resource = " + "link" + " RC=[" + or.getResultCode() + "]. Message: " + - * or.getResult()); } - */ - - } - - } - - - @Override - public SynchronizerState getState() { - - if (!isSyncDone()) { - return SynchronizerState.PERFORMING_SYNCHRONIZATION; - } - - return SynchronizerState.IDLE; - - } - - /* (non-Javadoc) - * @see org.onap.aai.sparky.synchronizer.IndexSynchronizer#getStatReport(boolean) - */ - @Override - public String getStatReport(boolean showFinalReport) { - syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs; - return this.getStatReport(syncDurationInMs, showFinalReport); - } - - /* (non-Javadoc) - * @see org.onap.aai.sparky.synchronizer.IndexSynchronizer#shutdown() - */ - @Override - public void shutdown() { - this.shutdownExecutors(); - } - - /** - * Populate geo document. - * - * @param doc the doc - * @param result the result - * @param resultDescriptor the result descriptor - * @param entityLink the entity link - * @throws JsonProcessingException the json processing exception - * @throws IOException Signals that an I/O exception has occurred. - */ - protected void populateGeoDocument(GeoIndexDocument doc, String result, - OxmEntityDescriptor resultDescriptor, String entityLink) - throws JsonProcessingException, IOException { - - doc.setSelfLink(entityLink); - doc.setEntityType(resultDescriptor.getEntityName()); - - JsonNode entityNode = mapper.readTree(result); - - List<String> primaryKeyValues = new ArrayList<String>(); - String pkeyValue = null; - - for (String keyName : resultDescriptor.getPrimaryKeyAttributeName()) { - pkeyValue = NodeUtils.getNodeFieldAsText(entityNode, keyName); - if (pkeyValue != null) { - primaryKeyValues.add(pkeyValue); - } else { - LOG.warn(AaiUiMsgs.ES_PKEYVALUE_NULL, resultDescriptor.getEntityName()); - } - } - - final String primaryCompositeKeyValue = NodeUtils.concatArray(primaryKeyValues, "/"); - doc.setEntityPrimaryKeyValue(primaryCompositeKeyValue); - String geoLatKey = resultDescriptor.getGeoLatName(); - String geoLongKey = resultDescriptor.getGeoLongName(); - - doc.setLatitude(NodeUtils.getNodeFieldAsText(entityNode, geoLatKey)); - doc.setLongitude(NodeUtils.getNodeFieldAsText(entityNode, geoLongKey)); - doc.deriveFields(); - - } - - @Override - protected boolean isSyncDone() { - int totalWorkOnHand = aaiWorkOnHand.get() + esWorkOnHand.get(); - - if (totalWorkOnHand > 0 || !allWorkEnumerated) { - return false; - } - - return true; - } - -} diff --git a/src/main/java/org/onap/aai/sparky/synchronizer/HistoricalEntitySummarizer.java b/src/main/java/org/onap/aai/sparky/synchronizer/HistoricalEntitySummarizer.java deleted file mode 100644 index 714e016..0000000 --- a/src/main/java/org/onap/aai/sparky/synchronizer/HistoricalEntitySummarizer.java +++ /dev/null @@ -1,368 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017 Amdocs - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ -package org.onap.aai.sparky.synchronizer; - -import static java.util.concurrent.CompletableFuture.supplyAsync; - -import java.io.IOException; -import java.sql.Timestamp; -import java.text.SimpleDateFormat; -import java.util.Collection; -import java.util.EnumSet; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Supplier; - -import javax.json.Json; - -import org.onap.aai.sparky.config.oxm.OxmEntityDescriptor; -import org.onap.aai.sparky.dal.rest.HttpMethod; -import org.onap.aai.sparky.dal.rest.OperationResult; -import org.onap.aai.sparky.logging.AaiUiMsgs; -import org.onap.aai.sparky.synchronizer.enumeration.OperationState; -import org.onap.aai.sparky.synchronizer.enumeration.SynchronizerState; -import org.onap.aai.sparky.util.NodeUtils; -import org.onap.aai.cl.api.Logger; -import org.onap.aai.cl.eelf.LoggerFactory; -import org.onap.aai.cl.mdc.MdcContext; -import org.slf4j.MDC; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; - -/** - * The Class HistoricalEntitySummarizer. - */ -public class HistoricalEntitySummarizer extends AbstractEntitySynchronizer - implements IndexSynchronizer { - - private static final Logger LOG = LoggerFactory.getInstance().getLogger(HistoricalEntitySummarizer.class); - private static final String INSERTION_DATE_TIME_FORMAT = "yyyyMMdd'T'HHmmssZ"; - - private boolean allWorkEnumerated; - private ConcurrentHashMap<String, AtomicInteger> entityCounters; - private boolean syncInProgress; - private Map<String, String> contextMap; - - /** - * Instantiates a new historical entity summarizer. - * - * @param indexName the index name - * @throws Exception the exception - */ - public HistoricalEntitySummarizer(String indexName) throws Exception { - super(LOG, "HES", 2, 5, 5, indexName); - - this.allWorkEnumerated = false; - this.entityCounters = new ConcurrentHashMap<String, AtomicInteger>(); - this.synchronizerName = "Historical Entity Summarizer"; - this.enabledStatFlags = EnumSet.of(StatFlag.AAI_REST_STATS, StatFlag.ES_REST_STATS); - this.syncInProgress = false; - this.contextMap = MDC.getCopyOfContextMap(); - this.syncDurationInMs = -1; - } - - /** - * Collect all the work. - * - * @return the operation state - */ - private OperationState collectAllTheWork() { - - Map<String, OxmEntityDescriptor> descriptorMap = - oxmModelLoader.getSearchableEntityDescriptors(); - - if (descriptorMap.isEmpty()) { - LOG.error(AaiUiMsgs.OXM_FAILED_RETRIEVAL, "historical entities"); - - return OperationState.ERROR; - } - - Collection<String> entityTypes = descriptorMap.keySet(); - - AtomicInteger asyncWoH = new AtomicInteger(0); - - asyncWoH.set(entityTypes.size()); - - try { - for (String entityType : entityTypes) { - - supplyAsync(new Supplier<Void>() { - - @Override - public Void get() { - MDC.setContextMap(contextMap); - try { - OperationResult typeLinksResult = - aaiDataProvider.getSelfLinksByEntityType(entityType); - updateActiveInventoryCounters(HttpMethod.GET, entityType, typeLinksResult); - processEntityTypeSelfLinks(entityType, typeLinksResult); - } catch (Exception exc) { - LOG.error(AaiUiMsgs.ERROR_GETTING_DATA_FROM_AAI, exc.getMessage()); - - } - - return null; - } - - }, aaiExecutor).whenComplete((result, error) -> { - - asyncWoH.decrementAndGet(); - - if (error != null) { - LOG.error(AaiUiMsgs.HISTORICAL_COLLECT_ERROR, error.getMessage()); - } - - }); - - } - - - while (asyncWoH.get() > 0) { - - if (LOG.isDebugEnabled()) { - LOG.debug(AaiUiMsgs.DEBUG_GENERIC, indexName + " summarizer waiting for all the links to be processed."); - } - - Thread.sleep(250); - } - - esWorkOnHand.set(entityCounters.size()); - - // start doing the real work - allWorkEnumerated = true; - - insertEntityTypeCounters(); - - if (LOG.isDebugEnabled()) { - - StringBuilder sb = new StringBuilder(128); - - sb.append("\n\nHistorical Entity Counters:"); - - for (Entry<String, AtomicInteger> entry : entityCounters.entrySet()) { - sb.append("\n").append(entry.getKey()).append(" = ").append(entry.getValue().get()); - } - - LOG.debug(AaiUiMsgs.DEBUG_GENERIC, sb.toString()); - - } - - } catch (Exception exc) { - LOG.error(AaiUiMsgs.HISTORICAL_COLLECT_ERROR, exc.getMessage()); - - - esWorkOnHand.set(0); - allWorkEnumerated = true; - - return OperationState.ERROR; - } - - return OperationState.OK; - - } - - /* (non-Javadoc) - * @see org.onap.aai.sparky.synchronizer.IndexSynchronizer#doSync() - */ - @Override - public OperationState doSync() { - String txnID = NodeUtils.getRandomTxnId(); - MdcContext.initialize(txnID, "HistoricalEntitySynchronizer", "", "Sync", ""); - - if (syncInProgress) { - LOG.info(AaiUiMsgs.HISTORICAL_SYNC_PENDING); - return OperationState.PENDING; - } - - clearCache(); - - syncInProgress = true; - this.syncStartedTimeStampInMs = System.currentTimeMillis(); - allWorkEnumerated = false; - - return collectAllTheWork(); - } - - /** - * Process entity type self links. - * - * @param entityType the entity type - * @param operationResult the operation result - */ - private void processEntityTypeSelfLinks(String entityType, OperationResult operationResult) { - - JsonNode rootNode = null; - - final String jsonResult = operationResult.getResult(); - - if (jsonResult != null && jsonResult.length() > 0 && operationResult.wasSuccessful()) { - - try { - rootNode = mapper.readTree(jsonResult); - } catch (IOException exc) { - LOG.error(AaiUiMsgs.JSON_PROCESSING_ERROR, exc.getMessage()); - return; - } - - JsonNode resultData = rootNode.get("result-data"); - ArrayNode resultDataArrayNode = null; - - if (resultData != null && resultData.isArray()) { - resultDataArrayNode = (ArrayNode) resultData; - entityCounters.put(entityType, new AtomicInteger(resultDataArrayNode.size())); - } - } - - } - - /** - * Insert entity type counters. - */ - private void insertEntityTypeCounters() { - - if (esWorkOnHand.get() <= 0) { - return; - } - - SimpleDateFormat dateFormat = new SimpleDateFormat(INSERTION_DATE_TIME_FORMAT); - Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - String currentFormattedTimeStamp = dateFormat.format(timestamp); - - Set<Entry<String, AtomicInteger>> entityCounterEntries = entityCounters.entrySet(); - - for (Entry<String, AtomicInteger> entityCounterEntry : entityCounterEntries) { - - supplyAsync(new Supplier<Void>() { - - @Override - public Void get() { - MDC.setContextMap(contextMap); - String jsonString = Json.createObjectBuilder().add( - "count", entityCounterEntry.getValue().get()) - .add("entityType", entityCounterEntry.getKey()) - .add("timestamp", currentFormattedTimeStamp).build().toString(); - - String link = null; - try { - link = getElasticFullUrl("", indexName); - OperationResult or = esDataProvider.doPost(link, jsonString, "application/json"); - updateElasticSearchCounters(HttpMethod.POST, entityCounterEntry.getKey(), or); - } catch (Exception exc) { - LOG.error(AaiUiMsgs.ES_STORE_FAILURE, exc.getMessage() ); - } - - return null; - } - - }, esExecutor).whenComplete((result, error) -> { - - esWorkOnHand.decrementAndGet(); - - }); - - } - - while (esWorkOnHand.get() > 0) { - - try { - Thread.sleep(500); - } catch (InterruptedException exc) { - LOG.error(AaiUiMsgs.INTERRUPTED, "historical Entities", exc.getMessage()); - } - } - - } - - @Override - public SynchronizerState getState() { - - if (!isSyncDone()) { - return SynchronizerState.PERFORMING_SYNCHRONIZATION; - } - - return SynchronizerState.IDLE; - - } - - /* (non-Javadoc) - * @see org.onap.aai.sparky.synchronizer.IndexSynchronizer#getStatReport(boolean) - */ - @Override - public String getStatReport(boolean showFinalReport) { - syncDurationInMs = System.currentTimeMillis() - syncStartedTimeStampInMs; - return this.getStatReport(syncDurationInMs, showFinalReport); - } - - /* (non-Javadoc) - * @see org.onap.aai.sparky.synchronizer.IndexSynchronizer#shutdown() - */ - @Override - public void shutdown() { - this.shutdownExecutors(); - } - - @Override - protected boolean isSyncDone() { - - int totalWorkOnHand = aaiWorkOnHand.get() + esWorkOnHand.get(); - - if (LOG.isDebugEnabled()) { - LOG.debug(AaiUiMsgs.DEBUG_GENERIC,indexName + ", isSyncDone(), totalWorkOnHand = " + totalWorkOnHand - + " all work enumerated = " + allWorkEnumerated); - } - - if (totalWorkOnHand > 0 || !allWorkEnumerated) { - return false; - } - - this.syncInProgress = false; - - return true; - } - - /* (non-Javadoc) - * @see org.onap.aai.sparky.synchronizer.AbstractEntitySynchronizer#clearCache() - */ - @Override - public void clearCache() { - - if (syncInProgress) { - LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "Historical Entity Summarizer in progress, request to clear cache ignored"); - return; - } - - super.clearCache(); - this.resetCounters(); - if (entityCounters != null) { - entityCounters.clear(); - } - - allWorkEnumerated = false; - - } - -} diff --git a/src/main/java/org/onap/aai/sparky/synchronizer/SyncHelper.java b/src/main/java/org/onap/aai/sparky/synchronizer/SyncHelper.java index 48331d0..3738acf 100644 --- a/src/main/java/org/onap/aai/sparky/synchronizer/SyncHelper.java +++ b/src/main/java/org/onap/aai/sparky/synchronizer/SyncHelper.java @@ -206,72 +206,6 @@ public class SyncHelper { } - /** - * The Class HistoricalEntityCountSummaryTask. - */ - private class HistoricalEntityCountSummaryTask implements Runnable { - - /** - * Instantiates a new historical entity count summary task. - */ - public HistoricalEntityCountSummaryTask() {} - - /* - * (non-Javadoc) - * - * @see java.lang.Runnable#run() - */ - @Override - public void run() { - - long opStartTime = System.currentTimeMillis(); - MDC.setContextMap(contextMap); - LOG.info(AaiUiMsgs.HISTORICAL_ENTITY_COUNT_SUMMARIZER_STARTING, sdf.format(opStartTime) - .replaceAll(SynchronizerConstants.TIME_STD, SynchronizerConstants.TIME_CONFIG_STD)); - - try { - if (entityCounterHistorySummarizer == null) { - LOG.error(AaiUiMsgs.HISTORICAL_ENTITY_COUNT_SUMMARIZER_NOT_STARTED); - return; - } - - LOG.info(AaiUiMsgs.INFO_GENERIC, - "EntityCounterHistorySummarizer, starting syncrhonization"); - - entityCounterHistorySummarizer.performAction(SyncActions.SYNCHRONIZE); - - while (entityCounterHistorySummarizer - .getState() == SynchronizerState.PERFORMING_SYNCHRONIZATION) { - Thread.sleep(1000); - } - - long opEndTime = System.currentTimeMillis(); - - LOG.info(AaiUiMsgs.HISTORICAL_SYNC_DURATION, - entityCounterHistorySummarizer.getControllerName(), - String.valueOf(opEndTime - opStartTime)); - - long taskFrequencyInMs = - syncConfig.getHistoricalEntitySummarizedFrequencyInMinutes() * 60 * 1000; - - if (syncConfig.isHistoricalEntitySummarizerEnabled()) { - String time = sdf.format(System.currentTimeMillis() + taskFrequencyInMs) - .replaceAll(SynchronizerConstants.TIME_STD, SynchronizerConstants.TIME_CONFIG_STD); - - LOG.info(AaiUiMsgs.HISTORICAL_SYNC_TO_BEGIN, time); - } - - - } catch (Exception exc) { - String message = "Caught an exception while attempting to populate entity country " - + "history elasticsearch table with an error cause = " - + ErrorUtil.extractStackTraceElements(5, exc); - LOG.error(AaiUiMsgs.ERROR_GENERIC, message); - } - - } - - } /** * Gets the first sync time. @@ -368,11 +302,6 @@ public class SyncHelper { cers.setEsDataProvider(esAdapter); syncController.registerEntitySynchronizer(cers); - GeoSynchronizer geo = new GeoSynchronizer(esConfig.getTopographicalSearchIndex()); - geo.setAaiDataProvider(aaiAdapter); - geo.setEsDataProvider(esAdapter); - syncController.registerEntitySynchronizer(geo); - if (syncConfig.isAutosuggestSynchronizationEnabled()) { initAutoSuggestionSynchronizer(esConfig, aaiAdapter, esAdapter, nonCachingRestProvider); initAggregationSynchronizer(esConfig, aaiAdapter, esAdapter, nonCachingRestProvider); @@ -389,14 +318,6 @@ public class SyncHelper { syncController.registerIndexCleaner(searchableIndexCleaner); - IndexCleaner geoIndexCleaner = new ElasticSearchIndexCleaner(nonCachingRestProvider, - esConfig.getTopographicalSearchIndex(), esConfig.getType(), esConfig.getIpAddress(), - esConfig.getHttpPort(), syncConfig.getScrollContextTimeToLiveInMinutes(), - syncConfig.getNumScrollContextItemsToRetrievePerRequest()); - - syncController.registerIndexCleaner(geoIndexCleaner); - - } catch (Exception exc) { String message = "Error: failed to sync with message = " + exc.getMessage(); LOG.error(AaiUiMsgs.ERROR_GENERIC, message); @@ -404,45 +325,6 @@ public class SyncHelper { } - /** - * Inits the entity counter history summarizer. - */ - private void initEntityCounterHistorySummarizer() { - - LOG.info(AaiUiMsgs.INFO_GENERIC, "initEntityCounterHistorySummarizer"); - - try { - entityCounterHistorySummarizer = new SyncController("entityCounterHistorySummarizer"); - - ActiveInventoryAdapter aaiAdapter = new ActiveInventoryAdapter(new RestClientBuilder()); - aaiAdapter.setCacheEnabled(false); - - RestClientBuilder clientBuilder = new RestClientBuilder(); - clientBuilder.setUseHttps(false); - - RestfulDataAccessor nonCachingRestProvider = new RestfulDataAccessor(clientBuilder); - ElasticSearchConfig esConfig = ElasticSearchConfig.getConfig(); - ElasticSearchAdapter esAdapter = new ElasticSearchAdapter(nonCachingRestProvider, esConfig); - - IndexIntegrityValidator entityCounterHistoryValidator = - new IndexIntegrityValidator(nonCachingRestProvider, esConfig.getEntityCountHistoryIndex(), - esConfig.getType(), esConfig.getIpAddress(), esConfig.getHttpPort(), - esConfig.buildElasticSearchEntityCountHistoryTableConfig()); - - entityCounterHistorySummarizer.registerIndexValidator(entityCounterHistoryValidator); - - HistoricalEntitySummarizer historicalSummarizer = - new HistoricalEntitySummarizer(esConfig.getEntityCountHistoryIndex()); - historicalSummarizer.setAaiDataProvider(aaiAdapter); - historicalSummarizer.setEsDataProvider(esAdapter); - entityCounterHistorySummarizer.registerEntitySynchronizer(historicalSummarizer); - - } catch (Exception exc) { - String message = "Error: failed to sync with message = " + exc.getMessage(); - LOG.error(AaiUiMsgs.ERROR_GENERIC, message); - } - } - private List<String> getAutosuggestableEntitiesFromOXM() { Map<String, OxmEntityDescriptor> map = oxmModelLoader.getSuggestionSearchEntityDescriptors(); List<String> suggestableEntities = new ArrayList<String>(); @@ -583,13 +465,6 @@ public class SyncHelper { if (syncConfig.isConfigOkForStartupSync() || syncConfig.isConfigOkForPeriodicSync()) { initializeSyncController(); } - - if (syncConfig.isHistoricalEntitySummarizerEnabled()) { - initEntityCounterHistorySummarizer(); - } else { - LOG.info(AaiUiMsgs.INFO_GENERIC, "history summarizer disabled"); - } - // schedule startup synchronization if (syncConfig.isConfigOkForStartupSync()) { @@ -641,11 +516,6 @@ public class SyncHelper { } } - // schedule periodic synchronization - if (syncConfig.isHistoricalEntitySummarizerEnabled()) { - scheduleHistoricalCounterSyncTask(); - } - } catch (Exception exc) { String message = "Caught an exception while starting up the SyncHelper. Error cause = \n" + ErrorUtil.extractStackTraceElements(5, exc); @@ -653,17 +523,6 @@ public class SyncHelper { } } - /** - * Schedule historical counter sync task. - */ - private void scheduleHistoricalCounterSyncTask() { - long taskFrequencyInMs = - syncConfig.getHistoricalEntitySummarizedFrequencyInMinutes() * 60 * 1000; - historicalExecutor.scheduleWithFixedDelay(new HistoricalEntityCountSummaryTask(), 0, - taskFrequencyInMs, TimeUnit.MILLISECONDS); - LOG.info(AaiUiMsgs.INFO_GENERIC, - "Historical Entity Count Summarizer synchronization is enabled."); - } /** * Shutdown. diff --git a/src/main/java/org/onap/aai/sparky/synchronizer/config/SynchronizerConfiguration.java b/src/main/java/org/onap/aai/sparky/synchronizer/config/SynchronizerConfiguration.java index e6fd31d..2285950 100644 --- a/src/main/java/org/onap/aai/sparky/synchronizer/config/SynchronizerConfiguration.java +++ b/src/main/java/org/onap/aai/sparky/synchronizer/config/SynchronizerConfiguration.java @@ -29,6 +29,7 @@ import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Properties; +import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -60,7 +61,6 @@ public class SynchronizerConfiguration { if (instance == null) { instance = new SynchronizerConfiguration(); - instance.initialize(); } return instance; @@ -69,18 +69,20 @@ public class SynchronizerConfiguration { /** * Instantiates a new synchronizer configuration. */ - public SynchronizerConfiguration() { - // test method - } - + public SynchronizerConfiguration() throws Exception { + Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE); + initialize(props); + } + + public SynchronizerConfiguration(Properties props) throws Exception { + initialize(props); + } /** * Initialize. * * @throws Exception the exception */ - protected void initialize() throws Exception { - - Properties props = ConfigHelper.loadConfigFromExplicitPath(CONFIG_FILE); + protected void initialize(Properties props) throws Exception { // parse config for startup sync try { @@ -173,11 +175,6 @@ public class SynchronizerConfiguration { nodesOnlyModifierEnabled = Boolean.parseBoolean(props.getProperty("synchronizer.applyNodesOnlyModifier")); - historicalEntitySummarizerEnabled = Boolean - .parseBoolean(props.getProperty("synchronizer.historicalEntitySummarizerEnabled", "true")); - historicalEntitySummarizedFrequencyInMinutes = Long.parseLong( - props.getProperty("synchronizer.historicalEntitySummarizedFrequencyInMinutes", "60")); - autosuggestSynchronizationEnabled = Boolean .parseBoolean(props.getProperty("synchronizer.autosuggestSynchronizationEnabled", "true")); @@ -210,24 +207,6 @@ public class SynchronizerConfiguration { this.displayVerboseQueueManagerStats = displayVerboseQueueManagerStats; } - public boolean isHistoricalEntitySummarizerEnabled() { - return historicalEntitySummarizerEnabled; - } - - public void setHistoricalEntitySummarizerEnabled(boolean historicalEntitySummarizerEnabled) { - this.historicalEntitySummarizerEnabled = historicalEntitySummarizerEnabled; - } - - public long getHistoricalEntitySummarizedFrequencyInMinutes() { - return historicalEntitySummarizedFrequencyInMinutes; - } - - public void setHistoricalEntitySummarizedFrequencyInMinutes( - long historicalEntitySummarizedFrequencyInMinutes) { - this.historicalEntitySummarizedFrequencyInMinutes = - historicalEntitySummarizedFrequencyInMinutes; - } - private int syncTaskInitialDelayInMs; private int syncTaskFrequencyInMs; @@ -262,13 +241,8 @@ public class SynchronizerConfiguration { private boolean nodesOnlyModifierEnabled; - private boolean historicalEntitySummarizerEnabled; - private boolean autosuggestSynchronizationEnabled; - private long historicalEntitySummarizedFrequencyInMinutes; - - private boolean configOkForStartupSync = true; private boolean configOkForPeriodicSync = true; @@ -413,7 +387,72 @@ public class SynchronizerConfiguration { public void setAutosuggestSynchronizationEnabled(boolean autosuggestSynchronizationEnabled) { this.autosuggestSynchronizationEnabled = autosuggestSynchronizationEnabled; } + + public Calendar getTargetSyncTime() { + + TimeZone tz = TimeZone.getTimeZone(getSyncTaskStartTimeTimeZone()); + Calendar targetSyncTime = Calendar.getInstance(tz); + + targetSyncTime.set(Calendar.HOUR_OF_DAY, getSyncTaskStartTimeHr()); + targetSyncTime.set(Calendar.MINUTE, getSyncTaskStartTimeMin()); + targetSyncTime.set(Calendar.SECOND, getSyncTaskStartTimeSec()); + + return targetSyncTime; + + } + + public long getDefaultInitialSyncDelayInMs(Calendar timeNow) { + + int taskFrequencyInDays = getSyncTaskFrequencyInDay(); + + long nextSyncTimeInMs = getNextSyncTime(getTargetSyncTime(), timeNow.getTimeInMillis(), + taskFrequencyInDays * 86400); + + /* + * If the the current time is after the scheduled start time, then delay + * by the initial task delay configuration value + */ + long delayUntilNextSyncInMs = Math.max(getSyncTaskInitialDelayInMs(), + nextSyncTimeInMs - timeNow.getTimeInMillis()); + + return delayUntilNextSyncInMs; + + } + + public long getNextSyncTime(Calendar syncTime, int taskFrequencyInSeconds) { + + TimeZone tz = TimeZone.getTimeZone(getSyncTaskStartTimeTimeZone()); + Calendar timeNow = Calendar.getInstance(tz); + + return getNextSyncTime(syncTime, timeNow.getTimeInMillis(), taskFrequencyInSeconds); + } + + /** + * Gets the first sync time. + * + * @param calendar + * the calendar + * @param timeNow + * the time now in ms + * @param taskFrequencyInMs + * task period in ms + * @return the first sync time + */ + public long getNextSyncTime(Calendar syncTime, long timeNowInMs, int taskFrequencyInSeconds) { + if (taskFrequencyInSeconds == 0) { + return 0; + } else if (timeNowInMs > syncTime.getTimeInMillis()) { + + /* + * If current time is after the scheduled sync start time, then + * we'll skip ahead to the next sync time period + */ + + syncTime.add(Calendar.SECOND, taskFrequencyInSeconds); + } + return syncTime.getTimeInMillis(); + } /* (non-Javadoc) * @see java.lang.Object#toString() */ diff --git a/src/test/java/org/onap/aai/sparky/dal/rest/RestClientBuilderTest.java b/src/test/java/org/onap/aai/sparky/dal/rest/RestClientBuilderTest.java index 4dbcdea..4d10c6a 100644 --- a/src/test/java/org/onap/aai/sparky/dal/rest/RestClientBuilderTest.java +++ b/src/test/java/org/onap/aai/sparky/dal/rest/RestClientBuilderTest.java @@ -41,6 +41,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; import org.onap.aai.sparky.dal.rest.RestClientBuilder; import org.onap.aai.sparky.security.SecurityContextFactory; import org.powermock.modules.junit4.PowerMockRunner; @@ -50,20 +51,11 @@ import ch.qos.logback.classic.Level; /** * The Class RestClientBuilderTest. */ -@RunWith(PowerMockRunner.class) +@RunWith(MockitoJUnitRunner.class) public class RestClientBuilderTest { /** - * Inits the. - * - * @throws Exception the exception - */ - @Before - public void init() throws Exception { - } - - /** * Basic construction test. * * @throws Exception the exception diff --git a/src/test/java/org/onap/aai/sparky/synchronizer/SyncControllerBuilder.java b/src/test/java/org/onap/aai/sparky/synchronizer/SyncControllerBuilder.java index 523f893..bcd0c9a 100644 --- a/src/test/java/org/onap/aai/sparky/synchronizer/SyncControllerBuilder.java +++ b/src/test/java/org/onap/aai/sparky/synchronizer/SyncControllerBuilder.java @@ -38,8 +38,6 @@ import org.onap.aai.sparky.dal.rest.RestfulDataAccessor; import org.onap.aai.sparky.synchronizer.AutosuggestionSynchronizer; import org.onap.aai.sparky.synchronizer.CrossEntityReferenceSynchronizer; import org.onap.aai.sparky.synchronizer.ElasticSearchIndexCleaner; -import org.onap.aai.sparky.synchronizer.GeoSynchronizer; -import org.onap.aai.sparky.synchronizer.HistoricalEntitySummarizer; import org.onap.aai.sparky.synchronizer.IndexCleaner; import org.onap.aai.sparky.synchronizer.IndexIntegrityValidator; import org.onap.aai.sparky.synchronizer.SearchableEntitySynchronizer; @@ -142,17 +140,6 @@ public class SyncControllerBuilder { syncController.registerIndexValidator(entityCounterHistoryValidator); - - ////// - - - - HistoricalEntitySummarizer historicalSummarizer = - new HistoricalEntitySummarizer(esConfig.getEntityCountHistoryIndex()); - historicalSummarizer.setAaiDataProvider(aaiAdapter); - historicalSummarizer.setEsDataProvider(esAdapter); - syncController.registerEntitySynchronizer(historicalSummarizer); - //// /* @@ -235,13 +222,6 @@ public class SyncControllerBuilder { syncController.registerIndexValidator(entitySearchIndexValidator); - ////// - - GeoSynchronizer geoSync = new GeoSynchronizer("topographysearchindex-localhost"); - geoSync.setAaiDataProvider(aaiAdapter); - geoSync.setEsDataProvider(esAdapter); - syncController.registerEntitySynchronizer(geoSync); - //// /* diff --git a/src/test/java/org/onap/aai/sparky/synchronizer/SyncHelperTest.java b/src/test/java/org/onap/aai/sparky/synchronizer/SyncHelperTest.java new file mode 100644 index 0000000..55688d9 --- /dev/null +++ b/src/test/java/org/onap/aai/sparky/synchronizer/SyncHelperTest.java @@ -0,0 +1,47 @@ +package org.onap.aai.sparky.synchronizer; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.util.Properties; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.aai.sparky.config.oxm.OxmModelLoader; +import org.onap.aai.sparky.dal.aai.config.ActiveInventoryConfig; +import org.onap.aai.sparky.dal.aai.config.ActiveInventorySslConfig; +import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig; +import org.onap.aai.sparky.synchronizer.config.SynchronizerConfiguration; +import org.onap.aai.sparky.synchronizer.config.TaskProcessorConfig; +import org.onap.aai.sparky.util.Encryptor; +import org.onap.aai.sparky.viewandinspect.config.TierSupportUiConstants; +import org.junit.*; +import static org.junit.Assert.*; +import org.onap.aai.sparky.synchronizer.SyncHelper; + +@RunWith(MockitoJUnitRunner.class) +public class SyncHelperTest { + + private SyncHelper syncHelper; + @BeforeClass + public static void initBeforeClass() throws IOException { + String configHomePath = (new File(".").getCanonicalPath() + "/src/test/resources/sync").replace('\\', '/'); + TierSupportUiConstants.AJSC_HOME = configHomePath; + TierSupportUiConstants.CONFIG_HOME = configHomePath; + } + + +@Test +public void testGetOxmModelLoader() throws Exception { + syncHelper = new SyncHelper(new OxmModelLoader()); + OxmModelLoader oxmLoader = new OxmModelLoader(); + syncHelper.setOxmModelLoader(oxmLoader); + assertEquals(oxmLoader, syncHelper.getOxmModelLoader()); +} + +} diff --git a/src/test/java/org/onap/aai/sparky/synchronizer/config/SynchronizerConfigurationTest.java b/src/test/java/org/onap/aai/sparky/synchronizer/config/SynchronizerConfigurationTest.java new file mode 100644 index 0000000..5f3bed9 --- /dev/null +++ b/src/test/java/org/onap/aai/sparky/synchronizer/config/SynchronizerConfigurationTest.java @@ -0,0 +1,400 @@ +package org.onap.aai.sparky.synchronizer.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Calendar; +import java.util.Locale; +import java.util.Properties; +import java.util.TimeZone; + +import org.junit.Before; +import org.junit.Test; + +public class SynchronizerConfigurationTest { + + private SynchronizerConfiguration config; + + private Properties generateTestProperties() { + + Properties props = new Properties(); + + props.put("synchronizer.syncTask.initialDelayInMs", "60000"); + props.put("synchronizer.syncTask.taskFrequencyInDay", "2"); + props.put("synchronizer.syncTask.startTimestamp", "05:00:00 UTC+00:00"); + props.put("synchronizer.historicalEntitySummarizerEnabled", "true"); + props.put("synchronizer.historicalEntitySummarizedFrequencyInMinutes", "5"); + props.put("synchronizer.resolver.progressLogFrequencyInMs", "60000"); + props.put("synchronizer.resolver.queueMonitorFrequencyInMs", "1000"); + props.put("synchronizer.resolver.displayVerboseQueueManagerStats", "false"); + + props.put("synchronizer.indexIntegrityValidator.enabled", "false"); + props.put("synchronizer.indexIntegrityValidatorFrequencyInMs", "3600000"); + props.put("synchronizer.scrollContextTimeToLiveInMinutes", "5"); + props.put("synchronizer.numScrollContextItemsToRetrievePerRequest", "5000"); + props.put("synchronizer.suppressResourceNotFoundErrors", "true"); + props.put("synchronizer.applyNodesOnlyModifier", "false"); + props.put("synchronizer.autosuggestSynchronizationEnabled", "true"); + + return props; + } + + + @Before + public void init() throws Exception { + + config = new SynchronizerConfiguration(generateTestProperties()); + + } + + @Test + public void validateAccessors() { + + assertEquals(60000, config.getSyncTaskInitialDelayInMs()); + assertEquals(2, config.getSyncTaskFrequencyInDay()); + assertEquals("05:00:00 UTC+00:00", config.getSyncTaskStartTime()); + assertEquals(5, config.getSyncTaskStartTimeHr()); + assertEquals(0, config.getSyncTaskStartTimeMin()); + assertEquals(0, config.getSyncTaskStartTimeSec()); + assertEquals(true, config.isAutosuggestSynchronizationEnabled()); + assertEquals(false, config.isConfigDisabledForInitialSync() ); + assertEquals(true, config.isConfigOkForPeriodicSync()); + assertEquals(true, config.isConfigOkForStartupSync()); + assertEquals(false, config.isDisplayVerboseQueueManagerStats()); + assertEquals(false, config.isIndexIntegrityValidatorEnabled()); + assertEquals(false, config.isNodesOnlyModifierEnabled()); + assertEquals(true, config.isResourceNotFoundErrorsSupressed()); + assertEquals(5000, config.getNumScrollContextItemsToRetrievePerRequest()); + assertEquals(5, config.getScrollContextTimeToLiveInMinutes()); + assertEquals("GMT+00:00", config.getSyncTaskStartTimeTimeZone()); + assertEquals(0, config.getSyncTaskFrequencyInMs()); + assertEquals(60000, config.getResolverProgressLogFrequencyInMs()); + assertEquals(1000, config.getResolverQueueMonitorFrequencyInMs()); + assertEquals(3600000, config.getIndexIntegrityValidatorFrequencyInMs()); + assertTrue(config.toString().startsWith("SynchronizerConfiguration")); + + + config.setSyncTaskInitialDelayInMs(1234); + assertEquals(1234, config.getSyncTaskInitialDelayInMs()); + + config.setSyncTaskFrequencyInDay(5); + assertEquals(5, config.getSyncTaskFrequencyInDay()); + config.setSyncTaskStartTime("06:10:22 UTC+00:00"); + assertEquals("06:10:22 UTC+00:00", config.getSyncTaskStartTime()); + + config.setSyncTaskStartTimeHr(6); + assertEquals(6, config.getSyncTaskStartTimeHr()); + + config.setSyncTaskStartTimeMin(10); + assertEquals(10, config.getSyncTaskStartTimeMin()); + + config.setSyncTaskStartTimeSec(22); + assertEquals(22, config.getSyncTaskStartTimeSec()); + + config.setAutosuggestSynchronizationEnabled(false); + assertEquals(false, config.isAutosuggestSynchronizationEnabled()); + + config.setDisplayVerboseQueueManagerStats(true); + assertEquals(true, config.isDisplayVerboseQueueManagerStats()); + + + config.setIndexIntegrityValidatorEnabled(true); + assertEquals(true, config.isIndexIntegrityValidatorEnabled()); + + config.setNodesOnlyModifierEnabled(true); + assertEquals(true, config.isNodesOnlyModifierEnabled()); + + config.setResourceNotFoundErrorsSupressed(false); + assertEquals(false, config.isResourceNotFoundErrorsSupressed()); + + config.setNumScrollContextItemsToRetrievePerRequest(1234); + assertEquals(1234, config.getNumScrollContextItemsToRetrievePerRequest()); + + config.setScrollContextTimeToLiveInMinutes(11); + assertEquals(11, config.getScrollContextTimeToLiveInMinutes()); + + + config.setSyncTaskStartTimeTimeZone("EST"); + assertEquals("EST", config.getSyncTaskStartTimeTimeZone()); + + config.setSyncTaskFrequencyInMs(1221); + assertEquals(1221, config.getSyncTaskFrequencyInMs()); + + config.setResolverProgressLogFrequencyInMs(1111); + assertEquals(1111, config.getResolverProgressLogFrequencyInMs()); + + config.setResolverQueueMonitorFrequencyInMs(1111); + assertEquals(1111, config.getResolverQueueMonitorFrequencyInMs()); + + config.setIndexIntegrityValidatorFrequencyInMs(5555); + assertEquals(5555, config.getIndexIntegrityValidatorFrequencyInMs()); + + config.setConfigOkForPeriodicSync(false); + assertEquals(false, config.isConfigOkForPeriodicSync()); + + config.setConfigOkForStartupSync(false); + assertEquals(false, config.isConfigOkForStartupSync()); + + + } + + @Test + public void validateFirstSyncTimeCalculation_taskFrequencyZeroDays() { + + Calendar syncThresholdTime = Calendar.getInstance(TimeZone.getTimeZone("EST"),Locale.CANADA); + + // set year, month, day, hour, minute, second + syncThresholdTime.set(2017,2,1,0,0,0); // 00:00 on 1-Feb-2017 + + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST"),Locale.CANADA); + + // set year, month, day, hour, minute, second + timeNow.set(2017,2,1,0,0,0); // 00:00 on 1-Feb-2017 + + assertEquals( 0, config.getNextSyncTime(syncThresholdTime, timeNow.getTimeInMillis(), 0)); + + } + + @Test + public void validateFirstSyncTimeCalculation_timeNowLessThanSyncTimeBoundary() { + + Calendar syncThresholdTime = Calendar.getInstance(TimeZone.getTimeZone("EST"),Locale.CANADA); + + // 06:00 on 1-Feb-2017 + syncThresholdTime.set(2017,2,1,6,0,0); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST"),Locale.CANADA); + + // set year, month, day, hour, minute, second + timeNow.set(2017,2,1,0,0,0); // 00:00 on 1-Feb-2017 + + // First sync time is the 06:00 am threshold time + assertEquals( syncThresholdTime.getTimeInMillis(), config.getNextSyncTime(syncThresholdTime, timeNow.getTimeInMillis(), 1*86400)); + + } + + @Test + public void validateFirstSyncTimeCalculation_timeNowEqualsSyncTimeBoundary() { + + Calendar syncThresholdTime = Calendar.getInstance(TimeZone.getTimeZone("EST"),Locale.CANADA); + + // 06:00 on 1-Feb-2017 + syncThresholdTime.set(2017,2,1,6,0,0); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST"),Locale.CANADA); + + // set year, month, day, hour, minute, second + timeNow.set(2017,2,1,6,0,0); // 00:00 on 1-Feb-2017 + + // First sync time is the 06:00 am threshold time + assertEquals( (syncThresholdTime.getTimeInMillis()/1000), (config.getNextSyncTime(syncThresholdTime, timeNow.getTimeInMillis(), 1*86400)/1000)); + + } + + @Test + public void validateFirstSyncTimeCalculation_timeNowGreaterThanSyncTimeBoundary() { + + Calendar syncThresholdTime = Calendar.getInstance(TimeZone.getTimeZone("EST"),Locale.CANADA); + + // 06:00 on 1-Feb-2017 + syncThresholdTime.set(2017,2,1,6,0,0); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST"),Locale.CANADA); + + // set year, month, day, hour, minute, second + timeNow.set(2017,2,1,8,0,0); // 00:00 on 1-Feb-2017 + + // First sync time is 1 full sync period past the sync threshold time + assertEquals( 86400000 + syncThresholdTime.getTimeInMillis(), config.getNextSyncTime(syncThresholdTime, timeNow.getTimeInMillis(), 1*86400)); + + } + +/* @Test + public void validateDefaultPeriodicInitialSyncDelay_dailySync_timeNowAtMidnight() { + + config.setSyncTaskStartTimeTimeZone("EST"); + config.setSyncTaskStartTimeHr(6); + config.setSyncTaskStartTimeMin(0); + config.setSyncTaskStartTimeSec(0); + config.setSyncTaskFrequencyInDay(1); + config.setSyncTaskInitialDelayInMs(60000); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST")); + + // set year, month, day, hour, minute, second + timeNow.set(Calendar.AM_PM, Calendar.AM); + timeNow.set(Calendar.HOUR, 0); + timeNow.set(Calendar.MINUTE, 0); + timeNow.set(Calendar.SECOND, 0); + + // First sync time is 6 hours from midnight + assertEquals( 6*3600*1000, config.getDefaultInitialSyncDelayInMs(timeNow)); + + }*/ + + @Test + public void validateDefaultPeriodicInitialSyncDelay_dailySync_timeNowAt4AM() { + + config.setSyncTaskStartTimeTimeZone("EST"); + config.setSyncTaskStartTimeHr(6); + config.setSyncTaskStartTimeMin(0); + config.setSyncTaskStartTimeSec(0); + config.setSyncTaskFrequencyInDay(1); + config.setSyncTaskInitialDelayInMs(60000); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST")); + + // set year, month, day, hour, minute, second + timeNow.set(Calendar.AM_PM, Calendar.AM); + timeNow.set(Calendar.HOUR, 4); + timeNow.set(Calendar.MINUTE, 0); + timeNow.set(Calendar.SECOND, 0); + + // round time to minutes to avoid millisecond alignment variance + assertEquals(2 * 3600, (config.getDefaultInitialSyncDelayInMs(timeNow)) / 1000); + + } + + @Test + public void validateDefaultPeriodicInitialSyncDelay_dailySync_timeNowAt6AM() { + + config.setSyncTaskStartTimeTimeZone("EST"); + config.setSyncTaskStartTimeHr(6); + config.setSyncTaskStartTimeMin(0); + config.setSyncTaskStartTimeSec(0); + config.setSyncTaskFrequencyInDay(1); + config.setSyncTaskInitialDelayInMs(60000); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST")); + + // set year, month, day, hour, minute, second + timeNow.set(Calendar.AM_PM, Calendar.AM); + timeNow.set(Calendar.HOUR, 6); + timeNow.set(Calendar.MINUTE, 0); + timeNow.set(Calendar.SECOND, 0); + + // First sync time is now with 60 seconds from now + assertEquals( 60000, config.getDefaultInitialSyncDelayInMs(timeNow)); + + } + + @Test + public void validateDefaultPeriodicInitialSyncDelay_dailySync_timeNowAt10PM() { + + config.setSyncTaskStartTimeTimeZone("EST"); + config.setSyncTaskStartTimeHr(6); + config.setSyncTaskStartTimeMin(0); + config.setSyncTaskStartTimeSec(0); + config.setSyncTaskFrequencyInDay(1); + config.setSyncTaskInitialDelayInMs(60000); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST")); + + // set year, month, day, hour, minute, second + timeNow.set(Calendar.AM_PM, Calendar.PM); + timeNow.set(Calendar.HOUR, 10); + timeNow.set(Calendar.MINUTE, 0); + timeNow.set(Calendar.SECOND, 0); + + // First sync time is now 1 sync period delayed. The time delay before + // the initial sync should now be 8 hours, assuming 10PM is the current time. + assertEquals( 8*3600, (config.getDefaultInitialSyncDelayInMs(timeNow)/1000)); + + } + +/* @Test + public void validateDefaultPeriodicInitialSyncDelay_weeklySync_timeNowAtMidnight() { + + config.setSyncTaskStartTimeTimeZone("EST"); + config.setSyncTaskStartTimeHr(6); + config.setSyncTaskStartTimeMin(0); + config.setSyncTaskStartTimeSec(0); + config.setSyncTaskFrequencyInDay(7); + config.setSyncTaskInitialDelayInMs(60000); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST")); + + // set year, month, day, hour, minute, second + timeNow.set(Calendar.AM_PM, Calendar.AM); + timeNow.set(Calendar.HOUR, 0); + timeNow.set(Calendar.MINUTE, 0); + timeNow.set(Calendar.SECOND, 0); + + // First sync time is 6 hours from midnight + assertTrue(6*3600*1000 >= config.getDefaultInitialSyncDelayInMs(timeNow)); + + }*/ + + @Test + public void validateDefaultPeriodicInitialSyncDelay_weeklySync_timeNowAt4AM() { + + config.setSyncTaskStartTimeTimeZone("EST"); + config.setSyncTaskStartTimeHr(6); + config.setSyncTaskStartTimeMin(0); + config.setSyncTaskStartTimeSec(0); + config.setSyncTaskFrequencyInDay(7); + config.setSyncTaskInitialDelayInMs(60000); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST")); + + // set year, month, day, hour, minute, second + timeNow.set(Calendar.AM_PM, Calendar.AM); + timeNow.set(Calendar.HOUR, 4); + timeNow.set(Calendar.MINUTE, 0); + timeNow.set(Calendar.SECOND, 0); + + // round time to minutes to avoid millisecond alignment variance + assertEquals(2 * 3600, (config.getDefaultInitialSyncDelayInMs(timeNow)) / 1000); + + } + + @Test + public void validateDefaultPeriodicInitialSyncDelay_weeklySync_timeNowAt6AM() { + + config.setSyncTaskStartTimeTimeZone("EST"); + config.setSyncTaskStartTimeHr(6); + config.setSyncTaskStartTimeMin(0); + config.setSyncTaskStartTimeSec(0); + config.setSyncTaskFrequencyInDay(7); + config.setSyncTaskInitialDelayInMs(60000); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST")); + + // set year, month, day, hour, minute, second + timeNow.set(Calendar.AM_PM, Calendar.AM); + timeNow.set(Calendar.HOUR, 6); + timeNow.set(Calendar.MINUTE, 0); + timeNow.set(Calendar.SECOND, 0); + + // First sync time is now with 60 seconds from now + assertEquals( 60000, config.getDefaultInitialSyncDelayInMs(timeNow)); + + } + + @Test + public void validateDefaultPeriodicInitialSyncDelay_weeklySync_timeNowAt10PM() { + + config.setSyncTaskStartTimeTimeZone("EST"); + config.setSyncTaskStartTimeHr(6); + config.setSyncTaskStartTimeMin(0); + config.setSyncTaskStartTimeSec(0); + config.setSyncTaskFrequencyInDay(7); + config.setSyncTaskInitialDelayInMs(60000); + + Calendar timeNow = Calendar.getInstance(TimeZone.getTimeZone("EST")); + + // set year, month, day, hour, minute, second + timeNow.set(Calendar.AM_PM, Calendar.PM); + timeNow.set(Calendar.HOUR, 10); + timeNow.set(Calendar.MINUTE, 0); + timeNow.set(Calendar.SECOND, 0); + + // First sync time is now 1 sync period delayed. The time delay before + // the initial sync should now be 8 hours + 24*6 days, assuming 10PM is the current time. + assertEquals( (8+(6*24))*3600, (config.getDefaultInitialSyncDelayInMs(timeNow)/1000)); + + } + + +} diff --git a/src/test/java/org/onap/aai/sparky/synchronizer/entity/AggregationSuggestionEntityTest.java b/src/test/java/org/onap/aai/sparky/synchronizer/entity/AggregationSuggestionEntityTest.java new file mode 100644 index 0000000..5b945cf --- /dev/null +++ b/src/test/java/org/onap/aai/sparky/synchronizer/entity/AggregationSuggestionEntityTest.java @@ -0,0 +1,69 @@ +/** + * ============LICENSE_START=================================================== + * SPARKY (AAI UI service) + * ============================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ============================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END===================================================== + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ + +package org.onap.aai.sparky.synchronizer.entity; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +import org.junit.Test; + +public class AggregationSuggestionEntityTest { + public String getResourceFileContents(String filePath) { + StringBuilder result = new StringBuilder(""); + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource(filePath).getFile()); + + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + result.append(line).append("\n"); + } + scanner.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + return result.toString(); + } + + @Test + public void testGetIndexDocumentJson() { + AggregationSuggestionEntity aggregationSuggestionEntity = new AggregationSuggestionEntity(); + + String expectedFilterListPayload = getResourceFileContents( + "sync/entity/AggregationSuggestionEntity_getIndexDocumentJson_expected.json"); + + assertTrue( + aggregationSuggestionEntity.getIndexDocumentJson().contains(expectedFilterListPayload.trim())); + } +} diff --git a/src/test/java/org/onap/aai/sparky/util/NodeUtilsTest.java b/src/test/java/org/onap/aai/sparky/util/NodeUtilsTest.java index 0388b1c..9329d9a 100644 --- a/src/test/java/org/onap/aai/sparky/util/NodeUtilsTest.java +++ b/src/test/java/org/onap/aai/sparky/util/NodeUtilsTest.java @@ -29,6 +29,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import java.util.ArrayList; @@ -471,6 +472,23 @@ public class NodeUtilsTest { } /** + * Test isNumeric expect true + */ + @Test + public void test_isNumeric_expectFalse(){ + String invalidNumber="number"; + assertFalse(NodeUtils.isNumeric(invalidNumber)); + } + + /** + * Test isNumeric expect true + */ + @Test + public void test_isNumeric_expectTrue(){ + String validNumber="123"; + assertTrue(NodeUtils.isNumeric(validNumber)); + } + /** * test calculate edit attributes urls */ @Test diff --git a/src/test/resources/sync/auth/emptyCert.p12 b/src/test/resources/sync/auth/emptyCert.p12 new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/test/resources/sync/auth/emptyCert.p12 diff --git a/src/test/resources/sync/entity/AggregationSuggestionEntity_getIndexDocumentJson_expected.json b/src/test/resources/sync/entity/AggregationSuggestionEntity_getIndexDocumentJson_expected.json new file mode 100644 index 0000000..b9e2501 --- /dev/null +++ b/src/test/resources/sync/entity/AggregationSuggestionEntity_getIndexDocumentJson_expected.json @@ -0,0 +1 @@ +{"entity_suggest":{"output":"VNFs","input":["VNFs","generic-vnfs"],"payload":{},"weight":100}}
\ No newline at end of file diff --git a/src/test/resources/sync/etc/autoSuggestMappings.json b/src/test/resources/sync/etc/autoSuggestMappings.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/src/test/resources/sync/etc/autoSuggestMappings.json @@ -0,0 +1 @@ +{}
\ No newline at end of file diff --git a/src/test/resources/sync/etc/autoSuggestSettings.json b/src/test/resources/sync/etc/autoSuggestSettings.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/src/test/resources/sync/etc/autoSuggestSettings.json @@ -0,0 +1 @@ +{}
\ No newline at end of file |