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