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