Commit 4b0482e7 by Eli Ben Baruch

Mde: first version that includes loading of services and adapters and…

Mde: first version that includes loading of services and adapters and implementation of some of the Rest methods (mostely READ)
parent 0abbf39b
...@@ -5,6 +5,7 @@ package defs; ...@@ -5,6 +5,7 @@ package defs;
*/ */
public class Constants { public class Constants {
public static final String ADAPTERS_PATH = "adapters.json"; public static final String ADAPTERS_PATH = "adapters.json";
public static final String SERVICES_PATH = "services.json";
public static final String CONFIG_LOCATION = "/opt/mcx/config/"; public static final String CONFIG_LOCATION = "/opt/mcx/config/";
} }
package logic; package logic;
import com.fasterxml.jackson.databind.JsonNode;
import common.JsonHandler;
import logic.adapter.BaseAdapter; import logic.adapter.BaseAdapter;
//import logic.adapter.HttpAdapter.model.DataHttpAdapter; //import logic.adapter.HttpAdapter.model.DataHttpAdapter;
import logic.adapter.HttpAdapter.HttpAdapter; import logic.adapter.HttpAdapter.HttpAdapter;
import logic.adapter.repository.AdaptersRepository; import logic.adapter.repository.AdaptersRepository;
import logic.service.model.Action;
import logic.service.model.Api;
import logic.service.model.Service;
import logic.service.repository.ServicesRepository;
import microservice.RequestContext; import microservice.RequestContext;
import microservice.io.iface.ILogger; import microservice.io.iface.ILogger;
import microservice.types.BaseRestResponse; import microservice.types.BaseRestResponse;
import rx.functions.Actions;
import util.MDEResult; import util.MDEResult;
import java.util.List; import java.util.List;
...@@ -19,68 +26,58 @@ public class MdeManager { ...@@ -19,68 +26,58 @@ public class MdeManager {
private static final String DS_IP_ADDRESS = System.getProperty("ds.IpAddress", "localhost:8080"); private static final String DS_IP_ADDRESS = System.getProperty("ds.IpAddress", "localhost:8080");
private ILogger logger; private ILogger logger;
private AdaptersRepository adaptersRepository; private AdaptersRepository adaptersRepository;
// // TODO: 7/5/16 private ServicesRepository servicesRepository;
// private ServicesRepository adaptersRepository;
private List<String> serviceList = null;
public MdeManager(ILogger logger) { public MdeManager(ILogger logger) {
try{ try{
this.logger = logger; this.logger = logger;
LoadServices(); LoadServices(logger);
loadAdapters(logger); loadAdapters(logger);
ValidityCheck();
} }
catch(Exception e){ catch(Exception e){
logger.error("Failed to construct MdeManager with exception "+e);
} }
} }
private void loadAdapters(ILogger logger) public ServicesRepository getServicesRepository() {
{ return servicesRepository;
adaptersRepository = new AdaptersRepository(logger);
adaptersRepository.load();
} }
//read the cfg file SERVICES_FILE_NAME, to load each of the supported services public void setServicesRepository(ServicesRepository servicesRepository) {
public void LoadServices() { this.servicesRepository = servicesRepository;
// final static String file = BASE_FILE_LOCATION+RELETIVE_FILE_PATH;
// JsonNode jNode = readJsonFromFile(file);
} }
// example: GET ../mde/chicago/transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
public BaseRestResponse doRead(RequestContext requestContext) { public BaseRestResponse doRead(RequestContext requestContext) {
BaseRestResponse brr=null;
BaseRestResponse brr; String serviceId = requestContext.params[0] +"." +requestContext.params[1];
// String tenantId = requestContext.params[0]; String apiId = requestContext.params[2];
String serviceId = requestContext.params[0];
String apiId = requestContext.params[1]; Api api= servicesRepository.getApi(serviceId,apiId);
List<Action> actions = api.getActions();
// example: GET ../mde/chicago.transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW // TODO: 7/18/16 currently handle one action only
for(Action action : actions)
// Service service = servicesRepository.getService(serviceId) {
// Actions actions = servicesRepository.getServiceActions(apiId); BaseAdapter adapter = adaptersRepository.getAdapter(action.getAdapterId());
// for(Action action : actions)
// {
// String actionType = action.getType();
// if (actionType.equals(("forwardRequest"))) {
// BaseAdapter adapter = adaptersRepository.getAdapter(action.getAdapterId() );
// adapter.activateCommand(action.apiOut);
// }
//
// }
// adapter.activateCommand(action.apiOut);
String name = Thread.currentThread().getStackTrace()[1].getMethodName();
HttpAdapter adapter = (HttpAdapter) adaptersRepository.getAdapter("adapter.chicago.transportation" );
if (adapter!=null) { if (adapter!=null) {
MDEResult mdeResult = adapter.activateCommand(requestContext,/*action.apiOut*/ "getroutes"); MDEResult mdeResult = adapter.activateCommand(requestContext,action.getApiOut());
brr = new BaseRestResponse(mdeResult.getSuccess(), mdeResult.getError()); if (mdeResult.getSuccess()) {
brr.objectNode = mdeResult.getJsonData().get("data"); brr = new BaseRestResponse(true, null);
JsonNode node = mdeResult.getJsonData().get("data");
String str = node.asText();
brr.objectNode = JsonHandler.getJsonNodeFromString(str);
}else{
brr=new BaseRestResponse(false, mdeResult.getError());
}
} }
else else
{ brr=new BaseRestResponse(false, "failed to find adapter "+ action.getAdapterId());
brr=new BaseRestResponse(false, "failed to find adapter "/*+ action.getAdapterId()*/+" adapter.chicago.transportation" );
} }
if (brr==null)
brr=new BaseRestResponse(false, "failed to activate command");
return brr; return brr;
...@@ -97,4 +94,24 @@ public class MdeManager { ...@@ -97,4 +94,24 @@ public class MdeManager {
public BaseRestResponse doDelete(RequestContext requestContext) { public BaseRestResponse doDelete(RequestContext requestContext) {
return new BaseRestResponse(false, "method " +Thread.currentThread().getStackTrace()[1].getMethodName() + "not implemented yet"); return new BaseRestResponse(false, "method " +Thread.currentThread().getStackTrace()[1].getMethodName() + "not implemented yet");
} }
// TODO: 7/18/16 check that each of the adapters loaded in the services actually exists
private void ValidityCheck() {
}
private void loadAdapters(ILogger logger)
{
adaptersRepository = new AdaptersRepository(logger);
adaptersRepository.load();
}
//read the cfg file SERVICES_FILE_NAME, to load each of the supported services
private void LoadServices(ILogger logger) {
servicesRepository = new ServicesRepository(logger);
servicesRepository.load();
}
} }
...@@ -30,28 +30,21 @@ public class AdaptersRepository { ...@@ -30,28 +30,21 @@ public class AdaptersRepository {
jsonNode = Utils.readJsonNodeFromFile(Constants.CONFIG_LOCATION+Constants.ADAPTERS_PATH); jsonNode = Utils.readJsonNodeFromFile(Constants.CONFIG_LOCATION+Constants.ADAPTERS_PATH);
if (jsonNode!=null) { if (jsonNode!=null) {
adaptersArray = (ArrayNode) jsonNode.get("adapters"); adaptersArray = (ArrayNode) jsonNode.get("adapters");
this.loadAdapters( adaptersArray);
if ( adaptersArray!= null && !adaptersArray.isNull() &&!adaptersArray.isArray()) { }else{
// logger.error("syntax error in file: " + CONFIG_LOCATION + ADAPTERS_PATH + ". the value of \"adapters\" must be array node"); logger.error(Thread.currentThread().getStackTrace()[1].getMethodName()+" Failed to read file: "+ Constants.CONFIG_LOCATION+Constants.ADAPTERS_PATH);
return;
} }
else }
// load adapters
{ private void loadAdapters(ArrayNode adaptersArray) {
if ( adaptersArray!= null && !adaptersArray.isNull() && adaptersArray.isArray()) {
for (int i = 0; i < adaptersArray.size(); i++) { for (int i = 0; i < adaptersArray.size(); i++) {
JsonNode jsonAdapter = adaptersArray.get(i); JsonNode jsonAdapter = adaptersArray.get(i);
String adapterId = jsonAdapter.get("id").asText(); String adapterId = jsonAdapter.get("id").asText();
String adapterType = jsonAdapter.get("type").asText(); String adapterType = jsonAdapter.get("type").asText();
switch (adapterType) { switch (adapterType) {
case "HttpAdapter": case "HttpAdapter":
HttpAdapter httpAdapter = null; this.addHttpAdapter(adapterId);
try {
httpAdapter = new HttpAdapter(logger, adapterId);
adapterList.put(adapterId, httpAdapter);
} catch (Exception e) {
e.printStackTrace();
logger.error("failed to load adapter: " + adapterId+ ", Exception:" + e.toString());
}
break; break;
default: default:
//shouldnt get here //shouldnt get here
...@@ -59,9 +52,19 @@ public class AdaptersRepository { ...@@ -59,9 +52,19 @@ public class AdaptersRepository {
} }
} }
} }
else
logger.error(Thread.currentThread().getStackTrace()[1].getMethodName()+" Failed to load adapters");
} }
private void addHttpAdapter(String adapterId) {
HttpAdapter httpAdapter;
try {
httpAdapter = new HttpAdapter(logger, adapterId);
adapterList.put(adapterId, httpAdapter);
} catch (Exception e) {
e.printStackTrace();
logger.error("failed to load adapter: " + adapterId+ ", Exception:" + e.toString());
}
} }
public BaseAdapter getAdapter(String adapterId){ public BaseAdapter getAdapter(String adapterId){
......
package logic.service.model;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Created by eli on 7/18/16.
*/
public class Action {
@JsonProperty("adapterId")
private String adapterId;
@JsonProperty("apiOut")
private String apiOut;
/**
*
* @return
* The adapterId
*/
@JsonProperty("adapterId")
public String getAdapterId() {
return adapterId;
}
/**
*
* @param adapterId
* The adapterId
*/
@JsonProperty("adapterId")
public void setAdapterId(String adapterId) {
this.adapterId = adapterId;
}
/**
*
* @return
* The apiOut
*/
@JsonProperty("apiOut")
public String getApiOut() {
return apiOut;
}
/**
*
* @param apiOut
* The apiOut
*/
@JsonProperty("apiOut")
public void setApiOut(String apiOut) {
this.apiOut = apiOut;
}
}
package logic.service.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
/**
* Created by eli on 7/18/16.
*/
public class Api {
@JsonProperty("apiIn")
private String apiIn;
@JsonProperty("actions")
private List<Action> actions = new ArrayList<>();
/**
*
* @return
* The apiIn
*/
@JsonProperty("apiIn")
public String getApiIn() {
return apiIn;
}
/**
*
* @param apiIn
* The apiIn
*/
@JsonProperty("apiIn")
public void setApiIn(String apiIn) {
this.apiIn = apiIn;
}
/**
*
* @return
* The actions
*/
@JsonProperty("actions")
public List<Action> getActions() {
return actions;
}
/**
*
* @param actions
* The actions
*/
@JsonProperty("actions")
public void setActions(List<Action> actions) {
this.actions = actions;
}
}
package logic.service.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.List;
/**
* Created by eli on 7/18/16.
*/
public class Service {
@JsonProperty("apiList")
private List<Api> apiList = new ArrayList<>();
/**
*
* @return
* The apiList
*/
@JsonProperty("apiList")
public List<Api> getApiList() {
return apiList;
}
/**
*
* @param apiList
* The apiList
*/
@JsonProperty("apiList")
public void setApiList(List<Api> apiList) {
this.apiList = apiList;
}
public Api getApi(String apiId){
for (Api api : apiList) {
if (api.getApiIn().equals(apiId))
return api;
}
return null;
}
}
package logic.service.repository;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import common.JsonHandler;
import defs.Constants;
import logic.adapter.BaseAdapter;
import logic.adapter.HttpAdapter.HttpAdapter;
import logic.service.model.Api;
import logic.service.model.Service;
import microservice.io.iface.ILogger;
import util.Utils;
import java.util.HashMap;
/**
* Created by eli on 7/18/16.
*/
public class ServicesRepository {
private ILogger logger;
private HashMap<String, Service> serviceList = null;
public ServicesRepository(ILogger logger) {
this.logger = logger;
serviceList = new HashMap<>();
}
public void load() {
ArrayNode servicesArray;
JsonNode jsonNode;
jsonNode = Utils.readJsonNodeFromFile(Constants.CONFIG_LOCATION+Constants.SERVICES_PATH);
if (jsonNode!=null) {
servicesArray = (ArrayNode) jsonNode.get("services");
this.loadServices( servicesArray);
}else{
logger.error(Thread.currentThread().getStackTrace()[1].getMethodName()+" Failed to read file: "+ Constants.CONFIG_LOCATION+Constants.SERVICES_PATH);
}
}
private void loadServices(ArrayNode servicesArray) {
if ( servicesArray!= null && !servicesArray.isNull() && servicesArray.isArray()) {
for (int i = 0; i < servicesArray.size(); i++) {
JsonNode jsonService = servicesArray.get(i);
String serviceId = jsonService.get("id").asText();
this.addService(serviceId);
}
}
else
logger.error(Thread.currentThread().getStackTrace()[1].getMethodName()+" Failed to load Services");
}
private void addService(String serviceId) {
JsonNode serviceNode;
if (serviceId!=null) {
StringBuilder serviceFile=new StringBuilder(Constants.CONFIG_LOCATION).
append("service.").append(serviceId).append(".json");
serviceNode = Utils.readJsonNodeFromFile(serviceFile.toString());
Service service = (Service)JsonHandler.getNodeAsObject(serviceNode,Service.class);
if (serviceList.putIfAbsent(serviceId,service) != null)
logger.debug("service " +serviceId + " was loaded successfully");
}
else{
logger.error("Failed to load serviceId (null)");
}
}
public Api getApi(String serviceId, String apiId) {
Service service = serviceList.get(serviceId);
if (service!=null){
return service.getApi(apiId);
}
else
return null;
}
}
import logic.adapter.HttpAdapter.HttpAdapter; import logic.adapter.HttpAdapter.HttpAdapter;
import logic.adapter.HttpAdapter.model.DataHttpApi; import logic.adapter.HttpAdapter.model.DataHttpApi;
import logic.adapter.model.DataModel; import logic.adapter.model.DataModel;
import logic.adapter.repository.AdaptersRepository;
import microservice.io.iface.ILogger; import microservice.io.iface.ILogger;
import org.junit.Test; import org.junit.Test;
...@@ -13,6 +14,7 @@ public class TestHttpAdapter { ...@@ -13,6 +14,7 @@ public class TestHttpAdapter {
public void testHttpAdapter() { public void testHttpAdapter() {
HttpAdapter httpAdapter; HttpAdapter httpAdapter;
try { try {
httpAdapter = new HttpAdapter(logger, "adapter.chicago.transportation"); httpAdapter = new HttpAdapter(logger, "adapter.chicago.transportation");
DataModel<DataHttpApi> data=httpAdapter.getDataModel(); DataModel<DataHttpApi> data=httpAdapter.getDataModel();
System.out.println(data.toString()); System.out.println(data.toString());
...@@ -21,4 +23,19 @@ public class TestHttpAdapter { ...@@ -21,4 +23,19 @@ public class TestHttpAdapter {
} }
} }
@Test
public void testAdapterRepository() {
HttpAdapter httpAdapter;
try {
AdaptersRepository repository = new AdaptersRepository(logger);
repository.load();
} catch (Exception e) {
e.printStackTrace();
}
}
} }
...@@ -4,7 +4,7 @@ import logic.adapter.HttpAdapter.model.DataHttpApi; ...@@ -4,7 +4,7 @@ import logic.adapter.HttpAdapter.model.DataHttpApi;
import logic.adapter.model.DataModel; import logic.adapter.model.DataModel;
import org.junit.Test; import org.junit.Test;
import util.Utils; import util.Utils;
import utils.common.JsonHandler; import common.JsonHandler;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
......
import handlers.MdeHandler;
import microservice.io.iface.ILogger;
import microservice.io.impl.ILogger4jImpl;
import org.junit.Test;
/**
* Created by eli on 7/18/16.
*/
public class TestMdeHandler {
@Test
public void testMdeHandler(){
ILogger logger = new ILogger4jImpl("testMdeHandler");
MdeHandler mdeHandler = new MdeHandler(logger);
mdeHandler.init();
System.out.println("testMdeHandler() completed");
}
}
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