Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / ChangesFormat.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.JsonArray;
24 import com.google.gson.JsonObject;
25 import org.apache.tinkerpop.gremlin.structure.Direction;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.onap.aai.config.SpringContextAware;
28 import org.onap.aai.db.props.AAIProperties;
29 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
30
31 import java.util.*;
32 import java.util.concurrent.TimeUnit;
33
34 public class ChangesFormat extends MultiFormatMapper {
35
36     private Long startTs = 0L;
37
38     public void startTs(String startTime) {
39         /*
40          * StartTs = truncate time
41          */
42         if (startTime == null || startTime.isEmpty() || "now".equals(startTime) || "0".equals(startTime) || "-1".equals(startTime)){
43            String historyTruncateDays = SpringContextAware.getApplicationContext().getEnvironment().getProperty("history.truncate.window.days", "365");
44            this.startTs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(Long.parseLong(historyTruncateDays));
45         } else {
46             this.startTs = Long.parseLong(startTime);
47         }
48     }
49
50     @Override
51     protected Optional<JsonObject> getJsonFromVertex(Vertex v) {
52         JsonObject json = new JsonObject();
53         if (!v.properties(AAIProperties.RESOURCE_VERSION).hasNext() ||
54             !v.properties(AAIProperties.NODE_TYPE).hasNext() ||
55             !v.properties(AAIProperties.AAI_URI).hasNext()) {
56             return Optional.empty();
57         }
58         json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
59         json.addProperty("uri", v.<String>value(AAIProperties.AAI_URI));
60
61         final Set<Long> changes = new HashSet<>();
62         v.properties(AAIProperties.RESOURCE_VERSION).forEachRemaining(o->
63             o.properties(AAIProperties.START_TS, AAIProperties.END_TS)
64             .forEachRemaining(p -> {
65                 Long val = (Long) p.value();
66                 if(val >= startTs) {
67                     changes.add(val);
68                 }
69             }
70             ));
71         v.edges(Direction.BOTH).forEachRemaining(e -> {
72             if(e.property(AAIProperties.START_TS).isPresent() && (Long)e.property(AAIProperties.START_TS).value() >= startTs) {
73                 changes.add((Long) e.property(AAIProperties.START_TS).value());
74             }
75             if(e.property(AAIProperties.END_TS).isPresent() && (Long)e.property(AAIProperties.END_TS).value() >= startTs) {
76                 changes.add((Long) e.property(AAIProperties.END_TS).value());
77             }
78         });
79
80         List<Long> sortedList = new ArrayList<>(changes);
81         sortedList.sort(Comparator.naturalOrder());
82         JsonArray jsonArray = new JsonArray();
83         sortedList.forEach(jsonArray::add);
84
85         json.add("changes", jsonArray);
86
87         return Optional.of(json);
88     }
89
90     @Override
91     protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException {
92         return Optional.empty();
93     }
94
95 }