Skip to content

Commit

Permalink
Merge pull request #49 from open-traffic-generator/models-0.3.1-align…
Browse files Browse the repository at this point in the history
…ment

align with models 0.3.1
  • Loading branch information
ashutshkumr authored Mar 3, 2021
2 parents 20eb591 + e07f64b commit 5927250
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
name: Build

on: [push, pull_request, workflow_dispatch]
on: [push, workflow_dispatch]

jobs:
generate:
Expand Down
7 changes: 3 additions & 4 deletions snappi/snappicommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def _get_child_class(self, property_name, is_property_list=False):
object_class = getattr(module, class_name)
if is_property_list is True:
list_class = object_class
object_class = getattr(module, class_name[0:-4])
object_class = getattr(module, class_name[0:-3])
return (list_class, object_class)

def __str__(self):
Expand Down Expand Up @@ -252,8 +252,7 @@ def _getitem(self, key):
found = item
if found is None:
raise IndexError()
if 'choice' in found._properties and \
found._properties.get("choice") is not None:
if 'choice' in found._properties and found._properties.get("choice") is not None:
return found._properties[found._properties['choice']]
return found

Expand Down Expand Up @@ -286,7 +285,7 @@ def _encode(self):
return [item._encode() for item in self._items]

def _decode(self, encoded_snappi_list):
item_class_name = self.__class__.__name__.replace('List', '')
item_class_name = self.__class__.__name__.replace('Seq', '')
module = importlib.import_module(self.__module__)
object_class = getattr(module, item_class_name)
self.clear()
Expand Down
22 changes: 11 additions & 11 deletions snappi/snappigenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import requests
from jsonpath_ng import parse

MODELS_RELEASE = 'v0.2.2'
MODELS_RELEASE = 'v0.3.1'


