085206084f2b37e07a7666266143bfb9a52eb3c5
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / sideeffect / SideEffect.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.introspection.sideeffect;
23
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;
35
36 import java.io.UnsupportedEncodingException;
37 import java.net.URISyntaxException;
38 import java.util.*;
39 import java.util.Map.Entry;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42
43 public abstract class SideEffect {
44
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) {
52                 this.obj = obj;
53                 this.dbEngine = dbEngine;
54                 this.serializer = serializer;
55                 this.self = self;
56         }
57
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);
64                 }
65         }
66
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));
73                         }
74                 }
75                 return result;
76         }
77         
78         protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
79                 
80                 final Map<String, String> result = new HashMap<>();
81                 final Set<String> missing = new LinkedHashSet<>();
82                 Matcher m = template.matcher(uriString);
83                 int properties = 0;
84                 while (m.find()) {
85                         String propName = m.group(1);
86                         String value = obj.getValue(propName);
87                         properties++;
88                         if (value != null) {
89                                 result.put(propName, value);
90                         } else {
91                                 if (replaceWithWildcard()) {
92                                         result.put(propName, "*");
93                                 }
94                                 missing.add(propName);
95                         }
96                 }
97                 
98                 if (!missing.isEmpty() && (properties != missing.size())) {
99                         throw new AAIMissingRequiredPropertyException("Cannot complete " + this.getPropertyMetadata().toString() + " uri. Missing properties " + missing);
100                 }
101                 return result;
102         }
103         
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();
109                 }
110                 for (Entry<String, String> entry : propMap.entrySet()) {
111                         result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
112                 }
113                 //drop out wildcards if they exist
114                 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
115                 return Optional.of(result);
116         }
117         
118         private Optional<String> resolveRelativePath(Optional<String> populatedUri) throws UnsupportedEncodingException {
119                 if (!populatedUri.isPresent()) {
120                         return Optional.empty();
121                 } else {
122                         return Optional.of(populatedUri.get().replaceFirst("\\./", this.serializer.getURIForVertex(self) + "/"));
123                 }
124         }
125         
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;
129 }