2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.introspection.sideeffect;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.db.props.AAIProperties;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.introspection.Introspector;
28 import org.onap.aai.introspection.Loader;
29 import org.onap.aai.introspection.LoaderFactory;
30 import org.onap.aai.introspection.ModelType;
31 import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
32 import org.onap.aai.schema.enums.PropertyMetadata;
33 import org.onap.aai.serialization.db.DBSerializer;
34 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
36 import java.io.UnsupportedEncodingException;
37 import java.net.URISyntaxException;
39 import java.util.Map.Entry;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
43 public abstract class SideEffect {
45 protected static final Pattern template = Pattern.compile("\\{(.*?)\\}");
46 protected final Introspector obj;
47 protected final TransactionalGraphEngine dbEngine;
48 protected final DBSerializer serializer;
49 protected final Loader latestLoader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
50 protected final Vertex self;
51 public SideEffect (Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
53 this.dbEngine = dbEngine;
54 this.serializer = serializer;
58 protected void execute() throws UnsupportedEncodingException, URISyntaxException, AAIException {
59 final Map<String, String> properties = this.findPopertiesWithMetadata(obj, this.getPropertyMetadata());
60 for (Entry<String, String> entry : properties.entrySet()) {
61 Optional<String> populatedUri = this.replaceTemplates(obj, entry.getValue());
62 Optional<String> completeUri = this.resolveRelativePath(populatedUri);
63 this.processURI(completeUri, entry);
67 protected Map<String, String> findPopertiesWithMetadata(Introspector obj, PropertyMetadata metadata) {
68 final Map<String, String> result = new HashMap<>();
69 for (String prop : obj.getProperties()) {
70 final Map<PropertyMetadata, String> map = obj.getPropertyMetadata(prop);
71 if (map.containsKey(metadata)) {
72 result.put(prop, map.get(metadata));
78 protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
80 final Map<String, String> result = new HashMap<>();
81 final Set<String> missing = new LinkedHashSet<>();
82 Matcher m = template.matcher(uriString);
85 String propName = m.group(1);
86 String value = obj.getValue(propName);
89 result.put(propName, value);
91 if (replaceWithWildcard()) {
92 result.put(propName, "*");
94 missing.add(propName);
98 if (!missing.isEmpty() && (properties != missing.size())) {
99 throw new AAIMissingRequiredPropertyException("Cannot complete " + this.getPropertyMetadata().toString() + " uri. Missing properties " + missing);
104 private Optional<String> replaceTemplates(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
105 String result = uriString;
106 final Map<String, String> propMap = this.findProperties(obj, uriString);
107 if (propMap.isEmpty()) {
108 return Optional.empty();
110 for (Entry<String, String> entry : propMap.entrySet()) {
111 result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
113 //drop out wildcards if they exist
114 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
115 return Optional.of(result);
118 private Optional<String> resolveRelativePath(Optional<String> populatedUri) throws UnsupportedEncodingException {
119 if (!populatedUri.isPresent()) {
120 return Optional.empty();
122 return Optional.of(populatedUri.get().replaceFirst("\\./", this.serializer.getURIForVertex(self) + "/"));
126 protected abstract boolean replaceWithWildcard();
127 protected abstract PropertyMetadata getPropertyMetadata();
128 protected abstract void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException;