#!/usr/bin/python
# Copyright (c) 2020 INSA Toulouse
# ALL RIGHTS RESERVED.
#
# This topology has been built by inspiring on the 'default_single_dc_topology.py' example of son-emu
# 
# Authors
# Abdel Kader CHABI SIKA BONI (Master2 ILoRD at INSA Toulouse, chabisik@etud.insa-toulouse.fr)
# Arnaud PRIEU                (5SDBD at INSA Toulouse, prieu@etud.insa-toulouse.fr)
# Year: 2020-2021


import logging
from mininet.log import setLogLevel, info
from emuvim.dcemulator.net import DCNetwork
from emuvim.api.rest.rest_api_endpoint import RestApiEndpoint
from emuvim.api.openstack.openstack_api_endpoint import OpenstackApiEndpoint

logging.basicConfig(level=logging.INFO)
setLogLevel('info')  # set Mininet loglevel
logging.getLogger('werkzeug').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.base').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.compute').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.keystone').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.nova').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.neutron').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.heat').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.heat.parser').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.glance').setLevel(logging.DEBUG)
logging.getLogger('api.openstack.helper').setLevel(logging.DEBUG)

#####################################################################################################
#                                OUR TOPOLOGY'S DETAILS                                             #
#  (if changed, bootstrap server script must be changed too and the new config:topo image builded)  #
#####################################################################################################
BOOTSTRAP_SERVER = '10.10.10.10' #name = btrap; port = 5555

SVR_IP = '10.0.0.1' #name = srv

GWI1_IP = '10.2.2.1' #name = gwi1
GWI2_IP = '10.2.2.2' #name = gwi2

GWF1_IP = '10.0.1.100' #name = gwf1
DEV1_GWF1_IP = '10.0.1.1' #name = dev1gwf1
DEV2_GWF1_IP = '10.0.1.2' #name = dev2gwf1
DEV3_GWF1_IP = '10.0.1.3' #name = dev3gwf1

GWF2_IP = '10.0.2.100' #name = gwf2
DEV1_GWF2_IP = '10.0.2.1' #name = dev1gwf2
DEV2_GWF2_IP = '10.0.2.2' #name = dev2gwf2
DEV3_GWF2_IP = '10.0.2.3' #name = dev3gwf2

GWF3_IP = '10.0.3.100' #name = gwf3
DEV1_GWF3_IP = '10.0.3.1' #name = dev1gwf3
DEV2_GWF3_IP = '10.0.3.2' #name = dev2gwf3
DEV3_GWF3_IP = '10.0.3.3' #name = dev3gwf3

'''
TOPOLOGY OVERVIEW
Necessary docker images: config:topo ; server:topo ; gateway:topo ; device:topo


                                                    ++++++++
                                                    + dev1 +
                                                    ++++++++
                                                       |  ______________++++++++
                                                       |  |             + dev2 +
                                ++++++++            ++++++++            ++++++++        ++++++++
                                + gwi1 +            + gwf1 +----------------------------+ dev3 +
                                ++++++++            ++++++++                            ++++++++
                                   |                 |
                                   |                 |
 +++++++        +++++++         +++++++         +++++++      ++++++++                    ++++++++
 + srv +--------+ s1  +---------+ s2  +---------+ s3  +------+ gwf2 +--------------------+ dev1 +
 +++++++        +++++++         +++++++         +++++++      ++++++++                    ++++++++
                                  |  |             |            | |             ++++++++
                                  |  |             |            | |_____________+ dev2 +
                                  |  |             |            |               ++++++++
                                  |  |          ++++++          |     ++++++++
                                  |  |__________+ s4 +----      |_____+ dev3 +
                                  |             ++++++   |            ++++++++
                              +++++++++           |      |
                              + btrap +           |      |____++++++++        ++++++++
                              +++++++++     ______|           + gwf3 +--------+ dev1 +
                                            |                 ++++++++        ++++++++
                                            |    _______________|  |
                                    ________|    |                 |
                                    |            |                 |___________++++++++
                                    |            |                             + dev2 +
                                 ++++++          |                             ++++++++
                                 + DC +      ++++++++
                                 ++++++      + dev3 +
                                             ++++++++
'''
#####################################################################################################


