Skip to content

Commit

Permalink
Merge pull request #17 from gopal-mahajan/minor-changes
Browse files Browse the repository at this point in the history
immudb-read-flow-refactored
  • Loading branch information
kailash authored Sep 8, 2022
2 parents c58ef92 + c49000f commit 9be297f
Show file tree
Hide file tree
Showing 18 changed files with 665 additions and 557 deletions.
109 changes: 97 additions & 12 deletions src/main/java/iudx/rs/proxy/apiserver/ApiServerVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.HEADER_HOST;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.ID;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.IUDXQUERY_OPTIONS;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.IUDX_CONSUMER_AUDIT_URL;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.IUDX_PROVIDER_AUDIT_URL;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.JSON_COUNT;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.JSON_INSTANCEID;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.JSON_TYPE;
Expand All @@ -23,6 +25,8 @@
import static iudx.rs.proxy.common.ResponseUrn.BACKING_SERVICE_FORMAT_URN;
import static iudx.rs.proxy.common.ResponseUrn.INVALID_PARAM_URN;
import static iudx.rs.proxy.common.ResponseUrn.INVALID_TEMPORAL_PARAM_URN;
import static iudx.rs.proxy.metering.util.Constants.RESULTS;
import static iudx.rs.proxy.metering.util.Constants.TOTAL_HITS;