class SnappiGenerator(object):
"""Builds the snappi python package based on a released version of the
Expand Down Expand Up @@ -172,7 +173,6 @@ def _get_methods_and_factories(self):
refs.append(ref)

response = parse('$..responses..schema').find(operation)
response_object = ''
response_type = None
if len(response) == 0:
# since some responses currently directly $ref to a schema
Expand Down Expand Up @@ -226,7 +226,7 @@ def _get_methods_and_factories(self):
if schema_object['type'] == 'array':
ref = schema_object['items']['$ref']
_, _, class_name, _ = self._get_object_property_class_names(ref)
class_name = '%sList' % class_name
class_name = '%sSeq' % class_name
self._top_level_schema_refs.append((ref, property_name))
self._top_level_schema_refs.append((ref, None))

Expand Down Expand Up @@ -526,7 +526,7 @@ def _write_snappi_list(self, ref, property_name):
yobject = self._get_object_from_ref(ref)
ref_name = ref.split('/')[-1]
contained_class_name = ref_name.replace('.', '')
class_name = '%sList' % contained_class_name
class_name = '%sSeq' % contained_class_name
if class_name in self._generated_classes:
return
self._generated_classes.append(class_name)
Expand Down Expand Up @@ -584,7 +584,7 @@ def _write_snappilist_special_methods(self, contained_class_name, schema_object)
self._write(2, 'return self._getitem(key)')
self._write()
self._write(1, 'def __iter__(self):')
self._write(2, '# type: () -> %sList' % contained_class_name)
self._write(2, '# type: () -> %sSeq' % contained_class_name)
self._write(2, 'return self._iter()')
self._write()
self._write(1, 'def __next__(self):')
Expand All @@ -609,7 +609,7 @@ def _write_factory_method(self,
self._imports.append('from .%s import %s' % (class_name.lower(), class_name))
self._write(1, 'def %s(self%s):' % (method_name, param_string))
if contained_class_name is not None:
self._write(2, "# type: () -> %sList" % (contained_class_name))
self._write(2, "# type: () -> %sSeq" % (contained_class_name))
else:
self._write(2, "# type: () -> %s" % (class_name))
self._write(2, '"""Factory method that creates an instance of %s class' % (class_name))
Expand Down Expand Up @@ -650,7 +650,7 @@ def _get_property_param_string(self, yobject):
properties.append(name)
if 'default' in property:
default = property['default']
if 'enum' in property:
if 'enum' in property or 'format' in property:
val = "=%s" % default if default == 'None' else "='%s'" % default
property_param_string += val
else:
Expand All @@ -664,7 +664,7 @@ def _write_snappi_property(self, schema_object, name, property, write_set_choice
object_name = ref[0].value.split('/')[-1]
class_name = object_name.replace('.', '')
if restriction.startswith('list['):
type_name = '%sList' % class_name
type_name = '%sSeq' % class_name
else:
type_name = class_name
else:
Expand Down Expand Up @@ -696,7 +696,7 @@ def _write_snappi_property(self, schema_object, name, property, write_set_choice
self._write(2, "self._set_property('%s', value)" % (name))
elif len(ref) > 0:
if restriction.startswith('list['):
self._write(2, "return self._get_property('%s', %sList)" % (name, class_name))
self._write(2, "return self._get_property('%s', %sSeq)" % (name, class_name))
else:
self._write(2, "return self._get_property('%s', %s)" % (name, class_name))

Expand Down Expand Up @@ -724,7 +724,7 @@ def _get_snappi_types(self, yobject):
object_name = ref[0].value.split('/')[-1]
class_name = object_name.replace('.', '')
if 'type' in yproperty and yproperty['type'] == 'array':
class_name += 'List'
class_name += 'Seq'
types.append((name, class_name))
return types

Expand Down Expand Up @@ -784,7 +784,7 @@ def _write_component_schemas(self):
if 'required' in yobject and 'choice' not in yobject[
'required']:
choice_tuples.append(
('None', choice_enum, choice_enum))
('None', None, None))
for choice_enum in yobject['properties']['choice']['enum']:
if choice_enum not in yobject['properties']:
choice_tuples.append(
Expand Down
16 changes: 8 additions & 8 deletions snappi/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ def b2b_config(api):
.port(name='Rx Port', location='10.36.74.26;02;14')

tx_device, rx_device = config.devices \
.device(name='Tx Devices', container_name=tx_port.name, device_count=1) \
.device(name='Rx Devices', container_name=tx_port.name, device_count=1)
.device(name='Tx Devices', container_name=tx_port.name) \
.device(name='Rx Devices', container_name=tx_port.name)
tx_device.ethernet.name = 'Tx Eth'
tx_device.ethernet.mac.value = '00:00:01:00:00:01'
tx_device.ethernet.mac = '00:00:01:00:00:01'
tx_device.ethernet.ipv4.name = 'Tx Ipv4'
tx_device.ethernet.ipv4.address.value = '1.1.1.1'
tx_device.ethernet.ipv4.gateway.value = '1.1.2.1'
tx_device.ethernet.ipv4.prefix.value = 16
tx_device.ethernet.ipv4.address = '1.1.1.1'
tx_device.ethernet.ipv4.gateway = '1.1.2.1'
tx_device.ethernet.ipv4.prefix = 16
vlan1, vlan2 = tx_device.ethernet.vlans.vlan().vlan()
vlan1.id.value = 1
vlan2.id.values = [2, 3, 4]
vlan1.id = 1
vlan2.id = 2

flow = config.flows.flow(name='Tx -> Rx Flow')[0]
flow.tx_rx.port.tx_name = tx_port.name
Expand Down
40 changes: 0 additions & 40 deletions snappi/tests/test_bgp_as_path.py

This file was deleted.

90 changes: 90 additions & 0 deletions snappi/tests/test_bgp_sr_te_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import pytest


def test_bgp_sr_te_policy(api):
"""Test BGP SR TE Policy functionality
"""
config = api.config()

# setup port container
p1 = config.ports.port(name='p1')[-1]

# setup device container
d = config.devices.device(name='d', container_name=p1.name)[-1]

# setup ethernet
eth = d.ethernet
eth.name = 'e'
eth.mac = '00:01:00:00:00:01'

# setup ipv6
ip = eth.ipv6
ip.name = 'i6'
ip.address = '2a00:1450:f013:c03:8402:0:0:2'
ip.gateway = '2a00:1450:f013:c03:0:0:0:1'
ip.prefix = 64

# setup bgp basic
bgp = ip.bgpv6
bgp.name = 'b6'
bgp.router_id = '193.0.0.1'
bgp.as_number = 65511
bgp.as_number_set_mode = bgp.DO_NOT_INCLUDE_AS
bgp.local_address = '2a00:1450:f013:c03:8402:0:0:2'
bgp.dut_address = '2001:4860:0:0:0:1c:4001:ec2'

# setup bgp advanced
bgp.advanced.hold_time_interval = 90
bgp.advanced.keep_alive_interval = 30

# setup bgp sr te policy
for i in range(1, 501):
policy = bgp.sr_te_policies.bgpsrtepolicy()[-1]
policy.policy_type = policy.IPV4
policy.distinguisher = 1
policy.color = i
policy.ipv6_endpoint = '0:0:0:0:0:0:0:0'

hop = policy.next_hop
hop.next_hop_mode = hop.MANUAL
hop.next_hop_address_type = hop.IPV6
hop.ipv6_address = '2a00:1450:f013:c07:8402:0:0:2'

# setup tunnel tlv
tunnel = policy.tunnel_tlvs.bgptunneltlv(active=True)[-1]

# setup tunnel tlv segment lists
seglist = tunnel.segment_lists.bgpsegmentlist(active=True)[-1]
seglist.segment_weight = 1

# setup segment list segments
for label in [1018001, 432999, 1048333, 1048561, 432001]:
seg = seglist.segments.bgpsegment(active=True)[-1]
seg.segment_type = seg.MPLS_SID
seg.mpls_label = label

# setup preference sub tlv
pref_sub_tlv = tunnel.preference_sub_tlv
pref_sub_tlv.preference = 400

# setup binding sub tlv
bind_sub_tlv = tunnel.binding_sub_tlv
bind_sub_tlv.binding_sid_type = bind_sub_tlv.FOUR_OCTET_SID
bind_sub_tlv.four_octet_sid = 483001
bind_sub_tlv.bsid_as_mpls_label = True
bind_sub_tlv.s_flag = False
bind_sub_tlv.i_flag = False

# setup explicit null label policy sub tlv
enlp_sub_tlv = tunnel.explicit_null_label_policy_sub_tlv
enlp_sub_tlv.explicit_null_label_policy = 2

# setup bgpv4 route range
v4rr = bgp.bgpv4_routes.bgpv4route(name='v4rr')[-1]
v4rr.addresses.bgpv4routeaddress(address='4.4.4.4', prefix=32, count=5000, step=1)
v4rr.as_path.as_set_mode = v4rr.as_path.INCLUDE_AS_SEQ
v4rr.as_path.as_path_segments.bgpaspathsegment(as_numbers=[1, 2, 3, 4, 5, 6])


if __name__ == '__main__':
pytest.main(['-s', __file__])
2 changes: 1 addition & 1 deletion snappi/tests/test_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_choice(api):
eth = flow.packet[0]
assert (eth.__class__ == snappi.FlowEthernet)
eth_type = eth.ether_type
assert (eth_type.__class__ == snappi.FlowPattern)
assert (eth_type.__class__ == snappi.PatternFlowEthernetEtherType)
eth.src.value = '00:00:01:00:00:01'
assert (eth.src.choice == 'value')
eth.src.values = ['00:00:01:00:00:01', '00:00:01:00:00:0a']
Expand Down
6 changes: 2 additions & 4 deletions snappi/tests/test_device_factory_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ def test_device_factory_methods(api):
"""
config = api.config()

param = ('name', 'container name', 11)
param = ('name', 'container name')
device = config.devices.device(name=param[0],
container_name=param[1],
device_count=param[2])[-1]
container_name=param[1])[-1]
assert (device.name == param[0])
assert (device.container_name == param[1])
assert (device.device_count == param[2])

name = 'eth name'
eth = device.ethernet
Expand Down
8 changes: 4 additions & 4 deletions snappi/tests/test_device_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ def test_device_stack(api):
d = config.devices.device(name='d', container_name=p1.name)[-1]
e = d.ethernet
e.name = 'e'
e.mac.value = '00:01:00:00:00:01'
e.mac = '00:01:00:00:00:01'
i4 = e.ipv4
i4.name = 'i4'
i4.address.value = '1.1.1.1'
i4.address = '1.1.1.1'
b4 = i4.bgpv4
b4.name = 'b4'
b4.bgpv4_route_ranges.bgpv4routerange()
b4.bgpv4_routes.bgpv4route()
i6 = e.ipv6
i6.name = 'i6'
i6.address.value = '2001::1'
i6.address = '2001::1'
b6 = i6.bgpv6
b6.name = 'b6'

Expand Down
5 changes: 3 additions & 2 deletions snappi/tests/test_e2e_port_flow_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def test_e2e_port_flow_config(api):
flow.rate.pps = 1000
flow.duration.fixed_packets.packets = 10000

_, _, ip, _ = flow.packet.ethernet().vlan().ipv4().tcp()
flow.packet.ethernet().vlan().ipv4().tcp()
ip = flow.packet[2]

eth = flow.packet[0]
eth.src.value = '00:00:01:00:00:01'
Expand All @@ -41,7 +42,7 @@ def test_e2e_port_flow_config(api):
ip.dst.decrement.step = '0.0.0.1'
ip.dst.decrement.count = 10

ip.priority.dscp.ecn.value = ip.priority.dscp.ECN_CAPABLE_TRANSPORT_1
ip.priority.dscp.ecn.value = ip.priority.dscp.ecn.CAPABLE_TRANSPORT_1
ip.priority.dscp.ecn.metric_group = 'ip.priority.dscp.ecn'

# set and get the configuration
Expand Down
31 changes: 31 additions & 0 deletions snappi/tests/test_lag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest


def test_lag(api):
"""Test LAG functionality
"""
config = api.config()

# setup port container
p1, p2, p3 = (config.ports
.port(name='p1')
.port(name='p2')
.port(name='p3')
)

# setup lag container
l1 = config.lags.lag(name='l1')[-1]
for port in config.ports:
lp = l1.ports.port(port_name=port.name)[-1]
lp.ethernet.name = 'lpe {}'.format(port.name)
lp.ethernet.mac = '00:00:01:00:00:01'
lacp = lp.protocol.lacp
lacp.actor_system_id = '00000A000001'
lacp.actor_key = 1
lacp.actor_port_number = 10

api.set_config(config)


if __name__ == '__main__':
pytest.main(['-s', __file__])
Loading

0 comments on commit 5927250

Please sign in to comment.