Skip to content

Commit

Permalink
Fixed JSON Array to XML Array conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmusavi committed Dec 6, 2018
1 parent cd2cd16 commit 929264f
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 53 deletions.
Binary file modified docs/ODE_Output_Schema_Reference.docx
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class AppContext {
public static final String METADATA_STRING = "metadata";
public static final String ODE_ASN1_DATA = "OdeAsn1Data";
public static final String DATA_STRING = "data";
public static final String ENCODING_STRING = "encodings";
public static final String ENCODINGS_STRING = "encodings";
public static final String PAYLOAD_TYPE_STRING = "payloadType";
public static final String DATA_TYPE_STRING = "dataType";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

public class TravelerMessageFromHumanToAsnConverter {

public static final String SEQUENCE_STRING = "SEQUENCE";
public static final String TCONTENT_STRING = "tcontent";

private static final Logger logger = LoggerFactory.getLogger(TravelerMessageFromHumanToAsnConverter.class);

// JSON cannot have empty fields like XML, so the XML must be modified by
Expand Down Expand Up @@ -65,7 +68,6 @@ public static JsonNode transformDataFrames(JsonNode dataFrames) throws JsonUtils
while (dataFramesIter.hasNext()) {
ObjectNode oldFrame = (ObjectNode) dataFramesIter.next();
replaceDataFrame(oldFrame);
// wrap each data frame inside a TravelerDataFrame
replacedDataFrames.add(oldFrame);
}
}
Expand Down Expand Up @@ -240,23 +242,15 @@ public static void replaceContent(ObjectNode dataFrame) {
}
}

// final step, transform into correct format
JsonNode sequence = JsonUtils.newNode().set("SEQUENCE", newItems);
JsonNode sequence = JsonUtils.newNode().set(SEQUENCE_STRING, newItems);

// TODO the following field is called "content" but this results in a
// failed conversion to XML
// see @us.dot.its.jpo.ode.traveler.TimController.publish
dataFrame.set("tcontent", JsonUtils.newNode().set(replacedContentName, sequence));
dataFrame.set(TCONTENT_STRING, JsonUtils.newNode().set(replacedContentName, sequence));
dataFrame.remove("items");
}

// public static JsonNode jsonArray2Asn1Array (ArrayNode items) {
// ArrayNode newItems = JsonUtils.newNode().arrayNode();
// // transform into a sequence array
// JsonNode sequence = JsonUtils.newNode().set("SEQUENCE", newItems);
// return sequence;
// }

private static JsonNode buildItem(String itemStr) {
JsonNode item = null;
// check to see if it is a itis code or text
Expand Down Expand Up @@ -346,10 +340,8 @@ public static JsonNode transformRegions(JsonNode regions) throws JsonUtilsExcept
while (regionsIter.hasNext()) {
JsonNode curRegion = regionsIter.next();
replaceRegion((ObjectNode) curRegion);
replacedRegions.add(JsonUtils.newNode().set("GeographicalPath", curRegion));
replacedRegions.add(curRegion);
}
} else {
replacedRegions.add(JsonUtils.newNode().put("GeographicalPath", EMPTY_FIELD_FLAG));
}

