50042b7624808c8447235ee6819a3de995ae57f1
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / Formatter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.serialization.queryformats;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParser;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
30
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.stream.Stream;
34
35 public class Formatter {
36
37         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(Formatter.class);
38
39         protected JsonParser parser = new JsonParser();
40         protected final FormatMapper format;
41         public Formatter (FormatMapper format) {
42                 this.format = format;
43         }
44         
45         public JsonObject output(List<Object> vertices) {
46                 Stream<Object> stream = null;
47                 JsonObject result = new JsonObject();
48                 JsonArray body = new JsonArray();
49                 if (vertices.size() >= format.parallelThreshold()) {
50                         stream = vertices.parallelStream();
51                 } else {
52                         stream = vertices.stream();
53                 }
54                 final boolean isParallel = stream.isParallel();
55                 stream.map(v -> {
56                         try {
57                                 return Optional.<JsonObject>of(format.formatObject(v));
58                         } catch (AAIFormatVertexException e) {
59                                 LOGGER.warn("Failed to format vertex, returning a partial list", e);
60                         }
61
62                         return Optional.<JsonObject>empty();
63                 }).forEach(obj -> {
64                         if (obj.isPresent()) {
65                                 if (isParallel) {
66                                         synchronized (body) {
67                                                 body.add(obj.get());
68                                         }
69                                 } else {
70                                         body.add(obj.get());
71                                 }
72                         }
73                 });
74                 
75                 result.add("results", body);
76                 
77                 return result.getAsJsonObject();
78         }
79         
80 }