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;
*/
public class Constants {
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/";
}
package logic;
import com.fasterxml.jackson.databind.JsonNode;
import common.JsonHandler;
import logic.adapter.BaseAdapter;
//import logic.adapter.HttpAdapter.model.DataHttpAdapter;
import logic.adapter.HttpAdapter.HttpAdapter;
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.io.iface.ILogger;
import microservice.types.BaseRestResponse;
import rx.functions.Actions;
import util.MDEResult;
import java.util.List;
......@@ -19,68 +26,58 @@ public class MdeManager {
private static final String DS_IP_ADDRESS = System.getProperty("ds.IpAddress", "localhost:8080");
private ILogger logger;
private AdaptersRepository adaptersRepository;
// // TODO: 7/5/16
// private ServicesRepository adaptersRepository;
private ServicesRepository servicesRepository;
private List<String> serviceList = null;
public MdeManager(ILogger logger) {
try{
this.logger = logger;
LoadServices();
LoadServices(logger);
loadAdapters(logger);
ValidityCheck();
}
catch(Exception e){
logger.error("Failed to construct MdeManager with exception "+e);
}
}
private void loadAdapters(ILogger logger)
{
adaptersRepository = new AdaptersRepository(logger);
adaptersRepository.load();
public ServicesRepository getServicesRepository() {
return servicesRepository;
}
//read the cfg file SERVICES_FILE_NAME, to load each of the supported services
public void LoadServices() {
// final static String file = BASE_FILE_LOCATION+RELETIVE_FILE_PATH;
// JsonNode jNode = readJsonFromFile(file);
public void setServicesRepository(ServicesRepository servicesRepository) {
this.servicesRepository = servicesRepository;
}
// example: GET ../mde/chicago/transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
public BaseRestResponse doRead(RequestContext requestContext) {
BaseRestResponse brr;
// String tenantId = requestContext.params[0];
String serviceId = requestContext.params[0];
String apiId = requestContext.params[1];
// example: GET ../mde/chicago.transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
// Service service = servicesRepository.getService(serviceId)
// Actions actions = servicesRepository.getServiceActions(apiId);
// 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) {
MDEResult mdeResult = adapter.activateCommand(requestContext,/*action.apiOut*/ "getroutes");
brr = new BaseRestResponse(mdeResult.getSuccess(), mdeResult.getError());
brr.objectNode = mdeResult.getJsonData().get("data");
}
else
BaseRestResponse brr=null;
String serviceId = requestContext.params[0] +"." +requestContext.params[1];
String apiId = requestContext.params[2];
Api api= servicesRepository.getApi(serviceId,apiId);
List<Action> actions = api.getActions();
// TODO: 7/18/16 currently handle one action only
for(Action action : actions)
{
brr=new BaseRestResponse(false, "failed to find adapter "/*+ action.getAdapterId()*/+" adapter.chicago.transportation" );
BaseAdapter adapter = adaptersRepository.getAdapter(action.getAdapterId());
if (adapter!=null) {
MDEResult mdeResult = adapter.activateCommand(requestContext,action.getApiOut());
if (mdeResult.getSuccess()) {
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
brr=new BaseRestResponse(false, "failed to find adapter "+ action.getAdapterId());
}
if (brr==null)
brr=new BaseRestResponse(false, "failed to activate command");
return brr;
......@@ -97,4 +94,24 @@ public class MdeManager {
public BaseRestResponse doDelete(RequestContext requestContext) {
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,38 +30,41 @@ public class AdaptersRepository {
jsonNode = Utils.readJsonNodeFromFile(Constants.CONFIG_LOCATION+Constants.ADAPTERS_PATH);
if (jsonNode!=null) {
adaptersArray = (ArrayNode) jsonNode.get("adapters");
this.loadAdapters( adaptersArray);
}else{
logger.error(Thread.currentThread().getStackTrace()[1].getMethodName()+" Failed to read file: "+ Constants.CONFIG_LOCATION+Constants.ADAPTERS_PATH);
}
}
if ( adaptersArray!= null && !adaptersArray.isNull() &&!adaptersArray.isArray()) {
// logger.error("syntax error in file: " + CONFIG_LOCATION + ADAPTERS_PATH + ". the value of \"adapters\" must be array node");
return;
}
else
// load adapters
{
for (int i = 0; i < adaptersArray.size(); i++) {
JsonNode jsonAdapter = adaptersArray.get(i);
String adapterId = jsonAdapter.get("id").asText();
String adapterType = jsonAdapter.get("type").asText();
switch (adapterType) {
case "HttpAdapter":
HttpAdapter httpAdapter = null;
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;
default:
//shouldnt get here
break;
}
private void loadAdapters(ArrayNode adaptersArray) {
if ( adaptersArray!= null && !adaptersArray.isNull() && adaptersArray.isArray()) {
for (int i = 0; i < adaptersArray.size(); i++) {
JsonNode jsonAdapter = adaptersArray.get(i);
String adapterId = jsonAdapter.get("id").asText();
String adapterType = jsonAdapter.get("type").asText();
switch (adapterType) {
case "HttpAdapter":
this.addHttpAdapter(adapterId);
break;
default:
//shouldnt get here
break;
}
}
}
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){
......
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.model.DataHttpApi;
import logic.adapter.model.DataModel;
import logic.adapter.repository.AdaptersRepository;
import microservice.io.iface.ILogger;
import org.junit.Test;
......@@ -13,6 +14,7 @@ public class TestHttpAdapter {
public void testHttpAdapter() {
HttpAdapter httpAdapter;
try {
httpAdapter = new HttpAdapter(logger, "adapter.chicago.transportation");
DataModel<DataHttpApi> data=httpAdapter.getDataModel();
System.out.println(data.toString());
......@@ -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;
import logic.adapter.model.DataModel;
import org.junit.Test;
import util.Utils;
import utils.common.JsonHandler;
import common.JsonHandler;
import java.lang.reflect.Type;
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