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