Merge "vnf_generic: Fix str object has no attribute items"
[yardstick.git] / yardstick / network_services / pipeline.py
1 # Copyright (c) 2017 Intel Corporation\r
2 #\r
3 # Licensed under the Apache License, Version 2.0 (the "License");\r
4 # you may not use this file except in compliance with the License.\r
5 # You may obtain a copy of the License at\r
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\r
9 # Unless required by applicable law or agreed to in writing, software\r
10 # distributed under the License is distributed on an "AS IS" BASIS,\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 # See the License for the specific language governing permissions and\r
13 # limitations under the License.\r
14 \r
15 from __future__ import absolute_import\r
16 from __future__ import print_function\r
17 import itertools\r
18 \r
19 from six.moves import zip\r
20 \r
21 FIREWALL_ADD_DEFAULT = "p {0} firewall add default 1"\r
22 FIREWALL_ADD_PRIO = """\\r
23 p {0} firewall add priority 1 ipv4  {1} 24 0.0.0.0 0 0 65535 0 65535 6 0xFF port 0"""\r
24 \r
25 FLOW_ADD_QINQ_RULES = """\\r
26 p {0} flow add qinq 128 512 port 0 id 1\r
27 p {0} flow add default 1"""\r
28 \r
29 ACTION_FLOW_BULK = "p {0} action flow bulk /tmp/action_bulk_512.txt"\r
30 ACTION_DSCP_CLASS_COLOR = "p {0} action dscp {1} class {2} color {3}"\r
31 ROUTE_ADD_DEFAULT = "p {0} route add default 1"\r
32 ROUTE_ADD_ETHER_QINQ = 'p {0} route add {1} {2} port 0 ether {3} qinq 0 {4}'\r
33 ROUTE_ADD_ETHER_MPLS = "p {0} route add {1} 21 port 0 ether {2} mpls 0:{3}"\r
34 \r
35 \r
36 class PipelineRules(object):\r
37 \r
38     def __init__(self, pipeline_id=0):\r
39         super(PipelineRules, self).__init__()\r
40         self.rule_list = []\r
41         self.pipeline_id = pipeline_id\r
42 \r
43     def __str__(self):\r
44         return '\n'.join(self.rule_list)\r
45 \r
46     def get_string(self):\r
47         return str(self)\r
48 \r
49     def next_pipeline(self, num=1):\r
50         self.pipeline_id += num\r
51 \r
52     def add_newline(self):\r
53         self.rule_list.append('')\r
54 \r
55     def add_rule(self, base, *args):\r
56         self.rule_list.append(base.format(self.pipeline_id, *args))\r
57 \r
58     def add_firewall_prio(self, ip):\r
59         self.add_rule(FIREWALL_ADD_PRIO, ip)\r
60 \r
61     def add_firewall_script(self, ip):\r
62         ip_addr = ip.split('.')\r
63         assert len(ip_addr) == 4\r
64         ip_addr[-1] = '0'\r
65         for i in range(256):\r
66             ip_addr[-2] = str(i)\r
67             ip = '.'.join(ip_addr)\r
68             self.add_firewall_prio(ip)\r
69         self.add_rule(FIREWALL_ADD_DEFAULT)\r
70         self.add_newline()\r
71 \r
72     def add_flow_classification_script(self):\r
73         self.add_rule(FLOW_ADD_QINQ_RULES)\r
74 \r
75     def add_flow_action(self):\r
76         self.add_rule(ACTION_FLOW_BULK)\r
77 \r
78     def add_dscp_class_color(self, dscp, color):\r
79         self.add_rule(ACTION_DSCP_CLASS_COLOR, dscp, dscp % 4, color)\r
80 \r
81     def add_flow_action2(self):\r
82         self.add_rule(ACTION_FLOW_BULK)\r
83         for dscp, color in zip(range(64), itertools.cycle('GYR')):\r
84             self.add_dscp_class_color(dscp, color)\r
85 \r
86     def add_route_ether_mpls(self, ip, mac_addr, index):\r
87         self.add_rule(ROUTE_ADD_ETHER_MPLS, ip, mac_addr, index)\r
88 \r
89     def add_route_script(self, ip, mac_addr):\r
90         ip_addr = ip.split('.')\r
91         assert len(ip_addr) == 4\r
92         ip_addr[-1] = '0'\r
93         for index in range(0, 256, 8):\r
94             ip_addr[-2] = str(index)\r
95             ip = '.'.join(ip_addr)\r
96             self.add_route_ether_mpls(ip, mac_addr, index)\r
97         self.add_rule(ROUTE_ADD_DEFAULT)\r
98         self.add_newline()\r
99 \r
100     def add_ether_qinq(self, ip, mask, mac_addr, index):\r
101         self.add_rule(ROUTE_ADD_ETHER_QINQ, ip, mask, mac_addr, index)\r
102 \r
103     def add_route_script2(self, ip, mac_addr):\r
104         ip_addr = ip.split('.')\r
105         assert len(ip_addr) == 4\r
106         ip_addr[-1] = '0'\r
107         mask = 24\r
108         for i in range(0, 256):\r
109             ip_addr[-2] = str(i)\r
110             ip = '.'.join(ip_addr)\r
111             self.add_ether_qinq(ip, mask, mac_addr, i)\r
112         self.add_rule(ROUTE_ADD_DEFAULT)\r
113         self.add_newline()\r