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