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