1 # Copyright (c) 2017 Intel Corporation
\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
7 # http://www.apache.org/licenses/LICENSE-2.0
\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
15 from __future__ import absolute_import
\r
16 from __future__ import print_function
\r
22 LOG = logging.getLogger(__name__)
\r
25 class YangModel(object):
\r
27 RULE_TEMPLATE = "p acl add 1 {0} {1} {2} {3} {4} {5} {6} {7} 0 0 {8}"
\r
29 def __init__(self, config_file):
\r
30 super(YangModel, self).__init__()
\r
31 self._config_file = config_file
\r
36 def config_file(self):
\r
37 return self._config_file
\r
40 def config_file(self, value):
\r
41 self._config_file = value
\r
45 def _read_config(self):
\r
46 # TODO: add some error handling in case of empty or non-existing file
\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
54 def _get_entries(self):
\r
55 if not self._options:
\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
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
73 lower_dport = matches['destination-port-range']['lower-port']
\r
74 upper_dport = matches['destination-port-range']['upper-port']
\r
76 lower_sport = matches['source-port-range']['lower-port']
\r
77 upper_sport = matches['source-port-range']['upper-port']
\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
84 port1_local_network,
\r
91 rule_list.append(self.RULE_TEMPLATE.format(port1_local_network,
\r
93 port0_local_network,
\r
101 self._rules = '\n'.join(rule_list)
\r
103 def get_rules(self):
\r
104 if not self._rules:
\r
105 self._read_config()
\r
106 self._get_entries()
\r