2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.introspection.sideeffect;
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.AAIMultiplePropertiesException;
29 import org.onap.aai.parsers.query.QueryParser;
30 import org.onap.aai.parsers.uri.URIToObject;
31 import org.onap.aai.restcore.util.URITools;
32 import org.onap.aai.schema.enums.PropertyMetadata;
33 import org.onap.aai.serialization.db.DBSerializer;
34 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
36 import javax.ws.rs.core.MultivaluedMap;
37 import java.io.UnsupportedEncodingException;
39 import java.net.URISyntaxException;
40 import java.util.List;
41 import java.util.Map.Entry;
42 import java.util.Optional;
44 public class DataLinkWriter extends SideEffect {
46 public DataLinkWriter(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
47 super(obj, self, dbEngine, serializer);
51 protected PropertyMetadata getPropertyMetadata() {
52 return PropertyMetadata.DATA_LINK;
56 protected void processURI(Optional<String> completeUri, Entry<String, String> entry)
57 throws URISyntaxException, UnsupportedEncodingException, AAIException {
58 if (completeUri.isPresent()) {
59 URI uri = new URI(completeUri.get());
60 MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
61 QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
62 List<Vertex> results = uriQuery.getQueryBuilder().toList();
63 if (results.size() == 1) {
64 if (results.get(0).<Boolean>property(AAIProperties.LINKED).orElse(false) && obj.getValue(entry.getKey()) == null) {
65 //delete vertex because property was removed
66 serializer.delete(results.get(0), "", false);
68 //link vertex that already exists
69 this.addLinkedProperty(results.get(0));
72 if (results.isEmpty()) {
73 //locate previously linked vertex
74 List<Vertex> linkedVertices = uriQuery.getQueryBuilder().getContainerQuery().getVerticesByProperty(AAIProperties.LINKED, true).toList();
75 if (!linkedVertices.isEmpty()) {
76 if (linkedVertices.size() > 1) {
77 throw new AAIMultiplePropertiesException("multiple vertices found for single cardinality propery found when searching " + uri);
79 //found one, remove the linked property because it didn't match the uri
80 linkedVertices.get(0).property(AAIProperties.LINKED).remove();
83 if (obj.getValue(entry.getKey()) != null) {
84 //add new vertex to database if we have values
85 URIToObject parser = new URIToObject(this.latestLoader, uri);
86 Introspector resultObj = parser.getEntity();
87 Vertex newV = serializer.createNewVertex(resultObj);
88 serializer.serializeToDb(resultObj, newV, uriQuery, completeUri.get(), this.latestLoader.getVersion().toString());
89 this.addLinkedProperty(newV);
91 } else if (results.size() > 1) {
92 throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri);
96 //skip processing because no required properties were specified
101 protected boolean replaceWithWildcard() {
105 private void addLinkedProperty(Vertex v) {
106 v.property(AAIProperties.LINKED, true);