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