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