1 # Copyright (c) 2017 Intel 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 implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from __future__ import absolute_import
16 from __future__ import print_function
21 from yardstick.common.yaml_loader import yaml_load
23 LOG = logging.getLogger(__name__)
26 class YangModel(object):
28 RULE_TEMPLATE = "p acl add 1 {0} {1} {2} {3} {4} {5} {6} {7} 0 0 {8}"
30 def __init__(self, config_file):
31 super(YangModel, self).__init__()
32 self._config_file = config_file
37 def config_file(self):
38 return self._config_file
41 def config_file(self, value):
42 self._config_file = value
46 def _read_config(self):
47 # TODO: add some error handling in case of empty or non-existing file
49 with open(self._config_file) as f:
50 self._options = yaml_load(f)
51 except Exception as e:
52 LOG.exception("Failed to load the yaml %s", e)
55 def _get_entries(self):
60 for ace in self._options['access-list1']['acl']['access-list-entries']:
61 # TODO: resolve ports using topology file and nodes'
62 # ids: public or private.
63 matches = ace['ace']['matches']
64 dst_ipv4_net = matches['destination-ipv4-network']
65 dst_ipv4_net_ip = ipaddress.ip_interface(six.text_type(dst_ipv4_net))
66 port0_local_network = dst_ipv4_net_ip.network.network_address.exploded
67 port0_prefix = dst_ipv4_net_ip.network.prefixlen
69 src_ipv4_net = matches['source-ipv4-network']
70 src_ipv4_net_ip = ipaddress.ip_interface(six.text_type(src_ipv4_net))
71 port1_local_network = src_ipv4_net_ip.network.network_address.exploded
72 port1_prefix = src_ipv4_net_ip.network.prefixlen
74 lower_dport = matches['destination-port-range']['lower-port']
75 upper_dport = matches['destination-port-range']['upper-port']
77 lower_sport = matches['source-port-range']['lower-port']
78 upper_sport = matches['source-port-range']['upper-port']
80 # TODO: proto should be read from file also.
81 # Now all rules in sample ACL file are TCP.
82 rule_list.append('') # get an extra new line
83 rule_list.append(self.RULE_TEMPLATE.format(port0_local_network,
92 rule_list.append(self.RULE_TEMPLATE.format(port1_local_network,
102 self._rules = '\n'.join(rule_list)