AAI-1523 Batch reformat aai-core
[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
21 package org.onap.aai.serialization.queryformats;
22
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParser;
25
26 import java.util.Optional;
27
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.introspection.Loader;
31 import org.onap.aai.serialization.db.DBSerializer;
32 import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
33 import org.onap.aai.serialization.queryformats.params.Depth;
34 import org.onap.aai.serialization.queryformats.params.NodesOnly;
35 import org.onap.aai.serialization.queryformats.utils.UrlBuilder;
36 import org.onap.aai.util.AAIConfig;
37
38 public class ResourceWithSoT extends MultiFormatMapper {
39     protected JsonParser parser = new JsonParser();
40     protected final DBSerializer serializer;
41     protected final Loader loader;
42     protected final UrlBuilder urlBuilder;
43     protected final int depth;
44     protected final boolean nodesOnly;
45
46     protected ResourceWithSoT(Builder builder) {
47         this.urlBuilder = builder.getUrlBuilder();
48         this.loader = builder.getLoader();
49         this.serializer = builder.getSerializer();
50         this.depth = builder.getDepth();
51         this.nodesOnly = builder.isNodesOnly();
52     }
53
54     @Override
55     public int parallelThreshold() {
56         return 100;
57     }
58
59     public static class Builder implements NodesOnly<Builder>, Depth<Builder> {
60
61         protected final Loader loader;
62         protected final DBSerializer serializer;
63         protected final UrlBuilder urlBuilder;
64         protected boolean includeUrl = false;
65         protected boolean nodesOnly = false;
66         protected int depth = 1;
67         protected boolean modelDriven = false;
68
69         public Builder(Loader loader, DBSerializer serializer, UrlBuilder urlBuilder) {
70             this.loader = loader;
71             this.serializer = serializer;
72             this.urlBuilder = urlBuilder;
73         }
74
75         protected Loader getLoader() {
76             return this.loader;
77         }
78
79         protected DBSerializer getSerializer() {
80             return this.serializer;
81         }
82
83         protected UrlBuilder getUrlBuilder() {
84             return this.urlBuilder;
85         }
86
87         public Builder includeUrl() {
88             this.includeUrl = true;
89             return this;
90         }
91
92         public Builder nodesOnly(Boolean nodesOnly) {
93             this.nodesOnly = nodesOnly;
94             return this;
95         }
96
97         public boolean isNodesOnly() {
98             return this.nodesOnly;
99         }
100
101         public Builder depth(Integer depth) {
102             this.depth = depth;
103             return this;
104         }
105
106         public int getDepth() {
107             return this.depth;
108         }
109
110         public boolean isIncludeUrl() {
111             return this.includeUrl;
112         }
113
114         public Builder modelDriven() {
115             this.modelDriven = true;
116             return this;
117         }
118
119         public boolean getModelDriven() {
120             return this.modelDriven;
121         }
122
123         public ResourceWithSoT build() {
124             return new ResourceWithSoT(this);
125         }
126     }
127
128     /**
129      *
130      * Returns an Optional<JsonObject> to convert the contents from the given Vertex object into a JsonObject.
131      * The fields returned are to record the time stamp of the creation/modification of the object, the user responsible
132      * for
133      * the change, and the last http method performed on the object.
134      *
135      * @param v
136      * @return
137      * @throws AAIFormatVertexException
138      */
139     @Override
140     protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException {
141         // Null check
142         if (v == null)
143             return null;
144
145         JsonObject json = new JsonObject();
146
147         Object createdTimestampObj = v.property(AAIProperties.CREATED_TS).value();
148         Object lastModifiedTimestampObj = v.property(AAIProperties.LAST_MOD_TS).value();
149         Object sotObj = v.property(AAIProperties.SOURCE_OF_TRUTH).value();
150         Object lastModSotObj = v.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
151         long createdTimestamp = Long.valueOf(createdTimestampObj.toString());
152         long lastModifiedTimestamp = Long.valueOf(lastModifiedTimestampObj.toString());
153         long threshold = Long.valueOf(AAIConfig.get("aai.resource.format.threshold", "10"));
154
155         // Add to the property field of the JSON payload
156         json.addProperty("aai-created-ts", createdTimestampObj.toString());
157         json.addProperty("aai-last-mod-ts", lastModifiedTimestampObj.toString());
158         json.addProperty("source-of-truth", sotObj.toString());
159         json.addProperty("last-mod-source-of-truth", lastModSotObj.toString());
160
161         // Check if the timestamp difference between creation and last modification are greater than a certain
162         // threshold, and if the source of truth differs
163         // If the timestamp difference is marginal and the SoT (creator/modifier) is the same, the last action performed
164         // is likely to be a creation.
165         long timestampDiff = lastModifiedTimestamp - createdTimestamp;
166         boolean isSameSoT = sotObj.toString().equals(lastModSotObj.toString());
167
168         if (timestampDiff <= threshold && isSameSoT)
169             json.addProperty("last-action-performed", "Created");
170         else
171             json.addProperty("last-action-performed", "Modified");
172
173         return Optional.of(json);
174     }
175 }