Merge "UDP relay"
[yardstick.git] / yardstick / network_services / yang_model.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 logging\r
18 import ipaddress\r
19 import yaml\r
20 import six\r
21 \r
22 LOG = logging.getLogger(__name__)\r
23 \r
24 \r
25 class YangModel(object):\r
26 \r
27     RULE_TEMPLATE = "p acl add 1 {0} {1} {2} {3} {4} {5} {6} {7} 0 0 {8}"\r
28 \r
29     def __init__(self, config_file):\r
30         super(YangModel, self).__init__()\r
31         self._config_file = config_file\r
32         self._options = {}\r
33         self._rules = ''\r
34 \r
35     @property\r
36     def config_file(self):\r
37         return self._config_file\r
38 \r
39     @config_file.setter\r
40     def config_file(self, value):\r
41         self._config_file = value\r
42         self._options = {}\r
43         self._rules = ''\r
44 \r
45     def _read_config(self):\r
46         # TODO: add some error handling in case of empty or non-existing file\r
47         try:\r
48             with open(self._config_file) as f:\r
49                 self._options = yaml.safe_load(f)\r
50         except Exception as e:\r
51             LOG.exception("Failed to load the yaml %s", e)\r
52             raise\r
53 \r
54     def _get_entries(self):\r
55         if not self._options:\r
56             return ''\r
57 \r
58         rule_list = []\r
59         for ace in self._options['access-list1']['acl']['access-list-entries']:\r
60             # TODO: resolve ports using topology file and nodes'\r
61             # ids: public or private.\r
62             matches = ace['ace']['matches']\r
63             dst_ipv4_net = matches['destination-ipv4-network']\r
64             dst_ipv4_net_ip = ipaddress.ip_interface(six.text_type(dst_ipv4_net))\r
65             port0_local_network = dst_ipv4_net_ip.network.network_address.exploded\r
66             port0_prefix = dst_ipv4_net_ip.network.prefixlen\r
67 \r
68             src_ipv4_net = matches['source-ipv4-network']\r
69             src_ipv4_net_ip = ipaddress.ip_interface(six.text_type(src_ipv4_net))\r
70             port1_local_network = src_ipv4_net_ip.network.network_address.exploded\r
71             port1_prefix = src_ipv4_net_ip.network.prefixlen\r
72 \r
73             lower_dport = matches['destination-port-range']['lower-port']\r
74             upper_dport = matches['destination-port-range']['upper-port']\r
75 \r
76             lower_sport = matches['source-port-range']['lower-port']\r
77             upper_sport = matches['source-port-range']['upper-port']\r
78 \r
79             # TODO: proto should be read from file also.\r
80             # Now all rules in sample ACL file are TCP.\r
81             rule_list.append('')  # get an extra new line\r
82             rule_list.append(self.RULE_TEMPLATE.format(port0_local_network,\r
83                                                        port0_prefix,\r
84                                                        port1_local_network,\r
85                                                        port1_prefix,\r
86                                                        lower_dport,\r
87                                                        upper_dport,\r
88                                                        lower_sport,\r
89                                                        upper_sport,\r
90                                                        0))\r
91             rule_list.append(self.RULE_TEMPLATE.format(port1_local_network,\r
92                                                        port1_prefix,\r
93                                                        port0_local_network,\r
94                                                        port0_prefix,\r
95                                                        lower_sport,\r
96                                                        upper_sport,\r
97                                                        lower_dport,\r
98                                                        upper_dport,\r
99                                                        1))\r
100 \r
101         self._rules = '\n'.join(rule_list)\r
102 \r
103     def get_rules(self):\r
104         if not self._rules:\r
105             self._read_config()\r
106             self._get_entries()\r
107         return self._rules\r