2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.introspection.sideeffect;
23 import com.google.common.collect.Multimap;
25 import java.io.UnsupportedEncodingException;
27 import java.net.URISyntaxException;
29 import java.util.Map.Entry;
31 import javax.ws.rs.core.MultivaluedMap;
33 import org.apache.tinkerpop.gremlin.structure.Edge;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
36 import org.onap.aai.config.SpringContextAware;
37 import org.onap.aai.db.props.AAIProperties;
38 import org.onap.aai.edges.EdgeIngestor;
39 import org.onap.aai.edges.EdgeRule;
40 import org.onap.aai.edges.EdgeRuleQuery;
41 import org.onap.aai.edges.enums.EdgeType;
42 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
43 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
44 import org.onap.aai.exceptions.AAIException;
45 import org.onap.aai.introspection.*;
46 import org.onap.aai.introspection.sideeffect.exceptions.AAIMultiplePropertiesException;
47 import org.onap.aai.parsers.query.QueryParser;
48 import org.onap.aai.restcore.util.URITools;
49 import org.onap.aai.schema.enums.PropertyMetadata;
50 import org.onap.aai.serialization.db.DBSerializer;
51 import org.onap.aai.serialization.db.EdgeSerializer;
52 import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException;
53 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
55 public class PrivateEdge extends SideEffect {
57 public PrivateEdge(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
58 super(obj, self, dbEngine, serializer);
62 protected void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException,
63 UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
64 if (completeUri.isPresent()) {
65 process(completeUri, entry);
67 // Check if the vertex self has the template keys or the db aliased keys
68 // If it does check if the self vertex has a edge to that model
69 // If it does, then remove the edge since the update happened and doesn't have required props anymore
70 // "service-design-and-creation/models/model/{model-invariant-id}/model-vers/model-ver/{model-version-id}"
71 // If the vertex does have
72 Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY,
74 Introspector introspector = loader.introspectorFromName(obj.getDbName());
75 List<Vertex> vertices = new ArrayList<>();
77 Introspector curObj = serializer.dbToObject(vertices, introspector, 0, true, "false");
78 Optional<String> populatedUri = this.replaceTemplates(curObj, entry.getValue());
79 process(populatedUri, entry);
83 private void process(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException,
84 UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
85 if (completeUri.isPresent()) {
86 URI uri = new URI(completeUri.get());
87 MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
88 QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
89 List<Vertex> results = uriQuery.getQueryBuilder().toList();
90 if (results.size() == 1) {
91 Vertex otherVertex = results.get(0);
92 VertexProperty otherVProperty = otherVertex.property("aai-node-type");
94 if (otherVProperty.isPresent()) {
96 EdgeRuleQuery edgeQuery =
97 new EdgeRuleQuery.Builder(obj.getName(), otherVProperty.value().toString())
98 .edgeType(EdgeType.COUSIN).setPrivate(true).build();
99 EdgeIngestor edgeIngestor = serializer.getEdgeIngestor();
100 EdgeSerializer edgeSerializer = serializer.getEdgeSeriailizer();
102 Multimap<String, EdgeRule> edgeRulesMap = edgeIngestor.getRules(edgeQuery);
104 if (edgeRulesMap.isEmpty()) {
105 String message = String.format("Unable to find edge between %s and %s", obj.getName(),
106 otherVProperty.value().toString());
107 throw new AAIException("AAI_6127", message);
108 } else if (edgeRulesMap.size() > 1) {
109 String message = String.format("Found multiple edges between %s and %s", obj.getName(),
110 otherVProperty.value().toString());
111 throw new EdgeMultiplicityException(message);
114 for (Entry<String, EdgeRule> edgeEntry : edgeRulesMap.entries()) {
115 EdgeRule edgeRule = edgeIngestor.getRule(edgeQuery);
116 Iterator<Edge> edges = self.edges(edgeRule.getDirection(), edgeRule.getLabel().toString());
117 if (edges.hasNext()) {
118 Edge edge = edges.next();
119 EdgeStatus status = checkStatus(obj, self);
122 edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self,
123 otherVertex, edgeRule.getLabel());
127 edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self,
128 otherVertex, edgeRule.getLabel());
137 edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self,
138 otherVertex, edgeRule.getLabel());
143 if (results.isEmpty()) {
144 throw new AAIException("AAI_6114", "object located at " + uri + " not found");
145 } else if (results.size() > 1) {
146 throw new AAIMultiplePropertiesException(
147 "multiple values of " + entry.getKey() + " found when searching " + uri);
153 public enum EdgeStatus {
154 CREATED, REMOVED, MODIFIED, UNCHANGED
157 private EdgeStatus checkStatus(Introspector obj, Vertex self) {
159 for (String key : templateKeys) {
160 String currentObjValue = obj.getValue(key);
161 Map<PropertyMetadata, String> map = obj.getPropertyMetadata(key);
162 String oldVertexValue = null;
164 if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
165 oldVertexValue = self.<String>property(key + AAIProperties.DB_ALIAS_SUFFIX).orElse(null);
167 oldVertexValue = self.<String>property(key).orElse(null);
170 if (currentObjValue == null && oldVertexValue == null) {
174 if (currentObjValue == null) {
175 if (oldVertexValue != null) {
176 return EdgeStatus.REMOVED;
180 if (oldVertexValue == null) {
181 if (currentObjValue != null) {
182 return EdgeStatus.CREATED;
186 if (!oldVertexValue.equals(currentObjValue)) {
187 return EdgeStatus.MODIFIED;
191 return EdgeStatus.UNCHANGED;
195 protected PropertyMetadata getPropertyMetadata() {
196 return PropertyMetadata.PRIVATE_EDGE;
200 protected boolean replaceWithWildcard() {