def create_topology():
    net = DCNetwork(monitor=False, enable_learning=True)

    dc1 = net.addDatacenter("dc1")
    # add OpenStack-like APIs to the emulated DC
    api1 = OpenstackApiEndpoint("0.0.0.0", 6001)
    api1.connect_datacenter(dc1)
    api1.start()
    api1.connect_dc_network(net)
    # add the command line interface endpoint to the emulated DC (REST API)
    rapi1 = RestApiEndpoint("0.0.0.0", 5001)
    rapi1.connectDCNetwork(net)
    rapi1.connectDatacenter(dc1)
    rapi1.start()

    info('*** Adding bootstrap server\n')
    btrap = net.addDocker('btrap', ip=BOOTSTRAP_SERVER, dimage="config:topo")

    info('*** Adding topology server\n')
    srv = net.addDocker('srv', ip=SVR_IP, dimage="server:topo", environment={'MY_IP':SVR_IP})

    info('*** Adding topology intermediary gateway\n')
    gwi1 = net.addDocker('gwi1', ip=GWI1_IP, dimage="gateway:topo", environment={'MY_IP':GWI1_IP})

    info('*** Adding topology final gateways\n')
    gwf1 = net.addDocker('gwf1', ip=GWF1_IP, dimage="gateway:topo", environment={'MY_IP':GWF1_IP})
    #gwf2 = net.addDocker('gwf2', ip=GWF2_IP, dimage="gateway:topo", environment={'MY_IP':GWF2_IP})
    #gwf3 = net.addDocker('gwf3', ip=GWF3_IP, dimage="gateway:topo", environment={'MY_IP':GWF3_IP})

    info('*** Adding 1st final gateway devices\n')
    dev1gwf1 = net.addDocker('dev1gwf1', ip=DEV1_GWF1_IP, dimage="device:topo", environment={'MY_IP':DEV1_GWF1_IP})
    dev2gwf1 = net.addDocker('dev2gwf1', ip=DEV2_GWF1_IP, dimage="device:topo", environment={'MY_IP':DEV2_GWF1_IP})
    dev3gwf1 = net.addDocker('dev3gwf1', ip=DEV3_GWF1_IP, dimage="device:topo", environment={'MY_IP':DEV3_GWF1_IP})

    info('*** Adding 2nd final gateway devices\n')
    #dev1gwf2 = net.addDocker('dev1gwf2', ip=DEV1_GWF2_IP, dimage="device:topo", environment={'MY_IP':DEV1_GWF2_IP})
    #dev2gwf2 = net.addDocker('dev2gwf2', ip=DEV2_GWF2_IP, dimage="device:topo", environment={'MY_IP':DEV2_GWF2_IP})
    #dev3gwf2 = net.addDocker('dev3gwf2', ip=DEV3_GWF2_IP, dimage="device:topo", environment={'MY_IP':DEV3_GWF2_IP})

    info('*** Adding 3rd final gateway devices\n')
    #dev1gwf3 = net.addDocker('dev1gwf3', ip=DEV1_GWF3_IP, dimage="device:topo", environment={'MY_IP':DEV1_GWF3_IP})
    #dev2gwf3 = net.addDocker('dev2gwf3', ip=DEV2_GWF3_IP, dimage="device:topo", environment={'MY_IP':DEV2_GWF3_IP})
    #dev3gwf3 = net.addDocker('dev3gwf3', ip=DEV3_GWF3_IP, dimage="device:topo", environment={'MY_IP':DEV3_GWF3_IP})

    info('*** Adding switches\n')
    s1 = net.addSwitch('s1')
    s2 = net.addSwitch('s2')
    s3 = net.addSwitch('s3')
    s4 = net.addSwitch('s4')
    s5 = net.addSwitch('s5')
    #s6 = net.addSwitch('s6')
    #s7 = net.addSwitch('s7')

    info('*** Creating links\n')
    net.addLink(btrap, s2)
    net.addLink(s1, s2)
    net.addLink(s1, srv)
    net.addLink(gwi1, s2)
    net.addLink(s2, s3)
    #net.addLink(s2, s4)
    net.addLink(s3, s4)
    net.addLink(dc1, s4)
    ########ZONE1###########
    net.addLink(s3, s5)
    net.addLink(gwf1, s5)
    net.addLink(dev1gwf1, s5)
    net.addLink(dev2gwf1, s5)
    net.addLink(dev3gwf1, s5)
    ########ZONE2###########
    #net.addLink(s3, s6)
    #net.addLink(gwf2, s6)
    #net.addLink(dev1gwf2, s6)
    #net.addLink(dev2gwf2, s6)
    #net.addLink(dev3gwf2, s6)
    ########ZONE3###########
    #net.addLink(s4, s7)
    #net.addLink(gwf3, s7)
    #net.addLink(dev1gwf3, s7)
    #net.addLink(dev2gwf3, s7)
    #net.addLink(dev3gwf3, s7)

    info('*** Starting network\n')
    net.start()
    net.CLI()
    # when the user types exit in the CLI, we stop the emulator
    net.stop()


def main():
    create_topology()


if __name__ == '__main__':
    main()
