2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.introspection.sideeffect;
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.AAIMultiplePropertiesException;
27 import org.onap.aai.parsers.query.QueryParser;
28 import org.onap.aai.parsers.uri.URIToObject;
29 import org.onap.aai.restcore.util.URITools;
30 import org.onap.aai.schema.enums.PropertyMetadata;
31 import org.onap.aai.serialization.db.DBSerializer;
32 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
34 import javax.ws.rs.core.MultivaluedMap;
35 import java.io.UnsupportedEncodingException;
37 import java.net.URISyntaxException;
38 import java.util.List;
39 import java.util.Map.Entry;
40 import java.util.Optional;
42 public class DataLinkWriter extends SideEffect {
44 public DataLinkWriter(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
45 super(obj, self, dbEngine, serializer);
49 protected PropertyMetadata getPropertyMetadata() {
50 return PropertyMetadata.DATA_LINK;
54 protected void processURI(Optional<String> completeUri, Entry<String, String> entry)
55 throws URISyntaxException, UnsupportedEncodingException, AAIException {
56 if (completeUri.isPresent()) {
57 URI uri = new URI(completeUri.get());
58 MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
59 QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
60 List<Vertex> results = uriQuery.getQueryBuilder().toList();
61 if (results.size() == 1) {
62 if (results.get(0).<Boolean>property(AAIProperties.LINKED).orElse(false) && obj.getValue(entry.getKey()) == null) {
63 //delete vertex because property was removed
64 serializer.delete(results.get(0), "", false);
66 //link vertex that already exists
67 this.addLinkedProperty(results.get(0));
70 if (results.isEmpty()) {
71 //locate previously linked vertex
72 List<Vertex> linkedVertices = uriQuery.getQueryBuilder().getContainerQuery().getVerticesByProperty(AAIProperties.LINKED, true).toList();
73 if (!linkedVertices.isEmpty()) {
74 if (linkedVertices.size() > 1) {
75 throw new AAIMultiplePropertiesException("multiple vertices found for single cardinality propery found when searching " + uri);
77 //found one, remove the linked property because it didn't match the uri
78 linkedVertices.get(0).property(AAIProperties.LINKED).remove();
81 if (obj.getValue(entry.getKey()) != null) {
82 //add new vertex to database if we have values
83 URIToObject parser = new URIToObject(this.latestLoader, uri);
84 Introspector resultObj = parser.getEntity();
85 Vertex newV = serializer.createNewVertex(resultObj);
86 serializer.serializeToDb(resultObj, newV, uriQuery, completeUri.get(), this.latestLoader.getVersion().toString());
87 this.addLinkedProperty(newV);
89 } else if (results.size() > 1) {
90 throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri);
94 //skip processing because no required properties were specified
99 protected boolean replaceWithWildcard() {
103 private void addLinkedProperty(Vertex v) {
104 v.property(AAIProperties.LINKED, true);