2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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=========================================================
20 package org.onap.aai.db;
22 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.Optional;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.parsers.query.QueryParser;
39 import org.onap.aai.parsers.relationship.RelationshipToURI;
40 import org.onap.aai.query.builder.QueryBuilder;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
43 public class DbMethHelper {
45 private final Loader loader;
46 private final TransactionalGraphEngine engine;
48 protected DbMethHelper() {
52 public DbMethHelper(Loader loader, TransactionalGraphEngine engine) {
59 * @param map - form [{type}.{propname}:{value}]
61 * @throws UnsupportedEncodingException
62 * @throws AAIException
64 public Optional<Vertex> searchVertexByIdentityMap(String type, Map<String, Object> map) throws AAIException {
66 Introspector relationship = constructRelationship(type, map);
67 RelationshipToURI parser;
69 QueryParser queryParser;
71 parser = new RelationshipToURI(loader, relationship);
72 uri = parser.getUri();
73 queryParser = this.engine.getQueryBuilder().createQueryFromURI(uri);
74 } catch (UnsupportedEncodingException e) {
75 throw new AAIException("AAI_3000");
78 List<Vertex> results = queryParser.getQueryBuilder().toList();
80 return reduceToSingleVertex(results, map);
85 * @param map - form [{propname}:{value}]
87 * @throws AAIException
89 public Optional<Vertex> locateUniqueVertex(String type, Map<String, Object> map) throws AAIException {
91 return reduceToSingleVertex(locateUniqueVertices(type, map), map);
94 public List<Vertex> locateUniqueVertices(String type, Map<String, Object> map) throws AAIException {
95 Introspector obj = this.createIntrospectorFromMap(type, map);
96 QueryBuilder builder = this.engine.getQueryBuilder().exactMatchQuery(obj);
98 return builder.toList();
100 private Introspector constructRelationship(String type, Map<String, Object> map) throws AAIUnknownObjectException {
101 final Introspector relationship = loader.introspectorFromName("relationship");
102 relationship.setValue("related-to", type);
103 final List<Object> data = relationship.getValue("relationship-data");
104 for (Entry<String, Object> entry : map.entrySet()) {
105 final Introspector dataObj = loader.introspectorFromName("relationship-data");
106 dataObj.setValue("relationship-key", entry.getKey());
107 dataObj.setValue("relationship-value", entry.getValue());
108 data.add(dataObj.getUnderlyingObject());
114 private Introspector createIntrospectorFromMap(String targetNodeType, Map<String, Object> propHash) throws AAIUnknownObjectException {
115 final Introspector result = loader.introspectorFromName(targetNodeType);
116 for (Entry<String, Object> entry : propHash.entrySet()) {
117 result.setValue(entry.getKey(), entry.getValue());
122 private Optional<Vertex> reduceToSingleVertex(List<Vertex> vertices, Map<String, Object> map) throws AAIException {
123 if (vertices.isEmpty()){
124 return Optional.empty();
125 } else if (vertices.size() > 1) {
126 throw new AAIException("AAI_6112", "More than one Node found by getUniqueNode for params: " + map);
129 return Optional.of(vertices.get(0));
131 public List<String> getVertexProperties(Vertex v) {
132 List<String> retArr = new ArrayList<>();
134 retArr.add("null Node object passed to showPropertiesForNode()\n");
138 Object ob = v.<Object>property("aai-node-type").orElse(null);
143 nodeType = ob.toString();
146 retArr.add(" AAINodeType/VtxID for this Node = [" + nodeType + "/" + v.id() + "]");
147 retArr.add(" Property Detail: ");
148 Iterator<VertexProperty<Object>> pI = v.properties();
149 while( pI.hasNext() ){
150 VertexProperty<Object> tp = pI.next();
151 Object val = tp.value();
152 retArr.add("Prop: [" + tp.key() + "], val = [" + val + "] ");