summaryrefslogtreecommitdiffstats
path: root/aai-traversal/src/main/java/org/onap/aai/rest/search
diff options
context:
space:
mode:
Diffstat (limited to 'aai-traversal/src/main/java/org/onap/aai/rest/search')
-rw-r--r--aai-traversal/src/main/java/org/onap/aai/rest/search/CustomQueryConfig.java34
-rw-r--r--aai-traversal/src/main/java/org/onap/aai/rest/search/GenericQueryProcessor.java31
-rw-r--r--aai-traversal/src/main/java/org/onap/aai/rest/search/GetCustomQueryConfig.java159
-rw-r--r--aai-traversal/src/main/java/org/onap/aai/rest/search/GremlinServerSingleton.java67
4 files changed, 253 insertions, 38 deletions
diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/search/CustomQueryConfig.java b/aai-traversal/src/main/java/org/onap/aai/rest/search/CustomQueryConfig.java
new file mode 100644
index 0000000..ada67c7
--- /dev/null
+++ b/aai-traversal/src/main/java/org/onap/aai/rest/search/CustomQueryConfig.java
@@ -0,0 +1,34 @@
+package org.onap.aai.rest.search;
+
+import java.util.List;
+
+public class CustomQueryConfig {
+ public CustomQueryConfig() {
+ // used by GetCustomQueryConfig
+ }
+
+
+ private String query;
+ private List<String> queryOptionalProperties;
+ private List<String> queryRequiredProperties;
+
+ public void setQuery(String query) {
+ this.query = query;
+ }
+ public String getQuery() {
+ return this.query;
+ }
+
+ public void setQueryOptionalProperties( List<String> queryOptionalProperties) {
+ this.queryOptionalProperties = queryOptionalProperties;
+ }
+ public List<String> getQueryOptionalProperties( ) {
+ return queryOptionalProperties;
+ }
+ public void setQueryRequiredProperties( List<String> queryRequiredProperties) {
+ this.queryRequiredProperties = queryRequiredProperties;
+ }
+ public List<String> getQueryRequiredProperties( ) {
+ return queryRequiredProperties;
+ }
+}
diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/search/GenericQueryProcessor.java b/aai-traversal/src/main/java/org/onap/aai/rest/search/GenericQueryProcessor.java
index 9f6f3aa..14e218f 100644
--- a/aai-traversal/src/main/java/org/onap/aai/rest/search/GenericQueryProcessor.java
+++ b/aai-traversal/src/main/java/org/onap/aai/rest/search/GenericQueryProcessor.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017 AT&T Intellectual Property. 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.
@@ -25,6 +25,7 @@ import java.io.FileNotFoundException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -42,6 +43,7 @@ import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.javatuples.Pair;
+import org.onap.aai.query.builder.MissingOptionalParameter;
import org.onap.aai.restcore.util.URITools;
import org.onap.aai.serialization.engines.TransactionalGraphEngine;
import org.onap.aai.serialization.queryformats.SubGraphStyle;
@@ -117,28 +119,41 @@ public abstract class GenericQueryProcessor {
if (!this.isGremlin) {
Matcher m = p.matcher(uri.get().getPath());
String queryName = "";
+ List<String> optionalParameters = Collections.emptyList();
if (m.find()) {
queryName = m.group(1);
+ CustomQueryConfig queryConfig = gremlinServerSingleton.getCustomQueryConfig(queryName);
+ if ( queryConfig != null ) {
+ query = queryConfig.getQuery();
+ optionalParameters = queryConfig.getQueryOptionalProperties();
+ }
}
-
+
for (String key : queryParams.keySet()) {
params.put(key, queryParams.getFirst(key));
+ if ( optionalParameters.contains(key) ){
+ optionalParameters.remove(key);
+ }
}
- query = gremlinServerSingleton.getStoredQuery(queryName);
- if (query == null) {
- query = "";
- } else {
- query = queryBuilderSingleton.executeTraversal(dbEngine, query, params);
+ if (!optionalParameters.isEmpty()){
+ MissingOptionalParameter missingParameter = MissingOptionalParameter.getInstance();
+ for ( String key : optionalParameters ) {
+ params.put(key, missingParameter);
+ }
}
-
List<Object> ids = new ArrayList<>();
if (vertices.isPresent() && !vertices.get().isEmpty()) {
for (Vertex v : vertices.get()) {
ids.add(v.id());
}
+ if (query == null) {
+ query = "";
+ } else {
+ query = queryBuilderSingleton.executeTraversal(dbEngine, query, params);
+ }
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(Joiner.on(",").join(ids));
diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/search/GetCustomQueryConfig.java b/aai-traversal/src/main/java/org/onap/aai/rest/search/GetCustomQueryConfig.java
new file mode 100644
index 0000000..9ea0b1d
--- /dev/null
+++ b/aai-traversal/src/main/java/org/onap/aai/rest/search/GetCustomQueryConfig.java
@@ -0,0 +1,159 @@
+package org.onap.aai.rest.search;
+
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. 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=========================================================
+ */
+
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+
+import org.onap.aai.util.AAIConstants;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.reflect.TypeToken;
+
+public class GetCustomQueryConfig {
+
+ private JsonArray storedQueries = null;
+ private CustomQueryConfig customQueryConfig;
+
+
+ private final static String QUERY_CONFIG = "query";
+ private final static String REQUIRED_CONFIG = "required-properties";
+ private final static String OPTIONAL_CONFIG = "optional-properties";
+ private final static String STORED_QUERIES_CONFIG = "stored-queries";
+ private final static String STORED_QUERY_CONFIG = "stored-query";
+
+ public static final String AAI_HOME_ETC_QUERY_JSON = AAIConstants.AAI_HOME_ETC + "query" + AAIConstants.AAI_FILESEP + "stored-queries.json";
+
+ public GetCustomQueryConfig(String customQueryJson ) {
+ init(customQueryJson);
+ }
+
+ private void init( String customQueryJson) {
+ JsonParser parser = new JsonParser();
+ JsonObject queriesObject = parser.parse(customQueryJson).getAsJsonObject();
+ if (queriesObject.has(STORED_QUERIES_CONFIG)) {
+
+ storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
+ }
+ }
+
+ private List<String> toStringList(JsonArray array) {
+ Gson converter = new Gson();
+ Type listType = new TypeToken<List<String>>() {}.getType();
+ return converter.fromJson(array, listType);
+ }
+
+ private List<String> getPropertyList(JsonObject configObject, String config ) {
+ JsonElement subqueryConfig;
+ JsonArray props;
+
+ if ( configObject.has(config)) {
+ subqueryConfig = configObject.get(config);
+ if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
+ props = subqueryConfig.getAsJsonArray();
+ if ( props != null ) {
+ return toStringList(props);
+ }
+ }
+ }
+ return toStringList(null);
+ }
+
+ private String getPropertyString(JsonObject configObject, String config) {
+ JsonElement subqueryConfig;
+
+ if ( configObject.has(config)) {
+ subqueryConfig = configObject.get(config);
+ if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
+ return subqueryConfig.getAsString();
+ }
+ }
+ return null;
+ }
+
+ private void getStoredQueryBlock( JsonObject configObject, String config ) {
+ if ( !configObject.has(config)) {
+ return;
+ }
+
+ JsonElement queryConfig;
+ JsonObject subObject;
+ String multipleStartNodes;
+ List<String> propertyList;
+
+ queryConfig = configObject.get(config);
+ subObject = queryConfig.getAsJsonObject();
+ propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
+ if ( QUERY_CONFIG.equals(config)) {
+ customQueryConfig.setQueryRequiredProperties( propertyList );
+ } else {
+ customQueryConfig.setQueryRequiredProperties( null );
+ }
+
+ propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
+ if ( QUERY_CONFIG.equals(config)) {
+ customQueryConfig.setQueryOptionalProperties( propertyList );
+ } else {
+ customQueryConfig.setQueryOptionalProperties( null );
+ }
+
+ }
+
+
+ public CustomQueryConfig getStoredQuery(String queryName ) {
+
+ customQueryConfig = null;
+ JsonObject configObject;
+ JsonElement query;
+ JsonElement queryConfig;
+ String queryString;
+
+ for (JsonElement storedQuery : storedQueries) {
+ if (storedQuery.isJsonObject()) {
+ JsonObject queryObject = storedQuery.getAsJsonObject();
+ query = queryObject.get(queryName);
+ if ( query != null ) {
+ customQueryConfig = new CustomQueryConfig();
+ configObject = query.getAsJsonObject();
+ getStoredQueryBlock(configObject, QUERY_CONFIG);
+ if ( configObject.has(STORED_QUERY_CONFIG)) {
+ queryConfig = configObject.get(STORED_QUERY_CONFIG);
+ customQueryConfig.setQuery(queryConfig.getAsString());
+ }
+ break;
+ }
+ }
+ }
+
+ return customQueryConfig;
+
+ }
+
+
+
+}
diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/search/GremlinServerSingleton.java b/aai-traversal/src/main/java/org/onap/aai/rest/search/GremlinServerSingleton.java
index e4ac815..2265680 100644
--- a/aai-traversal/src/main/java/org/onap/aai/rest/search/GremlinServerSingleton.java
+++ b/aai-traversal/src/main/java/org/onap/aai/rest/search/GremlinServerSingleton.java
@@ -31,6 +31,9 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
@@ -43,7 +46,10 @@ public class GremlinServerSingleton {
private Cluster cluster;
private boolean timerSet;
private Timer timer;
- private Properties properties;
+
+
+ private GetCustomQueryConfig queryConfig;
+
private static class Helper {
private static final GremlinServerSingleton INSTANCE = new GremlinServerSingleton();
@@ -68,8 +74,6 @@ public class GremlinServerSingleton {
*/
private void init() {
- properties = new Properties();
-
try {
cluster = Cluster.build(new File(AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "gremlin-server-config.yaml"))
.maxContentLength(6537920)
@@ -78,27 +82,29 @@ public class GremlinServerSingleton {
logger.error("Unable to find the file: " + e);
}
- File queryFile = new File(AAIConstants.AAI_HOME_ETC_QUERY);
+ try {
+ String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
+ Path path = Paths.get(filepath);
+ String customQueryConfigJson = new String(Files.readAllBytes(path));
+
- try (FileInputStream fis = new FileInputStream(queryFile)){
- properties.load(fis);
- } catch (IOException e) {
- logger.error("Error occurred during the processing of query file: " + e);
- }
+ queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
+ } catch (IOException e) {
+ logger.error("Error occurred during the processing of query json file: " + e);
+ }
- TimerTask task = new FileWatcher(new File(AAIConstants.AAI_HOME_ETC_QUERY)) {
+ TimerTask task = new FileWatcher(new File(GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON)) {
@Override
protected void onChange(File file) {
- File queryFile = new File(AAIConstants.AAI_HOME_ETC_QUERY);
- try (FileInputStream fis = new FileInputStream(queryFile)){
- properties.load(fis);
- logger.debug("File: " + file + " was changed so the cluster is rebuild for gremlin server");
- } catch (FileNotFoundException e) {
- logger.error("Unable to find the file: " + e);
- } catch (IOException e) {
- logger.error("Error occurred during the processing of query file: " + e);
- }
+ try {
+ String filepath = GetCustomQueryConfig.AAI_HOME_ETC_QUERY_JSON;
+ Path path = Paths.get(filepath);
+ String customQueryConfigJson = new String(Files.readAllBytes(path));
+ queryConfig = new GetCustomQueryConfig(customQueryConfigJson);
+ } catch (IOException e) {
+ logger.error("Error occurred during the processing of query json file: " + e);
+ }
}
};
@@ -113,19 +119,20 @@ public class GremlinServerSingleton {
public Cluster getCluster(){
return cluster;
}
-
+
/**
- * Gets the key if the properties contains that key
- *
- * Purposely not checking if the property exists due
- * to if you check for the property and then get the property
- * Then you are going to have to synchronize the method
- *
- * @param key the query to check if it exists in the file
- * @return string if the key exists or null if it doesn't
+ * Gets the query using CustomQueryConfig
+ * @param key
+ * @return
*/
- public String getStoredQuery(String key){
- return (String) properties.get(key);
+ public String getStoredQueryFromConfig(String key){
+ CustomQueryConfig customQueryConfig = queryConfig.getStoredQuery(key);
+ return customQueryConfig.getQuery();
}
+
+ public CustomQueryConfig getCustomQueryConfig(String key) {
+ return queryConfig.getStoredQuery(key);
+ }
+
}