09a1ce1896d0234b1ac7da3c0b9f783709291b36
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / introspection / sideeffect / DataLinkWriter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
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
21 package org.openecomp.aai.introspection.sideeffect;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.Optional;
29
30 import javax.ws.rs.core.MultivaluedMap;
31
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33
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;
44
45 public class DataLinkWriter extends SideEffect {
46
47         public DataLinkWriter(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
48                 super(obj, self, dbEngine, serializer);
49         }
50
51         @Override
52         protected PropertyMetadata getPropertyMetadata() {
53                 return PropertyMetadata.DATA_LINK;
54         }
55
56         @Override
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);
68                                 } else {
69                                         //link vertex that already exists
70                                         this.addLinkedProperty(results.get(0));
71                                 }
72                         } else {
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);
79                                                 } else {
80                                                         //found one, remove the linked property because it didn't match the uri
81                                                         linkedVertices.get(0).property(AAIProperties.LINKED).remove();
82                                                 }
83                                         }
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);
91                                         }
92                                 } else if (results.size() > 1) {
93                                         throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri);
94                                 }
95                         }
96                 } else {
97                         //skip processing because no required properties were specified
98                 }
99         }
100         
101         @Override
102         protected boolean replaceWithWildcard() {
103                 return true;
104         }
105         
106         private void addLinkedProperty(Vertex v) {
107                 v.property(AAIProperties.LINKED, true);
108         }
109
110
111 }