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;
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;
32 import java.util.concurrent.TimeUnit;
34 public class ChangesFormat extends MultiFormatMapper {
36 private Long startTs = 0L;
38 public void startTs(String startTime) {
40 * StartTs = truncate time
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));
46 this.startTs = Long.parseLong(startTime);
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();
58 json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE));
59 json.addProperty("uri", v.<String>value(AAIProperties.AAI_URI));
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();
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());
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());
80 List<Long> sortedList = new ArrayList<>(changes);
81 sortedList.sort(Comparator.naturalOrder());
82 JsonArray jsonArray = new JsonArray();
83 sortedList.forEach(jsonArray::add);
85 json.add("changes", jsonArray);
87 return Optional.of(json);
91 protected Optional<JsonObject> getJsonFromVertex(Vertex input, Map<String, List<String>> properties) throws AAIFormatVertexException {
92 return Optional.empty();