<commit_before>#!/usr/bin/env python

from ravel.app import AppConsole
from ravel.log import logger

class RoutingConsole(AppConsole):
    def do_addflow(self, line):
        """Add a flow between two hosts, using Mininet hostnames
           Usage: addflow [host1] [host2]"""
        args = line.split()
        if len(args) != 2:
            print "Invalid syntax"
            return

        hostnames = self.env.provider.cache_name
        src,dst = args
        if src not in hostnames:
            print "Unknown host", src
            return

        if dst not in hostnames:
            print "Unknown host", dst
            return

        src = hostnames[src]
        dst = hostnames[dst]

        # TODO: will remove this when we delete uhosts
        # convert to host uhid
        # self.db.cursor.execute("SELECT u_hid FROM uhosts WHERE hid={0}"
        #                       .format(src))
        # src = int(self.db.cursor.fetchall()[0][0])
        # self.db.cursor.execute("SELECT u_hid FROM uhosts WHERE hid={0}"
        #                       .format(dst))
        # dst = int(self.db.cursor.fetchall()[0][0])

        try:
            # get next flow id
            # TODO: change to tm?
            self.db.cursor.execute("SELECT * FROM tm;")
            fid = len(self.db.cursor.fetchall()) + 1
            self.db.cursor.execute("INSERT INTO tm (fid, src, dst) "
                                   "VALUES ({0}, {1}, {2});"
                                   .format(fid, src, dst))
            self.db.cursor.execute ("UPDATE tm set FW = 0 where fid = {0};"
                                    .format(fid, src, dst))
        except Exception, e:
            print "Failure: flow not installed --", e
            return

        print "Success: installed flow with fid", fid

    def _delFlowByName(self, src, dst):
        hostnames = self.env.provider.cache_name

        if src not in hostnames:
            print "Unknown host", src
            return

        if dst not in hostnames:
            print "Unknown host", dst
            return

        src = hostnames[src]
        dst = hostnames[dst]

        # TODO: will remove this when we delete uhosts
        # convert to host uhid
        # self.db.cursor.execute("SELECT u_hid FROM uhosts WHERE hid={0}"
        #                        .format(src))
        # src = int(self.db.cursor.fetchall()[0][0])
        # self.db.cursor.execute("SELECT u_hid FROM uhosts WHERE hid={0}"
        #                        .format(dst))
        # dst = int(self.db.cursor.fetchall()[0][0])

        # TODO: change to tm?
        self.db.cursor.execute("SELECT fid FROM tm WHERE src={0} and dst={1};"
                               .format(src, dst))
        result = self.db.cursor.fetchall()

        if len(result) == 0:
            logger.warning("no flow installed for hosts {0},{1}".format(src, dst))
            return None

        fids = [res[0] for res in result]
        for fid in fids:
            self._delFlowById(fid)

        return fids

    def _delFlowById(self, fid):
        try:
            # does the flow exist?
            self.db.cursor.execute("SELECT fid FROM tm WHERE fid={0}".format(fid))
            if len(self.db.cursor.fetchall()) == 0:
                logger.warning("no flow installed with fid %s", fid)
                return None

            self.db.cursor.execute("DELETE FROM tm WHERE fid={0}".format(fid))<commit_msg>Update rt addflow with fw option<commit_after>#!/usr/bin/env python

from ravel.app import AppConsole
from ravel.log import logger

class RoutingConsole(AppConsole):
    def do_addflow(self, line):
        """Add a flow between two hosts, using Mininet hostnames
           Usage: addflow [host1] [host2] [opt: firewall waypoint (0 or 1)]"""
        args = line.split()
        if len(args) != 2 and len(args) != 3:
            print "Invalid syntax"
            return

        hostnames = self.env.provider.cache_name
        src = args[0]
        dst = args[1]
        if src not in hostnames:
            print "Unknown host", src
            return

        if dst not in hostnames:
            print "Unknown host", dst
            return

        fw = 0
        if len(args) == 3:
            fw = int(args[2])
            if fw not in [0,1]:
                print "Invalid firewall option:", fw

        src = hostnames[src]
        dst = hostnames[dst]
        try:
            # get next flow id
            self.db.cursor.execute("SELECT * FROM tm;")
            fid = len(self.db.cursor.fetchall()) + 1
            self.db.cursor.execute("INSERT INTO tm (fid, src, dst) "
                                   "VALUES ({0}, {1}, {2});"
                                   .format(fid, src, dst))
            self.db.cursor.execute("UPDATE tm set FW = {0} where fid = {1};"
                                   .format(fw, fid))
        except Exception, e:
            print "Failure: flow not installed --", e
            return

        print "Success: installed flow with fid", fid

    def _delFlowByName(self, src, dst):
        hostnames = self.env.provider.cache_name

        if src not in hostnames:
            print "Unknown host", src
            return

        if dst not in hostnames:
            print "Unknown host", dst
            return

        src = hostnames[src]
        dst = hostnames[dst]
        self.db.cursor.execute("SELECT fid FROM tm WHERE src={0} and dst={1};"
                               .format(src, dst))
        result = self.db.cursor.fetchall()

        if len(result) == 0:
            logger.warning("no flow installed for hosts {0},{1}".format(src, dst))
            return None

        fids = [res[0] for res in result]
        for fid in fids:
            self._delFlowById(fid)

        return fids

    def _delFlowById(self, fid):
        try:
            # does the flow exist?
            self.db.cursor.execute("SELECT fid FROM tm WHERE fid={0}".format(fid))
            if len(self.db.cursor.fetchall()) == 0:
                logger.warning("no flow installed with fid %s", fid)
                return None

            self.db.cursor.execute("DELETE FROM tm WHERE fid={0}".format(fid))
{'fraction_non_alphanumeric': 0.11042845594179466, 'fraction_numerical': 0.009377526273241715, 'mean_word_length': 1.567870485678705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2589086', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1749, 'n_words': 530}