Initial commit with all the necessary files
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / queryformats / Formatter.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.serialization.queryformats;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28 import org.openecomp.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
29
30 import java.util.List;
31 import java.util.Optional;
32 import java.util.stream.Stream;
33
34 public class Formatter {
35
36         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(Formatter.class);
37
38         protected JsonParser parser = new JsonParser();
39         protected final FormatMapper format;
40         public Formatter(FormatMapper format) {
41                 this.format = format;
42         }
43         
44         public JsonObject output(List<Object> vertices) {
45                 Stream<Object> stream = null;
46                 JsonObject result = new JsonObject();
47                 JsonArray body = new JsonArray();
48                 if (vertices.size() >= format.parallelThreshold()) {
49                         stream = vertices.parallelStream();
50                 } else {
51                         stream = vertices.stream();
52                 }
53                 
54                 stream.map(v -> {
55                         try {
56                                 return Optional.<JsonObject>of(format.formatObject(v));
57                         } catch (AAIFormatVertexException e) {
58                                 LOGGER.warn("Failed to format vertex, returning a partial list", e);
59                         }
60
61                         return Optional.<JsonObject>empty();
62                 }).forEach(obj -> {
63                         if (obj.isPresent()) {
64                                 synchronized (body) {
65                                         body.add(obj.get());
66                                 }
67                         }
68                 });
69                 
70                 result.add("results", body);
71                 
72                 return result.getAsJsonObject();
73         }
74         
75 }