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