92402e3223f570aea6ac19082577e28c9b69e9ad
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / introspection / sideeffect / SideEffect.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
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
21 package org.openecomp.aai.introspection.sideeffect;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
25 import java.util.HashMap;
26 import java.util.LinkedHashSet;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Optional;
30 import java.util.Set;
31 import java.util.regex.Matcher;
32 import java.util.regex.Pattern;
33
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35
36 import org.openecomp.aai.db.props.AAIProperties;
37 import org.openecomp.aai.exceptions.AAIException;
38 import org.openecomp.aai.introspection.Introspector;
39 import org.openecomp.aai.introspection.Loader;
40 import org.openecomp.aai.introspection.LoaderFactory;
41 import org.openecomp.aai.introspection.ModelType;
42 import org.openecomp.aai.introspection.sideeffect.exceptions.AAIMissingRequiredPropertyException;
43 import org.openecomp.aai.schema.enums.PropertyMetadata;
44 import org.openecomp.aai.serialization.db.DBSerializer;
45 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
46
47 public abstract class SideEffect {
48
49         protected static final Pattern template = Pattern.compile("\\{(.*?)\\}");
50         protected final Introspector obj;
51         protected final TransactionalGraphEngine dbEngine;
52         protected final DBSerializer serializer;
53         protected final Loader latestLoader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
54         protected final Vertex self;
55         public SideEffect (Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
56                 this.obj = obj;
57                 this.dbEngine = dbEngine;
58                 this.serializer = serializer;
59                 this.self = self;
60         }
61
62         protected void execute() throws UnsupportedEncodingException, URISyntaxException, AAIException {
63                 final Map<String, String> properties = this.findPopertiesWithMetadata(obj, this.getPropertyMetadata());
64                 for (Entry<String, String> entry : properties.entrySet()) {
65                         Optional<String> populatedUri = this.replaceTemplates(obj, entry.getValue());
66                         Optional<String> completeUri = this.resolveRelativePath(populatedUri);
67                         this.processURI(completeUri, entry);
68                 }
69         }
70
71         protected Map<String, String> findPopertiesWithMetadata(Introspector obj, PropertyMetadata metadata) {
72                 final Map<String, String> result = new HashMap<>();
73                 for (String prop : obj.getProperties()) {
74                         final Map<PropertyMetadata, String> map = obj.getPropertyMetadata(prop);
75                         if (map.containsKey(metadata)) {
76                                 result.put(prop, map.get(metadata));
77                         }
78                 }
79                 return result;
80         }
81         
82         protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
83                 
84                 final Map<String, String> result = new HashMap<>();
85                 final Set<String> missing = new LinkedHashSet<>();
86                 Matcher m = template.matcher(uriString);
87                 int properties = 0;
88                 while (m.find()) {
89                         String propName = m.group(1);
90                         String value = obj.getValue(propName);
91                         properties++;
92                         if (value != null) {
93                                 result.put(propName, value);
94                         } else {
95                                 if (replaceWithWildcard()) {
96                                         result.put(propName, "*");
97                                 }
98                                 missing.add(propName);
99                         }
100                 }
101                 
102                 if (!missing.isEmpty() && (properties != missing.size())) {
103                         throw new AAIMissingRequiredPropertyException("Cannot complete " + this.getPropertyMetadata().toString() + " uri. Missing properties " + missing);
104                 }
105                 return result;
106         }
107         
108         private Optional<String> replaceTemplates(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
109                 String result = uriString;
110                 final Map<String, String> propMap = this.findProperties(obj, uriString);
111                 if (propMap.isEmpty()) {
112                         return Optional.empty();
113                 }
114                 for (Entry<String, String> entry : propMap.entrySet()) {
115                         result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
116                 }
117                 //drop out wildcards if they exist
118                 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
119                 return Optional.of(result);
120         }
121         
122         private Optional<String> resolveRelativePath(Optional<String> populatedUri) throws UnsupportedEncodingException {
123                 if (!populatedUri.isPresent()) {
124                         return Optional.empty();
125                 } else {
126                         return Optional.of(populatedUri.get().replaceFirst("\\./", this.serializer.getURIForVertex(self) + "/"));
127                 }
128         }
129         
130         protected abstract boolean replaceWithWildcard();
131         protected abstract PropertyMetadata getPropertyMetadata();
132         protected abstract void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException;
133 }