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=========================================================
20 package org.onap.aai.introspection.sideeffect;
22 import org.apache.tinkerpop.gremlin.structure.Vertex;
23 import org.onap.aai.db.props.AAIProperties;
24 import org.onap.aai.exceptions.AAIException;
25 import org.onap.aai.introspection.Introspector;
26 import org.onap.aai.introspection.Loader;
27 import org.onap.aai.introspection.LoaderFactory;
28 import org.onap.aai.introspection.ModelType;
29 import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
30 import org.onap.aai.schema.enums.PropertyMetadata;
31 import org.onap.aai.serialization.db.DBSerializer;
32 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
34 import java.io.UnsupportedEncodingException;
35 import java.net.URISyntaxException;
37 import java.util.Map.Entry;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
41 public abstract class SideEffect {
43 protected static final Pattern template = Pattern.compile("\\{(.*?)\\}");
44 protected final Introspector obj;
45 protected final TransactionalGraphEngine dbEngine;
46 protected final DBSerializer serializer;
47 protected final Loader latestLoader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
48 protected final Vertex self;
49 public SideEffect (Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
51 this.dbEngine = dbEngine;
52 this.serializer = serializer;
56 protected void execute() throws UnsupportedEncodingException, URISyntaxException, AAIException {
57 final Map<String, String> properties = this.findPopertiesWithMetadata(obj, this.getPropertyMetadata());
58 for (Entry<String, String> entry : properties.entrySet()) {
59 Optional<String> populatedUri = this.replaceTemplates(obj, entry.getValue());
60 Optional<String> completeUri = this.resolveRelativePath(populatedUri);
61 this.processURI(completeUri, entry);
65 protected Map<String, String> findPopertiesWithMetadata(Introspector obj, PropertyMetadata metadata) {
66 final Map<String, String> result = new HashMap<>();
67 for (String prop : obj.getProperties()) {
68 final Map<PropertyMetadata, String> map = obj.getPropertyMetadata(prop);
69 if (map.containsKey(metadata)) {
70 result.put(prop, map.get(metadata));
76 protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
78 final Map<String, String> result = new HashMap<>();
79 final Set<String> missing = new LinkedHashSet<>();
80 Matcher m = template.matcher(uriString);
83 String propName = m.group(1);
84 String value = obj.getValue(propName);
87 result.put(propName, value);
89 if (replaceWithWildcard()) {
90 result.put(propName, "*");
92 missing.add(propName);
96 if (!missing.isEmpty() && (properties != missing.size())) {
97 throw new AAIMissingRequiredPropertyException("Cannot complete " + this.getPropertyMetadata().toString() + " uri. Missing properties " + missing);
102 private Optional<String> replaceTemplates(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
103 String result = uriString;
104 final Map<String, String> propMap = this.findProperties(obj, uriString);
105 if (propMap.isEmpty()) {
106 return Optional.empty();
108 for (Entry<String, String> entry : propMap.entrySet()) {
109 result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
111 //drop out wildcards if they exist
112 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
113 return Optional.of(result);
116 private Optional<String> resolveRelativePath(Optional<String> populatedUri) throws UnsupportedEncodingException {
117 if (!populatedUri.isPresent()) {
118 return Optional.empty();
120 return Optional.of(populatedUri.get().replaceFirst("\\./", this.serializer.getURIForVertex(self) + "/"));
124 protected abstract boolean replaceWithWildcard();
125 protected abstract PropertyMetadata getPropertyMetadata();
126 protected abstract void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException;