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