import io.netty.handler.codec.http.HttpConstants;
import io.netty.handler.codec.http.QueryStringDecoder;
Expand Down Expand Up @@ -57,6 +61,7 @@
import iudx.rs.proxy.metering.MeteringService;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -106,8 +111,7 @@ public void start() throws Exception {
keystore = config().getString("keystore");
keystorePassword = config().getString("keystorePassword");

serverOptions
.setSsl(true)
serverOptions.setSsl(true)
.setKeyStoreOptions(new JksOptions().setPath(keystore).setPassword(keystorePassword));

} else {
Expand All @@ -128,22 +132,21 @@ public void start() throws Exception {
FailureHandler validationsFailureHandler = new FailureHandler();

ValidationHandler entityValidationHandler = new ValidationHandler(vertx, RequestType.ENTITY);
router
.get(NGSILD_ENTITIES_URL)
.handler(entityValidationHandler)
.handler(AuthHandler.create(vertx))
.handler(this::handleEntitiesQuery)
router.get(NGSILD_ENTITIES_URL).handler(entityValidationHandler)
.handler(AuthHandler.create(vertx)).handler(this::handleEntitiesQuery)
.failureHandler(validationsFailureHandler);

ValidationHandler temporalValidationHandler =
new ValidationHandler(vertx, RequestType.TEMPORAL);

router
.get(NGSILD_TEMPORAL_URL)
.handler(temporalValidationHandler)
.handler(AuthHandler.create(vertx))
.handler(this::handleTemporalQuery)
router.get(NGSILD_TEMPORAL_URL).handler(temporalValidationHandler)
.handler(AuthHandler.create(vertx)).handler(this::handleTemporalQuery)
.failureHandler(validationsFailureHandler);

router.get(IUDX_CONSUMER_AUDIT_URL).handler(AuthHandler.create(vertx))
.handler(this::getConsumerAuditDetail);
router.get(IUDX_PROVIDER_AUDIT_URL).handler(AuthHandler.create(vertx))
.handler(this::getProviderAuditDetail);
}

private void handleEntitiesQuery(RoutingContext routingContext) {
Expand Down Expand Up @@ -359,6 +362,88 @@ private void processBackendResponse(HttpServerResponse response, String failureM
}
}

private Future<Void> getConsumerAuditDetail(RoutingContext routingContext) {
LOGGER.debug("Info: getConsumerAuditDetail Started. ");
Promise<Void> promise = Promise.promise();
JsonObject entries = new JsonObject();
JsonObject consumer = (JsonObject) routingContext.data().get("authInfo");
HttpServerRequest request = routingContext.request();
HttpServerResponse response = routingContext.response();

entries.put("userid", consumer.getString("userid"));
entries.put("endPoint", consumer.getString("apiEndpoint"));
entries.put("startTime", request.getParam("time"));
entries.put("endTime", request.getParam("endTime"));
entries.put("timeRelation", request.getParam("timerel"));
entries.put("options", request.headers().get("options"));
entries.put("resourceId", request.getParam("id"));
entries.put("api", request.getParam("api"));

{
LOGGER.debug(entries);
meteringService.executeReadQuery(entries, handler -> {
if (handler.succeeded()) {
LOGGER.info("Success: Search Success ");
if (Objects.equals(request.headers().get("options"), "count") && Integer.parseInt(
handler.result().getJsonArray(RESULTS).getJsonObject(0).getString(TOTAL_HITS)) == 0) {
handleSuccessResponse(response, ResponseType.NoContent.getCode(),
handler.result().toString());
} else {
LOGGER.debug("Table Reading Done.");
handleSuccessResponse(response, ResponseType.Ok.getCode(), handler.result().toString());
}
} else if (handler.failed()) {
LOGGER.error("Fail: Search Fail " + handler.cause().getMessage());
LOGGER.debug(handler instanceof ServiceException);
processBackendResponse(response, handler.cause().getMessage());
}
});
return promise.future();
}
}

private Future<Void> getProviderAuditDetail(RoutingContext routingContext) {
LOGGER.trace("Info: getProviderAuditDetail Started.");
Promise<Void> promise = Promise.promise();
JsonObject entries = new JsonObject();
JsonObject provider = (JsonObject) routingContext.data().get("authInfo");
HttpServerRequest request = routingContext.request();
HttpServerResponse response = routingContext.response();

entries.put("endPoint", provider.getString("apiEndpoint"));
entries.put("userid", provider.getString("userid"));
entries.put("iid", provider.getString("iid"));
entries.put("startTime", request.getParam("time"));
entries.put("endTime", request.getParam("endTime"));
entries.put("timeRelation", request.getParam("timerel"));
entries.put("providerID", request.getParam("providerID"));
entries.put("consumerID", request.getParam("consumer"));
entries.put("resourceId", request.getParam("id"));
entries.put("api", request.getParam("api"));
entries.put("options", request.headers().get("options"));

{
LOGGER.debug(entries);
meteringService.executeReadQuery(entries, handler -> {
if (handler.succeeded()) {
LOGGER.info("Success: Search Success ");
if (Objects.equals(request.headers().get("options"), "count") && Integer.parseInt(
handler.result().getJsonArray(RESULTS).getJsonObject(0).getString(TOTAL_HITS)) == 0) {
handleSuccessResponse(response, ResponseType.NoContent.getCode(),
handler.result().toString());
} else {
handleSuccessResponse(response, ResponseType.Ok.getCode(), handler.result().toString());
}
} else if (handler.failed()) {
LOGGER.error("Fail: Search Fail " + handler.cause().getMessage());
LOGGER.debug(handler instanceof ServiceException);
processBackendResponse(response, handler.cause().getMessage());
}
});
return promise.future();
}
}

private void updateAuditTable(RoutingContext context) {
Promise<Void> promise = Promise.promise();
JsonObject authInfo = (JsonObject) context.data().get("authInfo");
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/iudx/rs/proxy/apiserver/handlers/AuthHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.APPLICATION_JSON;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.CONTENT_TYPE;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.ENTITIES_URL_REGEX;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.EXPIRY;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.HEADER_TOKEN;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.ID;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.IDS;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.IID;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.IUDX_CONSUMER_AUDIT_URL;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.IUDX_PROVIDER_AUDIT_URL;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.JSON_DETAIL;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.JSON_TITLE;
import static iudx.rs.proxy.apiserver.util.ApiServerConstants.JSON_TYPE;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class AuthHandler implements Handler<RoutingContext> {

private static final Logger LOGGER = LogManager.getLogger(AuthHandler.class);

static AuthenticationService authenticator;
static AuthenticationService authenticator;
private final String AUTH_INFO = "authInfo";
private HttpServerRequest request;

Expand Down Expand Up @@ -95,7 +96,6 @@ public void handle(RoutingContext context) {
return;
}
context.next();
return;
});
}