return replacedRegions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
public class Advisory extends Asn1Object {
private static final long serialVersionUID = 1L;
@JsonProperty("SEQUENCE")
private SEQUENCE[] sequence;

public SEQUENCE[] getSEQUENCE() {
return sequence;
private Sequence[] SEQUENCE;
public Sequence[] getSEQUENCE() {
return SEQUENCE;
}

public void setSEQUENCE(SEQUENCE[] sequence) {
this.sequence = sequence;
public void setSEQUENCE(Sequence[] sEQUENCE) {
SEQUENCE = sEQUENCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import us.dot.its.jpo.ode.plugin.asn1.Asn1Object;

public class SEQUENCE extends Asn1Object {
public class Sequence extends Asn1Object {
private static final long serialVersionUID = 1L;
private Item item;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TravelerDataFrame extends Asn1Object {

private String sspLocationRights;

private Region[] regions;
private GeographicalPath[] regions;

private String sspMsgRights1;

Expand All @@ -45,12 +45,12 @@ public void setSspLocationRights(String sspLocationRights) {
this.sspLocationRights = sspLocationRights;
}

public Region[] getRegions() {
return regions;
public GeographicalPath[] getRegions() {
return regions;
}

public void setRegions(Region[] regions) {
this.regions = regions;
public void setRegions(GeographicalPath[] regions) {
this.regions = regions;
}

public String getUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public String packageSignedTimIntoAsd(ServiceRequest request, String signedMsg)
metaObject.set("request", requestObj);

ArrayNode encodings = buildEncodings();
ObjectNode enc = XmlUtils.createEmbeddedJsonArrayForXmlConversion(AppContext.ENCODING_STRING, encodings);
ObjectNode enc = XmlUtils.createEmbeddedJsonArrayForXmlConversion(AppContext.ENCODINGS_STRING, encodings);
metaObject.set(AppContext.ENCODINGS_STRING, enc);

ObjectNode message = JsonUtils.newNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,30 @@ public Object process(String consumedData) {
JSONObject request = metadata.getJSONObject(TimController.REQUEST_STRING);

if (request.has(TimController.RSUS_STRING)) {
Object rsu = request.get(TimController.RSUS_STRING);
if (!(rsu instanceof JSONArray)) {
JSONArray rsus = new JSONArray();
rsus.put(rsu);
request.put(TimController.RSUS_STRING, rsus);
JSONObject rsusIn = (JSONObject) request.get(TimController.RSUS_STRING);
if (rsusIn.has(TimController.RSUS_STRING)) {
Object rsu_ = rsusIn.get(TimController.RSUS_STRING);
JSONArray rsusOut = new JSONArray();
if (rsu_ instanceof JSONArray) {
JSONArray rsusInArray = (JSONArray) rsu_;
for (int i = 0; i < rsusInArray.length(); i++) {
JSONObject rsu = (JSONObject) rsusInArray.get(i);
rsusOut.put(rsu);
}
} else {
rsusOut.put(rsu_);
}
request.put(TimController.RSUS_STRING, rsusOut);

// Convert JSON to POJO
ServiceRequest servicerequest = getServicerequest(consumedObj);

processEncodedTim(servicerequest, consumedObj);
}
}

// Convert JSON to POJO
ServiceRequest servicerequest = getServicerequest(consumedObj);

processEncodedTim(servicerequest, consumedObj);

} else {
throw new Asn1EncodedDataRouterException("Encoder response missing 'request'");
throw new Asn1EncodedDataRouterException("Invalid or missing '"
+ TimController.REQUEST_STRING + "' object in the encoder response");
}
} catch (Exception e) {
String msg = "Error in processing received message from ASN.1 Encoder module: " + consumedData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ public class TimController {

public static final String DATA_FRAMES_STRING = "dataFrames";

public static final String RSU_STRING = "rsu_";

public static final String RSUS_STRING = "rsus";

public static final String REQUEST_STRING = "request";
Expand Down Expand Up @@ -599,8 +597,6 @@ private String convertToXml(DdsAdvisorySituationData asd, ObjectNode encodableTi
fixedXml = fixedXml.replaceAll("node_LatLon>", "node-LatLon>");
fixedXml = fixedXml.replaceAll("nodeLL>", "NodeLL>");
fixedXml = fixedXml.replaceAll("nodeXY>", "NodeXY>");
fixedXml = fixedXml.replaceAll("sequence>", "SEQUENCE>");
fixedXml = fixedXml.replaceAll("geographicalPath>", "GeographicalPath>");

// workarounds for self-closing tags
fixedXml = fixedXml.replaceAll(TravelerMessageFromHumanToAsnConverter.EMPTY_FIELD_FLAG, "");
Expand All @@ -617,14 +613,14 @@ private String convertToXml(DdsAdvisorySituationData asd, ObjectNode encodableTi

private static void convertEncodingsArray(DdsAdvisorySituationData asd, ObjectNode metaObject) throws JsonUtilsException, XmlUtilsException {
ArrayNode encodings = buildEncodings(asd);
ObjectNode enc = XmlUtils.createEmbeddedJsonArrayForXmlConversion(AppContext.ENCODING_STRING, encodings);
ObjectNode enc = XmlUtils.createEmbeddedJsonArrayForXmlConversion(AppContext.ENCODINGS_STRING, encodings);
metaObject.set(AppContext.ENCODINGS_STRING, enc);
}

private static void convertRsusArray(ObjectNode inOrderTidObj, ObjectNode metaObject) {
//Convert 'rsus' JSON array to XML array
ObjectNode request = (ObjectNode) inOrderTidObj.get(REQUEST_STRING);
ObjectNode rsus = XmlUtils.createEmbeddedJsonArrayForXmlConversion(RSU_STRING, (ArrayNode) request.get(RSUS_STRING));
ObjectNode rsus = XmlUtils.createEmbeddedJsonArrayForXmlConversion(RSUS_STRING, (ArrayNode) request.get(RSUS_STRING));
request.set(RSUS_STRING, rsus);
metaObject.set(REQUEST_STRING, request);
}
Expand All @@ -640,8 +636,8 @@ private static ArrayNode convertRegionsArray(ObjectNode timObj) {
ArrayNode dataFrames = (ArrayNode) timObj.get(DATA_FRAMES_STRING);
for (int j = 0; j < dataFrames.size(); j++) {
ObjectNode dataFrame = (ObjectNode) dataFrames.get(j);

ArrayNode regionsOld = (ArrayNode) dataFrame.get(REGIONS_STRING);

ObjectNode regionsNew = XmlUtils.createEmbeddedJsonArrayForXmlConversion(GEOGRAPHICAL_PATH_STRING, regionsOld);
dataFrame.set(REGIONS_STRING, regionsNew);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,21 +552,21 @@ public void testConvertDataFramesArrays() throws JsonUtilsException, XmlUtilsExc
public void testConvertRsusArray() throws JsonUtilsException, XmlUtilsException {
String single = "{\"ode\":{\"version\":3,\"verb\":\"POST\"},\"rsus\":{\"rsu_\":[{\"rsuTarget\":\"127.0.0.3\",\"rsuUsername\":\"v3user\",\"rsuPassword\":\"password\",\"rsuRetries\":1,\"rsuTimeout\":1000,\"rsuIndex\":10}]},\"snmp\":{\"rsuid\":\"00000083\",\"msgid\":31,\"mode\":1,\"channel\":178,\"interval\":2,\"deliverystart\":\"2017-06-01T17:47:11-05:00\",\"deliverystop\":\"2018-01-01T17:47:11-05:15\",\"enable\":1,\"status\":4}}";
String singleXmlExpected = "";
assertConvertArray(single, TimController.RSUS_STRING, TimController.RSU_STRING, singleXmlExpected);
assertConvertArray(single, TimController.RSUS_STRING, TimController.RSUS_STRING, singleXmlExpected);

String multi = "{\"ode\":{\"version\":3,\"verb\":\"POST\"},\"rsus\":{\"rsu_\":[{\"rsuTarget\":\"127.0.0.1\",\"rsuUsername\":\"v3user\",\"rsuPassword\":\"password\",\"rsuRetries\":0,\"rsuTimeout\":2000,\"rsuIndex\":10},{\"rsuTarget\":\"127.0.0.2\",\"rsuUsername\":\"v3user\",\"rsuPassword\":\"password\",\"rsuRetries\":1,\"rsuTimeout\":1000,\"rsuIndex\":10},{\"rsuTarget\":\"127.0.0.3\",\"rsuUsername\":\"v3user\",\"rsuPassword\":\"password\",\"rsuRetries\":1,\"rsuTimeout\":1000,\"rsuIndex\":10}]},\"snmp\":{\"rsuid\":\"00000083\",\"msgid\":31,\"mode\":1,\"channel\":178,\"interval\":2,\"deliverystart\":\"2017-06-01T17:47:11-05:00\",\"deliverystop\":\"2018-01-01T17:47:11-05:15\",\"enable\":1,\"status\":4}}";
String multiXmlExpected = "";
assertConvertArray(multi, TimController.RSUS_STRING, TimController.RSU_STRING, multiXmlExpected);
assertConvertArray(multi, TimController.RSUS_STRING, TimController.RSUS_STRING, multiXmlExpected);
}

@Test @Ignore
public void testConvertEncodingsArray() throws JsonUtilsException, XmlUtilsException {
String single = "{\"payloadType\":\"us.dot.its.jpo.ode.model.OdeTimPayload\",\"serialId\":{\"streamId\":\"edbbf3f2-f559-4bee-ab81-cfdec8ba2701\",\"bundleSize\":1,\"bundleId\":2,\"recordId\":0,\"serialNumber\":2},\"odeReceivedAt\":\"2018-12-04T16:14:28.238Z\",\"schemaVersion\":6,\"recordGeneratedAt\":\"2017-03-13T06:07:11Z\",\"recordGeneratedBy\":\"TMC\",\"sanitized\":false,\"request\":{\"ode\":{\"version\":3,\"verb\":\"POST\"},\"rsus\":{\"rsu_\":[{\"rsuTarget\":\"127.0.0.1\",\"rsuUsername\":\"v3user\",\"rsuPassword\":\"password\",\"rsuRetries\":0,\"rsuTimeout\":2000,\"rsuIndex\":10},{\"rsuTarget\":\"127.0.0.2\",\"rsuUsername\":\"v3user\",\"rsuPassword\":\"password\",\"rsuRetries\":1,\"rsuTimeout\":1000,\"rsuIndex\":10},{\"rsuTarget\":\"127.0.0.3\",\"rsuUsername\":\"v3user\",\"rsuPassword\":\"password\",\"rsuRetries\":1,\"rsuTimeout\":1000,\"rsuIndex\":10}]},\"snmp\":{\"rsuid\":\"00000083\",\"msgid\":31,\"mode\":1,\"channel\":178,\"interval\":2,\"deliverystart\":\"2017-06-01T17:47:11-05:00\",\"deliverystop\":\"2018-01-01T17:47:11-05:15\",\"enable\":1,\"status\":4}},\"encodings\":[{\"elementName\":\"MessageFrame\",\"elementType\":\"MessageFrame\",\"encodingRule\":\"UPER\"}]}";
String singleXmlExpected = "";
assertConvertArray(single, AppContext.ENCODINGS_STRING, AppContext.ENCODING_STRING, singleXmlExpected);
assertConvertArray(single, AppContext.ENCODINGS_STRING, AppContext.ENCODINGS_STRING, singleXmlExpected);

String multi = "[{\"geographicalPath\":{\"name\":\"bob\",\"id\":{\"region\":\"23\",\"id\":\"33\"},\"anchor\":{\"lat\":\"416784730\",\"llong\":\"-1087827750\",\"elevation\":\"9171\"},\"laneWidth\":\"700\",\"directionality\":{\"both\":\"EMPTY_TAG\"},\"closedPath\":\"BOOLEAN_OBJECT_FALSE\",\"direction\":\"1010101010101010\",\"description\":{\"geometry\":{\"direction\":\"1010101010101010\",\"extent\":\"1\",\"laneWidth\":\"3300\",\"circle\":{\"center\":{\"lat\":\"416784730\",\"llong\":\"-1087827750\",\"elevation\":\"9171\"},\"radius\":\"15\",\"units\":\"7\"}}}}},{\"geographicalPath\":{\"name\":\"bob\",\"id\":{\"region\":\"23\",\"id\":\"33\"},\"anchor\":{\"lat\":\"416784730\",\"llong\":\"-1087827750\",\"elevation\":\"9171\"},\"laneWidth\":\"700\",\"directionality\":{\"both\":\"EMPTY_TAG\"},\"closedPath\":\"BOOLEAN_OBJECT_FALSE\",\"direction\":\"1010101010101010\",\"description\":{\"geometry\":{\"direction\":\"1010101010101010\",\"extent\":\"1\",\"laneWidth\":\"3300\",\"circle\":{\"center\":{\"lat\":\"416784730\",\"llong\":\"-1087827750\",\"elevation\":\"9171\"},\"radius\":\"15\",\"units\":\"7\"}}}}}]";
String multiXmlExpected = "";
assertConvertArray(multi, AppContext.ENCODINGS_STRING, AppContext.ENCODING_STRING, multiXmlExpected);
assertConvertArray(multi, AppContext.ENCODINGS_STRING, AppContext.ENCODINGS_STRING, multiXmlExpected);
}
}
9 changes: 9 additions & 0 deletions start-kafka-consumer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
# Use this script in a kafka shell instance to create a simple producer that reads from stdin and published to the given topic.

if [[ -z "$1" ]]; then
echo 'Usage:' $0 '<topic_name>'
exit 1;
fi

$KAFKA_HOME/bin/kafka-console-consumer.sh --topic=$1 --bootstrap-server=`broker-list.sh`
9 changes: 9 additions & 0 deletions start-kafka-producer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# Use this script in a kafka shell instance to create a simple producer that reads from stdin and published to the given topic.

if [[ -z "$1" ]]; then
echo 'Usage:' $0 '<topic_name>'
exit 1;
fi

$KAFKA_HOME/bin/kafka-console-producer.sh --topic=$1 --broker-list=`broker-list.sh`
2 changes: 1 addition & 1 deletion start-kafka-shell.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock --env-file ./.env -it jpoode_kafka:latest /bin/bash
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock --env-file ./.env -it wurstmeister/kafka:latest /bin/bash

0 comments on commit 929264f

Please sign in to comment.