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