2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.db;
24 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.Optional;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Introspector;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
40 import org.onap.aai.parsers.query.QueryParser;
41 import org.onap.aai.parsers.relationship.RelationshipToURI;
42 import org.onap.aai.query.builder.QueryBuilder;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
45 public class DbMethHelper {
47 private final Loader loader;
48 private final TransactionalGraphEngine engine;
50 protected DbMethHelper() {
54 public DbMethHelper(Loader loader, TransactionalGraphEngine engine) {
61 * @param map - form [{type}.{propname}:{value}]
63 * @throws UnsupportedEncodingException
64 * @throws AAIException
66 public Optional<Vertex> searchVertexByIdentityMap(String type, Map<String, Object> map) throws AAIException {
68 Introspector relationship = constructRelationship(type, map);
69 RelationshipToURI parser;
71 QueryParser queryParser;
73 parser = new RelationshipToURI(loader, relationship);
74 uri = parser.getUri();
75 queryParser = this.engine.getQueryBuilder().createQueryFromURI(uri);
76 } catch (UnsupportedEncodingException e) {
77 throw new AAIException("AAI_3000");
80 List<Vertex> results = queryParser.getQueryBuilder().toList();
82 return reduceToSingleVertex(results, map);
87 * @param map - form [{propname}:{value}]
89 * @throws AAIException
91 public Optional<Vertex> locateUniqueVertex(String type, Map<String, Object> map) throws AAIException {
93 return reduceToSingleVertex(locateUniqueVertices(type, map), map);
96 public List<Vertex> locateUniqueVertices(String type, Map<String, Object> map) throws AAIException {
97 Introspector obj = this.createIntrospectorFromMap(type, map);
98 QueryBuilder builder = this.engine.getQueryBuilder().exactMatchQuery(obj);
100 return builder.toList();
102 private Introspector constructRelationship(String type, Map<String, Object> map) throws AAIUnknownObjectException {
103 final Introspector relationship = loader.introspectorFromName("relationship");
104 relationship.setValue("related-to", type);
105 final List<Object> data = relationship.getValue("relationship-data");
106 for (Entry<String, Object> entry : map.entrySet()) {
107 final Introspector dataObj = loader.introspectorFromName("relationship-data");
108 dataObj.setValue("relationship-key", entry.getKey());
109 dataObj.setValue("relationship-value", entry.getValue());
110 data.add(dataObj.getUnderlyingObject());
116 private Introspector createIntrospectorFromMap(String targetNodeType, Map<String, Object> propHash) throws AAIUnknownObjectException {
117 final Introspector result = loader.introspectorFromName(targetNodeType);
118 for (Entry<String, Object> entry : propHash.entrySet()) {
119 result.setValue(entry.getKey(), entry.getValue());
124 private Optional<Vertex> reduceToSingleVertex(List<Vertex> vertices, Map<String, Object> map) throws AAIException {
125 if (vertices.isEmpty()){
126 return Optional.empty();
127 } else if (vertices.size() > 1) {
128 throw new AAIException("AAI_6112", "More than one Node found by getUniqueNode for params: " + map);
131 return Optional.of(vertices.get(0));
133 public List<String> getVertexProperties(Vertex v) {
134 List<String> retArr = new ArrayList<>();
136 retArr.add("null Node object passed to showPropertiesForNode()\n");
140 Object ob = v.<Object>property("aai-node-type").orElse(null);
145 nodeType = ob.toString();
148 retArr.add(" AAINodeType/VtxID for this Node = [" + nodeType + "/" + v.id() + "]");
149 retArr.add(" Property Detail: ");
150 Iterator<VertexProperty<Object>> pI = v.properties();
151 while( pI.hasNext() ){
152 VertexProperty<Object> tp = pI.next();
153 Object val = tp.value();
154 retArr.add("Prop: [" + tp.key() + "], val = [" + val + "] ");