* limitations under the License.
* ============LICENSE_END=========================================================
*/
+
package org.onap.aai.db;
import java.io.UnsupportedEncodingException;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
-
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.introspection.Introspector;
import org.onap.aai.introspection.Loader;
public class DbMethHelper {
- private final Loader loader;
- private final TransactionalGraphEngine engine;
-
- protected DbMethHelper() {
- this.loader = null;
- this.engine = null;
- }
- public DbMethHelper(Loader loader, TransactionalGraphEngine engine) {
- this.loader = loader;
- this.engine = engine;
- }
- /**
- *
- * @param type
- * @param map - form [{type}.{propname}:{value}]
- * @return
- * @throws UnsupportedEncodingException
- * @throws AAIException
- */
- public Optional<Vertex> searchVertexByIdentityMap(String type, Map<String, Object> map) throws AAIException {
-
- Introspector relationship = constructRelationship(type, map);
- RelationshipToURI parser;
- URI uri;
- QueryParser queryParser;
- try {
- parser = new RelationshipToURI(loader, relationship);
- uri = parser.getUri();
- queryParser = this.engine.getQueryBuilder().createQueryFromURI(uri);
- } catch (UnsupportedEncodingException e) {
- throw new AAIException("AAI_3000");
- }
-
- List<Vertex> results = queryParser.getQueryBuilder().toList();
-
- return reduceToSingleVertex(results, map);
- }
-
- /**
- * @param type
- * @param map - form [{propname}:{value}]
- * @return
- * @throws AAIException
- */
- public Optional<Vertex> locateUniqueVertex(String type, Map<String, Object> map) throws AAIException {
-
- return reduceToSingleVertex(locateUniqueVertices(type, map), map);
- }
-
- public List<Vertex> locateUniqueVertices(String type, Map<String, Object> map) throws AAIException {
- Introspector obj = this.createIntrospectorFromMap(type, map);
- QueryBuilder builder = this.engine.getQueryBuilder().exactMatchQuery(obj);
-
- return builder.toList();
- }
- private Introspector constructRelationship(String type, Map<String, Object> map) throws AAIUnknownObjectException {
- final Introspector relationship = loader.introspectorFromName("relationship");
- relationship.setValue("related-to", type);
- final List<Object> data = relationship.getValue("relationship-data");
- for (Entry<String, Object> entry : map.entrySet()) {
- final Introspector dataObj = loader.introspectorFromName("relationship-data");
- dataObj.setValue("relationship-key", entry.getKey());
- dataObj.setValue("relationship-value", entry.getValue());
- data.add(dataObj.getUnderlyingObject());
- }
-
- return relationship;
- }
-
- private Introspector createIntrospectorFromMap(String targetNodeType, Map<String, Object> propHash) throws AAIUnknownObjectException {
- final Introspector result = loader.introspectorFromName(targetNodeType);
- for (Entry<String, Object> entry : propHash.entrySet()) {
- result.setValue(entry.getKey(), entry.getValue());
- }
- return result;
- }
-
- private Optional<Vertex> reduceToSingleVertex(List<Vertex> vertices, Map<String, Object> map) throws AAIException {
- if (vertices.isEmpty()){
- return Optional.empty();
- } else if (vertices.size() > 1) {
- throw new AAIException("AAI_6112", "More than one Node found by getUniqueNode for params: " + map);
- }
-
- return Optional.of(vertices.get(0));
- }
- public List<String> getVertexProperties(Vertex v) {
- List<String> retArr = new ArrayList<>();
- if( v == null ){
- retArr.add("null Node object passed to showPropertiesForNode()\n");
- }
- else {
- String nodeType;
- Object ob = v.<Object>property("aai-node-type").orElse(null);
- if( ob == null ){
- nodeType = "null";
- }
- else{
- nodeType = ob.toString();
- }
-
- retArr.add(" AAINodeType/VtxID for this Node = [" + nodeType + "/" + v.id() + "]");
- retArr.add(" Property Detail: ");
- Iterator<VertexProperty<Object>> pI = v.properties();
- while( pI.hasNext() ){
- VertexProperty<Object> tp = pI.next();
- Object val = tp.value();
- retArr.add("Prop: [" + tp.key() + "], val = [" + val + "] ");
- }
- }
- return retArr;
- }
+ private final Loader loader;
+ private final TransactionalGraphEngine engine;
+
+ protected DbMethHelper() {
+ this.loader = null;
+ this.engine = null;
+ }
+
+ public DbMethHelper(Loader loader, TransactionalGraphEngine engine) {
+ this.loader = loader;
+ this.engine = engine;
+ }
+
+ /**
+ *
+ * @param type
+ * @param map - form [{type}.{propname}:{value}]
+ * @return
+ * @throws UnsupportedEncodingException
+ * @throws AAIException
+ */
+ public Optional<Vertex> searchVertexByIdentityMap(String type, Map<String, Object> map) throws AAIException {
+
+ Introspector relationship = constructRelationship(type, map);
+ RelationshipToURI parser;
+ URI uri;
+ QueryParser queryParser;
+ try {
+ parser = new RelationshipToURI(loader, relationship);
+ uri = parser.getUri();
+ queryParser = this.engine.getQueryBuilder().createQueryFromURI(uri);
+ } catch (UnsupportedEncodingException e) {
+ throw new AAIException("AAI_3000");
+ }
+
+ List<Vertex> results = queryParser.getQueryBuilder().toList();
+
+ return reduceToSingleVertex(results, map);
+ }
+
+ /**
+ * @param type
+ * @param map - form [{propname}:{value}]
+ * @return
+ * @throws AAIException
+ */
+ public Optional<Vertex> locateUniqueVertex(String type, Map<String, Object> map) throws AAIException {
+
+ return reduceToSingleVertex(locateUniqueVertices(type, map), map);
+ }
+
+ public List<Vertex> locateUniqueVertices(String type, Map<String, Object> map) throws AAIException {
+ Introspector obj = this.createIntrospectorFromMap(type, map);
+ QueryBuilder builder = this.engine.getQueryBuilder().exactMatchQuery(obj);
+
+ return builder.toList();
+ }
+
+ private Introspector constructRelationship(String type, Map<String, Object> map) throws AAIUnknownObjectException {
+ final Introspector relationship = loader.introspectorFromName("relationship");
+ relationship.setValue("related-to", type);
+ final List<Object> data = relationship.getValue("relationship-data");
+ for (Entry<String, Object> entry : map.entrySet()) {
+ final Introspector dataObj = loader.introspectorFromName("relationship-data");
+ dataObj.setValue("relationship-key", entry.getKey());
+ dataObj.setValue("relationship-value", entry.getValue());
+ data.add(dataObj.getUnderlyingObject());
+ }
+
+ return relationship;
+ }
+
+ private Introspector createIntrospectorFromMap(String targetNodeType, Map<String, Object> propHash)
+ throws AAIUnknownObjectException {
+ final Introspector result = loader.introspectorFromName(targetNodeType);
+ for (Entry<String, Object> entry : propHash.entrySet()) {
+ result.setValue(entry.getKey(), entry.getValue());
+ }
+ return result;
+ }
+
+ private Optional<Vertex> reduceToSingleVertex(List<Vertex> vertices, Map<String, Object> map) throws AAIException {
+ if (vertices.isEmpty()) {
+ return Optional.empty();
+ } else if (vertices.size() > 1) {
+ throw new AAIException("AAI_6112", "More than one Node found by getUniqueNode for params: " + map);
+ }
+
+ return Optional.of(vertices.get(0));
+ }
+
+ public List<String> getVertexProperties(Vertex v) {
+ List<String> retArr = new ArrayList<>();
+ if (v == null) {
+ retArr.add("null Node object passed to showPropertiesForNode()\n");
+ } else {
+ String nodeType;
+ Object ob = v.<Object>property("aai-node-type").orElse(null);
+ if (ob == null) {
+ nodeType = "null";
+ } else {
+ nodeType = ob.toString();
+ }
+
+ retArr.add(" AAINodeType/VtxID for this Node = [" + nodeType + "/" + v.id() + "]");
+ retArr.add(" Property Detail: ");
+ Iterator<VertexProperty<Object>> pI = v.properties();
+ while (pI.hasNext()) {
+ VertexProperty<Object> tp = pI.next();
+ Object val = tp.value();
+ retArr.add("Prop: [" + tp.key() + "], val = [" + val + "] ");
+ }
+ }
+ return retArr;
+ }
}