[AAI-178 Amsterdam] Make Edge Properties to be
[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 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;
34
35 import java.io.UnsupportedEncodingException;
36 import java.net.URISyntaxException;
37 import java.util.*;
38 import java.util.Map.Entry;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
41
42 public abstract class SideEffect {
43
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) {
51                 this.obj = obj;
52                 this.dbEngine = dbEngine;
53                 this.serializer = serializer;
54                 this.self = self;
55         }
56
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);
63                 }
64         }
65
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));
72                         }
73                 }
74                 return result;
75         }
76         
77         protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
78                 
79                 final Map<String, String> result = new HashMap<>();
80                 final Set<String> missing = new LinkedHashSet<>();
81                 Matcher m = template.matcher(uriString);
82                 int properties = 0;
83                 while (m.find()) {
84                         String propName = m.group(1);
85                         String value = obj.getValue(propName);
86                         properties++;
87                         if (value != null) {
88                                 result.put(propName, value);
89                         } else {
90                                 if (replaceWithWildcard()) {
91                                         result.put(propName, "*");
92                                 }
93                                 missing.add(propName);
94                         }
95                 }
96                 
97                 if (!missing.isEmpty() && (properties != missing.size())) {
98                         throw new AAIMissingRequiredPropertyException("Cannot complete " + this.getPropertyMetadata().toString() + " uri. Missing properties " + missing);
99                 }
100                 return result;
101         }
102         
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();
108                 }
109                 for (Entry<String, String> entry : propMap.entrySet()) {
110                         result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
111                 }
112                 //drop out wildcards if they exist
113                 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
114                 return Optional.of(result);
115         }
116         
117         private Optional<String> resolveRelativePath(Optional<String> populatedUri) throws UnsupportedEncodingException {
118                 if (!populatedUri.isPresent()) {
119                         return Optional.empty();
120                 } else {
121                         return Optional.of(populatedUri.get().replaceFirst("\\./", this.serializer.getURIForVertex(self) + "/"));
122                 }
123         }
124         
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;
128 }