AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / sideeffect / DataLinkWriter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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.onap.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 import org.onap.aai.db.props.AAIProperties;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.sideeffect.exceptions.AAIMultiplePropertiesException;
37 import org.onap.aai.parsers.query.QueryParser;
38 import org.onap.aai.parsers.uri.URIToObject;
39 import org.onap.aai.restcore.util.URITools;
40 import org.onap.aai.schema.enums.PropertyMetadata;
41 import org.onap.aai.serialization.db.DBSerializer;
42 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
43
44 public class DataLinkWriter extends SideEffect {
45
46     public DataLinkWriter(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
47         super(obj, self, dbEngine, serializer);
48     }
49
50     @Override
51     protected PropertyMetadata getPropertyMetadata() {
52         return PropertyMetadata.DATA_LINK;
53     }
54
55     @Override
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)
65                         && 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()
76                             .getVerticesByProperty(AAIProperties.LINKED, true).toList();
77                     if (!linkedVertices.isEmpty()) {
78                         if (linkedVertices.size() > 1) {
79                             throw new AAIMultiplePropertiesException(
80                                     "multiple vertices found for single cardinality propery found when searching "
81                                             + uri);
82                         } else {
83                             // found one, remove the linked property because it didn't match the uri
84                             linkedVertices.get(0).property(AAIProperties.LINKED).remove();
85                         }
86                     }
87                     if (obj.getValue(entry.getKey()) != null) {
88                         // add new vertex to database if we have values
89                         URIToObject parser = new URIToObject(this.latestLoader, uri);
90                         Introspector resultObj = parser.getEntity();
91                         Vertex newV = serializer.createNewVertex(resultObj);
92                         serializer.serializeToDb(resultObj, newV, uriQuery, completeUri.get(),
93                                 this.latestLoader.getVersion().toString());
94                         this.addLinkedProperty(newV);
95                     }
96                 } else if (results.size() > 1) {
97                     throw new AAIMultiplePropertiesException(
98                             "multiple values of " + entry.getKey() + " found when searching " + uri);
99                 }
100             }
101         } else {
102             // skip processing because no required properties were specified
103         }
104     }
105
106     @Override
107     protected boolean replaceWithWildcard() {
108         return true;
109     }
110
111     private void addLinkedProperty(Vertex v) {
112         v.property(AAIProperties.LINKED, true);
113     }
114
115 }