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