Commit 0da674dc by Adi Amir

merge mde-demo into develop

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