Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / Count.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
21 package org.onap.aai.serialization.queryformats;
22
23 import com.google.gson.JsonObject;
24
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29
30 import org.apache.tinkerpop.gremlin.process.traversal.Path;
31 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.javatuples.Pair;
34 import org.onap.aai.db.props.AAIProperties;
35 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
36 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
37
38 public class Count implements FormatMapper {
39
40     @Override
41     public Optional<JsonObject> formatObject(Object o) {
42         @SuppressWarnings("unchecked")
43         List<Object> list = (List<Object>) o;
44
45         final JsonObject countResult = new JsonObject();
46
47         list.stream().map(this::getCount).filter(Optional::isPresent).map(Optional::get)
48                 .collect(Collectors.toConcurrentMap(Pair::getValue0, Pair::getValue1, Long::sum))
49                 .forEach(countResult::addProperty);
50
51         return Optional.of(countResult);
52     }
53
54     @Override
55     public Optional<JsonObject> formatObject(Object o, Map<String, List<String>> properties) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
56         return Optional.empty();
57     }
58
59     @Override
60     public int parallelThreshold() {
61         return 20;
62     }
63
64     private Optional<Pair<String, Long>> getCount(Object o) {
65
66         Pair<String, Long> pair = null;
67
68         if (o instanceof Vertex) {
69             Vertex v = (Vertex) o;
70             pair = Pair.with(v.property(AAIProperties.NODE_TYPE).value().toString(), 1L);
71         } else if (o instanceof Tree) {
72             pair = Pair.with("trees", 1L);
73         } else if (o instanceof Path) {
74             pair = Pair.with("paths", 1L);
75         } else if (o instanceof Long) {
76             pair = Pair.with("count", (Long) o);
77         }
78
79         if (pair == null) {
80             return Optional.empty();
81         }
82
83         return Optional.of(pair);
84     }
85
86 }