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 com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.config.SpringContextAware;
26 import org.onap.aai.db.props.AAIProperties;
27 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
28 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.*;
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;
35 import org.onap.aai.setup.SchemaVersions;
38 import java.io.UnsupportedEncodingException;
39 import java.net.URISyntaxException;
41 import java.util.Map.Entry;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
45 public abstract class SideEffect {
47 protected static final Pattern template = Pattern.compile("\\{(.*?)\\}");
48 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SideEffect.class);
50 protected final Introspector obj;
51 protected final TransactionalGraphEngine dbEngine;
52 protected final DBSerializer serializer;
53 protected final Loader latestLoader;
54 protected final Vertex self;
56 protected Set<String> templateKeys = new HashSet<>();
58 public SideEffect (Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
60 this.dbEngine = dbEngine;
61 this.serializer = serializer;
63 this.latestLoader = LoaderUtil.getLatestVersion();
66 protected void execute() throws UnsupportedEncodingException, URISyntaxException, AAIException {
67 final Map<String, String> properties = this.findPopertiesWithMetadata(obj, this.getPropertyMetadata());
68 for (Entry<String, String> entry : properties.entrySet()) {
69 Optional<String> populatedUri = this.replaceTemplates(obj, entry.getValue());
70 Optional<String> completeUri = this.resolveRelativePath(populatedUri);
72 this.processURI(completeUri, entry);
73 } catch (EdgeRuleNotFoundException | AmbiguousRuleChoiceException e) {
74 logger.warn("Unable to execute the side effect {} due to ", e, this.getClass().getName());
79 protected Map<String, String> findPopertiesWithMetadata(Introspector obj, PropertyMetadata metadata) {
80 final Map<String, String> result = new HashMap<>();
81 for (String prop : obj.getProperties()) {
82 final Map<PropertyMetadata, String> map = obj.getPropertyMetadata(prop);
83 if (map.containsKey(metadata)) {
84 result.put(prop, map.get(metadata));
90 protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
92 final Map<String, String> result = new HashMap<>();
93 final Set<String> missing = new LinkedHashSet<>();
94 Matcher m = template.matcher(uriString);
97 String propName = m.group(1);
98 String value = obj.getValue(propName);
101 result.put(propName, value);
103 if (replaceWithWildcard()) {
104 result.put(propName, "*");
106 missing.add(propName);
110 if (!missing.isEmpty() && (properties != missing.size())) {
111 throw new AAIMissingRequiredPropertyException("Cannot complete " + this.getPropertyMetadata().toString() + " uri. Missing properties " + missing);
116 protected Optional<String> replaceTemplates(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
117 String result = uriString;
118 final Map<String, String> propMap = this.findProperties(obj, uriString);
119 if (propMap.isEmpty()) {
120 return Optional.empty();
122 for (Entry<String, String> entry : propMap.entrySet()) {
123 templateKeys.add(entry.getKey());
124 result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
126 //drop out wildcards if they exist
127 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
128 return Optional.of(result);
131 private Optional<String> resolveRelativePath(Optional<String> populatedUri) throws UnsupportedEncodingException {
132 if (!populatedUri.isPresent()) {
133 return Optional.empty();
135 return Optional.of(populatedUri.get().replaceFirst("\\./", this.serializer.getURIForVertex(self) + "/"));
139 protected abstract boolean replaceWithWildcard();
140 protected abstract PropertyMetadata getPropertyMetadata();
141 protected abstract void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException;