1 # Copyright (C) 2017 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
20 from nose.tools import eq_, raises
22 from ryu.lib.packet.bgp import (
23 BGPFlowSpecTrafficRateCommunity,
24 BGPFlowSpecTrafficActionCommunity,
25 BGPFlowSpecRedirectCommunity,
26 BGPFlowSpecTrafficMarkingCommunity,
27 BGPFlowSpecVlanActionCommunity,
28 BGPFlowSpecTPIDActionCommunity,
31 from ryu.services.protocols.bgp.core import BgpCoreError
32 from ryu.services.protocols.bgp.utils.bgp import create_v4flowspec_actions
33 from ryu.services.protocols.bgp.utils.bgp import create_v6flowspec_actions
34 from ryu.services.protocols.bgp.utils.bgp import create_l2vpnflowspec_actions
37 LOG = logging.getLogger(__name__)
40 class Test_Utils_BGP(unittest.TestCase):
42 Test case for ryu.services.protocols.bgp.utils.bgp
45 def _test_create_v4flowspec_actions(self, actions, expected_communities):
46 communities = create_v4flowspec_actions(actions)
47 expected_communities.sort(key=lambda x: x.subtype)
48 communities.sort(key=lambda x: x.subtype)
49 eq_(str(expected_communities), str(communities))
51 def test_create_v4flowspec_actions_all_actions(self):
62 'local_administrator': 10,
68 expected_communities = [
69 BGPFlowSpecTrafficRateCommunity(as_number=0, rate_info=100.0),
70 BGPFlowSpecTrafficActionCommunity(action=3),
71 BGPFlowSpecRedirectCommunity(as_number=10, local_administrator=10),
72 BGPFlowSpecTrafficMarkingCommunity(dscp=24),
74 self._test_create_v4flowspec_actions(actions, expected_communities)
76 def test_create_v4flowspec_actions_without_actions(self):
78 expected_communities = []
79 self._test_create_v4flowspec_actions(actions, expected_communities)
82 def test_create_v4flowspec_actions_not_exist_actions(self):
88 expected_communities = []
89 self._test_create_v4flowspec_actions(actions, expected_communities)
91 def _test_create_v6flowspec_actions(self, actions, expected_communities):
92 communities = create_v6flowspec_actions(actions)
93 expected_communities.sort(key=lambda x: x.subtype)
94 communities.sort(key=lambda x: x.subtype)
95 eq_(str(expected_communities), str(communities))
97 def test_create_v6flowspec_actions_all_actions(self):
108 'local_administrator': 10,
114 expected_communities = [
115 BGPFlowSpecTrafficRateCommunity(as_number=0, rate_info=100.0),
116 BGPFlowSpecTrafficActionCommunity(action=3),
117 BGPFlowSpecRedirectCommunity(as_number=10, local_administrator=10),
118 BGPFlowSpecTrafficMarkingCommunity(dscp=24),
120 self._test_create_v6flowspec_actions(actions, expected_communities)
122 def test_create_v6flowspec_actions_without_actions(self):
124 expected_communities = []
125 self._test_create_v6flowspec_actions(actions, expected_communities)
128 def test_create_v6flowspec_actions_not_exist_actions(self):
134 expected_communities = []
135 self._test_create_v6flowspec_actions(actions, expected_communities)
137 def _test_create_l2vpnflowspec_actions(self, actions, expected_communities):
138 communities = create_l2vpnflowspec_actions(actions)
139 expected_communities.sort(key=lambda x: x.subtype)
140 communities.sort(key=lambda x: x.subtype)
141 eq_(str(expected_communities), str(communities))
143 def test_create_l2vpnflowspec_actions_all_actions(self):
154 'local_administrator': 10,
160 'actions_1': (BGPFlowSpecVlanActionCommunity.POP |
161 BGPFlowSpecVlanActionCommunity.SWAP),
164 'actions_2': BGPFlowSpecVlanActionCommunity.PUSH,
169 'actions': (BGPFlowSpecTPIDActionCommunity.TI |
170 BGPFlowSpecTPIDActionCommunity.TO),
175 expected_communities = [
176 BGPFlowSpecTrafficRateCommunity(as_number=0, rate_info=100.0),
177 BGPFlowSpecTrafficActionCommunity(action=3),
178 BGPFlowSpecRedirectCommunity(as_number=10, local_administrator=10),
179 BGPFlowSpecTrafficMarkingCommunity(dscp=24),
180 BGPFlowSpecVlanActionCommunity(
181 actions_1=(BGPFlowSpecVlanActionCommunity.POP |
182 BGPFlowSpecVlanActionCommunity.SWAP),
185 actions_2=BGPFlowSpecVlanActionCommunity.PUSH,
189 BGPFlowSpecTPIDActionCommunity(
190 actions=(BGPFlowSpecTPIDActionCommunity.TI |
191 BGPFlowSpecTPIDActionCommunity.TO),
196 self._test_create_l2vpnflowspec_actions(actions, expected_communities)
198 def test_create_l2vpnflowspec_actions_without_actions(self):
200 expected_communities = []
201 self._test_create_l2vpnflowspec_actions(actions, expected_communities)
204 def test_create_l2vpnflowspec_actions_not_exist_actions(self):
210 expected_communities = []
211 self._test_create_l2vpnflowspec_actions(actions, expected_communities)