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