Commit 0da674dc by Adi Amir

merge mde-demo into develop

parent a918827a
......@@ -8,3 +8,4 @@ server.port=50040
server.host=0.0.0.0
server.worker.threads=1
-DconfigFile.location=/home/adi/git/ipgallery/java/mde/cfg/config.properties
-DconfigLocation=/home/adi/git/ipgallery/java/mde/cfg/
-Dds.IpAddress=172.16.1.244:8080
-Dredis.host=172.16.1.151
\ No newline at end of file
......@@ -8,7 +8,8 @@ mde:
- "8200:8000"
- "50040:50040"
extra_hosts:
- "transportation:172.16.1.56"
- "parking:172.16.1.56"
- "transportation:172.16.1.151"
- "parking:172.16.1.151"
- "public-safety:172.16.1.151"
# volumes:
# - "/opt/mcz/user_images:/opt/mcz/user_images"
\ No newline at end of file
......@@ -21,6 +21,8 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static util.Utils.currentDate;
/**
* Created by eli on 6/7/16.
*/
......@@ -59,14 +61,19 @@ public class MdeManager {
// example: GET ../mde/api/v1/chicago/transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
public BaseRestResponse doRead(RequestContext requestContext) {
BaseRestResponse brr=null;
String[] params = requestContext.params;
String serviceId = params[0] +"." +params[1];
String[] apiIdAsParams = getApiIdAsParams(requestContext);
String error = null;
SimpleHttpResponse resp=null;
brr = executeRequest(serviceId,apiIdAsParams,requestContext,null);
BaseRestResponse brr;
String serviceId;// = params[0] +"." +params[1];
boolean isMaintenanceRequest=false;
int offset=0;
if (requestContext.params[0].equals("maintenance")) {
isMaintenanceRequest=true;
//to skip first param("maintenance")
offset=1;
}
serviceId=getServiceId(requestContext, offset);
String[] apiIdAsParams = getApiIdAsParams(requestContext,offset);
brr = executeRequest(serviceId,apiIdAsParams,requestContext,null,isMaintenanceRequest);
return brr;
}
......@@ -75,20 +82,28 @@ public class MdeManager {
public BaseRestResponse doCreate(RequestContext requestContext, JsonNode content) {
BaseRestResponse brr=null;
String serviceId ;
int i;
serviceId = getServiceId(requestContext);
String[] apiIdAsParams = getApiIdAsParams(requestContext);
brr = executeRequest(serviceId,apiIdAsParams,requestContext,content);
int offset=0;
boolean isMaintenanceRequest=false;
System.out.print(Thread.currentThread().getName()+ " " + currentDate() +" MdeManager.doCreate: ");
if (requestContext.params[0].equals("maintenance")) {
System.out.print(" MAINTENANCE\n");
isMaintenanceRequest=true;
//to skip first param("maintenance")
offset=1;
}
serviceId = getServiceId(requestContext,offset);
String[] apiIdAsParams = getApiIdAsParams(requestContext, offset);
brr = executeRequest(serviceId,apiIdAsParams,requestContext,content, isMaintenanceRequest);
return brr;
}
private BaseRestResponse executeRequest(String serviceId, String[] apiIdAsParams, RequestContext requestContext, JsonNode content) {
private BaseRestResponse executeRequest(String serviceId, String[] apiIdAsParams, RequestContext requestContext, JsonNode content, boolean isMaintenanceRequest) {
SimpleHttpResponse resp=null;
String error=null;
if (apiIdAsParams!=null) {
Api api = servicesRepository.getApi(serviceId, apiIdAsParams);
if (api != null) {
......@@ -98,7 +113,7 @@ public class MdeManager {
for (Action action : actions) {
BaseAdapter adapter = adaptersRepository.getAdapter(action.getAdapterId());
if (adapter != null) {// && adapter.getClass().isInstance(HttpAdapter.class)) {
RequestParams requestParams = convertToRequestParams(requestContext, content, api.getMapKeyToParamIndex());
RequestParams requestParams = convertToRequestParams(requestContext, apiIdAsParams, content, api.getMapKeyToParamIndex(),isMaintenanceRequest);
resp = ((HttpAdapter) adapter).executeFlow(action.getApiOut(), requestParams);
break;//currently only one action
}
......@@ -114,17 +129,17 @@ public class MdeManager {
return convertToBaseRestResponse(resp,error);
}
private String getServiceId(RequestContext requestContext) {
return requestContext.params[0]+"."+requestContext.params[1];
private String getServiceId(RequestContext requestContext, int offset) {
return requestContext.params[offset]+"."+requestContext.params[offset+1];
}
private String[] getApiIdAsParams(RequestContext requestContext) {
private String[] getApiIdAsParams(RequestContext requestContext, int offset) {
String[] params = null;
int size = requestContext.params.length;
if (size>2){
params = new String[size-2];
System.arraycopy(requestContext.params, 2, params,0, size-2);
if (size-offset>2){
params = new String[size-offset-2];
System.arraycopy(requestContext.params, 2+offset, params,0, size-offset-2);
}
return params;
}
......@@ -181,19 +196,12 @@ public class MdeManager {
private RequestParams convertToRequestParams(RequestContext requestContext, JsonNode content, Map<String, Integer> mapVariableToParamIndex, boolean isMaintenanceRequest) {
private RequestParams convertToRequestParams(RequestContext requestContext, String[] apiIdAsParams, JsonNode content, Map<String, Integer> mapVariableToParamIndex, boolean isMaintenanceRequest) {
RequestParams requestParams = new RequestParams();
if (isMaintenanceRequest){
String[] params = new String[requestContext.params.length-1];
for (int i = 1 ; i< requestContext.params.length; i++){
params[i-1]=requestContext.params[i];
}
requestParams.setParams(params);
requestParams.setIsMaintenance(true);
}
else
requestParams.setParams(requestContext.params);
requestParams.setParams(apiIdAsParams);
requestParams.setIsMaintenance(isMaintenanceRequest);
requestParams.setQueryParameters(requestContext.queryParameters);
requestParams.setMethod(requestContext.enumCrudMethod);
if (content != null )
......@@ -204,14 +212,15 @@ public class MdeManager {
Map<String,String> keyValueVariables = mapVariableToParamIndex.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey(),
e -> requestContext.params[e.getValue()]));
//if maintenance request, set offset of 1 to skip "maintenance"
e -> requestContext.params[((isMaintenanceRequest)?(e.getValue()+1):e.getValue())]));
requestParams.setVariablesValues(keyValueVariables);
}
return requestParams;
}
private RequestParams convertToRequestParams(RequestContext requestContext, JsonNode content,Map<String, Integer> mapVariableToParamIndex) {
return convertToRequestParams(requestContext, content, mapVariableToParamIndex, false);
private RequestParams convertToRequestParams(RequestContext requestContext, String[] apiIdAsParams, JsonNode content,Map<String, Integer> mapVariableToParamIndex) {
return convertToRequestParams(requestContext, apiIdAsParams, content, mapVariableToParamIndex, false);
}
}
......@@ -64,6 +64,8 @@ public class WebSocketAction extends BaseAction<WebSocketActionParams ,RequestPa
RequestParams inRequestParams = (requestParams!=null)?requestParams:getRunTimeInput();
String mdeKey=null;
String uid=null;
if (!inRequestParams.isMaintenance()){
String httpPayload = inRequestParams.getContent();
try {
details = (OnEventDetails) Utils.readObjectFromString1(httpPayload, OnEventDetails.class);
......@@ -71,6 +73,7 @@ public class WebSocketAction extends BaseAction<WebSocketActionParams ,RequestPa
e.printStackTrace();
return new SimpleHttpResponse(500, "Failed to read Request payload OnEventDetails");
}
}
Map<String, String> runTimeParameters = getRunTimeVariables();
String webSocket=null;
......@@ -84,7 +87,7 @@ public class WebSocketAction extends BaseAction<WebSocketActionParams ,RequestPa
mdeKey=getParams().getResolvedMdeKey(inRequestParams.getVariablesValues());
uid=getParams().getResolvedUid(inRequestParams.getVariablesValues());
webSocket=getParams().getResolvedWebSocket(getRunTimeVariables());
webSocketManager.connect(mdeKey,uid,webSocket,details);
webSocketManager.connect(mdeKey,uid,webSocket,details, inRequestParams.isMaintenance());
} catch (UnsupportedEncodingException e) {
return new SimpleHttpResponse(500, "failed to connect to webSocket with error: "+e);
} catch (ErrorLoginException e) {
......
......@@ -43,32 +43,57 @@ public class WebSocketManager {
return false;
}
public void connect(String connectionId, String uid, String webSocketUrl, OnEventDetails details) throws Exception {
public void connect(String connectionId,
String uid,
String webSocketUrl,
OnEventDetails details,
boolean isReconnect) throws Exception {
StringBuilder sb = new StringBuilder();
sb.append(connectionId).append(":").append(uid);
String key = sb.toString();
WebSocketConnection connection,reConnection;
WebSocketConnection connection, reConnection;
// synchronized (this.webSocketConnections) {
if (!webSocketConnections.containsKey(connectionId)) {
connection = new WebSocketConnection(adapterId,connectionId, uid, webSocketUrl, auth2Client, defaultHeaders, details,null);
this.startConnection(connection,details);
System.out.println("websocketManager.connect[connectionId = " + connectionId + "]. create new connection");
connection = new WebSocketConnection(adapterId, connectionId, uid, webSocketUrl, auth2Client, defaultHeaders, details, null);
synchronized (connection) {
this.startConnection(connection, details, false);
webSocketConnections.put(connection.getConnectionId(), connection);
System.out.println(((isReconnect) ? "RECONNECTED " : "") + "websocket created successfully: "
+ connection.getConnectionId() + " uid=" + connection.getUid() + " " + ((details != null) ? details.toString() : ""));
}
else{
// }
} else {
connection = webSocketConnections.get(connectionId);
synchronized (connection) {
if (connection.isClosedWebSocketWaitingForMaintenance()) {
// connection = webSocketConnections.get().get(connectionId);
System.out.println("websocketManager.connect[connectionId = " + connectionId + ", adapterId=" + connection.getAdapterId() + "]. isReconnect=" + isReconnect + ", isConnectionWaitingForMaintenance" + connection.isDisconnectedWaitingForMaintenance());
if (isReconnect && connection.isDisconnectedWaitingForMaintenance()) {
System.out.println("websocketManager.RECONNECT[connectionId = " + connectionId + ", adapterId=" + connection.getAdapterId() + "]. RECONNECTING starts ");
reConnection = createReconnection(webSocketUrl, connection);
this.startConnection(reConnection, details);
connection.setIsClosedWebSocketWaitForMaintenance(false);
} else {
this.startConnection(reConnection, details, true);
webSocketConnections.put(connection.getConnectionId(), connection);
System.out.println("RECONNECTED websocket created successfully: "
+ connection.getConnectionId() + " uid=" + connection.getUid());
connection.setDisconnectedWaitingForMaintenance(false);
connection.notify();
} else { //if (!isReconnect){
connection.addEventListener(uid, details);
System.out.println("websocket updated successfully: " + connectionId + " " + "uid="
+ uid + " was added to websocket. " + ((details != null) ? details.toString() : "Details=NULL"));
}
}
}
// }
}
private WebSocketConnection createReconnection(String reConnectUrl, WebSocketConnection connection) throws ErrorLoginException, IOException, WebSocketException {
private WebSocketConnection createReconnection(String reConnectUrl,
WebSocketConnection connection) throws ErrorLoginException, IOException, WebSocketException {
WebSocketConnection reConnection = new WebSocketConnection((connection.getAdapterId()),
connection.getConnectionId(),
......@@ -82,18 +107,13 @@ public class WebSocketManager {
return reConnection;
}
private void startConnection(WebSocketConnection connection, OnEventDetails details) throws Exception {
synchronized (connection){
private void startConnection(WebSocketConnection connection, OnEventDetails details, boolean isReconnect) throws Exception {
connection.start();
connection.wait();
if (connection.getException()!=null)
if (connection.getException() != null)
throw connection.getException();
else {
webSocketConnections.put(connection.getConnectionId(), connection);
System.out.println(((details==null)?"RECONNECTED ":"")+"websocket created successfully: "
+ connection.getConnectionId()+ " uid="+connection.getUid()+" "+ ((details!=null)?details.toString():""));
}
}
}
}
}
......@@ -2,8 +2,11 @@ package util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.google.api.client.util.DateTime;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* Created by eli on 6/23/16.
......@@ -11,7 +14,11 @@ import java.io.IOException;
public class Utils {
public static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
public static String currentDate() {
Date date = new Date(System.currentTimeMillis());
DateTime dateTime = new DateTime(date);
return dateTime.toStringRfc3339();
}
public static String myTypeOf(Object obj){
int end_index = obj.toString().indexOf('@');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment