Bug 3269 - Neutron External Gateway not functioning with floatingIP
[groupbasedpolicy.git] / util / testOfOverlay / testOfOverlay.py
1 #!/usr/bin/python
2
3 import mininet_gbp
4 import odl_gbp
5 import mininet.cli
6 import ipaddr
7 import uuid
8 import re
9 import argparse, sys
10 from config import *
11
12 def getSubnet(ip):
13     nw = ipaddr.IPv4Network(ip)
14     return "{}/{}".format(nw.network + 1, nw.prefixlen)
15
16 if __name__ == '__main__':
17
18     # Validate all parameters are present
19     parser = argparse.ArgumentParser()
20     parser.add_argument('--local',
21                         help='Set up distributed mininet on local host with the specified switch')
22     parser.add_argument('--policy', action='store_true',
23                         help='Configure the policy on the controller')
24     parser.add_argument('--controller', default='127.0.0.1',
25                         help='Use the specified controller IP address')
26     args = parser.parse_args()
27
28     if (not args.local and not args.policy):
29         parser.print_help()
30         sys.exit(3)
31
32     # switches is a list from config.py, when this script is called with --local (switch) and its present in config, it is added to the conf_switches
33     conf_switches = []
34     if args.local:
35         for switch in switches:
36             if switch['name'] == args.local:
37                 conf_switches = [switch]
38                 break
39
40     # Assuming we have switches defined (and hence conf_switches), start mininet with the "hosts" list also from config.py
41     net = None
42     if len(conf_switches) > 0:
43         net = mininet_gbp.startMininet(conf_switches, hosts, args.controller)
44     try :
45         if args.policy:
46             for switch in switches:
47                 # This leverages a global from odl_gbp called "nodes", which appends "data" from this for loop
48                 odl_gbp.get_node_config(switch['dpid'], switch['tunnelIp'])
49             #This also uses the global "nodes" from odl_gbp
50             odl_gbp.register_nodes(args.controller)
51
52         # TENANT, L3CTX, L2BD are imported from config.py
53         # get_tenant looks for the TENANT UUID in a global tenant dictionary in odl_gbp.
54         # If TENANT doesn't already exist in that dict. then a bunch of 'default' tenant data is defined, inluding
55         # subjects and classifiers (at writing specific to HTTP source/dest and ICMP)
56         tenant = odl_gbp.get_tenant(TENANT)
57
58         # Layer3 context and Layer BridgeDomain are SET into the tenant{} structure in odl_gbp 
59         # TODO: (maybe call these set???)
60         odl_gbp.get_l3c(TENANT, L3CTX)
61         odl_gbp.get_bd(TENANT, L2BD, L3CTX)
62
63        # subnets and fds (flood domains)
64         subnets = {}
65         fds = {}
66         # hosts comes from config.py, which contains target switch, IP Address, MAC address, tenant and EPG
67         for host in hosts:
68             # ??????
69             if args.local and host['switch'] != args.local:
70                 continue
71             nw = ipaddr.IPv4Network(host['ip'])
72             snet = "{}/{}".format(nw.network + 1, nw.prefixlen)
73             router = "{}".format(nw.network + 1)
74
75             if snet not in subnets:
76                  snid = str(uuid.uuid4())
77                  fdid = str(uuid.uuid4())
78                  # Sets flood domain where parent is L2BD from config.py
79                  fds[fdid] = odl_gbp.get_fd(TENANT, fdid, L2BD)
80
81                  # sets subnet from tenant, which also includes the flood domain
82                  subnets[snet] = odl_gbp.get_subnet(TENANT, snid, fdid, snet, router)
83
84                  # Sets the "network-domain" in global endpointGroups dict in odl_gbp.py
85                  odl_gbp.get_epg(TENANT, host['endpointGroup'])["network-domain"] = snid
86
87             # Creates EP information and appends to endpoint list, a global
88             odl_gbp.get_ep(TENANT, 
89                            host['endpointGroup'], 
90                            L3CTX, 
91                            re.sub(r'/\d+$', '', host['ip']),
92                            L2BD,
93                            host['mac'], 
94                            int(net.get(host['switch']).dpid), host['port'])
95
96         # contracts is a global list from config.py.
97         # get_contract creates the specific subject, classifiers, rules etc for the contract
98         #     and appends this to the global tenant list.
99         for contract in contracts:
100              odl_gbp.get_contract(TENANT, 
101                           contract['provider'], contract['consumer'], 
102                           contract['id'])
103         
104         # POST to the controller to register tenants
105         if args.policy:
106             odl_gbp.register_tenants(args.controller)
107
108         # POST to controller to register EPS
109         # TODO: Should this be done on a per Tenant basis
110         odl_gbp.register_eps(args.controller)
111
112         if net is not None:
113             mininet.cli.CLI(net)
114     finally:
115         if net is not None:
116             net.stop()