2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.serialization.queryformats;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonObject;
27 import java.util.concurrent.TimeUnit;
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;
35 public class ChangesFormat extends MultiFormatMapper {
37 private Long startTs = 0L;
39 public void startTs(String startTime) {
41 * StartTs = truncate time
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));
49 this.startTs = Long.parseLong(startTime);
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();
60 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
61 json.addProperty("uri", v.<String>value(AAIProperties.AAI_URI));
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();
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());
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());
82 List<Long> sortedList = new ArrayList<>(changes);
83 sortedList.sort(Comparator.naturalOrder());
84 JsonArray jsonArray = new JsonArray();
85 sortedList.forEach(jsonArray::add);
87 json.add("changes", jsonArray);
89 return Optional.of(json);
93 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties)
94 throws AAIFormatVertexException {
95 return Optional.empty();