2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.aai.introspection.sideeffect;
23 import java.io.UnsupportedEncodingException;
25 import java.net.URISyntaxException;
26 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.Optional;
30 import javax.ws.rs.core.MultivaluedMap;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.openecomp.aai.db.props.AAIProperties;
35 import org.openecomp.aai.exceptions.AAIException;
36 import org.openecomp.aai.introspection.Introspector;
37 import org.openecomp.aai.introspection.sideeffect.exceptions.AAIMultiplePropertiesException;
38 import org.openecomp.aai.parsers.query.QueryParser;
39 import org.openecomp.aai.parsers.uri.URIToObject;
40 import org.openecomp.aai.restcore.util.URITools;
41 import org.openecomp.aai.schema.enums.PropertyMetadata;
42 import org.openecomp.aai.serialization.db.DBSerializer;
43 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
45 public class DataLinkWriter extends SideEffect {
47 public DataLinkWriter(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
48 super(obj, self, dbEngine, serializer);
52 protected PropertyMetadata getPropertyMetadata() {
53 return PropertyMetadata.DATA_LINK;
57 protected void processURI(Optional<String> completeUri, Entry<String, String> entry)
58 throws URISyntaxException, UnsupportedEncodingException, AAIException {
59 if (completeUri.isPresent()) {
60 URI uri = new URI(completeUri.get());
61 MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
62 QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
63 List<Vertex> results = uriQuery.getQueryBuilder().toList();
64 if (results.size() == 1) {
65 if (results.get(0).<Boolean>property(AAIProperties.LINKED).orElse(false) && obj.getValue(entry.getKey()) == null) {
66 //delete vertex because property was removed
67 serializer.delete(results.get(0), "", false);
69 //link vertex that already exists
70 this.addLinkedProperty(results.get(0));
73 if (results.isEmpty()) {
74 //locate previously linked vertex
75 List<Vertex> linkedVertices = uriQuery.getQueryBuilder().getContainerQuery().getVerticesByProperty(AAIProperties.LINKED, true).toList();
76 if (!linkedVertices.isEmpty()) {
77 if (linkedVertices.size() > 1) {
78 throw new AAIMultiplePropertiesException("multiple vertices found for single cardinality propery found when searching " + uri);
80 //found one, remove the linked property because it didn't match the uri
81 linkedVertices.get(0).property(AAIProperties.LINKED).remove();
84 if (obj.getValue(entry.getKey()) != null) {
85 //add new vertex to database if we have values
86 URIToObject parser = new URIToObject(this.latestLoader, uri);
87 Introspector resultObj = parser.getEntity();
88 Vertex newV = serializer.createNewVertex(resultObj);
89 serializer.serializeToDb(resultObj, newV, uriQuery, completeUri.get(), this.latestLoader.getVersion().toString());
90 this.addLinkedProperty(newV);
92 } else if (results.size() > 1) {
93 throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri);
97 //skip processing because no required properties were specified
102 protected boolean replaceWithWildcard() {
106 private void addLinkedProperty(Vertex v) {
107 v.property(AAIProperties.LINKED, true);