Commit 59ecf6b8 authored by Ryan Berkheimer's avatar Ryan Berkheimer
Browse files

Successfully tested new feature - endpoint and connections can be used in the...

Successfully tested new feature - endpoint and connections can be used in the old style or the entire endpoint can be specced in the parameters map and referenced as a string in the manifest (see the basic-native set changes as of this commit to see the new format).
parent cc89221a
......@@ -18,10 +18,7 @@
"protocol": {
"plugin": "gov.noaa.messageapi.protocols.DefaultProtocol",
"constructor": {
"endpoints": [{
"plugin": "gov.noaa.messageapi.endpoints.NativeEndpoint",
"connections": "{}/resources/test/basic-native/parameters.json"
}]
"endpoints": "{}/resources/test/basic-native/parameters.json"
}
}
}
......
......@@ -25,18 +25,41 @@
"fields": ["initial-value", "string-test", "int-list-test", "null-test"]},
{"id": "coll-2",
"fields": ["initial-value", "string-test", "int-list-test"]}],
"connections": [{"id": "1",
"collections": ["coll-1"],
"constructor": {"native-library": "/workspaces/messageapi/libs/test/c/endpoint/libEndpointDemo.so",
"default-fields": [{"id": "test-integer",
"type": "integer",
"required": false,
"value": 5},
{"id": "return-list",
"type": "list(string)",
"required": false}],
"state-container": [{"id": "counter",
"type": "integer",
"required": false,
"value": 0}]}}]
"endpoints": [
{
"plugin": "gov.noaa.messageapi.endpoints.NativeEndpoint",
"connections": [
{
"id": "1",
"collections": [
"coll-1"
],
"constructor": {
"native-library": "/workspaces/messageapi/libs/test/c/endpoint/libEndpointDemo.so",
"default-fields": [
{
"id": "test-integer",
"type": "integer",
"required": false,
"value": 5
},
{
"id": "return-list",
"type": "list(string)",
"required": false
}
],
"state-container": [
{
"id": "counter",
"type": "integer",
"required": false,
"value": 0
}
]
}
}
]
}
]
}
......@@ -82,7 +82,7 @@ public class ContainerDefinition {
this.setEmptyTransformationMaps();
}
} catch (Exception e) {
System.out.println("WARNING - transformation parsing failed. Setting empty transformations.");
System.err.println("WARNING - transformation parsing failed. Setting empty transformations.");
this.setEmptyTransformationMaps();
}
}
......
......@@ -9,7 +9,7 @@ import java.util.stream.Collectors;
import gov.noaa.messageapi.parsers.protocols.MetadataParser;
import gov.noaa.messageapi.parsers.protocols.ConnectionParser;
import gov.noaa.messageapi.parsers.protocols.EndpointParser;
import gov.noaa.messageapi.utils.general.ListUtils;
/**
......@@ -26,7 +26,6 @@ public class ProtocolDefinition {
private Map<String,Object> metadataMap = null;
private Map<String,List<Map<String,Object>>> endpointMap = null;
@SuppressWarnings("unchecked")
public ProtocolDefinition(Map<String,Object> properties) throws Exception {
if (properties.containsKey("metadata")) {
this.parseMetadataSpec((String) properties.get("metadata"));
......@@ -34,7 +33,7 @@ public class ProtocolDefinition {
this.setEmptyMetadata();
}
if (properties.containsKey("endpoints")) {
this.parseEndpoints((List<Map<String,String>>) properties.get("endpoints"));
this.parseEndpoints(properties.get("endpoints"));
} else {
throw new Exception("Missing necessary 'endpoint' key when parsing protocol definition.");
}
......@@ -50,6 +49,52 @@ public class ProtocolDefinition {
this.metadataMap = parser.getMetadataMap();
}
/**
*
* @param endpointSpec A spec that is used to determine how to parse the session endpoints, either through a path or a direct map
* @throws Exception
*/
private void parseEndpoints(Object endpoints) throws Exception {
this.endpointMap = new HashMap<String,List<Map<String,Object>>>();
try {
if (endpoints instanceof String) {
this.parseEndpointsFromSpec((String) endpoints);
} else if (endpoints instanceof List) {
this.parseEndpointsFromManifest((List<Map<String,String>>) endpoints);
}
if (this.endpointMap.isEmpty()) {
System.err.println("After endpoint parsing, there are no endpoints. Nowhere to send data, so killing session before creation.");
System.exit(1);
}
} catch (Exception e) {
System.out.println("The endpoint specification was an invalid data structure Needs to be list or map.");
System.err.println("The protocol layer must contain a valid endpoint. Ending session building now.");
System.exit(1);
}
}
private void parseEndpointsFromSpec(String endpointSpec) throws Exception {
EndpointParser endpointParser = new EndpointParser(endpointSpec);
ListUtils.removeAllNulls(endpointParser.getEndpointMaps().stream().map(endpointMap -> {
if (endpointMap.containsKey("plugin")) {
if (endpointMap.containsKey("connections")) {
try {
return new AbstractMap.SimpleEntry<String, List<Map<String,Object>>>
((String)endpointMap.get("plugin"), (List<Map<String,Object>>)endpointMap.get("connections"));
} catch (Exception e) {
System.err.println("Warning - malformed endpoint spec (it will not get data, but this does not crash the session if there are other endpoints).");
System.err.println(String.format("Check formatting of the %s endpoint connections",(String) endpointMap.get("plugin")));
return null;
}
}
}
return null;
}).collect(Collectors.toList()))
.stream()
.forEach(endpointMapEntry -> this.endpointMap.put(endpointMapEntry.getKey(),
endpointMapEntry.getValue()));
}
/**
* Takes a list of endpoint entries from the global session spec and converts
* them to a single endpoint map, where endpoint classes point to a list of
......@@ -57,8 +102,7 @@ public class ProtocolDefinition {
* @param endpoints A list of endpoint entries that contain pointers to endpoint classes and a location of their connection maps.
* @throws Exception Throws an exception if there's a failure when constructing the ProtocolDefinition endpoint map.
*/
private void parseEndpoints(List<Map<String,String>> endpoints) throws Exception {
this.endpointMap = new HashMap<String,List<Map<String,Object>>>();
private void parseEndpointsFromManifest(List<Map<String,String>> endpoints) throws Exception {
ListUtils.removeAllNulls(endpoints.stream().map(endpointMap -> {
if (endpointMap.containsKey("plugin")) {
if (endpointMap.containsKey("connections")) {
......@@ -68,6 +112,8 @@ public class ProtocolDefinition {
((String) endpointMap.get("plugin"),
(List<Map<String,Object>>) connParser.getConnectionMaps());
} catch (Exception e) {
System.err.println("Warning - malformed endpoint spec (it will not get data, but this does not crash the session if there are other endpoints).");
System.err.println(String.format("Check formatting of the %s endpoint",(String) endpointMap.get("plugin")));
return null;
}
}
......
package gov.noaa.messageapi.parsers.protocols;
import java.util.Map;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import gov.noaa.messageapi.parsers.BaseParser;
import gov.noaa.messageapi.utils.general.PathUtils;
/**
* @author Ryan Berkheimer
*/
public class EndpointParser extends BaseParser {
public EndpointParser(String spec) throws Exception {
super(PathUtils.reconcileKeywords(spec));
}
@SuppressWarnings("unchecked")
public List<Map<String, Object>> getEndpointMaps() {
return (List<Map<String,Object>>) super.getValue("endpoints");
}
public void process(){
}
public Set<String> getRequiredKeys() {
Set<String> set = new HashSet<String>();
set.add("endpoints");
return set;
}
}
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 to comment