Statistics
| Branch: | Revision:

iof-tools / confFileGenerator / Node.py @ 7c83f065

History | View | Annotate | Download (6.05 KB)

1 29b7b024 tiamilani
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
#
17
# Copyright (C) 2019  Mattia Milani <mattia.milani@studenti.unitn.it>
18
19 731bee12 tiamilani
import ipaddress
20 2e91210d tiamilani
from constants import *
21
import os.path
22 7c83f065 marco.nesler
from jinja2 import Environment, FileSystemLoader
23 731bee12 tiamilani
24
class Node:
25
    nodeIpAddr_network = ipaddress.ip_network(u'200.0.0.0/8')
26 2e91210d tiamilani
    counter_node = 1
27 1fd2a19a tiamilani
    # ATTENTION if you would like to change the network 100.0.0.0/8 also change it in the conf file commonFilters.conf
28
    # otherwise the configuration probably would drop all update packets.
29 2e91210d tiamilani
    nodeIpNetworks_network = list(ipaddress.ip_network(u'100.0.0.0/8').subnets(new_prefix=24))
30
    counter_networks = 1
31 1fd2a19a tiamilani
    ClientList = []
32 731bee12 tiamilani
33 61af9e95 tiamilani
    def __init__(self, name, node_type, out_folder, mrai='0'):
34 731bee12 tiamilani
        self.name = name
35 e5903271 tiamilani
        self.type = node_type
36 61af9e95 tiamilani
        self.mrai = int(float(mrai)*1000)
37 1fd2a19a tiamilani
38
        if self.type == "C":
39
            Node.ClientList.append(int(self.name)+1)
40
41 b210f15e tiamilani
        self.outFolder = out_folder
42 2e91210d tiamilani
        self.mainOutFileName = "bgp_h_" + str(self.name) + ".conf"
43
        self.sessionExporterFile_name = "bgpSessionExporter_h_" + str(name) + ".conf"
44
        if Node.counter_node < Node.nodeIpAddr_network.num_addresses - 1:
45
            self.router_addr = Node.nodeIpAddr_network[Node.counter_node]
46
            Node.counter_node += 1
47 731bee12 tiamilani
        else:
48
            raise ValueError('No more addresses for routers')
49
50 2e91210d tiamilani
        # Templates bases
51
        with open(BIRD_TEMPLATE_PATH, "r") as bird_file:
52
            self.bird_template = bird_file.read()
53
        with open(BGP_SESSION_EXPORTER_TEMPLATE_PATH, "r") as bgp_file:
54
            self.bgp_session_exporter = bgp_file.read()
55 227f2eb7 tiamilani
        with open(MRAI_TEMPLATE_FILE, "r") as mrai_file:
56
            self.mrai_template = mrai_file.read()
57 2e91210d tiamilani
58
        self.mainOutFile = open(self.outFolder + self.mainOutFileName, 'w+')
59
        self.sessionExporterFile = None
60 1fd2a19a tiamilani
        self.exporting_proto_name = "static_bgp"
61 2e91210d tiamilani
        self.exportedNetworks = []
62
        self.exportedNetworks_str = ""
63 b210f15e tiamilani
        self.log_file_name = "log_h_" + str(self.name) + ".log"
64 7c83f065 marco.nesler
        self.network_file_name = "network-config-node-" + str(self.name) + ".sh"
65 b210f15e tiamilani
66
        self.eth_dict = {}
67 656667d3 tiamilani
        self.customer = {}
68
        self.peer = {}
69
        self.servicer = {}
70 b210f15e tiamilani
71 2e91210d tiamilani
        self.write_main_file()
72
73 731bee12 tiamilani
    def __str__(self):
74 b210f15e tiamilani
        return "{" + str(self.name) + "," + str(self.router_addr) + "}"
75 2e91210d tiamilani
76 656667d3 tiamilani
    def add_customer(self, node):
77
        self.customer[node.name] = node
78
79
    def add_peer(self, node):
80
        self.peer[node.name] = node
81
82
    def add_servicer(self, node):
83
        self.servicer[node.name] = node
84
85 e95eeecf tiamilani
    def get_customers_addresses(self):
86
        addr_list = []
87
        for node in self.customer.values():
88
            addr_list.append(str(node.get_external_addr(self)))
89
        return addr_list
90 656667d3 tiamilani
91 2e91210d tiamilani
    def add_addr_to_export(self):
92
        if Node.counter_networks < len(Node.nodeIpNetworks_network):
93
            self.exportedNetworks.append(Node.nodeIpNetworks_network[Node.counter_networks])
94
            Node.counter_networks += 1
95
        else:
96
            raise ValueError('No more networks free')
97
        self.delete_export_file()
98
99 1fd2a19a tiamilani
        self.exportedNetworks_str += "route " + str(self.exportedNetworks[-1]) + " via \"lo\";\n\t\t\t"
100 2e91210d tiamilani
101
        self.write_export_file()
102 b210f15e tiamilani
        self.include_in_main(self.sessionExporterFile_name)
103 2e91210d tiamilani
104
    def write_main_file(self):
105
        # Write the template inside the file
106 b210f15e tiamilani
        open(self.outFolder + self.log_file_name, "a").close()
107
        self.mainOutFile.write(
108 e5903271 tiamilani
            self.bird_template.format(log_file_path="/etc/bird/"+self.log_file_name, log_mode=LOG_MODE,
109
                                      dbg_mode=DBG_MODE, dbg_commands_mode=DBG_COMMANDS_MODE, addr=self.router_addr,
110 29b7b024 tiamilani
                                      kernel_conf_path=KERNEL_CONF_PATH,
111
                                      direct_conf_path=DIRECT_CONF_PATH,
112
                                      device_conf_path=DEVICE_CONF_PATH,
113
                                      filter_conf_path=FILTER_CONF_PATH,
114
                                      bgp_session_export_path="", bgp_session_path=""))
115 2e91210d tiamilani
116 7c83f065 marco.nesler
    def write_network_configuration(self):
117
        # Write the network configuration of the node
118
        env = Environment(loader=FileSystemLoader('templates/'))
119
        baseline = env.get_template(NETWORK_TEMPLATE_PATH)
120
        ip_addresses = []
121
        for _,ip in self.eth_dict.items():
122
            ip_addresses.append(str(ip))
123
        rendered_template = baseline.render(name=self.name,ipaddresses=ip_addresses)
124
        with open(self.outFolder + self.network_file_name, "w") as netfd:
125
            netfd.write(rendered_template)
126
127 2e91210d tiamilani
    def delete_export_file(self):
128
        if os.path.isfile(self.outFolder + self.sessionExporterFile_name):
129
            os.remove(self.outFolder + self.sessionExporterFile_name)
130
131
    def write_export_file(self):
132
        self.sessionExporterFile = open(self.outFolder + self.sessionExporterFile_name, "w+")
133
        self.sessionExporterFile.write(self.bgp_session_exporter.format(session_protocol_name=self.exporting_proto_name,
134
                                                                        addr_to_export=self.exportedNetworks_str))
135
136 b210f15e tiamilani
    def include_in_main(self, file_name):
137 1fd2a19a tiamilani
        self.mainOutFile.write("include  \"/etc/bird/" + file_name + "\";\n")
138 b210f15e tiamilani
139 29b7b024 tiamilani
    def set_new_external_addr(self, neighbor_node, addr):
140
        self.eth_dict[str(neighbor_node.name)] = addr
141
142
    def get_external_addr(self, neighbor_node):
143
        if str(neighbor_node.name) in self.eth_dict.keys():
144
            return self.eth_dict[str(neighbor_node.name)]
145
        return None