Addendum 1: “Model” Code

The Model (app5.py) source code follows:

The Model is responsible for data I/O, including updating the black hole prefix database and executing system stdout writes that will be picked up by the parent ExaBGP process for prefix announcement. 

#!/usr/bin/python3
import os
import sys
from flask import Flask, render_template
from flask_bower import Bower
from flask import request
import json
from flask import jsonify
from pymongo import MongoClient, DESCENDING
from sys import stdout
from sys import stdin
from time import sleep
client = MongoClient()
db = client.portalDB
messages = []
prefix = “null”
neighbor = “null”
aspath = “null”
nexthop = “null”
localpref = “null”
opnotes = “null”
app = Flask(__name__)
@app.route(“/”)
def main():
    return render_template(‘sandbox5.html’)
 
@app.route(“/AnnouncePrefix”,methods=[‘POST’])
def AnnouncePrefix():
    try:
        json_data = request.json[‘info’]
        prefix = json_data[‘prefix’]
        neighbor = json_data[‘neighbor’]
        nexthop = json_data[‘nexthop’]
        aspath = json_data[‘aspath’]
        localpref = json_data[‘localpref’]
        opnotes = json_data[‘opnotes’]
        db.prefixinfo2.insert_one({
            ‘prefix’:prefix,’neighbor’:neighbor,’nexthop’:nexthop,’aspath’:aspath,’localpref’:localpref, ‘opnotes’:opnotes
            })
        route_adv = “announce route ” + ” ” + prefix + ” next-hop” + ” ” + nexthop
        stdout.write( route_adv + ‘\n’)
        stdout.flush()
        sleep(1)
        return jsonify(status=’OK’,message=’inserted successfully’,result=prefix)
 
    except Exception as e:
        return jsonify(status=’ERROR’,message=str(e))                                                                                                       
@app.route(“/RemovePrefix”,methods=[‘POST’])    
def RemovePrefix():
    try:
         json_data = request.json[‘info’]
         prefix = json_data[‘prefix’]
         db.prefixinfo2.delete_many({‘prefix’:prefix})  
#         return jsonify(status=’OK’,message=’deleted successfully’,result= DeleteResult)
#    except Exception,e:
#        return jsonify(status=’ERROR’,message=str(e)) 
         prefixcount = db.prefixinfo2.count({‘prefix’:prefix})
         route_withdraw = “withdraw route ” + ” ” + prefix 
         stdout.write( route_withdraw + ‘\n’)
         stdout.flush()
         sleep(.1)
         return jsonify(status=’OK’,message=’Prefixes Remaining’,result=prefixcount)
    except Exception as e:
        return jsonify(status=’ERROR’,message=str(e))
@app.route(“/SearchPrefix”,methods=[‘POST’])
def SearchPrefix():
    try:
        json_data = request.json[‘info’]
        prefix = json_data[‘prefix’]
        prefixcount = db.prefixinfo2.count({‘prefix’:prefix})
        return jsonify(status=’OK’,message=’found successfully’,result=prefixcount)
    except Exception as e:
        return jsonify(status=’ERROR’,message=str(e))
@app.route(“/repush”,methods=[‘POST’])
def repush():
      try:
         prefix_adv = 0
         message = ” “
         cursorPos= db.prefixinfo2.find()
         for record in cursorPos:
             next_entry = “announce route ” + ” ” + record[‘prefix’] + ” next-hop” + ” ” + record[‘nexthop’]
             stdout.write( next_entry + ‘\n’)
             prefix_adv  = prefix_adv+1    
             stdout.flush()
             sleep(.1)
         return jsonify(pusherr=’false’,pushed=”true”,message=message,result=prefix_adv)
      except Exception as e:
        return jsonify(pusherr=’true’,pushed=”true”,stat=’ERROR at some place in the prefix DB push:’,msg=str(e))    
 
   
if __name__ == “__main__”:
    app.run()