503f3a586c6444518ca427cba31756d454c55965
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / ResourceWithSoT.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 package org.onap.aai.serialization.queryformats;
21
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;
33
34 import java.util.Optional;
35
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();
49     }
50
51     @Override
52     public int parallelThreshold() {
53         return 100;
54     }
55
56     public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
57
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) {
66             this.loader = loader;
67             this.serializer = serializer;
68             this.urlBuilder = urlBuilder;
69         }
70
71         protected Loader getLoader() {
72             return this.loader;
73         }
74
75         protected DBSerializer getSerializer() {
76             return this.serializer;
77         }
78
79         protected UrlBuilder getUrlBuilder() {
80             return this.urlBuilder;
81         }
82
83         public Builder includeUrl() {
84             this.includeUrl = true;
85             return this;
86         }
87
88         public Builder nodesOnly(Boolean nodesOnly) {
89             this.nodesOnly = nodesOnly;
90             return this;
91         }
92         public boolean isNodesOnly() {
93             return this.nodesOnly;
94         }
95
96         public Builder depth(Integer depth) {
97             this.depth = depth;
98             return this;
99         }
100
101         public int getDepth() {
102             return this.depth;
103         }
104
105         public boolean isIncludeUrl() {
106             return this.includeUrl;
107         }
108
109         public Builder modelDriven() {
110             this.modelDriven = true;
111             return this;
112         }
113
114         public boolean getModelDriven() {
115             return this.modelDriven;
116         }
117         public ResourceWithSoT build() {
118             return new ResourceWithSoT(this);
119         }
120     }
121
122     /**
123      *
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.
127      *
128      * @param v
129      * @return
130      * @throws AAIFormatVertexException
131      */
132     @Override
133     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
134         // Null check
135         if (v == null)
136             return null;
137
138         JsonObject json = new JsonObject();
139
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"));
147
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());
153
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());
158
159         if (timestampDiff <= threshold && isSameSoT)
160             json.addProperty("last-action-performed", "Created");
161         else
162             json.addProperty("last-action-performed", "Modified");
163
164         return Optional.of(json);
165     }
166 }