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