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