Update schema service to fail to start
[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 com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.config.SpringContextAware;
26 import org.onap.aai.db.props.AAIProperties;
27 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
28 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
29 import org.onap.aai.exceptions.AAIException;
30 import org.onap.aai.introspection.*;
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 import org.onap.aai.setup.SchemaVersions;
36
37
38 import java.io.UnsupportedEncodingException;
39 import java.net.URISyntaxException;
40 import java.util.*;
41 import java.util.Map.Entry;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
44
45 public abstract class SideEffect {
46
47         protected static final Pattern template = Pattern.compile("\\{(.*?)\\}");
48         private static final EELFLogger logger = EELFManager.getInstance().getLogger(SideEffect.class);
49
50         protected final Introspector obj;
51         protected final TransactionalGraphEngine dbEngine;
52         protected final DBSerializer serializer;
53         protected final Loader latestLoader;
54         protected final Vertex self;
55
56         protected Set<String> templateKeys = new HashSet<>();
57
58         public SideEffect (Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
59                 this.obj = obj;
60                 this.dbEngine = dbEngine;
61                 this.serializer = serializer;
62                 this.self = self;
63                 this.latestLoader = LoaderUtil.getLatestVersion();
64         }
65
66         protected void execute() throws UnsupportedEncodingException, URISyntaxException, AAIException {
67                 final Map<String, String> properties = this.findPopertiesWithMetadata(obj, this.getPropertyMetadata());
68                 for (Entry<String, String> entry : properties.entrySet()) {
69                         Optional<String> populatedUri = this.replaceTemplates(obj, entry.getValue());
70                         Optional<String> completeUri = this.resolveRelativePath(populatedUri);
71                         try {
72                                 this.processURI(completeUri, entry);
73                         } catch (EdgeRuleNotFoundException | AmbiguousRuleChoiceException e) {
74                             logger.warn("Unable to execute the side effect {} due to ", e, this.getClass().getName());
75                         }
76                 }
77         }
78
79         protected Map<String, String> findPopertiesWithMetadata(Introspector obj, PropertyMetadata metadata) {
80                 final Map<String, String> result = new HashMap<>();
81                 for (String prop : obj.getProperties()) {
82                         final Map<PropertyMetadata, String> map = obj.getPropertyMetadata(prop);
83                         if (map.containsKey(metadata)) {
84                                 result.put(prop, map.get(metadata));
85                         }
86                 }
87                 return result;
88         }
89
90         protected Map<String, String> findProperties(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
91
92                 final Map<String, String> result = new HashMap<>();
93                 final Set<String> missing = new LinkedHashSet<>();
94                 Matcher m = template.matcher(uriString);
95                 int properties = 0;
96                 while (m.find()) {
97                         String propName = m.group(1);
98                         String value = obj.getValue(propName);
99                         properties++;
100                         if (value != null) {
101                                 result.put(propName, value);
102                         } else {
103                                 if (replaceWithWildcard()) {
104                                         result.put(propName, "*");
105                                 }
106                                 missing.add(propName);
107                         }
108                 }
109
110                 if (!missing.isEmpty() && (properties != missing.size())) {
111                         throw new AAIMissingRequiredPropertyException("Cannot complete " + this.getPropertyMetadata().toString() + " uri. Missing properties " + missing);
112                 }
113                 return result;
114         }
115
116         protected Optional<String> replaceTemplates(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException {
117                 String result = uriString;
118                 final Map<String, String> propMap = this.findProperties(obj, uriString);
119                 if (propMap.isEmpty()) {
120                         return Optional.empty();
121                 }
122                 for (Entry<String, String> entry : propMap.entrySet()) {
123                         templateKeys.add(entry.getKey());
124                         result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue());
125                 }
126                 //drop out wildcards if they exist
127                 result = result.replaceFirst("/[^/]+?(?:/\\*)+", "");
128                 return Optional.of(result);
129         }
130
131         private Optional<String> resolveRelativePath(Optional<String> populatedUri) throws UnsupportedEncodingException {
132                 if (!populatedUri.isPresent()) {
133                         return Optional.empty();
134                 } else {
135                         return Optional.of(populatedUri.get().replaceFirst("\\./", this.serializer.getURIForVertex(self) + "/"));
136                 }
137         }
138
139         protected abstract boolean replaceWithWildcard();
140         protected abstract PropertyMetadata getPropertyMetadata();
141         protected abstract void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException;
142 }