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