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