aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/sa/searchdbabstraction/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openecomp/sa/searchdbabstraction/util')
-rw-r--r--src/main/java/org/openecomp/sa/searchdbabstraction/util/AggregationParsingUtil.java106
-rw-r--r--src/main/java/org/openecomp/sa/searchdbabstraction/util/DocumentSchemaUtil.java123
-rw-r--r--src/main/java/org/openecomp/sa/searchdbabstraction/util/SearchDbConstants.java56
3 files changed, 285 insertions, 0 deletions
diff --git a/src/main/java/org/openecomp/sa/searchdbabstraction/util/AggregationParsingUtil.java b/src/main/java/org/openecomp/sa/searchdbabstraction/util/AggregationParsingUtil.java
new file mode 100644
index 0000000..38ff943
--- /dev/null
+++ b/src/main/java/org/openecomp/sa/searchdbabstraction/util/AggregationParsingUtil.java
@@ -0,0 +1,106 @@
+/**
+ * ============LICENSE_START=======================================================
+ * Search Data 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 ati
+ *
+ * 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.openecomp.sa.searchdbabstraction.util;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.openecomp.sa.searchdbabstraction.entity.AggregationBucket;
+import org.openecomp.sa.searchdbabstraction.entity.AggregationResult;
+
+import java.util.Iterator;
+import java.util.Set;
+
+public class AggregationParsingUtil {
+ public static AggregationResult[] parseAggregationResults(JSONObject aggregations)
+ throws JsonProcessingException {
+
+ // Obtain the set of aggregation names
+ Set keySet = aggregations.keySet();
+ AggregationResult[] aggResults = new AggregationResult[keySet.size()];
+
+ int index = 0;
+ for (Iterator it = keySet.iterator(); it.hasNext(); ) {
+ String key = (String) it.next();
+ AggregationResult aggResult = new AggregationResult();
+ aggResult.setName(key);
+
+ JSONObject bucketsOrNested = (JSONObject) aggregations.get(key);
+ Object buckets = bucketsOrNested.get("buckets");
+ if (buckets == null) {
+ // we have a nested
+ Number count = (Number) bucketsOrNested.remove("doc_count");
+ aggResult.setCount(count);
+ AggregationResult[] nestedResults = parseAggregationResults(bucketsOrNested);
+ aggResult.setNestedAggregations(nestedResults);
+ } else {
+ AggregationBucket[] aggBuckets = parseAggregationBuckets((JSONArray) buckets);
+ aggResult.setBuckets(aggBuckets);
+ }
+
+ aggResults[index] = aggResult;
+ index++;
+ }
+
+ return aggResults;
+
+ }
+
+ private static AggregationBucket[] parseAggregationBuckets(JSONArray buckets)
+ throws JsonProcessingException {
+ AggregationBucket[] aggBuckets = new AggregationBucket[buckets.size()];
+ for (int i = 0; i < buckets.size(); i++) {
+ AggregationBucket aggBucket = new AggregationBucket();
+ JSONObject bucketContent = (JSONObject) buckets.get(i);
+ Object key = bucketContent.remove("key");
+ aggBucket.setKey(key);
+ Object formatted = bucketContent.remove("key_as_string");
+ if (formatted != null) {
+ aggBucket.setFormattedKey((String) formatted);
+ }
+ Object count = bucketContent.remove("doc_count");
+ if (count != null) {
+ aggBucket.setCount((Number) count);
+ }
+ bucketContent.remove("from");
+ bucketContent.remove("from_as_string");
+ bucketContent.remove("to");
+ bucketContent.remove("to_as_string");
+
+
+ if (!bucketContent.entrySet().isEmpty()) {
+ // we have results from sub-aggregation
+ AggregationResult[] subResult = parseAggregationResults(bucketContent);
+ if (subResult != null) {
+ aggBucket.setSubAggregationResult(subResult);
+ }
+ }
+ aggBuckets[i] = aggBucket;
+ }
+
+ return aggBuckets;
+ }
+
+}
diff --git a/src/main/java/org/openecomp/sa/searchdbabstraction/util/DocumentSchemaUtil.java b/src/main/java/org/openecomp/sa/searchdbabstraction/util/DocumentSchemaUtil.java
new file mode 100644
index 0000000..579d201
--- /dev/null
+++ b/src/main/java/org/openecomp/sa/searchdbabstraction/util/DocumentSchemaUtil.java
@@ -0,0 +1,123 @@
+/**
+ * ============LICENSE_START=======================================================
+ * Search Data 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 ati
+ *
+ * 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.openecomp.sa.searchdbabstraction.util;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.openecomp.sa.rest.DocumentFieldSchema;
+import org.openecomp.sa.rest.DocumentSchema;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public class DocumentSchemaUtil {
+
+ public static String generateDocumentMappings(String documentSchema)
+ throws JsonParseException, JsonMappingException, IOException {
+
+ // Unmarshal the json content into a document schema object.
+ ObjectMapper mapper = new ObjectMapper();
+ DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
+
+ return generateDocumentMappings(schema);
+ }
+
+ public static String generateDocumentMappings(DocumentSchema schema) {
+
+ // Now, generate the Elastic Search mapping json and return it.
+ StringBuilder sb = new StringBuilder();
+ sb.append("{");
+ sb.append("\"properties\": {");
+
+ generateFieldMappings(schema.getFields(), sb);
+
+ sb.append("}");
+ sb.append("}");
+
+ return sb.toString();
+ }
+
+
+ private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
+
+ AtomicBoolean firstField = new AtomicBoolean(true);
+
+ for (DocumentFieldSchema field : fields) {
+
+ // If this isn't the first field in the list, prepend it with a ','
+ if (!firstField.compareAndSet(true, false)) {
+ sb.append(", ");
+ }
+
+ // Now, append the translated field contents.
+ generateFieldMapping(field, sb);
+ }
+ }
+
+ private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
+
+ sb.append("\"").append(fieldSchema.getName()).append("\": {");
+
+ // The field type is mandatory.
+ sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
+
+ // For date type fields we may optionally supply a format specifier.
+ if (fieldSchema.getDataType().equals("date")) {
+ if (fieldSchema.getFormat() != null) {
+ sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
+ }
+ }
+
+ // If the index field was specified, then append it.
+ if (fieldSchema.getSearchable() != null) {
+ sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
+ ? "analyzed" : "not_analyzed").append("\"");
+ }
+
+ // If a search analyzer was specified, then append it.
+ if (fieldSchema.getSearchAnalyzer() != null) {
+ sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
+ }
+
+ // If an indexing analyzer was specified, then append it.
+ if (fieldSchema.getIndexAnalyzer() != null) {
+ sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
+ }
+
+
+ if (fieldSchema.getDataType().equals("nested")) {
+
+ sb.append(", \"properties\": {");
+ generateFieldMappings(fieldSchema.getSubFields(), sb);
+ sb.append("}");
+ }
+
+ sb.append("}");
+ }
+
+}
+
diff --git a/src/main/java/org/openecomp/sa/searchdbabstraction/util/SearchDbConstants.java b/src/main/java/org/openecomp/sa/searchdbabstraction/util/SearchDbConstants.java
new file mode 100644
index 0000000..6790b24
--- /dev/null
+++ b/src/main/java/org/openecomp/sa/searchdbabstraction/util/SearchDbConstants.java
@@ -0,0 +1,56 @@
+/**
+ * ============LICENSE_START=======================================================
+ * Search Data 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 ati
+ *
+ * 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.openecomp.sa.searchdbabstraction.util;
+
+public class SearchDbConstants {
+ public static final String SDB_FILESEP = (System.getProperty("file.separator") == null) ? "/"
+ : System.getProperty("file.separator");
+ public static final String SDB_BUNDLECONFIG_NAME =
+ (System.getProperty("BUNDLECONFIG_DIR") == null)
+ ? "bundleconfig" : System.getProperty("BUNDLECONFIG_DIR");
+
+ public static final String SDB_HOME_BUNDLECONFIG = (System.getProperty("AJSC_HOME") == null)
+ ? SDB_FILESEP + "opt" + SDB_FILESEP + "app" + SDB_FILESEP + "searchdb"
+ + SDB_FILESEP + SDB_BUNDLECONFIG_NAME
+ : System.getProperty("AJSC_HOME") + SDB_FILESEP + SDB_BUNDLECONFIG_NAME;
+
+ public static final String SDB_HOME_ETC =
+ SDB_HOME_BUNDLECONFIG + SDB_FILESEP + "etc" + SDB_FILESEP;
+ public static final String SDB_CONFIG_APP_LOCATION = SDB_HOME_ETC + "appprops" + SDB_FILESEP;
+
+ // Elastic Search related
+ public static final String SDB_SPECIFIC_CONFIG = (System.getProperty("CONFIG_HOME") == null)
+ ? SDB_CONFIG_APP_LOCATION : System.getProperty("CONFIG_HOME") + SDB_FILESEP;
+ public static final String ES_CONFIG_FILE = SDB_SPECIFIC_CONFIG + SDB_FILESEP
+ + "elastic-search.properties";
+ public static final String SDB_AUTH = SDB_SPECIFIC_CONFIG + "auth" + SDB_FILESEP;
+ public static final String SDB_AUTH_CONFIG_FILENAME = SDB_AUTH + "search_policy.json";
+ public static final String SDB_FILTER_CONFIG_FILE = SDB_SPECIFIC_CONFIG + "filter-config.json";
+ public static final String SDB_ANALYSIS_CONFIG_FILE =
+ SDB_SPECIFIC_CONFIG + "analysis-config.json";
+
+ // Logging related
+ public static final String SDB_SERVICE_NAME = "SearchDataService";
+}