a3e4d71f60283407f684ab52ec7b993b419b73a7
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / db / DbMethHelper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.db;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Optional;
32
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
35
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;
44
45 public class DbMethHelper {
46
47         private final Loader loader;
48         private final TransactionalGraphEngine engine;
49         
50         protected DbMethHelper() {
51                 this.loader = null;
52                 this.engine = null;
53         }
54         public DbMethHelper(Loader loader, TransactionalGraphEngine engine) {
55                 this.loader = loader;
56                 this.engine = engine;
57         }
58         /**
59          * 
60          * @param type
61          * @param map - form [{type}.{propname}:{value}]
62          * @return
63          * @throws UnsupportedEncodingException
64          * @throws AAIException
65          */
66         public Optional<Vertex> searchVertexByIdentityMap(String type, Map<String, Object> map) throws AAIException {
67                 
68                 Introspector relationship = constructRelationship(type, map);
69                 RelationshipToURI parser;
70                 URI uri;
71                 QueryParser queryParser;
72                 try {
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");
78                 }
79                 
80                 List<Vertex> results = queryParser.getQueryBuilder().toList();
81                 
82                 return reduceToSingleVertex(results, map);
83         }
84         
85         /**
86          * @param type
87          * @param map - form [{propname}:{value}]
88          * @return
89          * @throws AAIException
90          */
91         public Optional<Vertex> locateUniqueVertex(String type, Map<String, Object> map) throws AAIException {
92         
93                 return reduceToSingleVertex(locateUniqueVertices(type, map), map);
94         }
95         
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);
99                 
100                 return builder.toList();
101         }
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());
111                 }
112                 
113                 return relationship;
114         }
115         
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());
120                 }
121                 return result;
122         }
123         
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);
129                 }
130                 
131                 return Optional.of(vertices.get(0));
132         }
133         public List<String> getVertexProperties(Vertex v) {
134                 List<String> retArr = new ArrayList<>();
135                 if( v == null ){
136                         retArr.add("null Node object passed to showPropertiesForNode()\n");
137                 }
138                 else {
139                         String nodeType;
140                         Object ob = v.<Object>property("aai-node-type").orElse(null);
141                         if( ob == null ){
142                                 nodeType = "null";
143                         }
144                         else{
145                                 nodeType = ob.toString();
146                         }
147                         
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 + "] ");
155                         }
156                 }
157                 return retArr;
158         }
159 }