2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.aai.db;
23 import java.io.UnsupportedEncodingException;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.Optional;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
35 import org.openecomp.aai.exceptions.AAIException;
36 import org.openecomp.aai.introspection.Introspector;
37 import org.openecomp.aai.introspection.Loader;
38 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
39 import org.openecomp.aai.parsers.query.QueryParser;
40 import org.openecomp.aai.parsers.relationship.RelationshipToURI;
41 import org.openecomp.aai.query.builder.QueryBuilder;
42 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
44 public class DbMethHelper {
46 private final Loader loader;
47 private final TransactionalGraphEngine engine;
49 protected DbMethHelper() {
53 public DbMethHelper(Loader loader, TransactionalGraphEngine engine) {
60 * @param map - form [{type}.{propname}:{value}]
62 * @throws UnsupportedEncodingException
63 * @throws AAIException
65 public Optional<Vertex> searchVertexByIdentityMap(String type, Map<String, Object> map) throws AAIException {
67 Introspector relationship = constructRelationship(type, map);
68 RelationshipToURI parser;
70 QueryParser queryParser;
72 parser = new RelationshipToURI(loader, relationship);
73 uri = parser.getUri();
74 queryParser = this.engine.getQueryBuilder().createQueryFromURI(uri);
75 } catch (UnsupportedEncodingException e) {
76 throw new AAIException("AAI_3000");
79 List<Vertex> results = queryParser.getQueryBuilder().toList();
81 return reduceToSingleVertex(results, map);
86 * @param map - form [{propname}:{value}]
88 * @throws AAIException
90 public Optional<Vertex> locateUniqueVertex(String type, Map<String, Object> map) throws AAIException {
92 return reduceToSingleVertex(locateUniqueVertices(type, map), map);
95 public List<Vertex> locateUniqueVertices(String type, Map<String, Object> map) throws AAIException {
96 Introspector obj = this.createIntrospectorFromMap(type, map);
97 QueryBuilder builder = this.engine.getQueryBuilder().exactMatchQuery(obj);
99 return builder.toList();
101 private Introspector constructRelationship(String type, Map<String, Object> map) throws AAIUnknownObjectException {
102 final Introspector relationship = loader.introspectorFromName("relationship");
103 relationship.setValue("related-to", type);
104 final List<Object> data = relationship.getValue("relationship-data");
105 for (Entry<String, Object> entry : map.entrySet()) {
106 final Introspector dataObj = loader.introspectorFromName("relationship-data");
107 dataObj.setValue("relationship-key", entry.getKey());
108 dataObj.setValue("relationship-value", entry.getValue());
109 data.add(dataObj.getUnderlyingObject());
115 private Introspector createIntrospectorFromMap(String targetNodeType, Map<String, Object> propHash) throws AAIUnknownObjectException {
116 final Introspector result = loader.introspectorFromName(targetNodeType);
117 for (Entry<String, Object> entry : propHash.entrySet()) {
118 result.setValue(entry.getKey(), entry.getValue());
123 private Optional<Vertex> reduceToSingleVertex(List<Vertex> vertices, Map<String, Object> map) throws AAIException {
124 if (vertices.isEmpty()){
125 return Optional.empty();
126 } else if (vertices.size() > 1) {
127 throw new AAIException("AAI_6112", "More than one Node found by getUniqueNode for params: " + map);
130 return Optional.of(vertices.get(0));
132 public List<String> getVertexProperties(Vertex v) {
133 List<String> retArr = new ArrayList<>();
135 retArr.add("null Node object passed to showPropertiesForNode()\n");
139 Object ob = v.<Object>property("aai-node-type").orElse(null);
144 nodeType = ob.toString();
147 retArr.add(" AAINodeType/VtxID for this Node = [" + nodeType + "/" + v.id() + "]");
148 retArr.add(" Property Detail: ");
149 Iterator<VertexProperty<Object>> pI = v.properties();
150 while( pI.hasNext() ){
151 VertexProperty<Object> tp = pI.next();
152 Object val = tp.value();
153 retArr.add("Prop: [" + tp.key() + "], val = [" + val + "] ");