d002f06889eebc5c7141e6fcd60c0002c1f2ea7c
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / sideeffect / DataCopy.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.introspection.sideeffect;
23
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.exceptions.AAIException;
26 import org.onap.aai.introspection.Introspector;
27 import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
28 import org.onap.aai.introspection.sideeffect.exceptions.AAIMultiplePropertiesException;
29 import org.onap.aai.parsers.query.QueryParser;
30 import org.onap.aai.restcore.util.URITools;
31 import org.onap.aai.schema.enums.PropertyMetadata;
32 import org.onap.aai.serialization.db.DBSerializer;
33 import org.onap.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.Collections;
40 import java.util.List;
41 import java.util.Map.Entry;
42 import java.util.Objects;
43 import java.util.Optional;
44
45
46 public class DataCopy extends SideEffect {
47
48         
49         public DataCopy(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
50                 super(obj, self, dbEngine, serializer);
51         }
52         
53         @Override
54         protected void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException {
55                 if (completeUri.isPresent()) {
56                         URI uri = new URI(completeUri.get());
57                         MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
58                         QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
59                         List<Vertex> results = uriQuery.getQueryBuilder().toList();
60                         Introspector resultObj = this.latestLoader.introspectorFromName(uriQuery.getResultType());
61                         if (results.size() == 1) {
62                                 serializer.dbToObject(Collections.singletonList(results.get(0)), resultObj, 0, true, "false");
63                                 try {
64                                         obj.setValue(entry.getKey(), Objects.requireNonNull(resultObj.getValue(uri.getFragment()), uri.getFragment() + " was null"));
65                                 } catch (NullPointerException e) {
66                                         throw new AAIMissingRequiredPropertyException("property " + uri.getFragment() + " not found at " + uri);
67                                 }
68                         } else {
69                                 if (results.isEmpty()) {
70                                         throw new AAIException("AAI_6114", "object located at " + uri + " not found");
71                                 } else if (results.size() > 1) {
72                                         throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri);
73                                 }
74                         }
75                 } else {
76                         //skip processing because no required properties were specified
77                 }
78         }
79
80         @Override
81         protected PropertyMetadata getPropertyMetadata() {
82                 return PropertyMetadata.DATA_COPY;
83         }
84
85         @Override
86         protected boolean replaceWithWildcard() {
87                 return false;
88         }
89         
90 }