Expand Down Expand Up @@ -127,7 +127,7 @@ private JsonObject generateResponse(ResponseUrn urn, HttpStatusCode statusCode)
/**
* extract id from request (path/query or body )
*
* @param ctx current routing context
* @param ctx current routing context
* @param forPath endpoint called for
* @return id extraced fro path if present
*/
Expand Down Expand Up @@ -159,6 +159,10 @@ private String getNormalizedPath(String url) {
path = NGSILD_TEMPORAL_URL;
} else if (url.matches(ENTITIES_URL_REGEX)) {
path = NGSILD_ENTITIES_URL;
} else if (url.matches(IUDX_CONSUMER_AUDIT_URL)) {
path = IUDX_CONSUMER_AUDIT_URL;
} else if (url.matches(IUDX_PROVIDER_AUDIT_URL)) {
path = IUDX_PROVIDER_AUDIT_URL;
}
return path;
}
Expand Down
64 changes: 6 additions & 58 deletions src/main/java/iudx/rs/proxy/apiserver/util/ApiServerConstants.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package iudx.rs.proxy.apiserver.util;

import io.vertx.core.http.HttpMethod;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import io.vertx.core.http.HttpMethod;
import java.util.regex.Pattern;

public class ApiServerConstants {
Expand Down Expand Up @@ -34,17 +34,15 @@ public class ApiServerConstants {
public static final String NGSILD_ENTITIES_URL = NGSILD_BASE_PATH + "/entities";
public static final String ENTITIES_URL_REGEX = NGSILD_ENTITIES_URL + "(.*)";
public static final String NGSILD_TEMPORAL_URL = NGSILD_BASE_PATH + "/temporal/entities";

// path regex
public static final String TEMPORAL_URL_REGEX = NGSILD_TEMPORAL_URL + "(.*)";
public static final String IUDX_CONSUMER_AUDIT_URL = NGSILD_BASE_PATH + "/consumer/audit";
// date-time format

public static final String IUDX_PROVIDER_AUDIT_URL = NGSILD_BASE_PATH + "/provider/audit";
public static final String API_ENDPOINT = "apiEndpoint";
public static final String API_METHOD = "method";
public static final String ID = "id";
public static final String IDS = "ids";

// path regex
public static final String TEMPORAL_URL_REGEX = NGSILD_TEMPORAL_URL + "(.*)";

// ngsi-ld/IUDX query paramaters
public static final String NGSILDQUERY_ID = "id";
public static final String NGSILDQUERY_IDPATTERN = "idpattern";
Expand All @@ -61,10 +59,8 @@ public class ApiServerConstants {
public static final String NGSILDQUERY_GEOPROPERTY = "geoproperty";
public static final String NGSILDQUERY_TIMEPROPERTY = "timeproperty";
public static final String NGSILDQUERY_MAXDISTANCE = "maxdistance";
public static final String NGSILDQUERY_MINDISTANCE = "mindistance";
public static final String IUDXQUERY_OPTIONS = "options";
public static final String NGSILDQUERY_ENTITIES = "entities";
public static final String NGSILDQUERY_GEOQ = "geoQ";
public static final String NGSILDQUERY_TEMPORALQ = "temporalQ";
public static final String NGSILDQUERY_TIME_PROPERTY = "timeProperty";
public static final String NGSILDQUERY_FROM = "offset";
Expand All @@ -73,77 +69,29 @@ public class ApiServerConstants {

//json fields
public static final String JSON_INSTANCEID = "instanceID";
public static final String JSON_CONSUMER = "consumer";
public static final String JSON_PROVIDER = "provider";
public static final String JSON_TYPE = "type";
public static final String JSON_NAME = "name";
public static final String JSON_ENTITIES = "entities";
public static final String JSON_ID = "id";
public static final String JSON_ATTRIBUTE_FILTER = "attrs";
public static final String JSON_NEAR = "near";
public static final String JSON_LAT = "lat";
public static final String JSON_LON = "lon";
public static final String JSON_RADIUS = "radius";
public static final String JSON_GEOMETRY = "geometry";
public static final String JSON_COORDINATES = "coordinates";
public static final String JSON_GEOREL = "georel";
public static final String JSON_WITHIN = "within";
public static final String JSON_MAXDISTANCE = "maxdistance";
public static final String JSON_MINDISTANCE = "mindistance";
public static final String JSON_DURING = "during";
public static final String JSON_TIME = "time";
public static final String JSON_ENDTIME = "endtime";
public static final String JSON_TIMEREL = "timerel";
public static final String JSON_ATTR_QUERY = "attr-query";
public static final String JSON_GEOPROPERTY = "geoproperty";
public static final String JSON_ATTRIBUTE = "attribute";
public static final String JSON_OPERATOR = "operator";
public static final String JSON_VALUE = "value";
public static final String JSON_TITLE = "title";
public static final String JSON_DETAIL = "detail";
public static final String JSON_EXCHANGE_NAME = "exchangeName";
public static final String JSON_QUEUE_NAME = "queueName";
public static final String JSON_VHOST_NAME = "vHostName";
public static final String JSON_VHOST = "vHost";
public static final String JSON_VHOST_ID = "vhostId";
public static final String DOMAIN = "domain";
public static final String USERSHA = "userSha";
public static final String JSON_ALIAS = "alias";
public static final String JSON_STREAMING_TYPE = "streaming";
public static final String JSON_EXCHANGE = "exchange";
public static final String JSON_QUEUE = "queue";
public static final String JSON_USERNAME = "username";
public static final String JSON_APIKEY = "apiKey";
public static final String JSON_STATUS = "status";
public static final String JSON_STATUS_HEARTBEAT = "heartbeat";
public static final String JSON_STATUS_SERVERISSUE = "Server Issue";
public static final String JSON_STATUS_DATAISSUE = "Server Issue";
public static final String JSON_STREAMING_NAME = "test-streaming-name";
public static final String JSON_SUBS_ID = "subscriptionID";
public static final String JSON_COUNT = "Count";
public static final String JSON_URL = "url";
public static final String JSON_METHOD = "method";
public static final String JSON_PASSWORD = "password";
public static final String RESOURCE_SERVER = "resourceServer";
public static final String RESOURCE_GROUP = "resourceGroup";
public static final String RESOURCE_NAME = "resourceName";
public static final String EXPIRY = "expiry";
public static final String IID = "iid";
public static final String API = "api";
public static final String USER_ID = "userid";


// Geometry
public static final String GEOM_POINT = "point";

// searchtype
public static final String JSON_SEARCH_TYPE = "searchType";
public static final String JSON_TEMPORAL_SEARCH = "temporalSearch_";
public static final String JSON_GEO_SEARCH = "geoSearch_";
public static final String JSON_RESPONSE_FILTER_SEARCH = "responseFilter_";
public static final String JSON_ATTRIBUTE_SEARCH = "attributeSearch_";
public static final String JSON_LATEST_SEARCH = "latestSearch_";


// request/response params
public static final String CONTENT_TYPE = "content-type";
Expand All @@ -167,7 +115,7 @@ public class ApiServerConstants {
public static final Pattern ID_REGEX =
Pattern.compile(
"^[a-zA-Z0-9.]{4,100}/{1}[a-zA-Z0-9.]{4,100}/{1}[a-zA-Z.]{4,100}/{1}[a-zA-Z-_.]{4,100}/{1}[a-zA-Z0-9-_.]{4,100}$");
public static final String RESPONSE_SIZE="response_size";
public static final String RESPONSE_SIZE = "response_size";

public static final double VALIDATION_ALLOWED_DIST = 1000.0;
public static final int VALIDATION_PAGINATION_LIMIT_MAX = 5000;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/iudx/rs/proxy/authenticator/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import java.util.List;

public class Constants {
public static final List<String> OPEN_ENDPOINTS = List.of("/ngsi-ld/v1/temporal/entities","/ngsi-ld/v1/entities");
public static final List<String> OPEN_ENDPOINTS = List.of("/ngsi-ld/v1/temporal/entities","/ngsi-ld/v1/entities","/ngsi-ld/v1/consumer/audit");
public static final long CACHE_TIMEOUT_AMOUNT = 30;
public static final String CAT_RSG_PATH = "/iudx/cat/v1/search";
public static final String CAT_ITEM_PATH = "/iudx/cat/v1/item";
public static final String CAT_RESOURCE_GROUP_PATH="/iudx/cat/v1/list/resourceGroup";
public static final String JSON_USERID = "userid";
public static final String JSON_IID = "iid";
public static final String JSON_EXPIRY = "expiry";
Expand Down
Loading

0 comments on commit 9be297f

Please sign in to comment.