initial artemis application commit

Change-Id: I01967b1e8e8df08cf95a2865566423a4aeb34ea9
diff --git a/tools/tutorials/artemis/configs/R1-quagga.conf b/tools/tutorials/artemis/configs/R1-quagga.conf
new file mode 100644
index 0000000..271a18f
--- /dev/null
+++ b/tools/tutorials/artemis/configs/R1-quagga.conf
@@ -0,0 +1,14 @@
+!
+hostname bgp
+password sdnip
+!
+!
+router bgp 65001
+	bgp router-id 1.1.1.1
+	network 10.0.0.0/8
+	neighbor 10.0.0.3 remote-as 65001
+	neighbor 150.1.1.1 remote-as 65003
+	neighbor 150.1.2.2 remote-as 65002
+!
+
+log stdout
\ No newline at end of file
diff --git a/tools/tutorials/artemis/configs/R2-quagga.conf b/tools/tutorials/artemis/configs/R2-quagga.conf
new file mode 100644
index 0000000..bacdafd
--- /dev/null
+++ b/tools/tutorials/artemis/configs/R2-quagga.conf
@@ -0,0 +1,14 @@
+!
+hostname bgp
+password sdnip
+!
+!
+router bgp 65002
+	bgp router-id 2.2.2.2
+	network 20.0.0.0/8
+	neighbor 150.1.2.1 remote-as 65001
+	neighbor 150.1.3.2 remote-as 65004
+!
+
+
+log stdout
\ No newline at end of file
diff --git a/tools/tutorials/artemis/configs/R3-quagga.conf b/tools/tutorials/artemis/configs/R3-quagga.conf
new file mode 100644
index 0000000..d02b053
--- /dev/null
+++ b/tools/tutorials/artemis/configs/R3-quagga.conf
@@ -0,0 +1,12 @@
+!
+hostname bgp
+password sdnip
+!
+!
+router bgp 65003
+	bgp router-id 3.3.3.3
+	network 30.0.0.0/8
+	neighbor 150.1.1.2 remote-as 65001
+!
+
+log stdout
\ No newline at end of file
diff --git a/tools/tutorials/artemis/configs/R4-quagga.conf b/tools/tutorials/artemis/configs/R4-quagga.conf
new file mode 100644
index 0000000..777d866
--- /dev/null
+++ b/tools/tutorials/artemis/configs/R4-quagga.conf
@@ -0,0 +1,16 @@
+!
+hostname bgp
+password sdnip
+!
+! 
+router bgp 65004
+bgp router-id 4.4.4.4
+	network 40.0.0.0/8
+	neighbor 10.10.10.2 remote-as 65004
+	neighbor 10.10.10.2 port 2000
+	neighbor 150.1.3.1 remote-as 65002
+	neighbor 150.1.3.1 next-hop-self
+!
+
+
+log stdout
\ No newline at end of file
diff --git a/tools/tutorials/artemis/configs/exabgp.conf b/tools/tutorials/artemis/configs/exabgp.conf
new file mode 100644
index 0000000..1dfd456
--- /dev/null
+++ b/tools/tutorials/artemis/configs/exabgp.conf
@@ -0,0 +1,20 @@
+group r1 {
+    router-id 10.0.0.3;
+    
+    process message-logger { 
+        encoder json;
+        receive {
+            parsed;
+            update;
+            neighbor-changes;
+        }
+        run ./absolute/path/to/onos/tools/tutorials/artemis/server.py;
+    }
+
+    neighbor 10.0.0.1 {
+        local-address 10.0.0.3;
+        local-as 65001;
+        peer-as 65001;
+    }
+
+}
diff --git a/tools/tutorials/artemis/configs/network-cfg.json b/tools/tutorials/artemis/configs/network-cfg.json
new file mode 100644
index 0000000..8b5d56f
--- /dev/null
+++ b/tools/tutorials/artemis/configs/network-cfg.json
@@ -0,0 +1,84 @@
+{
+    "ports" : {
+        "of:00002a45d713e141/2" : {
+            "interfaces" : [
+                {
+                    "name" : "sw1-1",
+                    "ips"  : [ "150.1.3.2/30" ],
+                    "mac"  : "e2:f5:32:16:9a:46"
+                }
+            ]
+        },
+        "of:00002a45d713e141/3" : {
+            "interfaces" : [
+                {
+                    "name" : "sw1-1",
+                    "ips"  : [ "40.0.0.1/24" ],
+                    "mac"  : "e2:f5:32:16:9a:46"
+                }
+            ]
+        }
+    },
+    "apps" : {
+        "org.onosproject.router" : {
+            "bgp" : {
+                "bgpSpeakers" : [
+                    {
+                        "name" : "speaker1",
+                        "connectPoint" : "of:00002a45d713e141/4",
+                        "peers" : [
+                            "150.1.3.1"
+                        ]
+                    }
+                ]
+            }
+        },
+        "org.onosproject.reactive.routing" : {
+            "reactiveRouting" : {
+                "ip4LocalPrefixes" : [
+                    {
+                        "ipPrefix" : "40.0.0.0/24",
+                        "type" : "PUBLIC",
+                        "gatewayIp" : "40.0.0.1"
+                    },
+                    {
+                        "ipPrefix" : "150.1.3.0/30",
+                        "type" : "PRIVATE",
+                        "gatewayIp" : "150.1.3.2"
+                    }
+                ],
+                "ip6LocalPrefixes" : [
+                ],
+                "virtualGatewayMacAddress" : "e2:f5:32:16:9a:46"
+            }
+        },
+        "org.onosproject.artemis" : {
+            "artemis" : {
+                "prefixes" : [ 
+                    {
+                        "prefix" : "40.0.0.0/8",
+                        "paths" : [ 
+                            {
+                                "origin" : 65004,
+                                "neighbor" : [
+                                    {
+                                        "asn" : 65002,
+                                        "neighbor": [
+                                            65001
+                                        ]
+                                    }
+                                ]
+                            }
+                        ],
+                        "moas" : [ ]
+                    }
+                ],
+                "frequency" : 3000,
+                "monitors" : {
+                    "ripe" : [ ],
+                    "exabgp": [ "192.168.1.2:5000" ]
+                }
+            }
+        }
+    }
+}
diff --git a/tools/tutorials/artemis/configs/zebra.conf b/tools/tutorials/artemis/configs/zebra.conf
new file mode 100644
index 0000000..eafc15e
--- /dev/null
+++ b/tools/tutorials/artemis/configs/zebra.conf
@@ -0,0 +1,5 @@
+! Configuration for zebra (NB: it is the same for all routers)
+!
+hostname zebra 
+password sdnip
+log stdout
\ No newline at end of file
diff --git a/tools/tutorials/artemis/requirements.txt b/tools/tutorials/artemis/requirements.txt
new file mode 100644
index 0000000..8cc6ce6
--- /dev/null
+++ b/tools/tutorials/artemis/requirements.txt
@@ -0,0 +1,3 @@
+python-socketio==1.7.6
+netaddr==0.7.19
+Flask==0.12.2
\ No newline at end of file
diff --git a/tools/tutorials/artemis/server.py b/tools/tutorials/artemis/server.py
new file mode 100755
index 0000000..a5947b9
--- /dev/null
+++ b/tools/tutorials/artemis/server.py
@@ -0,0 +1,80 @@
+#!/usr/bin/python3
+
+async_mode = 'threading'
+
+import time
+from flask import Flask, render_template, abort
+import socketio
+from sys import stdin, stdout, stderr
+import json
+import time
+from netaddr import IPNetwork, IPAddress
+
+sio = socketio.Server(logger=False, async_mode=async_mode)
+app = Flask(__name__)
+app.wsgi_app = socketio.Middleware(sio, app.wsgi_app)
+app.config['SECRET_KEY'] = 'secret!'
+thread = None
+clients = {}
+
+
+def message_parser(line):
+    try:
+        temp_message = json.loads(line)
+        if temp_message['type'] == 'update':
+            for origin in temp_message['neighbor']['message']['update']['announce']['ipv4 unicast']:
+                message = {
+                    'type': 'A',
+                    'timestamp': temp_message['time'],
+                    'peer': temp_message['neighbor']['ip'],
+                    'host': 'exabgp',
+                    'path': temp_message['neighbor']['message']['update']['attribute']['as-path'],
+                }
+                for prefix in temp_message['neighbor']['message']['update']['announce']['ipv4 unicast'][origin]:
+                    message['prefix'] = prefix
+                    for sid in clients.keys():
+                        try:
+                            if IPAddress(str(prefix).split('/')[0]) in clients[sid][0]:
+                                print('Sending exa_message to ' +
+                                      str(clients[sid][0]), file=stderr)
+                                sio.emit(
+                                    'exa_message', message, room=sid, namespace='/onos')
+                        except:
+                            print('Invalid format received from %s'.format(str(sid)))
+    except Exception as e:
+        print(str(e), file=stderr)
+
+
+def exabgp_update_event():
+    while True:
+        line = stdin.readline().strip()
+        messages = message_parser(line)
+
+
+@app.route('/')
+def index():
+    abort(404)
+
+
+@sio.on('connect', namespace='/onos')
+def onos_connect(sid, environ):
+    global thread
+    if thread is None:
+        thread = sio.start_background_task(exabgp_update_event)
+
+
+@sio.on('disconnect', namespace='/onos')
+def onos_disconnect(sid):
+    if sid in clients:
+        del clients[sid]
+
+
+@sio.on('exa_subscribe', namespace='/onos')
+def onos_exa_subscribe(sid, message):
+    try:
+        clients[sid] = [IPNetwork(message['prefix']), True]
+    except:
+        print('Invalid format received from %s'.format(str(sid)))
+
+if __name__ == '__main__':
+    app.run(host='0.0.0.0', threaded=True)
diff --git a/tools/tutorials/artemis/topo.py b/tools/tutorials/artemis/topo.py
new file mode 100755
index 0000000..a612611
--- /dev/null
+++ b/tools/tutorials/artemis/topo.py
@@ -0,0 +1,259 @@
+#!/usr/bin/python
+
+from mininet.topo import Topo
+from mininet.net import Mininet
+from mininet.cli import CLI
+from mininet.log import setLogLevel, info, debug
+from mininet.node import Host, RemoteController, OVSSwitch
+import os
+
+QUAGGA_DIR = '/usr/lib/quagga'
+# Must exist and be owned by quagga user (quagga:quagga by default on Ubuntu)
+QUAGGA_RUN_DIR = '/var/run/quagga'
+EXABGP_RUN_EXE = '~/exabgp/sbin/exabgp'
+CONFIG_DIR = 'configs/'
+
+onos = RemoteController('onos', ip='192.168.0.1', port=6633)
+
+
+class Onos(Host):
+
+    def __init__(self, name, intfDict, *args, **kwargs):
+        Host.__init__(self, name, *args, **kwargs)
+
+        self.intfDict = intfDict
+
+    def config(self, **kwargs):
+        Host.config(self, **kwargs)
+
+        for intf, attrs in self.intfDict.items():
+            self.cmd('ip addr flush dev %s' % intf)
+            if 'mac' in attrs:
+                self.cmd('ip link set %s down' % intf)
+                self.cmd('ip link set %s address %s' % (intf, attrs['mac']))
+                self.cmd('ip link set %s up ' % intf)
+            for addr in attrs['ipAddrs']:
+                self.cmd('ip addr add %s dev %s' % (addr, intf))
+
+
+class QuaggaRouter(Host):
+
+    def __init__(self, name, quaggaConfFile, zebraConfFile, intfDict, *args, **kwargs):
+        Host.__init__(self, name, *args, **kwargs)
+
+        self.quaggaConfFile = quaggaConfFile
+        self.zebraConfFile = zebraConfFile
+        self.intfDict = intfDict
+
+    def config(self, **kwargs):
+        Host.config(self, **kwargs)
+        self.cmd('sysctl net.ipv4.ip_forward=1')
+
+        for intf, attrs in self.intfDict.items():
+            self.cmd('ip addr flush dev %s' % intf)
+            if 'mac' in attrs:
+                self.cmd('ip link set %s down' % intf)
+                self.cmd('ip link set %s address %s' % (intf, attrs['mac']))
+                self.cmd('ip link set %s up ' % intf)
+            for addr in attrs['ipAddrs']:
+                self.cmd('ip addr add %s dev %s' % (addr, intf))
+
+        self.cmd('/usr/lib/quagga/zebra -d -f %s -z %s/zebra%s.api -i %s/zebra%s.pid' %
+                 (self.zebraConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name))
+        self.cmd('/usr/lib/quagga/bgpd -d -f %s -z %s/zebra%s.api -i %s/bgpd%s.pid' %
+                 (self.quaggaConfFile, QUAGGA_RUN_DIR, self.name, QUAGGA_RUN_DIR, self.name))
+
+    def terminate(self):
+        self.cmd("ps ax | egrep 'bgpd%s.pid|zebra%s.pid' | awk '{print $1}' | xargs kill" % (
+            self.name, self.name))
+
+        Host.terminate(self)
+
+
+class ExaBGPRouter(Host):
+
+    def __init__(self, name, exaBGPconf, intfDict, *args, **kwargs):
+        Host.__init__(self, name, *args, **kwargs)
+
+        self.exaBGPconf = exaBGPconf
+        self.intfDict = intfDict
+
+    def config(self, **kwargs):
+        Host.config(self, **kwargs)
+        self.cmd('sysctl net.ipv4.ip_forward=1')
+
+        for intf, attrs in self.intfDict.items():
+            self.cmd('ip addr flush dev %s' % intf)
+            if 'mac' in attrs:
+                self.cmd('ip link set %s down' % intf)
+                self.cmd('ip link set %s address %s' % (intf, attrs['mac']))
+                self.cmd('ip link set %s up ' % intf)
+            for addr in attrs['ipAddrs']:
+                self.cmd('ip addr add %s dev %s' % (addr, intf))
+
+        self.cmd('%s %s > /dev/null 2> exabgp.log &' % (EXABGP_RUN_EXE, self.exaBGPconf))
+
+    def terminate(self):
+        self.cmd(
+            "ps ax | egrep 'lib/exabgp/application/bgp.py' | awk '{print $1}' | xargs kill")
+        self.cmd(
+            "ps ax | egrep 'server.py' | awk '{print $1}' | xargs kill")
+        Host.terminate(self)
+
+
+class ONOSSwitch(OVSSwitch):
+
+    def start(self, controllers):
+        return OVSSwitch.start(self, [onos])
+
+
+class L2Switch(OVSSwitch):
+
+    def start(self, controllers):
+        return OVSSwitch.start(self, [])
+
+
+class ArtemisTopo(Topo):
+    "Artemis tutorial topology"
+
+    def build(self):
+        zebraConf = '%szebra.conf' % CONFIG_DIR
+
+        quaggaConf = '%sR1-quagga.conf' % CONFIG_DIR
+        name = 'R1'
+        eth0 = {
+            'ipAddrs': ['150.1.1.2/30']
+        }
+        eth1 = {
+            'ipAddrs': ['10.0.0.1/8']
+        }
+        eth2 = {
+            'ipAddrs': ['150.1.2.1/30']
+        }
+        intfs = {
+            '%s-eth0' % name: eth0,
+            '%s-eth1' % name: eth1,
+            '%s-eth2' % name: eth2
+        }
+        r1 = self.addHost(name, cls=QuaggaRouter, quaggaConfFile=quaggaConf,
+                          zebraConfFile=zebraConf, intfDict=intfs)
+
+        quaggaConf = '%sR2-quagga.conf' % CONFIG_DIR
+        name = 'R2'
+        eth0 = {
+            'ipAddrs': ['150.1.3.1/30']
+        }
+        eth1 = {
+            'ipAddrs': ['150.1.2.2/30']
+        }
+        intfs = {
+            '%s-eth0' % name: eth0,
+            '%s-eth1' % name: eth1
+        }
+        r2 = self.addHost(name, cls=QuaggaRouter, quaggaConfFile=quaggaConf,
+                          zebraConfFile=zebraConf, intfDict=intfs)
+
+        quaggaConf = '%sR3-quagga.conf' % CONFIG_DIR
+        name = 'R3'
+        eth0 = {
+            'ipAddrs': ['40.0.0.1/8']
+        }
+        eth1 = {
+            'ipAddrs': ['150.1.1.1/30']
+        }
+        intfs = {
+            '%s-eth0' % name: eth0,
+            '%s-eth1' % name: eth1
+        }
+        r3 = self.addHost(name, cls=QuaggaRouter, quaggaConfFile=quaggaConf,
+                          zebraConfFile=zebraConf, intfDict=intfs)
+
+        quaggaConf = '%sR4-quagga.conf' % CONFIG_DIR
+        name = 'R4'
+        eth0 = {
+            'ipAddrs': ['150.1.3.2/30'],
+            'mac': 'e2:f5:32:16:9a:46'
+        }
+        eth1 = {
+            'ipAddrs': ['10.10.10.1/24']
+        }
+        intfs = {
+            '%s-eth0' % name: eth0,
+            '%s-eth1' % name: eth1
+        }
+        r4 = self.addHost(name, cls=QuaggaRouter, quaggaConfFile=quaggaConf,
+                          zebraConfFile=zebraConf, intfDict=intfs)
+
+        ovs = self.addSwitch('ovs', dpid='00002a45d713e141', cls=ONOSSwitch)
+
+        l2_switch = self.addSwitch(
+            'l2_switch', dpid='0000000000000001', failMode='standalone', cls=L2Switch)
+
+        h1 = self.addHost('h1', ip='10.0.0.100/8', defaultRoute='via 10.0.0.1')
+        h4 = self.addHost('h4', ip='40.0.0.100/8', defaultRoute='via 40.0.0.1')
+
+        # Set up the internal BGP speaker
+
+        name = 'exabgp'
+        eth0 = {
+            'ipAddrs': ['10.0.0.3/8']
+        }
+        eth1 = {
+            'ipAddrs': ['192.168.1.2/24']
+        }
+        intfs = {
+            '%s-eth0' % name: eth0,
+            '%s-eth1' % name: eth1
+        }
+        exabgp = self.addHost(name, cls=ExaBGPRouter,
+                              exaBGPconf='%sexabgp.conf' % CONFIG_DIR,
+                              intfDict=intfs)
+
+        self.addLink(r1, r3, port1=0, port2=1)
+        self.addLink(r1, l2_switch, port1=1, port2=2)
+        self.addLink(r1, r2, port1=2, port2=1)
+
+        self.addLink(ovs, r2, port1=2, port2=0)
+        self.addLink(ovs, h4, port1=3, port2=0)
+        self.addLink(ovs, r4, port1=4, port2=0)
+
+        self.addLink(l2_switch, h1, port1=1, port2=0)
+        self.addLink(l2_switch, exabgp, port1=3, port2=0)
+
+        name = 'onos'
+        eth0 = {
+            'ipAddrs': ['192.168.0.1/24']
+        }
+        eth1 = {
+            'ipAddrs': ['10.10.10.2/24']
+        }
+        eth2 = {
+            'ipAddrs': ['192.168.1.1/24']
+        }
+        intfs = {
+            '%s-eth0' % name: eth0,
+            '%s-eth1' % name: eth1,
+            '%s-eth2' % name: eth2
+        }
+        onos = self.addHost(name, inNamespace=False, cls=Onos, intfDict=intfs)
+
+        self.addLink(onos, ovs, port1=0, port2=1)
+        self.addLink(onos, r4, port1=1, port2=1)
+        self.addLink(onos, exabgp, port1=2, port2=1)
+
+topos = {'artemis': ArtemisTopo}
+
+if __name__ == '__main__':
+    setLogLevel('debug')
+    topo = ArtemisTopo()
+
+    net = Mininet(topo=topo, build=False)
+    net.addController(onos)
+    net.build()
+    net.start()
+
+    CLI(net)
+
+    net.stop()
+
+    info("done\n")