Skip to content

Commit

Permalink
generate and load default XML file to configure whitelists (#69)
Browse files Browse the repository at this point in the history
* generate default XML to donfigure whitelists and load it

* using dom to set attributes and whitelist separated by spaces

* Generate XMl usign xml.dom.minidom and script documentation

* do not remove the xml version line
  • Loading branch information
EduPonz authored and Esteve Fernandez committed Jun 19, 2019
1 parent 6ae8375 commit 04430ed
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 1 deletion.
174 changes: 174 additions & 0 deletions performance_test/helper_scripts/generate_apex_xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#!/usr/bin/env python

# Copyright 2017 Apex.AI, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Generate DEFAULT XML file for apex tests.
Helper script to generate a DEFAULT_FASTRTPS_PROFILES.xml XML file to add an
interface whitelist to be used in the Fast-RTPS communication plugin. The
script takes two optional arguments: a list of IP addresses (to create the
interface whitelist), and a directory to place the resulting
DEFAULT_FASTRTPS_PROFILES.xml (this directory defaults to the current
directory).
:Usage:
generate_apex_xml.py [-h] [-w WHITELIST [WHITELIST ...]] [-d DIRECTORY]
optional arguments:
-h, --help show this help message and exit
-w WHITELIST [WHITELIST ...], --whitelist WHITELIST [WHITELIST ...]
List of whitelist interfaces separated by spaces
(default: None)
-d DIRECTORY, --directory DIRECTORY
Directory to store the XML file (default: .)
:Example:
generate_apex_xml.py -w 192.168.1.1 127.0.0.1 -f some_directory
The previous command would place a DEFAULT_FASTRTPS_PROFILES.xml file in
the 'some_directory' directory. The resulting output would be:
<profiles>
<transport_descriptors>
<transport_descriptor>
<transport_id>apex_transport_descriptor</transport_id>
<type>UDPv4</type>
<interfaceWhiteList>
<address>192.168.1.11</address>
<address>127.0.0.1</address>
</interfaceWhiteList>
</transport_descriptor>
</transport_descriptors>
<participant is_default_profile="true" profile_name="apex_profile">
<rtps>
<name>Apex participant profile</name>
<useBuiltinTransports>false</useBuiltinTransports>
<userTransports>
<transport_id>apex_transport_descriptor</transport_id>
</userTransports>
</rtps>
</participant>
</profiles>
"""
import argparse
import os
from xml.dom.minidom import Document


def directory_type(directory):
"""
Check whether the argument is a directory.
:param directory: The directory path.
:return: The directory path without ending /.
:raise: argparse.ArgumentTypeError if the argument is not a directory.
"""
if directory.endswith('/'):
directory = directory[:-1]
if not os.path.isdir(directory):
raise argparse.ArgumentTypeError(
'Cannot find directory "{}"'.format(directory)
)
return directory


if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-w',
'--whitelist',
nargs='+',
help='List of whitelist interfaces separated by spaces',
required=False,
)
parser.add_argument(
'-d',
'--directory',
type=directory_type,
help='Directory to store the XML file',
required=False,
default='.'
)
args = parser.parse_args()
interface_whitelist = args.whitelist
xml_file = '{}/DEFAULT_FASTRTPS_PROFILES.xml'.format(args.directory)

doc = Document()
# Top level tag profiles
profiles = doc.createElement('profiles')

# Transport desciptors
transport_descriptors = doc.createElement('transport_descriptors')

# Transport desciptor
transport_descriptor = doc.createElement('transport_descriptor')
# Transport desciptor ID
transport_id = doc.createElement('transport_id')
transport_id.appendChild(doc.createTextNode('apex_transport_descriptor'))
transport_descriptor.appendChild(transport_id)
# Transport desciptor type
transport_type = doc.createElement('type')
transport_type.appendChild(doc.createTextNode('UDPv4'))
transport_descriptor.appendChild(transport_type)
# Transport desciptor whitelist
if interface_whitelist:
whitelists = doc.createElement('interfaceWhiteList')
for element in interface_whitelist:
address = doc.createElement('address')
address.appendChild(doc.createTextNode(element))
whitelists.appendChild(address)
transport_descriptor.appendChild(whitelists)
# Append transport descriptor to transport_descriptor
transport_descriptors.appendChild(transport_descriptor)
# Append transport descriptors to profile
profiles.appendChild(transport_descriptors)

# Participant
participant = doc.createElement('participant')
# Participant attributes
participant.setAttribute('profile_name', 'apex_profile')
participant.setAttribute('is_default_profile', 'true')
# Participant RTPS
rtps = doc.createElement('rtps')
# Participant RTPS name
name = doc.createElement('name')
name.appendChild(doc.createTextNode('Apex participant profile'))
rtps.appendChild(name)
# Participant RTPS useBuiltinTransports flag
use_btp = doc.createElement('useBuiltinTransports')
use_btp.appendChild(doc.createTextNode('false'))
rtps.appendChild(use_btp)
# Participant RTPS user transports
user_transports = doc.createElement('userTransports')
# Participant RTPS user transports ID
transport_id = doc.createElement('transport_id')
transport_id.appendChild(doc.createTextNode('apex_transport_descriptor'))
user_transports.appendChild(transport_id)
rtps.appendChild(user_transports)

# Append RTPS to participant
participant.appendChild(rtps)
# Append participant to profiles
profiles.appendChild(participant)
# Append profiles to document
doc.appendChild(profiles)

# Write to file
with open(xml_file, 'w') as f:
# Convert to string with 2-space identation,
# not taking the first line of the XML.
f.write(doc.toprettyxml(indent=' '*2))
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ eprosima::fastrtps::Participant * ResourceManager::fastrtps_participant() const
std::lock_guard<std::mutex> lock(m_global_mutex);

eprosima::fastrtps::Participant * result = nullptr;

eprosima::fastrtps::ParticipantAttributes PParam;

eprosima::fastrtps::xmlparser::XMLProfileManager::loadDefaultXMLFile();
eprosima::fastrtps::xmlparser::XMLProfileManager::getDefaultParticipantAttributes(PParam);
PParam.rtps.sendSocketBufferSize = 1048576;
PParam.rtps.listenSocketBufferSize = 4194304;
PParam.rtps.builtin.use_SIMPLE_RTPSParticipantDiscoveryProtocol = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifdef PERFORMANCE_TEST_FASTRTPS_ENABLED
#include <fastrtps/participant/Participant.h>
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/xmlparser/XMLProfileManager.h>
#include <fastrtps/Domain.h>
#endif

Expand Down

0 comments on commit 04430ed

Please sign in to comment.