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=========================================================
20 package org.onap.aai.serialization.queryformats;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24 import org.apache.tinkerpop.gremlin.structure.Vertex;
25 import org.onap.aai.db.props.AAIProperties;
26 import org.onap.aai.introspection.Loader;
27 import org.onap.aai.serialization.db.DBSerializer;
28 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
29 import org.onap.aai.serialization.queryformats.params.Depth;
30 import org.onap.aai.serialization.queryformats.params.NodesOnly;
31 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
32 import org.onap.aai.util.AAIConfig;
34 import java.util.Optional;
36 public class ResourceWithSoT extends MultiFormatMapper {
37 protected JsonParser parser = new JsonParser();
38 protected final DBSerializer serializer;
39 protected final Loader loader;
40 protected final UrlBuilder urlBuilder;
41 protected final int depth;
42 protected final boolean nodesOnly;
43 protected ResourceWithSoT(Builder builder) {
44 this.urlBuilder = builder.getUrlBuilder();
45 this.loader = builder.getLoader();
46 this.serializer = builder.getSerializer();
47 this.depth = builder.getDepth();
48 this.nodesOnly = builder.isNodesOnly();
52 public int parallelThreshold() {
56 public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
58 protected final Loader loader;
59 protected final DBSerializer serializer;
60 protected final UrlBuilder urlBuilder;
61 protected boolean includeUrl = false;
62 protected boolean nodesOnly = false;
63 protected int depth = 1;
64 protected boolean modelDriven = false;
65 public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
67 this.serializer = serializer;
68 this.urlBuilder = urlBuilder;
71 protected Loader getLoader() {
75 protected DBSerializer getSerializer() {
76 return this.serializer;
79 protected UrlBuilder getUrlBuilder() {
80 return this.urlBuilder;
83 public Builder includeUrl() {
84 this.includeUrl = true;
88 public Builder nodesOnly(Boolean nodesOnly) {
89 this.nodesOnly = nodesOnly;
92 public boolean isNodesOnly() {
93 return this.nodesOnly;
96 public Builder depth(Integer depth) {
101 public int getDepth() {
105 public boolean isIncludeUrl() {
106 return this.includeUrl;
109 public Builder modelDriven() {
110 this.modelDriven = true;
114 public boolean getModelDriven() {
115 return this.modelDriven;
117 public ResourceWithSoT build() {
118 return new ResourceWithSoT(this);
124 * Returns an Optional<JsonObject> to convert the contents from the given Vertex object into a JsonObject.
125 * The fields returned are to record the time stamp of the creation/modification of the object, the user responsible for
126 * the change, and the last http method performed on the object.
130 * @throws AAIFormatVertexException
133 protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
138 JsonObject json = new JsonObject();
140 Object createdTimestampObj = v.property(AAIProperties.CREATED_TS).value();
141 Object lastModifiedTimestampObj = v.property(AAIProperties.LAST_MOD_TS).value();
142 Object sotObj = v.property(AAIProperties.SOURCE_OF_TRUTH).value();
143 Object lastModSotObj = v.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
144 long createdTimestamp = Long.valueOf(createdTimestampObj.toString());
145 long lastModifiedTimestamp = Long.valueOf(lastModifiedTimestampObj.toString());
146 long threshold = Long.valueOf(AAIConfig.get("aai.resource.format.threshold", "10"));
148 // Add to the property field of the JSON payload
149 json.addProperty("aai-created-ts", createdTimestampObj.toString());
150 json.addProperty("aai-last-mod-ts", lastModifiedTimestampObj.toString());
151 json.addProperty("source-of-truth", sotObj.toString());
152 json.addProperty("last-mod-source-of-truth", lastModSotObj.toString());
154 // Check if the timestamp difference between creation and last modification are greater than a certain threshold, and if the source of truth differs
155 // If the timestamp difference is marginal and the SoT (creator/modifier) is the same, the last action performed is likely to be a creation.
156 long timestampDiff = lastModifiedTimestamp - createdTimestamp;
157 boolean isSameSoT = sotObj.toString().equals(lastModSotObj.toString());
159 if (timestampDiff <= threshold && isSameSoT)
160 json.addProperty("last-action-performed", "Created");
162 json.addProperty("last-action-performed", "Modified");
164 return Optional.of(json);