From cf3d508e7e30d087f88e1c31e2e2e14a3827dc02 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 18 Oct 2023 13:46:08 -0400 Subject: [PATCH 01/12] Update CARLA Ambassador to use XMLRPC Client --- co-simulation/NOTICE-THIRD-PARTY.md | 34 +++++++++++ .../fed/carla/ambassador/CarlaAmbassador.java | 36 ++++++++++-- .../carla/carlaconnect/CarlaXmlRpcClient.java | 56 ++++++++++--------- .../InfrastructureMessageAmbassador.java | 2 +- 4 files changed, 96 insertions(+), 32 deletions(-) diff --git a/co-simulation/NOTICE-THIRD-PARTY.md b/co-simulation/NOTICE-THIRD-PARTY.md index 705c50ee..d0e22226 100644 --- a/co-simulation/NOTICE-THIRD-PARTY.md +++ b/co-simulation/NOTICE-THIRD-PARTY.md @@ -69,6 +69,30 @@ Apache Commons Text (1.6) * Source: https://git-wip-us.apache.org/repos/asf?p=commons-text +Apache WebServices Common Utilities (1.0.2) + + * License: The Apache Software License, Version 2.0 + * Maven artifact: `org.apache.ws.commons.util:ws-commons-util:1.0.2` + * Project: http://ws.apache.org/commons/util + * Source: https://svn.apache.org/viewcvs.cgi/webservices/commons/trunk/util + + +Apache XML-RPC Client Library (3.1.3) + + * License: The Apache Software License, Version 2.0 + * Maven artifact: `org.apache.xmlrpc:xmlrpc-client:3.1.3` + * Project: http://ws.apache.org/xmlrpc/xmlrpc-client/ + * Source: https://svn.apache.org/viewvc/webservices/xmlrpc/tags/xmlrpc-3.1.3/xmlrpc-client + + +Apache XML-RPC Common Library (3.1.3) + + * License: The Apache Software License, Version 2.0 + * Maven artifact: `org.apache.xmlrpc:xmlrpc-common:3.1.3` + * Project: http://ws.apache.org/xmlrpc/xmlrpc-common/ + * Source: https://svn.apache.org/viewvc/webservices/xmlrpc/tags/xmlrpc-3.1.3/xmlrpc-common + + Commons Compiler (2.7.5) * License: New BSD License @@ -355,3 +379,13 @@ Woodstox (5.1.0) * Maven artifact: `com.fasterxml.woodstox:woodstox-core:5.1.0` * Project: https://github.com/FasterXML/woodstox * Source: https://github.com/FasterXML/woodstox + + +XML Commons External Components XML APIs (1.0.b2) + + * License: The Apache Software License, Version 2.0 + * Maven artifact: `xml-apis:xml-apis:1.0.b2` + * Project: http://xml.apache.org/commons/#external + * Source: https://svn.apache.org/viewvc/xml/commons/tags/xml-commons-1_0_b2 + + diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index b0b35528..7047372c 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -16,6 +16,8 @@ import com.google.common.collect.Lists; import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang3.StringUtils; +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.client.XmlRpcClientException; import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaConnection; import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient; import org.eclipse.mosaic.fed.carla.config.CarlaConfiguration; @@ -23,6 +25,8 @@ import org.eclipse.mosaic.fed.sumo.traci.writer.ListTraciWriter; import org.eclipse.mosaic.fed.sumo.traci.writer.StringTraciWriter; import org.eclipse.mosaic.interactions.application.*; +import org.eclipse.mosaic.interactions.detector.DetectorRegistration; +import org.eclipse.mosaic.lib.objects.detector.Detector; import org.eclipse.mosaic.lib.util.ProcessLoggingThread; import org.eclipse.mosaic.lib.util.objects.ObjectInstantiation; import org.eclipse.mosaic.rti.TIME; @@ -37,6 +41,7 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.TimeUnit; @@ -110,6 +115,8 @@ public class CarlaAmbassador extends AbstractFederateAmbassador { */ private final PriorityBlockingQueue carlaV2xInteractionQueue = new PriorityBlockingQueue<>(); + private List registeredDetectors = new ArrayList<>(); + /** * Creates a new {@link CarlaAmbassador} object. * @@ -355,12 +362,16 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder this.executedTimes++; //call CarlaXmlRpcClient to ask for data whenever time advances - carlaXmlRpcClient.requestCarlaList(); - + for (Detector detector: registeredDetectors ) { + carlaXmlRpcClient.get_detected_objects(detector.getSensorId()); + } } catch (IllegalValueException e) { log.error("Error during advanceTime(" + time + ")", e); throw new InternalFederateException(e); } + catch (XmlRpcException e) { + log.error("Failed to connect to CARLA Adapter : ", e); + } } /** @@ -494,6 +505,21 @@ public void processInteraction(Interaction interaction) { } else if (interaction.getTypeId().equals(CarlaV2xMessageReception.TYPE_ID)) { this.receiveInteraction((CarlaV2xMessageReception) interaction); } + else if (interaction.getTypeId().equals(DetectorRegistration.TYPE_ID)) { + this.receiveInteraction((DetectorRegistration) interaction); + } + } + + + private void receiveInteraction(DetectorRegistration interaction) { + try { + carlaXmlRpcClient.create_sensor(interaction); + registeredDetectors.add(interaction.getDetector()); + } + catch(XmlRpcException e) { + log.error("Error occurred attempting to create sensor : {}", interaction.getDetector(), e); + } + } /** @@ -508,7 +534,7 @@ private void receiveInteraction(CarlaTraciResponse interaction) { carlaConnection.getDataOutputStream().write(interaction.getResult()); } } catch (Exception e) { - log.error("error occurs during process carla traci response interaction: " + e.getMessage()); + log.error("error occurs during process carla traci response interaction: {} ", e.getMessage()); } } @@ -526,7 +552,7 @@ private void receiveInteraction(SimulationStepResponse interaction) { } } catch (Exception e) { - log.error("error occurs during process simulation step response interaction: " + e.getMessage()); + log.error("error occurs during process simulation step response interaction: {}", e.getMessage()); } } @@ -536,7 +562,7 @@ private void receiveInteraction(SimulationStepResponse interaction) { * @param interaction CarlaV2xMessageReception interaction */ private void receiveInteraction(CarlaV2xMessageReception interaction) { - log.info("{} received V2x message: {}.", interaction.getReceiverID(), interaction.getMessage().toString()); + log.info("{} received V2x message: {}.", interaction.getReceiverID(), interaction.getMessage()); interactionQueue.add(interaction); } diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java index d74984a8..d4f1c06f 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java @@ -16,10 +16,15 @@ package org.eclipse.mosaic.fed.carla.carlaconnect; import java.net.URL; +import java.util.Arrays; +import java.util.List; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; +import org.apache.xmlrpc.client.XmlRpcClientException; +import org.eclipse.mosaic.interactions.detector.DetectorRegistration; +import org.eclipse.mosaic.lib.objects.detector.DetectedObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,7 +34,10 @@ public class CarlaXmlRpcClient{ boolean isConnected; - private String registeredFunction = "test.echo"; + private static final String CREATE_SENSOR = "create_simulated_semantic_lidar_sensor"; + private static final String GET_SENSOR = "get_simulated_sensor"; + private static final String GET_DETECTED_OBJECTS = "get_detected_objects"; + private XmlRpcClient client; private final Logger log = LoggerFactory.getLogger(this.getClass()); @@ -63,33 +71,29 @@ public void initialize(URL xmlRpcServerUrl) } - /** - * This method uses xmlrpc to connect with CARLA CDASim adapter service - * @throws XmlRpcException - */ - public void requestCarlaList() - { - if(!isConnected) - { - - try { - throw new XmlRpcException("Server is not connected!"); - } - catch (XmlRpcException e) - { - log.error("Server is not connected! {}", e.getMessage()); - } + public void create_sensor(DetectorRegistration registration) throws XmlRpcException{ + List location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); + List orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); + + if (isConnected) { + Object[] params = new Object[]{1/** TODO Infrastructure ID */, registration.getDetector().getSensorId(), location, orientation}; + Object result = client.execute(CREATE_SENSOR, params); + log.info((String)result); } - try{ - Object[] params = new Object[]{"Test " + java.time.LocalDateTime.now()}; - Object result = client.execute(registeredFunction, params); + else { + log.warn("XMLRpcClient is not connected to CARLA Adapter!"); + } + } + + public void get_detected_objects(String sensorId) throws XmlRpcException{ + if (isConnected) { + Object[] params = new Object[]{1/** TODO Infrastructure ID */, sensorId}; + Object result = client.execute(GET_DETECTED_OBJECTS, params); log.info((String)result); } - catch (XmlRpcException x) - { - log.error("Errors occurred with xmlrpc connection {}", x.getMessage()); - closeConnection(); - } - + else { + log.warn("XMLRpcClient is not connected to CARLA Adapter!"); + } } + } diff --git a/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java b/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java index f2376038..c6b0d116 100644 --- a/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java +++ b/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java @@ -40,6 +40,7 @@ import org.eclipse.mosaic.lib.objects.addressing.IpResolver; import org.eclipse.mosaic.lib.objects.communication.AdHocConfiguration; import org.eclipse.mosaic.lib.objects.communication.InterfaceConfiguration; +import org.eclipse.mosaic.lib.objects.detector.DetectedObject; import org.eclipse.mosaic.lib.objects.detector.Detector; import org.eclipse.mosaic.lib.objects.v2x.ExternalV2xMessage; import org.eclipse.mosaic.lib.objects.v2x.V2xMessage; @@ -290,7 +291,6 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder } log.info("Infrastructure message ambassador processing timestep to {}.", time); try { - // Handle any new infrastructure registration requests List newRegistrations = infrastructureRegistrationReceiver .getReceivedMessages(); From d88dbc3e4d42338223c97bea5903fb29507322bd Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 25 Oct 2023 00:54:20 -0400 Subject: [PATCH 02/12] Updated detection to use multidimensional arrays. Update CARLA Ambassador to trigger interactions for detections Updated Detector registration to include infrastructure ID --- .../fed/carla/ambassador/CarlaAmbassador.java | 27 +++++-- .../carla/carlaconnect/CarlaXmlRpcClient.java | 22 ++++-- .../InfrastructureMessageAmbassador.java | 2 +- .../detector/DetectorRegistration.java | 21 ++++- .../detector/DetectorRegistrationTest.java | 4 +- .../lib/objects/detector/DetectedObject.java | 79 ++++++------------- .../objects/detector/DetectedObjectTest.java | 20 ++--- 7 files changed, 92 insertions(+), 83 deletions(-) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index 7047372c..80dc6814 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -25,7 +25,9 @@ import org.eclipse.mosaic.fed.sumo.traci.writer.ListTraciWriter; import org.eclipse.mosaic.fed.sumo.traci.writer.StringTraciWriter; import org.eclipse.mosaic.interactions.application.*; +import org.eclipse.mosaic.interactions.detector.DetectedObjectInteraction; import org.eclipse.mosaic.interactions.detector.DetectorRegistration; +import org.eclipse.mosaic.lib.objects.detector.DetectedObject; import org.eclipse.mosaic.lib.objects.detector.Detector; import org.eclipse.mosaic.lib.util.ProcessLoggingThread; import org.eclipse.mosaic.lib.util.objects.ObjectInstantiation; @@ -115,7 +117,7 @@ public class CarlaAmbassador extends AbstractFederateAmbassador { */ private final PriorityBlockingQueue carlaV2xInteractionQueue = new PriorityBlockingQueue<>(); - private List registeredDetectors = new ArrayList<>(); + private List registeredDetectors = new ArrayList<>(); /** * Creates a new {@link CarlaAmbassador} object. @@ -358,16 +360,25 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder nextTimeStep += carlaConfig.updateInterval * TIME.MILLI_SECOND; isSimulationStep = false; } + // TODO: What is this. Why are we request a time advance based on this counter and + // what is it counting. It is labeled as counting the times we attempt to connect to + // CARLA but it seems to increment every time processTimeAdvanceGrant is called rti.requestAdvanceTime(nextTimeStep + this.executedTimes, 0, (byte) 2); this.executedTimes++; - - //call CarlaXmlRpcClient to ask for data whenever time advances - for (Detector detector: registeredDetectors ) { - carlaXmlRpcClient.get_detected_objects(detector.getSensorId()); + List detectedObjectInteractions = new ArrayList(); + // Get all detections from all currently registered detectors. + for (DetectorRegistration registration: registeredDetectors ) { + DetectedObject[] detections = carlaXmlRpcClient.getDetectedObjects( registration.getInfrastructureId() , registration.getDetector().getSensorId()); + for (DetectedObject detected: detections) { + detectedObjectInteractions.add(new DetectedObjectInteraction(time, detected)); + } + } + // trigger all detection interactions + for (DetectedObjectInteraction detectionInteraction: detectedObjectInteractions) { + this.rti.triggerInteraction(detectionInteraction); } } catch (IllegalValueException e) { log.error("Error during advanceTime(" + time + ")", e); - throw new InternalFederateException(e); } catch (XmlRpcException e) { log.error("Failed to connect to CARLA Adapter : ", e); @@ -513,8 +524,8 @@ else if (interaction.getTypeId().equals(DetectorRegistration.TYPE_ID)) { private void receiveInteraction(DetectorRegistration interaction) { try { - carlaXmlRpcClient.create_sensor(interaction); - registeredDetectors.add(interaction.getDetector()); + carlaXmlRpcClient.createSensor(interaction); + registeredDetectors.add(interaction); } catch(XmlRpcException e) { log.error("Error occurred attempting to create sensor : {}", interaction.getDetector(), e); diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java index d4f1c06f..1f6c3a92 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java @@ -16,6 +16,7 @@ package org.eclipse.mosaic.fed.carla.carlaconnect; import java.net.URL; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -23,11 +24,14 @@ import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import org.apache.xmlrpc.client.XmlRpcClientException; +import org.eclipse.mosaic.interactions.detector.DetectedObjectInteraction; import org.eclipse.mosaic.interactions.detector.DetectorRegistration; import org.eclipse.mosaic.lib.objects.detector.DetectedObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.gson.Gson; + /** * This is a class uses xmlrpc to connect with CARLA CDASim adapter service */ @@ -71,12 +75,12 @@ public void initialize(URL xmlRpcServerUrl) } - public void create_sensor(DetectorRegistration registration) throws XmlRpcException{ + public void createSensor(DetectorRegistration registration) throws XmlRpcException{ List location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); List orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); if (isConnected) { - Object[] params = new Object[]{1/** TODO Infrastructure ID */, registration.getDetector().getSensorId(), location, orientation}; + Object[] params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId(), location, orientation}; Object result = client.execute(CREATE_SENSOR, params); log.info((String)result); } @@ -85,14 +89,20 @@ public void create_sensor(DetectorRegistration registration) throws XmlRpcExcept } } - public void get_detected_objects(String sensorId) throws XmlRpcException{ + public DetectedObject[] getDetectedObjects(String infrastructureId ,String sensorId) throws XmlRpcException{ if (isConnected) { - Object[] params = new Object[]{1/** TODO Infrastructure ID */, sensorId}; + Object[] params = new Object[]{infrastructureId, sensorId}; Object result = client.execute(GET_DETECTED_OBJECTS, params); - log.info((String)result); + log.info("Detections from infrastructure {} sensor {} : {}", infrastructureId, sensorId, result); + String jsonResult = (String)result; + Gson gson = new Gson(); + DetectedObject[] parsedMessage = gson.fromJson(jsonResult, + DetectedObject[].class); + return parsedMessage; } else { - log.warn("XMLRpcClient is not connected to CARLA Adapter!"); + throw new XmlRpcException("XMLRpcClient is not connected to CARLA Adapter!"); + } } diff --git a/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java b/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java index c6b0d116..867cbb58 100644 --- a/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java +++ b/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java @@ -311,7 +311,7 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder log.debug("Sending SensorRegistration interactions for sensor : {}", reg.getSensors()); for (Detector sensor : reg.getSensors()) { // Trigger Sensor registrations for all listed sensors. - this.rti.triggerInteraction(new DetectorRegistration(time,sensor)); + this.rti.triggerInteraction(new DetectorRegistration(time,sensor,reg.getInfrastructureId())); } } else { diff --git a/co-simulation/lib/mosaic-interactions/src/main/java/org/eclipse/mosaic/interactions/detector/DetectorRegistration.java b/co-simulation/lib/mosaic-interactions/src/main/java/org/eclipse/mosaic/interactions/detector/DetectorRegistration.java index 034f17b4..2463aac1 100644 --- a/co-simulation/lib/mosaic-interactions/src/main/java/org/eclipse/mosaic/interactions/detector/DetectorRegistration.java +++ b/co-simulation/lib/mosaic-interactions/src/main/java/org/eclipse/mosaic/interactions/detector/DetectorRegistration.java @@ -22,14 +22,17 @@ public class DetectorRegistration extends Interaction { public static final String TYPE_ID = createTypeIdentifier(DetectorRegistration.class); private Detector detector; + + private String infrastructureId; /** * Constructor * @param time for interaction. * @param sensor to register. */ - public DetectorRegistration(long time, Detector sensor) { + public DetectorRegistration(long time, Detector sensor, String infrastructureId) { super(time); this.detector = sensor; + this.infrastructureId = infrastructureId; } /** * Getter for sensor/detector information. @@ -45,15 +48,20 @@ public Detector getDetector() { public void setDetector(Detector sensor) { this.detector = sensor; } - + public String getInfrastructureId() { + return infrastructureId; + } + public void setInfrastructureId(String infrastructureId) { + this.infrastructureId = infrastructureId; + } @Override public int hashCode() { final int prime = 31; int result = super.hashCode(); result = prime * result + ((detector == null) ? 0 : detector.hashCode()); + result = prime * result + ((infrastructureId == null) ? 0 : infrastructureId.hashCode()); return result; } - @Override public boolean equals(Object obj) { if (this == obj) @@ -68,8 +76,15 @@ public boolean equals(Object obj) { return false; } else if (!detector.equals(other.detector)) return false; + if (infrastructureId == null) { + if (other.infrastructureId != null) + return false; + } else if (!infrastructureId.equals(other.infrastructureId)) + return false; return true; } + + } diff --git a/co-simulation/lib/mosaic-interactions/src/test/java/org/eclipse/mosaic/interactions/detector/DetectorRegistrationTest.java b/co-simulation/lib/mosaic-interactions/src/test/java/org/eclipse/mosaic/interactions/detector/DetectorRegistrationTest.java index eff0fd8c..130247fa 100644 --- a/co-simulation/lib/mosaic-interactions/src/test/java/org/eclipse/mosaic/interactions/detector/DetectorRegistrationTest.java +++ b/co-simulation/lib/mosaic-interactions/src/test/java/org/eclipse/mosaic/interactions/detector/DetectorRegistrationTest.java @@ -29,7 +29,7 @@ public class DetectorRegistrationTest { DetectorRegistration detectorRegistration; @Before public void setUp() throws Exception { - detectorRegistration = new DetectorRegistration(0, null); + detectorRegistration = new DetectorRegistration(0, null, ""); } @Test @@ -39,7 +39,7 @@ public void testGetterSetterConstructor() { detectorRegistration.setDetector(detector); assertEquals(detector, detectorRegistration.getDetector()); - DetectorRegistration detectorRegistration1 = new DetectorRegistration(0, detector); + DetectorRegistration detectorRegistration1 = new DetectorRegistration(0, detector, "rsu_1"); assertNotEquals(detectorRegistration, detectorRegistration1); DetectorRegistration detectorRegistration2 = detectorRegistration1; diff --git a/co-simulation/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/detector/DetectedObject.java b/co-simulation/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/detector/DetectedObject.java index 59f22cdb..6c28e4aa 100644 --- a/co-simulation/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/detector/DetectedObject.java +++ b/co-simulation/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/detector/DetectedObject.java @@ -37,15 +37,15 @@ public final class DetectedObject implements Serializable { private CartesianPoint position; - private Double[] positionCovariance = new Double[9]; + private Double[][] positionCovariance = new Double[3][3]; private Vector3d velocity; - private Double[] velocityCovariance = new Double[9]; + private Double[][] velocityCovariance = new Double[3][3]; private Vector3d angularVelocity; - private Double[] angularVelocityCovariance = new Double[9]; + private Double[][] angularVelocityCovariance = new Double[3][3]; private Size size; /** @@ -96,56 +96,15 @@ public String getProjString() { return projString; } - /** - * Getter for 3x3 covariance associated with position represented as - * a 9 element vector for JSON serialization/deserialization. - * @return - */ - public Double[] getPositionCovariance() { - return positionCovariance; - } - - /** - * Setter for 3x3 covariance associated with position represented as - * a 9 element vector for JSON serialization/deserialization. - * @param positionCovariance - */ - public void setPositionCovariance(Double[] positionCovariance) { + public void setPositionCovariance(Double[][] positionCovariance) { this.positionCovariance = positionCovariance; } - /** - * Getter for 3x3 covariance associated with velocity represented as - * a 9 element vector for JSON serialization/deserialization. - * @return - */ - public Double[] getVelocityCovariance() { - return velocityCovariance; - } - - /** - * Setter for 3x3 covariance associated with velocity represented as - * a 9 element vector for JSON serialization/deserialization. - * @param positionCovariance - */ - public void setVelocityCovariance(Double[] velocityCovariance) { + public void setVelocityCovariance(Double[][] velocityCovariance) { this.velocityCovariance = velocityCovariance; } - /** - * Getter for 3x3 covariance associated with angular velocity represented - * as a 9 element vector for JSON serialization/deserialization. - * @return - */ - public Double[] getAngularVelocityCovariance() { - return angularVelocityCovariance; - } - /** - * Setter for 3x3 covariance associated with angular velocity represented - * as a 9 element vector for JSON serialization/deserialization. - * @param positionCovariance - */ - public void setAngularVelocityCovariance(Double[] angularVelocityCovariance) { + public void setAngularVelocityCovariance(Double[][] angularVelocityCovariance) { this.angularVelocityCovariance = angularVelocityCovariance; } @@ -181,6 +140,18 @@ public CartesianPoint getPosition() { return position; } + public Double[][] getPositionCovariance() { + return positionCovariance; + } + + public Double[][] getVelocityCovariance() { + return velocityCovariance; + } + + public Double[][] getAngularVelocityCovariance() { + return angularVelocityCovariance; + } + /** * Getter for object velocity. * @return @@ -289,11 +260,11 @@ public int hashCode() { result = prime * result + ((projString == null) ? 0 : projString.hashCode()); result = prime * result + ((objectId == null) ? 0 : objectId.hashCode()); result = prime * result + ((position == null) ? 0 : position.hashCode()); - result = prime * result + Arrays.hashCode(positionCovariance); + result = prime * result + Arrays.deepHashCode(positionCovariance); result = prime * result + ((velocity == null) ? 0 : velocity.hashCode()); - result = prime * result + Arrays.hashCode(velocityCovariance); + result = prime * result + Arrays.deepHashCode(velocityCovariance); result = prime * result + ((angularVelocity == null) ? 0 : angularVelocity.hashCode()); - result = prime * result + Arrays.hashCode(angularVelocityCovariance); + result = prime * result + Arrays.deepHashCode(angularVelocityCovariance); result = prime * result + ((size == null) ? 0 : size.hashCode()); return result; } @@ -331,21 +302,21 @@ public boolean equals(Object obj) { return false; } else if (!position.equals(other.position)) return false; - if (!Arrays.equals(positionCovariance, other.positionCovariance)) + if (!Arrays.deepEquals(positionCovariance, other.positionCovariance)) return false; if (velocity == null) { if (other.velocity != null) return false; } else if (!velocity.equals(other.velocity)) return false; - if (!Arrays.equals(velocityCovariance, other.velocityCovariance)) + if (!Arrays.deepEquals(velocityCovariance, other.velocityCovariance)) return false; if (angularVelocity == null) { if (other.angularVelocity != null) return false; } else if (!angularVelocity.equals(other.angularVelocity)) return false; - if (!Arrays.equals(angularVelocityCovariance, other.angularVelocityCovariance)) + if (!Arrays.deepEquals(angularVelocityCovariance, other.angularVelocityCovariance)) return false; if (size == null) { if (other.size != null) @@ -355,4 +326,6 @@ public boolean equals(Object obj) { return true; } + + } diff --git a/co-simulation/lib/mosaic-objects/src/test/java/org/eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java b/co-simulation/lib/mosaic-objects/src/test/java/org/eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java index 616e2ea7..7cb2117f 100644 --- a/co-simulation/lib/mosaic-objects/src/test/java/org/eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java +++ b/co-simulation/lib/mosaic-objects/src/test/java/org/eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java @@ -45,7 +45,7 @@ public void testDetectObjectJsonSerialization() { new Vector3d(0, 0, 0), new Vector3d(), new Size(0, 0, 0)); - Double[] covarianceMatrix = new Double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; + Double[][] covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; detectedObject.setPositionCovariance(covarianceMatrix); detectedObject.setVelocityCovariance(covarianceMatrix); detectedObject.setAngularVelocityCovariance(covarianceMatrix); @@ -63,21 +63,21 @@ public void testDetectObjectJsonSerialization() { + "\"y\":2.0," + "\"z\":3.2" + "}," - + "\"positionCovariance\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]," + + "\"positionCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," + "\"velocity\":" + "{" + "\"x\":0.0," + "\"y\":0.0," + "\"z\":0.0" + "}," - + "\"velocityCovariance\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]," + + "\"velocityCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," + "\"angularVelocity\":" + "{" + "\"x\":0.0," + "\"y\":0.0," + "\"z\":0.0" + "}," - + "\"angularVelocityCovariance\":[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]," + + "\"angularVelocityCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," + "\"size\":" + "{" + "\"length\":0.0," @@ -105,21 +105,21 @@ public void testDetectObjectJsonDeserialization() { + "\"y\":-2.0," + "\"z\":-3.2" + "}," - + "\"positionCovariance\":[1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]," + + "\"positionCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," + "\"velocity\":" + "{" + "\"x\":1," + "\"y\":1," + "\"z\":1" + "}," - + "\"velocityCovariance\":[1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]," + + "\"velocityCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," + "\"angularVelocity\":" + "{" + "\"x\":0.1," + "\"y\":0.2," + "\"z\":0.3" + "}," - + "\"angularVelocityCovariance\":[1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0]," + + "\"angularVelocityCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," + "\"size\":" + "{" + "\"length\":2.0," @@ -139,7 +139,7 @@ public void testDetectObjectJsonDeserialization() { new Vector3d(1, 1, 1), new Vector3d(.1, .2, .3), new Size(2, 1, .5)); - Double[] covarianceMatrix = new Double[] { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 }; + Double[][] covarianceMatrix = { {1.0, 0.0, 0.0} , {1.0, 0.0, 0.0} , {1.0, 0.0, 0.0}}; predictedDetectedObject.setPositionCovariance(covarianceMatrix); predictedDetectedObject.setVelocityCovariance(covarianceMatrix); predictedDetectedObject.setAngularVelocityCovariance(covarianceMatrix); @@ -160,7 +160,7 @@ public void testGetterSetterConstructor() { new Vector3d(2, 3, 4), new Vector3d(-4.4,-5.5,-6.6), new Size(3, 4, 5)); - Double[] covarianceMatrix = new Double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; + Double[][] covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; detectedObject.setPositionCovariance(covarianceMatrix); detectedObject.setVelocityCovariance(covarianceMatrix); detectedObject.setAngularVelocityCovariance(covarianceMatrix); @@ -193,7 +193,7 @@ public void testEquals() { new Vector3d(2, 3, 4), new Vector3d(-4.4,-5.5,-6.6), new Size(3, 4, 5)); - Double[] covarianceMatrix = new Double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; + Double[][] covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; detectedObject.setPositionCovariance(covarianceMatrix); detectedObject.setVelocityCovariance(covarianceMatrix); detectedObject.setAngularVelocityCovariance(covarianceMatrix); From 0a415261530c96a4b74a63db06a3fe47d761a028 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 25 Oct 2023 01:05:43 -0400 Subject: [PATCH 03/12] Fix unit tests --- .../ambassador/InfrastructureInstanceManagerTest.java | 2 +- .../infrastructure/ambassador/InfrastructureInstanceTest.java | 2 +- .../ambassador/InfrastructureMessageAmbassadorTest.java | 2 +- .../interactions/detector/DetectedObjectInteractionTest.java | 2 +- .../eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstanceManagerTest.java b/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstanceManagerTest.java index 91d805c5..e213a36c 100644 --- a/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstanceManagerTest.java +++ b/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstanceManagerTest.java @@ -133,7 +133,7 @@ public void testOnDetectedObject() throws IOException{ new Vector3d(2, 3, 4), new Vector3d(-4.4,-5.5,-6.6), new Size(3, 4, 5)); - Double[] covarianceMatrix = new Double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; + Double[][] covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; detectedObject1.setPositionCovariance(covarianceMatrix); detectedObject1.setVelocityCovariance(covarianceMatrix); detectedObject1.setAngularVelocityCovariance(covarianceMatrix); diff --git a/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstanceTest.java b/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstanceTest.java index 7a99a95e..5ea4c8d5 100644 --- a/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstanceTest.java +++ b/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureInstanceTest.java @@ -187,7 +187,7 @@ public void testSendInteraction() throws IOException { new Vector3d(0, 0, 0), new Vector3d(), new Size(0, 0, 0)); - Double[] covarianceMatrix = new Double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; + Double[][] covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; test_msg.setPositionCovariance(covarianceMatrix); test_msg.setVelocityCovariance(covarianceMatrix); test_msg.setAngularVelocityCovariance(covarianceMatrix); diff --git a/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassadorTest.java b/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassadorTest.java index a0103dcf..9fbc4e7c 100644 --- a/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassadorTest.java +++ b/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassadorTest.java @@ -196,7 +196,7 @@ public void testProcessInteraction() throws InternalFederateException{ new Vector3d(2, 3, 4), new Vector3d(-4.4,-5.5,-6.6), new Size(3, 4, 5)); - Double[] covarianceMatrix = new Double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; + Double[][] covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; detectedObject.setPositionCovariance(covarianceMatrix); detectedObject.setVelocityCovariance(covarianceMatrix); detectedObject.setAngularVelocityCovariance(covarianceMatrix); diff --git a/co-simulation/lib/mosaic-interactions/src/test/java/org/eclipse/mosaic/interactions/detector/DetectedObjectInteractionTest.java b/co-simulation/lib/mosaic-interactions/src/test/java/org/eclipse/mosaic/interactions/detector/DetectedObjectInteractionTest.java index bb0772ef..8c7a0a8e 100644 --- a/co-simulation/lib/mosaic-interactions/src/test/java/org/eclipse/mosaic/interactions/detector/DetectedObjectInteractionTest.java +++ b/co-simulation/lib/mosaic-interactions/src/test/java/org/eclipse/mosaic/interactions/detector/DetectedObjectInteractionTest.java @@ -46,7 +46,7 @@ public void testGetterSetterConstructor() { new Vector3d(1, 1, 1), new Vector3d(.1, .2, .3), new Size(2, 1, .5)); - Double[] covarianceMatrix = new Double[] { 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 }; + Double[][] covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; detectedObject.setPositionCovariance(covarianceMatrix); detectedObject.setVelocityCovariance(covarianceMatrix); detectedObject.setAngularVelocityCovariance(covarianceMatrix); diff --git a/co-simulation/lib/mosaic-objects/src/test/java/org/eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java b/co-simulation/lib/mosaic-objects/src/test/java/org/eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java index 7cb2117f..7368a5e3 100644 --- a/co-simulation/lib/mosaic-objects/src/test/java/org/eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java +++ b/co-simulation/lib/mosaic-objects/src/test/java/org/eclipse/mosaic/lib/objects/detector/DetectedObjectTest.java @@ -87,7 +87,7 @@ public void testDetectObjectJsonSerialization() { + "}"; assertEquals(json, json_prediction); - + System.out.println(json); } @Test From 25ea36d5c522caaf5cf6b114d884e756f63675e5 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 25 Oct 2023 01:49:43 -0400 Subject: [PATCH 04/12] PR Updates --- .../resources/scenarios/Town04/carla/carla_config.json | 4 +++- .../resources/scenarios/Town04_test/carla/carla_config.json | 3 ++- .../eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java | 5 +---- .../mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java | 4 ---- .../eclipse/mosaic/fed/carla/config/CarlaConfiguration.java | 5 +++++ .../mosaic/fed/carla/config/CarlaConfigurationTest.java | 1 + .../fed/mosaic-carla/src/test/resources/carla_config.json | 4 +++- .../ambassador/InfrastructureMessageAmbassadorTest.java | 1 - 8 files changed, 15 insertions(+), 12 deletions(-) diff --git a/co-simulation/bundle/src/assembly/resources/scenarios/Town04/carla/carla_config.json b/co-simulation/bundle/src/assembly/resources/scenarios/Town04/carla/carla_config.json index 1be28b9c..b65ef566 100644 --- a/co-simulation/bundle/src/assembly/resources/scenarios/Town04/carla/carla_config.json +++ b/co-simulation/bundle/src/assembly/resources/scenarios/Town04/carla/carla_config.json @@ -2,5 +2,7 @@ "updateInterval": 100, "carlaUE4Path": "/opt/carla/", "bridgePath": "/opt/carma-simulation/scenarios/Town04/carla; bridge.sh", - "carlaConnectionPort": 8913 + "carlaConnectionPort": 8913, + "carlaCDASimAdapterUrl":"http://127.0.0.1:8090/RPC2" + } diff --git a/co-simulation/bundle/src/assembly/resources/scenarios/Town04_test/carla/carla_config.json b/co-simulation/bundle/src/assembly/resources/scenarios/Town04_test/carla/carla_config.json index 51bc8ef2..aff6f191 100644 --- a/co-simulation/bundle/src/assembly/resources/scenarios/Town04_test/carla/carla_config.json +++ b/co-simulation/bundle/src/assembly/resources/scenarios/Town04_test/carla/carla_config.json @@ -2,5 +2,6 @@ "updateInterval": 100, "carlaUE4Path": "/opt/carla/", "bridgePath": "/opt/carma-simulation/scenarios/Town04_test/carla; bridge.sh", - "carlaConnectionPort": 8913 + "carlaConnectionPort": 8913, + "carlaCDASimAdapterUrl":"http://127.0.0.1:8090/RPC2" } diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index 80dc6814..5d34076c 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -17,7 +17,6 @@ import org.apache.commons.codec.binary.Hex; import org.apache.commons.lang3.StringUtils; import org.apache.xmlrpc.XmlRpcException; -import org.apache.xmlrpc.client.XmlRpcClientException; import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaConnection; import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient; import org.eclipse.mosaic.fed.carla.config.CarlaConfiguration; @@ -28,7 +27,6 @@ import org.eclipse.mosaic.interactions.detector.DetectedObjectInteraction; import org.eclipse.mosaic.interactions.detector.DetectorRegistration; import org.eclipse.mosaic.lib.objects.detector.DetectedObject; -import org.eclipse.mosaic.lib.objects.detector.Detector; import org.eclipse.mosaic.lib.util.ProcessLoggingThread; import org.eclipse.mosaic.lib.util.objects.ObjectInstantiation; import org.eclipse.mosaic.rti.TIME; @@ -43,7 +41,6 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.TimeUnit; @@ -219,7 +216,7 @@ public void initialize(long startTime, long endTime) throws InternalFederateExce //initialize CarlaXmlRpcClient //set the connected server URL try{ - URL xmlRpcServerUrl = new URL("http://127.0.0.1:8090/RPC2"); + URL xmlRpcServerUrl = new URL(carlaConfig.carlaCDASimAdapterUrl); carlaXmlRpcClient = new CarlaXmlRpcClient(xmlRpcServerUrl); } catch (MalformedURLException m) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java index 1f6c3a92..cd32d516 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java @@ -16,15 +16,12 @@ package org.eclipse.mosaic.fed.carla.carlaconnect; import java.net.URL; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; -import org.apache.xmlrpc.client.XmlRpcClientException; -import org.eclipse.mosaic.interactions.detector.DetectedObjectInteraction; import org.eclipse.mosaic.interactions.detector.DetectorRegistration; import org.eclipse.mosaic.lib.objects.detector.DetectedObject; import org.slf4j.Logger; @@ -39,7 +36,6 @@ public class CarlaXmlRpcClient{ boolean isConnected; private static final String CREATE_SENSOR = "create_simulated_semantic_lidar_sensor"; - private static final String GET_SENSOR = "get_simulated_sensor"; private static final String GET_DETECTED_OBJECTS = "get_detected_objects"; private XmlRpcClient client; diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/config/CarlaConfiguration.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/config/CarlaConfiguration.java index 6f1f5015..2e17b7a8 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/config/CarlaConfiguration.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/config/CarlaConfiguration.java @@ -48,5 +48,10 @@ public class CarlaConfiguration implements Serializable { * Carla connection port */ public int carlaConnectionPort; + /** + * URL where CARLACDASimAdapter XMLRPC Server is hosted + */ + public String carlaCDASimAdapterUrl; + } diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/config/CarlaConfigurationTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/config/CarlaConfigurationTest.java index ba123715..f993d362 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/config/CarlaConfigurationTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/config/CarlaConfigurationTest.java @@ -41,6 +41,7 @@ public void readConfig_assertProperties() throws InstantiationException { assertEquals("D:/CARLA_0.9.10/", carlaConfiguration.carlaUE4Path); assertEquals("./scenarios/Town04_10/carla; bridge.bat", carlaConfiguration.bridgePath); assertEquals(8913, carlaConfiguration.carlaConnectionPort); + assertEquals("http://127.0.0.1:8090/RPC2",carlaConfiguration.carlaCDASimAdapterUrl); } /** diff --git a/co-simulation/fed/mosaic-carla/src/test/resources/carla_config.json b/co-simulation/fed/mosaic-carla/src/test/resources/carla_config.json index d2004afe..30b91345 100644 --- a/co-simulation/fed/mosaic-carla/src/test/resources/carla_config.json +++ b/co-simulation/fed/mosaic-carla/src/test/resources/carla_config.json @@ -2,5 +2,7 @@ "updateInterval": 200, "carlaUE4Path": "D:/CARLA_0.9.10/", "bridgePath": "./scenarios/Town04_10/carla; bridge.bat", - "carlaConnectionPort": 8913 + "carlaConnectionPort": 8913, + "carlaCDASimAdapterUrl":"http://127.0.0.1:8090/RPC2" + } \ No newline at end of file diff --git a/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassadorTest.java b/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassadorTest.java index 9fbc4e7c..3bad18b6 100644 --- a/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassadorTest.java +++ b/co-simulation/fed/mosaic-infrastructure/src/test/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassadorTest.java @@ -59,7 +59,6 @@ import org.eclipse.mosaic.rti.api.parameters.AmbassadorParameter; import org.eclipse.mosaic.rti.api.parameters.FederateDescriptor; import org.eclipse.mosaic.rti.config.CLocalHost; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; From 9ccc1337f83b6d8e74773d151488ac946aeb0bf4 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 25 Oct 2023 01:58:44 -0400 Subject: [PATCH 05/12] fix unit tests --- .../eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java | 2 +- .../mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index 5d34076c..1b48b41c 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -222,7 +222,6 @@ public void initialize(long startTime, long endTime) throws InternalFederateExce catch (MalformedURLException m) { log.error("Errors occurred with {}", m.getMessage()); - carlaXmlRpcClient.closeConnection(); } // Start the CARLA simulator startCarlaLocal(); @@ -379,6 +378,7 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder } catch (XmlRpcException e) { log.error("Failed to connect to CARLA Adapter : ", e); + carlaXmlRpcClient.closeConnection(); } } diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java index 92b929e3..b382b24d 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java @@ -19,6 +19,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient; import org.eclipse.mosaic.lib.util.junit.TestFileRule; import org.eclipse.mosaic.rti.TIME; import org.eclipse.mosaic.rti.api.RtiAmbassador; @@ -55,6 +56,8 @@ public void setup() throws IOException { rtiMock = mock(RtiAmbassador.class); + + FederateDescriptor handleMock = mock(FederateDescriptor.class); File workingDir = temporaryFolder.getRoot(); From 62e83694179a84bc85493e1fb3181960dc6ca9f2 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 31 Oct 2023 17:38:47 -0400 Subject: [PATCH 06/12] Added unit tests --- .../fed/carla/ambassador/CarlaAmbassador.java | 1 + .../carla/carlaconnect/CarlaXmlRpcClient.java | 23 +- .../carla/ambassador/CarlaAmbassadorTest.java | 1 - .../carlaconnect/CarlaXmlRpcClientTest.java | 259 ++++++++++++++++++ 4 files changed, 269 insertions(+), 15 deletions(-) create mode 100644 co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index 1b48b41c..374df841 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -218,6 +218,7 @@ public void initialize(long startTime, long endTime) throws InternalFederateExce try{ URL xmlRpcServerUrl = new URL(carlaConfig.carlaCDASimAdapterUrl); carlaXmlRpcClient = new CarlaXmlRpcClient(xmlRpcServerUrl); + carlaXmlRpcClient.initialize(); } catch (MalformedURLException m) { diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java index cd32d516..69ffdbf3 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java @@ -34,34 +34,25 @@ */ public class CarlaXmlRpcClient{ - boolean isConnected; + private boolean isConnected; private static final String CREATE_SENSOR = "create_simulated_semantic_lidar_sensor"; private static final String GET_DETECTED_OBJECTS = "get_detected_objects"; private XmlRpcClient client; + private URL xmlRpcServerUrl; private final Logger log = LoggerFactory.getLogger(this.getClass()); public CarlaXmlRpcClient(URL xmlRpcServerUrl) { - initialize(xmlRpcServerUrl); + this.xmlRpcServerUrl = xmlRpcServerUrl; } - /** - * This method is used to send a stop message to the python server side to shut down server from there - */ - public void closeConnection() - { - log.info("carla connection server closing"); - isConnected = false; - } - - /** * need to getting a URL to connect to from ambassador * @param xmlRpcServerUrl */ - public void initialize(URL xmlRpcServerUrl) + public void initialize() { XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); config.setServerURL(xmlRpcServerUrl); @@ -89,7 +80,7 @@ public DetectedObject[] getDetectedObjects(String infrastructureId ,String senso if (isConnected) { Object[] params = new Object[]{infrastructureId, sensorId}; Object result = client.execute(GET_DETECTED_OBJECTS, params); - log.info("Detections from infrastructure {} sensor {} : {}", infrastructureId, sensorId, result); + log.debug("Detections from infrastructure {} sensor {} : {}", infrastructureId, sensorId, result); String jsonResult = (String)result; Gson gson = new Gson(); DetectedObject[] parsedMessage = gson.fromJson(jsonResult, @@ -102,4 +93,8 @@ public DetectedObject[] getDetectedObjects(String infrastructureId ,String senso } } + public void closeConnection() { + log.warn("Closing XML RPC Client connection in CARLA Ambassador!"); + isConnected = false; + } } diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java index b382b24d..237c6e7c 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java @@ -19,7 +19,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient; import org.eclipse.mosaic.lib.util.junit.TestFileRule; import org.eclipse.mosaic.rti.TIME; import org.eclipse.mosaic.rti.api.RtiAmbassador; diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java new file mode 100644 index 00000000..a8f5dc1d --- /dev/null +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java @@ -0,0 +1,259 @@ +package org.eclipse.mosaic.fed.carla.carlaconnect; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.List; + +import org.apache.xmlrpc.XmlRpcException; +import org.apache.xmlrpc.client.XmlRpcClient; +import org.eclipse.mosaic.interactions.detector.DetectorRegistration; +import org.eclipse.mosaic.lib.geo.CartesianPoint; +import org.eclipse.mosaic.lib.math.Vector3d; +import org.eclipse.mosaic.lib.objects.detector.DetectedObject; +import org.eclipse.mosaic.lib.objects.detector.DetectionType; +import org.eclipse.mosaic.lib.objects.detector.Detector; +import org.eclipse.mosaic.lib.objects.detector.DetectorType; +import org.eclipse.mosaic.lib.objects.detector.Orientation; +import org.eclipse.mosaic.lib.objects.detector.Size; +import org.junit.Before; +import org.junit.Test; +import org.mockito.internal.util.reflection.FieldSetter; + +public class CarlaXmlRpcClientTest { + + private XmlRpcClient mockClient; + + private CarlaXmlRpcClient carlaConnection; + + + @Before + public void setup() throws NoSuchFieldException, MalformedURLException{ + mockClient = mock(XmlRpcClient.class); + URL xmlRpcServerUrl = new URL("http://test_url"); + carlaConnection = new CarlaXmlRpcClient(xmlRpcServerUrl); + carlaConnection.initialize(); + // Set mock after initialize since initialize overwrites member + FieldSetter.setField(carlaConnection, carlaConnection.getClass().getDeclaredField("client"), mockClient); + } + + /** + * Test to see if createSensor runs without exception + * @throws XmlRpcException + */ + @Test + public void testCreateSensor() throws XmlRpcException { + // Create Detector Registration + Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); + DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); + // Create request params + List location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); + List orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); + Object[] params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId(), location, orientation}; + // Tell mock to return sensor ID when following method is called with following parameters + when( mockClient.execute("create_simulated_semantic_lidar_sensor", params)).thenReturn(registration.getSenderId()); + // Method has no return so verifying that it is successful is just verifying no exception is thrown + carlaConnection.createSensor(registration); + // Verify following method was called on mock + verify( mockClient, times(1)).execute("create_simulated_semantic_lidar_sensor", params); + } + + @Test + public void testCreateSensorException() throws XmlRpcException { + // Create Detector Registration + Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); + DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); + // Create request params + List location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); + List orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); + Object[] params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId(), location, orientation}; + // Tell mock to return sensor ID when following method is called with following parameters + when( mockClient.execute("create_simulated_semantic_lidar_sensor", params)).thenThrow(XmlRpcException.class); + try { + // Method has no return so verifying that it is successful is just verifying no exception is thrown + carlaConnection.createSensor(registration); + // Verify following method was called on mock + } + catch( Exception e) { + assertEquals(e.getClass(), XmlRpcException.class); + } + verify( mockClient, times(1)).execute("create_simulated_semantic_lidar_sensor", params); + } + + @Test + public void testGetDetectedObjects() throws XmlRpcException { + // Create return JSON String + String json = "[{" + + "\"type\":\"CAR\"," + + "\"confidence\":0.7," + + "\"sensorId\":\"sensor1\"," + + "\"projString\":\"projection String2\"," + + "\"objectId\":\"Object7\"," + + "\"position\":" + + "{" + + "\"x\":-1.1," + + "\"y\":-2.0," + + "\"z\":-3.2" + + "}," + + "\"positionCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," + + "\"velocity\":" + + "{" + + "\"x\":1," + + "\"y\":1," + + "\"z\":1" + + "}," + + "\"velocityCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," + + "\"angularVelocity\":" + + "{" + + "\"x\":0.1," + + "\"y\":0.2," + + "\"z\":0.3" + + "}," + + "\"angularVelocityCovariance\":[[1.0,0.0,0.0],[1.0,0.0,0.0],[1.0,0.0,0.0]]," + + "\"size\":" + + "{" + + "\"length\":2.0," + + "\"height\":1.0," + + "\"width\":0.5" + + "}" + + "}," + + "{" + + "\"type\":\"BUS\"," + + "\"confidence\":0.5," + + "\"sensorId\":\"sensor1\"," + + "\"projString\":\"projection String\"," + + "\"objectId\":\"Object1\"," + + "\"position\":" + + "{" + + "\"x\":1.1," + + "\"y\":2.0," + + "\"z\":3.2" + + "}," + + "\"positionCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," + + "\"velocity\":" + + "{" + + "\"x\":0.0," + + "\"y\":0.0," + + "\"z\":0.0" + + "}," + + "\"velocityCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," + + "\"angularVelocity\":" + + "{" + + "\"x\":0.0," + + "\"y\":0.0," + + "\"z\":0.0" + + "}," + + "\"angularVelocityCovariance\":[[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]]," + + "\"size\":" + + "{" + + "\"length\":0.0," + + "\"height\":0.0," + + "\"width\":0.0" + + "}" + + "}" + + "]"; + // Create request params + Object[] params = new Object[]{"rsu_1", "sensorId_1"}; + // Tell mock to return sensor ID when following method is called with following parameters + when( mockClient.execute("get_detected_objects", params)).thenReturn(json); + // Method has no return so verifying that it is successful is just verifying no exception is thrown + DetectedObject[] detectedObjects = carlaConnection.getDetectedObjects("rsu_1", "sensorId_1"); + // Verify following method was called on mock + verify( mockClient, times(1)).execute("get_detected_objects", params); + // Confirm JSON payload is correctly serialized + assertEquals(2, detectedObjects.length); + // Object 1 is CAR + DetectedObject predictedCar = new DetectedObject( + DetectionType.CAR, + 0.7, + "sensor1", + "projection String2", + "Object7", + CartesianPoint.xyz(-1.1, -2, -3.2), + new Vector3d(1, 1, 1), + new Vector3d(.1, .2, .3), + new Size(2, 1, .5)); + Double[][] covarianceMatrix = { {1.0, 0.0, 0.0} , {1.0, 0.0, 0.0} , {1.0, 0.0, 0.0}}; + predictedCar.setPositionCovariance(covarianceMatrix); + predictedCar.setVelocityCovariance(covarianceMatrix); + predictedCar.setAngularVelocityCovariance(covarianceMatrix); + assertEquals(predictedCar, detectedObjects[0]); + + DetectedObject predictedBus = new DetectedObject( + DetectionType.BUS, + 0.5, + "sensor1", + "projection String", + "Object1", + CartesianPoint.xyz(1.1, 2, 3.2), + new Vector3d(0, 0, 0), + new Vector3d(), + new Size(0, 0, 0)); + Double[][] bus_covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; + predictedBus.setPositionCovariance(bus_covarianceMatrix); + predictedBus.setVelocityCovariance(bus_covarianceMatrix); + predictedBus.setAngularVelocityCovariance(bus_covarianceMatrix); + assertEquals(predictedBus, detectedObjects[1]); + + } + + @Test + public void testGetDetectedObjectsException() throws XmlRpcException { + // Create request params + Object[] params = new Object[]{"rsu_1", "sensorId_1"}; + // Tell mock to return sensor ID when following method is called with following parameters + when( mockClient.execute("get_detected_objects", params)).thenThrow(XmlRpcException.class); + try{ + // Method has no return so verifying that it is successful is just verifying no exception is thrown + DetectedObject[] detectedObjects = carlaConnection.getDetectedObjects("rsu_1", "sensorId_1"); + } + catch(Exception e) { + assertEquals(e.getClass(), XmlRpcException.class); + } + // Verify following method was called on mock + verify( mockClient, times(1)).execute("get_detected_objects", params); + } + + @Test + public void testCloseConnection() throws XmlRpcException { + // Create request params + // Create Detector Registration + Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); + DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); + List location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); + List orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); + Object[] params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId(), location, orientation}; + // Tell mock to return sensor ID when following method is called with following parameters + when( mockClient.execute("create_simulated_semantic_lidar_sensor", params)).thenReturn(registration.getSenderId()); + Object[] get_detected_object_params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId()}; + // Tell mock to return sensor ID when following method is called with following parameters + when( mockClient.execute("get_detected_objects", get_detected_object_params)).thenReturn(""); + carlaConnection.closeConnection(); + try { + carlaConnection.createSensor(registration); + } + catch( Exception e ) { + assertEquals(XmlRpcException.class, e.getClass()); + assertEquals("XMLRpcClient is not connected to CARLA Adapter!", e.getMessage()); + } + try { + carlaConnection.getDetectedObjects(registration.getInfrastructureId(), registration.getDetector().getSensorId()); + } + catch( Exception e ) { + assertEquals(XmlRpcException.class, e.getClass()); + assertEquals("XMLRpcClient is not connected to CARLA Adapter!", e.getMessage()); + } + // Assert execute is never called on XMLRPC Client after connection is closed. + verify( mockClient, times(0)).execute(any(String.class), any(Object[].class)); + + } +} From 025a619912f74ac623b83fb5c7887e14cfefbb27 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 31 Oct 2023 18:03:04 -0400 Subject: [PATCH 07/12] Added Unit tests --- .../fed/carla/ambassador/CarlaAmbassador.java | 2 +- .../carla/ambassador/CarlaAmbassadorTest.java | 71 ++++++++++++++++++- .../carlaconnect/CarlaXmlRpcClientTest.java | 47 ++---------- 3 files changed, 77 insertions(+), 43 deletions(-) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index 374df841..38b329fd 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -362,7 +362,7 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder // CARLA but it seems to increment every time processTimeAdvanceGrant is called rti.requestAdvanceTime(nextTimeStep + this.executedTimes, 0, (byte) 2); this.executedTimes++; - List detectedObjectInteractions = new ArrayList(); + List detectedObjectInteractions = new ArrayList(); // Get all detections from all currently registered detectors. for (DetectorRegistration registration: registeredDetectors ) { DetectedObject[] detections = carlaXmlRpcClient.getDetectedObjects( registration.getInfrastructureId() , registration.getDetector().getSensorId()); diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java index 237c6e7c..8c47055d 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java @@ -19,8 +19,20 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import org.apache.xmlrpc.XmlRpcException; +import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient; +import org.eclipse.mosaic.interactions.detector.DetectorRegistration; +import org.eclipse.mosaic.lib.geo.CartesianPoint; +import org.eclipse.mosaic.lib.math.Vector3d; +import org.eclipse.mosaic.lib.objects.detector.DetectedObject; +import org.eclipse.mosaic.lib.objects.detector.DetectionType; +import org.eclipse.mosaic.lib.objects.detector.Detector; +import org.eclipse.mosaic.lib.objects.detector.DetectorType; +import org.eclipse.mosaic.lib.objects.detector.Orientation; +import org.eclipse.mosaic.lib.objects.detector.Size; import org.eclipse.mosaic.lib.util.junit.TestFileRule; import org.eclipse.mosaic.rti.TIME; +import org.eclipse.mosaic.rti.api.InternalFederateException; import org.eclipse.mosaic.rti.api.RtiAmbassador; import org.eclipse.mosaic.rti.api.parameters.AmbassadorParameter; import org.eclipse.mosaic.rti.api.parameters.FederateDescriptor; @@ -30,9 +42,12 @@ import org.junit.Rule; import org.junit.rules.RuleChain; import org.junit.rules.TemporaryFolder; +import org.mockito.internal.util.reflection.FieldSetter; import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** * Tests for {@link CarlaAmbassador}. @@ -50,12 +65,14 @@ public class CarlaAmbassadorTest { private CarlaAmbassador ambassador; + private CarlaXmlRpcClient carlaXmlRpcClientMock; + @Before - public void setup() throws IOException { + public void setup() throws IOException, NoSuchFieldException { rtiMock = mock(RtiAmbassador.class); - + carlaXmlRpcClientMock = mock(CarlaXmlRpcClient.class); FederateDescriptor handleMock = mock(FederateDescriptor.class); @@ -75,6 +92,11 @@ public void setup() throws IOException { ambassador.setRtiAmbassador(rtiMock); ambassador.setFederateDescriptor(handleMock); + + FieldSetter.setField(ambassador, ambassador.getClass().getDeclaredField("carlaXmlRpcClient"), carlaXmlRpcClientMock); + + + } @Test @@ -86,4 +108,49 @@ public void initialize() throws Throwable { verify(rtiMock, times(1)).requestAdvanceTime(eq(0L), eq(0L), eq((byte) 1)); } + @Test + public void processTimeAdvanceGrant() throws InternalFederateException, NoSuchFieldException, SecurityException, XmlRpcException { + List registeredDetectors = new ArrayList<>(); + Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); + DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); + registeredDetectors.add( registration); + FieldSetter.setField(ambassador, ambassador.getClass().getDeclaredField("registeredDetectors"), registeredDetectors); + + // Setup Get detected objects return + // Object 1 is CAR + DetectedObject predictedCar = new DetectedObject( + DetectionType.CAR, + 0.7, + "sensorID1", + "projection String2", + "Object7", + CartesianPoint.xyz(-1.1, -2, -3.2), + new Vector3d(1, 1, 1), + new Vector3d(.1, .2, .3), + new Size(2, 1, .5)); + Double[][] covarianceMatrix = { {1.0, 0.0, 0.0} , {1.0, 0.0, 0.0} , {1.0, 0.0, 0.0}}; + predictedCar.setPositionCovariance(covarianceMatrix); + predictedCar.setVelocityCovariance(covarianceMatrix); + predictedCar.setAngularVelocityCovariance(covarianceMatrix); + DetectedObject predictedBus = new DetectedObject( + DetectionType.BUS, + 0.5, + "sensorID1", + "projection String", + "Object1", + CartesianPoint.xyz(1.1, 2, 3.2), + new Vector3d(0, 0, 0), + new Vector3d(), + new Size(0, 0, 0)); + Double[][] bus_covarianceMatrix = { {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0} , {0.0, 0.0, 0.0}}; + predictedBus.setPositionCovariance(bus_covarianceMatrix); + predictedBus.setVelocityCovariance(bus_covarianceMatrix); + predictedBus.setAngularVelocityCovariance(bus_covarianceMatrix); + + DetectedObject[] detectedObjects = {predictedBus, predictedCar}; + when(carlaXmlRpcClientMock.getDetectedObjects(registration.getInfrastructureId(), registration.getDetector().getSensorId() )).thenReturn(detectedObjects); + + ambassador.processTimeAdvanceGrant(100); + } + } diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java index a8f5dc1d..3b5ba7c6 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java @@ -30,12 +30,16 @@ import org.mockito.internal.util.reflection.FieldSetter; public class CarlaXmlRpcClientTest { - + // Mock XmlRpcClient private XmlRpcClient mockClient; - + // CarlaXmlRpcClient under test private CarlaXmlRpcClient carlaConnection; - + /** + * Initialize CarlaXmlRpcClient and setup mock + * @throws NoSuchFieldException + * @throws MalformedURLException + */ @Before public void setup() throws NoSuchFieldException, MalformedURLException{ mockClient = mock(XmlRpcClient.class); @@ -67,27 +71,6 @@ public void testCreateSensor() throws XmlRpcException { verify( mockClient, times(1)).execute("create_simulated_semantic_lidar_sensor", params); } - @Test - public void testCreateSensorException() throws XmlRpcException { - // Create Detector Registration - Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); - DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); - // Create request params - List location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); - List orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); - Object[] params = new Object[]{registration.getInfrastructureId(), registration.getDetector().getSensorId(), location, orientation}; - // Tell mock to return sensor ID when following method is called with following parameters - when( mockClient.execute("create_simulated_semantic_lidar_sensor", params)).thenThrow(XmlRpcException.class); - try { - // Method has no return so verifying that it is successful is just verifying no exception is thrown - carlaConnection.createSensor(registration); - // Verify following method was called on mock - } - catch( Exception e) { - assertEquals(e.getClass(), XmlRpcException.class); - } - verify( mockClient, times(1)).execute("create_simulated_semantic_lidar_sensor", params); - } @Test public void testGetDetectedObjects() throws XmlRpcException { @@ -206,22 +189,6 @@ public void testGetDetectedObjects() throws XmlRpcException { } - @Test - public void testGetDetectedObjectsException() throws XmlRpcException { - // Create request params - Object[] params = new Object[]{"rsu_1", "sensorId_1"}; - // Tell mock to return sensor ID when following method is called with following parameters - when( mockClient.execute("get_detected_objects", params)).thenThrow(XmlRpcException.class); - try{ - // Method has no return so verifying that it is successful is just verifying no exception is thrown - DetectedObject[] detectedObjects = carlaConnection.getDetectedObjects("rsu_1", "sensorId_1"); - } - catch(Exception e) { - assertEquals(e.getClass(), XmlRpcException.class); - } - // Verify following method was called on mock - verify( mockClient, times(1)).execute("get_detected_objects", params); - } @Test public void testCloseConnection() throws XmlRpcException { From 9efc21d10bb773f8df604a672a11981d44b93727 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 31 Oct 2023 21:39:14 -0400 Subject: [PATCH 08/12] Added unit tests --- .../fed/carla/ambassador/CarlaAmbassador.java | 3 +- .../carla/ambassador/CarlaAmbassadorTest.java | 56 ++++++++++++++++++- .../carlaconnect/CarlaXmlRpcClientTest.java | 2 - 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index 38b329fd..9a02715b 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -526,7 +526,8 @@ private void receiveInteraction(DetectorRegistration interaction) { registeredDetectors.add(interaction); } catch(XmlRpcException e) { - log.error("Error occurred attempting to create sensor : {}", interaction.getDetector(), e); + log.error("Error occurred attempting to create sensor : {}\n{}", interaction.getDetector(), e); + carlaXmlRpcClient.closeConnection(); } } diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java index 8c47055d..3cc86c52 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java @@ -13,7 +13,10 @@ package org.eclipse.mosaic.fed.carla.ambassador; import org.junit.Test; + +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -21,6 +24,7 @@ import org.apache.xmlrpc.XmlRpcException; import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient; +import org.eclipse.mosaic.interactions.detector.DetectedObjectInteraction; import org.eclipse.mosaic.interactions.detector.DetectorRegistration; import org.eclipse.mosaic.lib.geo.CartesianPoint; import org.eclipse.mosaic.lib.math.Vector3d; @@ -32,6 +36,7 @@ import org.eclipse.mosaic.lib.objects.detector.Size; import org.eclipse.mosaic.lib.util.junit.TestFileRule; import org.eclipse.mosaic.rti.TIME; +import org.eclipse.mosaic.rti.api.IllegalValueException; import org.eclipse.mosaic.rti.api.InternalFederateException; import org.eclipse.mosaic.rti.api.RtiAmbassador; import org.eclipse.mosaic.rti.api.parameters.AmbassadorParameter; @@ -109,7 +114,7 @@ public void initialize() throws Throwable { } @Test - public void processTimeAdvanceGrant() throws InternalFederateException, NoSuchFieldException, SecurityException, XmlRpcException { + public void processTimeAdvanceGrant() throws InternalFederateException, NoSuchFieldException, SecurityException, XmlRpcException, IllegalValueException { List registeredDetectors = new ArrayList<>(); Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); @@ -151,6 +156,55 @@ public void processTimeAdvanceGrant() throws InternalFederateException, NoSuchFi when(carlaXmlRpcClientMock.getDetectedObjects(registration.getInfrastructureId(), registration.getDetector().getSensorId() )).thenReturn(detectedObjects); ambassador.processTimeAdvanceGrant(100); + + verify(carlaXmlRpcClientMock, times(1)).getDetectedObjects(registration.getInfrastructureId(), registration.getDetector().getSensorId()); + verify(rtiMock, times(2)).triggerInteraction(any(DetectedObjectInteraction.class)); } + @Test + public void processTimeAdvanceGrantException() throws InternalFederateException, NoSuchFieldException, SecurityException, XmlRpcException, IllegalValueException { + List registeredDetectors = new ArrayList<>(); + Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); + DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); + registeredDetectors.add( registration); + FieldSetter.setField(ambassador, ambassador.getClass().getDeclaredField("registeredDetectors"), registeredDetectors); + + + when(carlaXmlRpcClientMock.getDetectedObjects(registration.getInfrastructureId(), registration.getDetector().getSensorId() )).thenThrow(XmlRpcException.class); + // Verify that when exceptiopn is thrown by CarlaXmlRpcClient, no interactions are trigger and exception is caught + ambassador.processTimeAdvanceGrant(100); + + verify(carlaXmlRpcClientMock, times(1)).getDetectedObjects(registration.getInfrastructureId(), registration.getDetector().getSensorId()); + verify(rtiMock, times(0)).triggerInteraction(any(DetectedObjectInteraction.class)); + verify(carlaXmlRpcClientMock, times(1)).closeConnection(); + + } + + @Test + public void processDetectorRegistrationInteraction() throws XmlRpcException { + Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); + DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); + + ambassador.processInteraction(registration); + + verify(carlaXmlRpcClientMock, times(1)).createSensor(registration); + + } + + @Test + public void processDetectorRegistrationInteractionException() throws XmlRpcException { + Detector detector = new Detector("sensorID1", DetectorType.SEMANTIC_LIDAR, new Orientation( 0.0,0.0,0.0), CartesianPoint.ORIGO); + DetectorRegistration registration = new DetectorRegistration(0, detector, "rsu_2"); + + doThrow(new XmlRpcException("")).when(carlaXmlRpcClientMock).createSensor(registration); + ambassador.processInteraction(registration); + + verify(carlaXmlRpcClientMock, times(1)).createSensor(registration); + verify(carlaXmlRpcClientMock, times(1)).closeConnection(); + + + } + + + } diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java index 3b5ba7c6..661f8e82 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java @@ -1,9 +1,7 @@ package org.eclipse.mosaic.fed.carla.carlaconnect; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; From 0c08f669b2144d4908e03216516dce1261eacd31 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 1 Nov 2023 09:24:54 -0400 Subject: [PATCH 09/12] Update Javadoc comments. --- .../fed/carla/ambassador/CarlaAmbassador.java | 5 ++++- .../carla/carlaconnect/CarlaXmlRpcClient.java | 22 +++++++++++++++---- .../carlaconnect/CarlaXmlRpcClientTest.java | 10 +++++++-- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index 9a02715b..2ce8cbe4 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -519,7 +519,10 @@ else if (interaction.getTypeId().equals(DetectorRegistration.TYPE_ID)) { } } - + /** + * Method to call XMLRPC method to create sensor on reception of DetectionRegistration interactions. + * @param interaction Interaction triggered by Ambassadors attempting to create sensors in CARLA. + */ private void receiveInteraction(DetectorRegistration interaction) { try { carlaXmlRpcClient.createSensor(interaction); diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java index 69ffdbf3..fe7f649e 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java @@ -30,7 +30,8 @@ import com.google.gson.Gson; /** - * This is a class uses xmlrpc to connect with CARLA CDASim adapter service + * This is a class uses xmlrpc to connect with CARLA CDASim Adapter. It includes calls + * to dynamically create senors and get detected objects from created sensors. */ public class CarlaXmlRpcClient{ @@ -61,7 +62,11 @@ public void initialize() isConnected = true; } - + /** + * Calls CARLA CDA Sim Adapter create_sensor XMLRPC method and logs sensor ID of created sensor. + * @param registration DetectorRegistration interaction used to create sensor. + * @throws XmlRpcException if XMLRPC call fails or connection is lost. + */ public void createSensor(DetectorRegistration registration) throws XmlRpcException{ List location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ()); List orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw()); @@ -75,7 +80,13 @@ public void createSensor(DetectorRegistration registration) throws XmlRpcExcepti log.warn("XMLRpcClient is not connected to CARLA Adapter!"); } } - + /** + * Calls CARLA CDA Sim Adapter get_detected_objects XMLRPC method and returns an array of DetectedObject. + * @param infrastructureId String infrastructure ID of sensor to get detections from. + * @param sensorId String sensor ID of sensor to get detections from + * @return DetectedObject[] from given sensor. + * @throws XmlRpcException if XMLRPC call fails or connection is lost. + */ public DetectedObject[] getDetectedObjects(String infrastructureId ,String sensorId) throws XmlRpcException{ if (isConnected) { Object[] params = new Object[]{infrastructureId, sensorId}; @@ -92,7 +103,10 @@ public DetectedObject[] getDetectedObjects(String infrastructureId ,String senso } } - + /** + * Method to set isConnected field to false. Does not actually close the underlying http connection session but + * is used to avoid repeated timeouts/exceptions on misconfiguration of XMLRPC client. + */ public void closeConnection() { log.warn("Closing XML RPC Client connection in CARLA Ambassador!"); isConnected = false; diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java index 661f8e82..2226641c 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClientTest.java @@ -69,7 +69,10 @@ public void testCreateSensor() throws XmlRpcException { verify( mockClient, times(1)).execute("create_simulated_semantic_lidar_sensor", params); } - + /** + * Test GetDectedObjects + * @throws XmlRpcException + */ @Test public void testGetDetectedObjects() throws XmlRpcException { // Create return JSON String @@ -187,7 +190,10 @@ public void testGetDetectedObjects() throws XmlRpcException { } - + /** + * Test close connection + * @throws XmlRpcException + */ @Test public void testCloseConnection() throws XmlRpcException { // Create request params From f2faced25c6286f63df9a51a5119190768042285 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 1 Nov 2023 10:05:23 -0400 Subject: [PATCH 10/12] Fix typos --- .../mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java index fe7f649e..8145f2b8 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java @@ -30,8 +30,8 @@ import com.google.gson.Gson; /** - * This is a class uses xmlrpc to connect with CARLA CDASim Adapter. It includes calls - * to dynamically create senors and get detected objects from created sensors. + * This is a class that uses xmlrpc to connect with CARLA CDASim Adapter. It includes calls + * to dynamically create sensors and get detected objects from created sensors. */ public class CarlaXmlRpcClient{ @@ -50,7 +50,7 @@ public CarlaXmlRpcClient(URL xmlRpcServerUrl) { /** - * need to getting a URL to connect to from ambassador + * Initialize XmlRpcClient. * @param xmlRpcServerUrl */ public void initialize() From 787c2e522b92cc4188770ffe7ae35f74e15e8391 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 2 Nov 2023 09:43:01 -0400 Subject: [PATCH 11/12] Sonar Scanner updates --- .../mosaic/fed/carla/ambassador/CarlaAmbassador.java | 6 +++--- .../mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java | 4 +--- .../ambassador/InfrastructureMessageAmbassador.java | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index 2ce8cbe4..e8e3a399 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -362,7 +362,7 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder // CARLA but it seems to increment every time processTimeAdvanceGrant is called rti.requestAdvanceTime(nextTimeStep + this.executedTimes, 0, (byte) 2); this.executedTimes++; - List detectedObjectInteractions = new ArrayList(); + List detectedObjectInteractions = new ArrayList<>(); // Get all detections from all currently registered detectors. for (DetectorRegistration registration: registeredDetectors ) { DetectedObject[] detections = carlaXmlRpcClient.getDetectedObjects( registration.getInfrastructureId() , registration.getDetector().getSensorId()); @@ -623,7 +623,7 @@ private void sendReceivedV2xMessageToCarla() { } } } catch (Exception e) { - log.error("error occurs during sending messages to bridge: " + e.getMessage()); + log.error("error occurs during sending messages to bridge: {}", e.getMessage()); } } @@ -649,7 +649,7 @@ private String[] processReceivedV2xMessageFromCarla(int length, byte[] command) carlaConnection.getDataOutputStream().write(new byte[] { 0x07, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x00 }); } } catch (Exception e) { - log.error("error occurs during process received messages: " + e.getMessage()); + log.error("error occurs during process received messages: {}", e.getMessage()); } return message.split(";"); } diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java index 8145f2b8..644b5e47 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/carlaconnect/CarlaXmlRpcClient.java @@ -94,9 +94,7 @@ public DetectedObject[] getDetectedObjects(String infrastructureId ,String senso log.debug("Detections from infrastructure {} sensor {} : {}", infrastructureId, sensorId, result); String jsonResult = (String)result; Gson gson = new Gson(); - DetectedObject[] parsedMessage = gson.fromJson(jsonResult, - DetectedObject[].class); - return parsedMessage; + return gson.fromJson(jsonResult,DetectedObject[].class); } else { throw new XmlRpcException("XMLRpcClient is not connected to CARLA Adapter!"); diff --git a/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java b/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java index 867cbb58..73569eb7 100644 --- a/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java +++ b/co-simulation/fed/mosaic-infrastructure/src/main/java/org/eclipse/mosaic/fed/infrastructure/ambassador/InfrastructureMessageAmbassador.java @@ -40,7 +40,6 @@ import org.eclipse.mosaic.lib.objects.addressing.IpResolver; import org.eclipse.mosaic.lib.objects.communication.AdHocConfiguration; import org.eclipse.mosaic.lib.objects.communication.InterfaceConfiguration; -import org.eclipse.mosaic.lib.objects.detector.DetectedObject; import org.eclipse.mosaic.lib.objects.detector.Detector; import org.eclipse.mosaic.lib.objects.v2x.ExternalV2xMessage; import org.eclipse.mosaic.lib.objects.v2x.V2xMessage; From cb44e4a9f55125d9bdf93e0236dfded4099f8815 Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 2 Nov 2023 18:43:09 -0400 Subject: [PATCH 12/12] Update unit tests --- .../mosaic/fed/carla/ambassador/CarlaAmbassador.java | 6 ++++-- .../mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java index e8e3a399..c9f8cd7b 100644 --- a/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java +++ b/co-simulation/fed/mosaic-carla/src/main/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassador.java @@ -216,8 +216,10 @@ public void initialize(long startTime, long endTime) throws InternalFederateExce //initialize CarlaXmlRpcClient //set the connected server URL try{ - URL xmlRpcServerUrl = new URL(carlaConfig.carlaCDASimAdapterUrl); - carlaXmlRpcClient = new CarlaXmlRpcClient(xmlRpcServerUrl); + if (carlaXmlRpcClient== null) { + URL xmlRpcServerUrl = new URL(carlaConfig.carlaCDASimAdapterUrl); + carlaXmlRpcClient = new CarlaXmlRpcClient(xmlRpcServerUrl); + } carlaXmlRpcClient.initialize(); } catch (MalformedURLException m) diff --git a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java index 3cc86c52..b420779f 100644 --- a/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java +++ b/co-simulation/fed/mosaic-carla/src/test/java/org/eclipse/mosaic/fed/carla/ambassador/CarlaAmbassadorTest.java @@ -24,6 +24,7 @@ import org.apache.xmlrpc.XmlRpcException; import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient; +import org.eclipse.mosaic.fed.carla.config.CarlaConfiguration; import org.eclipse.mosaic.interactions.detector.DetectedObjectInteraction; import org.eclipse.mosaic.interactions.detector.DetectorRegistration; import org.eclipse.mosaic.lib.geo.CartesianPoint; @@ -106,11 +107,15 @@ public void setup() throws IOException, NoSuchFieldException { @Test public void initialize() throws Throwable { + CarlaConfiguration config = new CarlaConfiguration(); + config.carlaCDASimAdapterUrl="https://testing/something"; + FieldSetter.setField(ambassador, ambassador.getClass().getDeclaredField("carlaConfig"), config); // RUN ambassador.initialize(0, 100 * TIME.SECOND); // ASSERT verify(rtiMock, times(1)).requestAdvanceTime(eq(0L), eq(0L), eq((byte) 1)); + verify(carlaXmlRpcClientMock, times(1)).initialize(); } @Test