aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/main/java/org/onap/aai/serialization/queryformats/MultiFormatMapper.java
blob: 4977cb8673dd2595f1c3e6fcfde07f01b380bd5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.serialization.queryformats;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public abstract class MultiFormatMapper implements FormatMapper {

    Logger logger = LoggerFactory.getLogger(MultiFormatMapper.class);

    protected boolean isTree = false;
    protected static final String PROPERTIES_KEY = "properties";
    protected static final String NODE_TYPE_KEY = "node-type";

    protected static final String RETURNED_EMPTY_JSONARRAY_MSG =
        "Returned empty JsonArray - Could not populate nested json objects for wrapper: {}";

    @Override
    public Optional<JsonObject> formatObject(Object input)
        throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
        if (input instanceof Vertex) {
            logger.debug("Formatting vertex object");
            return this.getJsonFromVertex((Vertex) input);
        } else if (input instanceof Tree) {
            logger.debug("Formatting tree object");
            if (isTree) {
                return this.getRelatedNodesFromTree((Tree<?>) input, null);
            } else {
                return this.getJsonFromTree((Tree<?>) input);
            }
        } else if (input instanceof Path) {
            logger.debug("Formatting path object");
            return this.getJsonFromPath((Path) input);
        } else {
            throw new AAIFormatQueryResultFormatNotSupported();
        }
    }

    @Override
    public Optional<JsonObject> formatObject(Object input, Map<String, List<String>> properties)
        throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
        if (input instanceof Vertex) {
            logger.debug("Formatting vertex object with properties map filter");
            return this.getJsonFromVertex((Vertex) input, properties);
        } else if (input instanceof Tree) {
            logger.debug("Formatting tree object with properties map filter");
            if (isTree) {
                return this.getRelatedNodesFromTree((Tree<?>) input, properties);
            } else {
                return this.getJsonFromTree((Tree<?>) input);
            }
        } else if (input instanceof Path) {
            logger.debug("Formatting path object");
            return this.getJsonFromPath((Path) input);
        } else {
            throw new AAIFormatQueryResultFormatNotSupported();
        }
    }

    protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
    protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException;

    protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException {
        List<Object> path = input.objects();

        JsonObject jo = new JsonObject();
        JsonArray ja = new JsonArray();

        for (Object o : path) {
            if (o instanceof Vertex) {
                Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) o);
                obj.ifPresent(ja::add);
            }
        }

        jo.add("path", ja);
        return Optional.of(jo);
    }

    /**
     * Returns an Optional<JsonObject> object using "nodes" as a wrapper to encapsulate json objects
     * @param tree
     * @return
     * @throws AAIFormatVertexException
     */
    protected Optional<JsonObject> getJsonFromTree(Tree<?> tree) throws AAIFormatVertexException {
        if (tree.isEmpty()) {
            return Optional.of(new JsonObject());
        }
        String nodeIdentifier = "nodes";

        JsonObject t = new JsonObject();
        JsonArray ja = this.getNodesArray(tree, null, nodeIdentifier);
        if (ja.size() > 0) {
            t.add("nodes", ja);
        } else {
            logger.debug(RETURNED_EMPTY_JSONARRAY_MSG, nodeIdentifier);
        }

        return Optional.of(t);
    }

    /**
     * Returns an Optional<JsonObject> object using "related-nodes" to encapsulate nested json objects.
     * Primarily intended to be utilized by the "as-tree" query parameter feature
     * @param tree
     * @param properties
     * @return
     * @throws AAIFormatVertexException
     */
    protected Optional<JsonObject> getRelatedNodesFromTree(Tree<?> tree, Map<String, List<String>> properties) throws AAIFormatVertexException {
        if (tree.isEmpty()) {
            return Optional.of(new JsonObject());
        }
        String nodeIdentifier = "related-nodes";

        // Creating another DS to help with calls in O(1)
        Map<String, Set<String>> filterPropertiesMap = createFilteredPropertyMap(properties);

        JsonObject t = new JsonObject();
        JsonArray ja = this.getNodesArray(tree, filterPropertiesMap, nodeIdentifier);
        if (ja.size() > 0) {
            t.add("results", ja);
            return Optional.of(t);
        } else {
            logger.debug(RETURNED_EMPTY_JSONARRAY_MSG, nodeIdentifier);
        }

        return Optional.empty();
    }

    /**
     * Returns JsonArray Object populated with nested json wrapped by the nodeIdentifier parameter
     * @param tree
     * @param filterPropertiesMap
     * @param nodeIdentifier
     * @return
     * @throws AAIFormatVertexException
     */
    protected JsonArray getNodesArray(Tree<?> tree, Map<String, Set<String>> filterPropertiesMap, String nodeIdentifier) throws AAIFormatVertexException {
        JsonArray nodes = new JsonArray();
        for (Map.Entry<?, ? extends Tree<?>> entry : tree.entrySet()) {
            JsonObject me = new JsonObject();
            if (entry.getKey() instanceof Vertex) {
                Optional<JsonObject> obj = this.getJsonFromVertex((Vertex) entry.getKey());
                if (obj.isPresent()) {
                    me = getPropertyFilteredObject(obj, filterPropertiesMap);
                } else {
                    continue;
                }
            }
            JsonArray ja = this.getNodesArray(entry.getValue(), filterPropertiesMap, nodeIdentifier);
            if (ja.size() > 0) {
                me.add(nodeIdentifier, ja);
            } else {
                logger.debug(RETURNED_EMPTY_JSONARRAY_MSG, nodeIdentifier);
            }
            nodes.add(me);
        }
        return nodes;
    }

    /**
     * Returns a Map<String, Set<String>> object through converting given map parameter
     * @param properties
     * @return
     */
    protected Map<String, Set<String>> createFilteredPropertyMap(Map<String, List<String>> properties) {
        if (properties == null)
            return new HashMap<>();

        return properties.entrySet().stream()
            .map(entry -> {
                    Set<String> newSet = entry.getValue().stream()
                        .map(this::truncateApostrophes)
                        .collect(Collectors.toSet());

                    return Pair.of(entry.getKey(), newSet);
                }
            ).collect(Collectors.toMap(Pair::getKey, Pair::getValue));
    }

    /**
     * Returns a string with it's apostrophes truncated at the start and end.
     * @param s
     * @return
     */
    protected String truncateApostrophes(String s) {
        if (s == null || s.isEmpty()) {
            return s;
        }
        if (s.startsWith("'") && s.endsWith("'")) {
            s = s.substring(1, s.length() - 1);
        }
        return s;
    }

    /**
     * Filters the given Optional<JsonObject> with the properties under a properties field
     * or the properties under its respective node type.
     * @param obj
     * @param filterPropertiesMap
     * @return
     */
    protected JsonObject getPropertyFilteredObject(Optional<JsonObject> obj,
        Map<String, Set<String>> filterPropertiesMap) {
        return obj.map(
            jsonObj -> {
                if (filterPropertiesMap == null || filterPropertiesMap.isEmpty()) {
                    return jsonObj;
                } else {
                    ImmutableTriple<JsonObject, Optional<String>, Optional<JsonObject>> triple =
                        cloneObjectAndExtractNodeTypeAndProperties(jsonObj);

                    JsonObject result = triple.left;
                    Optional<String> nodeType = triple.middle;
                    Optional<JsonObject> properties = triple.right;

                    // Filter current object based on it containing fields: "node-type" and "properties"
                    if (nodeType.isPresent() && properties.isPresent()) {
                        filterByNodeTypeAndProperties(result, nodeType.get(), properties.get(), filterPropertiesMap);
                    } else {
                        // filter current object based on the: key - nodeType & value - JsonObject of nodes properties
                        filterByJsonObj(result, jsonObj, filterPropertiesMap);
                    }

                    return result;
                }
            }
        ).orElseGet(JsonObject::new);
    }

    private ImmutableTriple<JsonObject, Optional<String>, Optional<JsonObject>> cloneObjectAndExtractNodeTypeAndProperties(
        JsonObject jsonObj) {
        JsonObject result = new JsonObject();
        Optional<String> nodeType = Optional.empty();
        Optional<JsonObject> properties = Optional.empty();

        // clone object
        for (Map.Entry<String, JsonElement> mapEntry : jsonObj.entrySet()) {
            String key = mapEntry.getKey();
            JsonElement value = mapEntry.getValue();

            // also, check if payload has node-type and properties fields
            if (key.equals(NODE_TYPE_KEY) && value != null) {
                nodeType = Optional.of(value.getAsString());
            } else if (key.equals(PROPERTIES_KEY) && value != null && value.isJsonObject()) {
                properties = Optional.of(value.getAsJsonObject());
            }
            result.add(key, value);
        }

        return ImmutableTriple.of(result, nodeType, properties);
    }

    /**
     * Returns a JsonObject with filtered properties using "node-type" and "properties"
     * Used for formats with payloads similar to simple and raw
     * @param result
     * @param nodeType
     * @param properties
     * @param filterPropertiesMap
     * @return
     */
    private JsonObject filterByNodeTypeAndProperties(JsonObject result, String nodeType, JsonObject properties, Map<String, Set<String>> filterPropertiesMap) {
        if (result == null || nodeType == null || nodeType.isEmpty() || properties == null || filterPropertiesMap == null) {
            return result;
        }
        if (filterPropertiesMap.containsKey(nodeType)) {    // filterPropertiesMap keys are nodeTypes - keys are obtained from the incoming query request
            Set<String> filterSet = filterPropertiesMap.get(nodeType);
            JsonObject filteredProperties = new JsonObject();
            for (String property : filterSet) {             // Each nodeType should have a set of properties to be retained in the response
                if (properties.get(property) != null) {
                    filteredProperties.add(property, properties.get(property));
                }
            }
            result.remove(PROPERTIES_KEY);
            result.add(PROPERTIES_KEY, filteredProperties);
        }
        return result;
    }

    /**
     * Returns a JsonObject with its properties filtered
     * @param result
     * @param jsonObj
     * @param filterPropertiesMap
     * @return
     */
    private JsonObject filterByJsonObj(JsonObject result, JsonObject jsonObj, Map<String, Set<String>> filterPropertiesMap) {
        if (result == null || jsonObj == null || filterPropertiesMap == null) {
            return result;
        }

        for (Map.Entry<String, JsonElement> mapEntry : jsonObj.entrySet()) {
            String key = mapEntry.getKey();
            JsonElement value = mapEntry.getValue();
            JsonObject filteredProperties = new JsonObject();
            if (value != null && value.isJsonObject() && filterPropertiesMap.containsKey(key)) {
                JsonObject joProperties = value.getAsJsonObject();
                Set<String> filterSet = filterPropertiesMap.get(key);
                for (String property : filterSet) {
                    if (joProperties.get(property) != null) {
                        filteredProperties.add(property, joProperties.get(property));
                    }
                }
                result.remove(key);
                result.add(key, filteredProperties);
            }
        }
        return result;
    }

    /**
     * Returns a filtered JsonObject with properties contained in the parameter filterPropertiesMap
     * @param properties
     * @param filterPropertiesMap
     * @return
     */
    protected JsonObject filterProperties(Optional<JsonObject> properties, String nodeType,
        Map<String, Set<String>> filterPropertiesMap) {
        return properties.map(jo -> {
            if (filterPropertiesMap == null || filterPropertiesMap.isEmpty()) {
                return properties.get();
            }

            JsonObject result = new JsonObject();
            // clone the object
            for (Map.Entry<String, JsonElement> mapEntry : jo.entrySet()) {
                String key = mapEntry.getKey();
                JsonElement value = mapEntry.getValue();
                result.add(key, value);
            }

            // filter the object
            if (filterPropertiesMap.containsKey(nodeType)) {
                Set<String> filterSet = filterPropertiesMap.get(nodeType);
                for (Map.Entry<String, JsonElement> mapEntry : jo.entrySet()) {
                    String key = mapEntry.getKey();
                    if (!filterSet.contains(key)) {
                        result.remove(key);
                    }
                }
            }
            return result;
        }).orElseGet(JsonObject::new);
    }

    @Override
    public int parallelThreshold() {
        return 100;
    }

}