2 * ============LICENSE_START=======================================================
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
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.openecomp.aai.introspection.sideeffect;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
25 import java.util.HashMap;
26 import java.util.LinkedHashSet;
28 import java.util.Map.Entry;
29 import java.util.Optional;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.openecomp.aai.db.props.AAIProperties;
37 import org.openecomp.aai.exceptions.AAIException;
38 import org.openecomp.aai.introspection.Introspector;
39 import org.openecomp.aai.introspection.Loader;
40 import org.openecomp.aai.introspection.LoaderFactory;
41 import org.openecomp.aai.introspection.ModelType;
42 import org.openecomp.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
43 import org.openecomp.aai.schema.enums.PropertyMetadata;
44 import org.openecomp.aai.serialization.db.DBSerializer;
45 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
47 public abstract class SideEffect {
49 protected static final Pattern template = Pattern.compile("\\{(.*?)\\}");
50 protected final Introspector obj;
51 protected final TransactionalGraphEngine dbEngine;
52 protected final DBSerializer serializer;
53 protected final Loader latestLoader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
54 protected final Vertex self;
55 public SideEffect (Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
57 this.dbEngine = dbEngine;
58 this.serializer = serializer;
62 protected void execute() throws UnsupportedEncodingException, URISyntaxException, AAIException {
63 final Map<String, String> properties = this.findPopertiesWithMetadata(obj, this.getPropertyMetadata());
64 for (Entry<String, String> entry : properties.entrySet()) {
65 Optional<String> populatedUri = this.replaceTemplates(obj, entry.getValue());
66 Optional<String> completeUri = this.resolveRelativePath(populatedUri);
67 this.processURI(completeUri, entry);
71 protected Map<String, String> findPopertiesWithMetadata(Introspector obj, PropertyMetadata metadata) {
72 final Map<String, String> result = new HashMap<>();
73 for (String prop : obj.getProperties()) {
74 final Map<PropertyMetadata, String> map = obj.getPropertyMetadata(prop);
75 if (map.containsKey(metadata)) {
76 result.put(prop, map.get(metadata));
82 protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
84 final Map<String, String> result = new HashMap<>();
85 final Set<String> missing = new LinkedHashSet<>();
86 Matcher m = template.matcher(uriString);
89 String propName = m.group(1);
90 String value = obj.getValue(propName);
93 result.put(propName, value);
95 if (replaceWithWildcard()) {
96 result.put(propName, "*");
98 missing.add(propName);
102 if (!missing.isEmpty() && (properties != missing.size())) {
103 throw new AAIMissingRequiredPropertyException("Cannot complete " + this.getPropertyMetadata().toString() + " uri. Missing properties " + missing);
108 private Optional<String> replaceTemplates(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
109 String result = uriString;
110 final Map<String, String> propMap = this.findProperties(obj, uriString);
111 if (propMap.isEmpty()) {
112 return Optional.empty();
114 for (Entry<String, String> entry : propMap.entrySet()) {
115 result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
117 //drop out wildcards if they exist
118 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
119 return Optional.of(result);
122 private Optional<String> resolveRelativePath(Optional<String> populatedUri) throws UnsupportedEncodingException {
123 if (!populatedUri.isPresent()) {
124 return Optional.empty();
126 return Optional.of(populatedUri.get().replaceFirst("\\./", this.serializer.getURIForVertex(self) + "/"));
130 protected abstract boolean replaceWithWildcard();
131 protected abstract PropertyMetadata getPropertyMetadata();
132 protected abstract void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException;