Initial commit with all the necessary files
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / introspection / sideeffect / DataLinkReader.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.introspection.sideeffect;
22
23 import org.apache.tinkerpop.gremlin.structure.Vertex;
24 import org.openecomp.aai.db.props.AAIProperties;
25 import org.openecomp.aai.exceptions.AAIException;
26 import org.openecomp.aai.introspection.Introspector;
27 import org.openecomp.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
28 import org.openecomp.aai.parsers.query.QueryParser;
29 import org.openecomp.aai.restcore.util.URITools;
30 import org.openecomp.aai.schema.enums.PropertyMetadata;
31 import org.openecomp.aai.serialization.db.DBSerializer;
32 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
33
34 import javax.ws.rs.core.MultivaluedMap;
35 import java.io.UnsupportedEncodingException;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Map.Entry;
42 import java.util.Optional;
43 import java.util.regex.Matcher;
44
45 public class DataLinkReader extends SideEffect {
46         
47         public DataLinkReader(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
48                 super(obj, self, dbEngine, serializer);
49         }
50         
51         @Override
52         protected boolean replaceWithWildcard() {
53                 return true;
54         }
55
56         @Override
57         protected PropertyMetadata getPropertyMetadata() {
58                 return PropertyMetadata.DATA_LINK;
59         }
60
61         @Override
62         protected void processURI(Optional<String> completeUri, Entry<String, String> entry)
63                         throws URISyntaxException, UnsupportedEncodingException, AAIException {
64
65                 if (completeUri.isPresent()) {
66                         URI uri = new URI(completeUri.get());
67                         MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
68                         QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
69                         List<Vertex> results = uriQuery.getQueryBuilder().getVerticesByProperty(AAIProperties.LINKED, true).toList();
70                         if (results.size() == 1) {
71                                 if (results.get(0).<Boolean>property(AAIProperties.LINKED).orElse(false) && obj.getValue(entry.getKey()) == null) {
72                                         obj.setValue(entry.getKey(), results.get(0).property(entry.getKey()).orElse(null));
73                                 }
74                         } else {
75                         //log something about not being able to return any values because there was more than one
76                         }
77                 }
78         }
79         
80         /**
81          * always fuzzy search on reads
82          */
83         @Override
84         protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
85                 
86                 final Map<String, String> result = new HashMap<>();
87                 Matcher m = template.matcher(uriString);
88                 while (m.find()) {
89                         String propName = m.group(1);
90                         if (replaceWithWildcard()) {
91                                 result.put(propName, "*");
92                         }
93                 }
94                 return result;
95         }
96
97 }