Merge "Import "traffic_profile" modules only once"
[yardstick.git] / yardstick / network_services / yang_model.py
1 # Copyright (c) 2017 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 from __future__ import absolute_import
16 from __future__ import print_function
17 import logging
18 import ipaddress
19 import six
20
21 from yardstick.common.yaml_loader import yaml_load
22
23 LOG = logging.getLogger(__name__)
24
25
26 class YangModel(object):
27
28     RULE_TEMPLATE = "p acl add 1 {0} {1} {2} {3} {4} {5} {6} {7} 0 0 {8}"
29
30     def __init__(self, config_file):
31         super(YangModel, self).__init__()
32         self._config_file = config_file
33         self._options = {}
34         self._rules = ''
35
36     @property
37     def config_file(self):
38         return self._config_file
39
40     @config_file.setter
41     def config_file(self, value):
42         self._config_file = value
43         self._options = {}
44         self._rules = ''
45
46     def _read_config(self):
47         # TODO: add some error handling in case of empty or non-existing file
48         try:
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)
53             raise
54
55     def _get_entries(self):
56         if not self._options:
57             return ''
58
59         rule_list = []
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
68
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
73
74             lower_dport = matches['destination-port-range']['lower-port']
75             upper_dport = matches['destination-port-range']['upper-port']
76
77             lower_sport = matches['source-port-range']['lower-port']
78             upper_sport = matches['source-port-range']['upper-port']
79
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,
84                                                        port0_prefix,
85                                                        port1_local_network,
86                                                        port1_prefix,
87                                                        lower_dport,
88                                                        upper_dport,
89                                                        lower_sport,
90                                                        upper_sport,
91                                                        0))
92             rule_list.append(self.RULE_TEMPLATE.format(port1_local_network,
93                                                        port1_prefix,
94                                                        port0_local_network,
95                                                        port0_prefix,
96                                                        lower_sport,
97                                                        upper_sport,
98                                                        lower_dport,
99                                                        upper_dport,
100                                                        1))
101
102         self._rules = '\n'.join(rule_list)
103
104     def get_rules(self):
105         if not self._rules:
106             self._read_config()
107             self._get_entries()
108         return self._rules