Пример #1
0
    def on_block_received(self, piece_index, begin, data):
        '''Implements writing off pieces after download is finished
        
        Removes pieces from `self.pieces` \n
        Verifies Piece hash \n
        Sets `self.have_pieces[piece_index]` to True if hash verifies \n
        Else reinserts piece to `self.pieces`
        '''
        piece = self.piece[piece_index]
        piece.save_block(begin, data)

        if not piece.is_complete():
            return
        
        piece_data = piece.data
        res_hash = hashlib.sha1(piece_data).digest()
        exp_hash = self.torrent.get_piece_hash(piece_index)

        self.pieces_in_progress.pop(piece.index) # I added this
        
        if res_hash != exp_hash:
            # todo - re-enqueue request for this piece
            LOG.info('Hash check failed for piece {}'.format(piece_index))
            piece.flush()
            return
        else:
            import pdb; pdb.set_trace()
            LOG.info('Hash for piece {} is valid'.format(piece_index))
            
        self.received_pieces[piece.index] = piece # I added this.

        self.received_blocks.put_nowait((piece_index*self.piece_size, data))
Пример #2
0
 def get_file_path(self, outdir, torrent):
     name = torrent[b'info'][b'name'].decode()
     file_path = os.path.join(outdir, name)
     if os.path.exists(file_path):
         # TODO: add (num) to file name
         LOG.info('Previous download exists')
     return file_path
Пример #3
0
 def blocks():
     while True:
         piece = self.session.get_piece_request(self.have_pieces)
         LOG.info('[{}] generating blocks for piece {}'.format(
             self, piece))
         for block in piece.blocks:
             yield block
Пример #4
0
    def instr_change(track, ptr, ev):
        # (ev), ev2, value
        # ev=width, ev2=type

        if ev == 0xA4:
            get = ptr.u8
            LOG.info('instr_change u8')
        elif ev == 0xAC:
            get = ptr.u16
            LOG.info('instr_change u16')
        else:
            assert False

        ev2 = ptr.u8()
        value = get()

        if ev2 == 0x20:
            ev2 = 'bank'
        elif ev2 == 0x21:
            ev2 = 'patch'
        else:
            LOG.warning('[instr %s] Unknown type %02X value=%02X', b2h(ev), ev2, value)
            ev2 = 'unknown %s' % b2h(ev2)

        track.insert(InstrChange, ev2=ev2, value=value)
Пример #5
0
 def run(self):
     """
         run each of the manager in its given thread
     """
     for key in self.managers:
         LOG.info('starting SouthBoundExtended for %s' % key)
         tmp_th = threading.Thread(target=self.managers[key].run)
         tmp_th.start()
Пример #6
0
    async def start(self):
        while True:
            block = await self.received_blocks_queue.get()
            if not block:
                LOG.info('Received poison pill.Exiting')

            block_abs_location, block_data = block
            os.lseek(self.fd, block_abs_location, os.SEEK_SET)
            os.write(self.fd, block_data)
Пример #7
0
    async def request_a_piece(self, writer):
        if self.inflight_requests > 1:
            return
        blocks_generator = self.get_blocks_generator()
        block = next(blocks_generator)

        LOG.info('[{}] Request Block: {}'.format(self, block))
        msg = struct.pack('>IbIII', 13, 6, block.piece, block.begin,
                          block.length)
        writer.write(msg)
        self.inflight_requests += 1
        await writer.drain()
Пример #8
0
def xiaoyong():
    if flask.request.method == 'GET':
        return verify_request(flask.request, 'get request failed')
    elif flask.request.method == 'POST':
        if not verify_request(flask.request):
            return 'post request failed'

        flask.request.get_data()
        raw_xml = flask.request.data
        LOG.info(raw_xml)
        reqmsg = parse_xml_message(raw_xml)
        rspmsg = process_reqmsg(reqmsg)
        return rspmsg
Пример #9
0
 def _start(self):
     """
         for each of the controllers,
         read config CFG and build the
         corresponding SouthBoundExtended manager
     """
     for ctrl_name in self.controllers:
         # CFG.read(pathtoREScfg)
         CFG_p = '/tmp/%s.cfg' % ctrl_name
         # CFG.read(CFG_p)
         LOG.info('creating SouthBoundExtended for %s...' % ctrl_name)
         self.managers[ctrl_name] = SouthBoundExtended(cfg=CFG_p)
         time.sleep(10)
Пример #10
0
    def do_traceroute(self, line):
        args = line.split()
        if len(args) != 2:
            LOG.error('invalid number of args: traceroute source dest\n')
        else:
            if self.running:
                if args[0] in self.network and args[1] in self.network:
                    node1 = self.network[args[0]]
                    node2 = self.network[args[1]]
                    Route = node1.cmd('traceroute -n ' + node2.IP())

                    LOG.info(str(Route))

            else:
                LOG.error('Network must be running')
Пример #11
0
    def grace_full_shutdown(self):
        """
            Shutdown the App, by first closing
            the controller and then closing the Network
        """

        if self.controllerrunning:
            self.do_stop_controller("Stopping controller...")
            time.sleep(5)  # waiting for shapeshifter

        if self.running:
            self.do_stop_network('Stopping Network...')

        # shutdown the daemon thread
        self.shutdown()
        LOG.info('Stopping the App...')
Пример #12
0
 async def request_peers(self):
     async with aiohttp.ClientSession() as session:
         resp = await session.get(self.tracker_url,
                                  params=self._get_request_params())
         resp_data = await resp.read()
         LOG.info('Tracker response: {}'.format(resp))
         peers = None
         try:
             peers = bencoder.decode(resp_data)
         except AssertionError:
             LOG.error(
                 'Failed to decode Tracker response: {}'.format(resp_data))
             LOG.error('Tracker request URL: {}'.format(
                 str(resp.url).split('&')))
             raise RuntimeError('Failed to get Peers from Tracker')
         return peers
Пример #13
0
    def control_change(track, ptr, ev) -> ControlChange:
        # u8 cctype, [layout/4] value, [layout%4] duration

        #        duration
        #  val | 0    ?    u8   u16
        # -----+---- ---- ---- -----
        #  u8  | 94   95   96   97
        #  s8  | 98   99   9a   9b
        #  s16 | 9c   9d   9e   9f

        layout = ev - 0x94

        # READ CCTYPE
        cctype = ptr.u8()

        # READ VALUE
        values = [ptr.u8, ptr.s8, ptr.s16]
        value = values[layout // 4]()

        # READ LENGTH
        durations = [lambda: 0, None, ptr.u8, ptr.u16]
        duration = durations[layout % 4]

        if duration is not None:
            duration = duration()
        else:
            duration = None
            LOG.error('Unknown event 0x%s', b2h(ev))

        # TODO: missing CCTYPEs
        try:
            cctype = BMS2CC[cctype]
        except KeyError:
            LOG.info('[CC %s] Unknown control change %s = %s', b2h(ev), b2h(cctype), value)
            cctype = 'unknown %s' % b2h(cctype)

        # LOG.info('control change - %s', cctype)
        # # ', %02X, %02X', value, duration
        # LOG.info('    value %s', 'u8 s8 s16'.split()[layout // 4])
        # LOG.info('    duration %s', '0 ? u8 u16'.split()[layout % 4])


        track.insert(ControlChange,
                    cctype=cctype,
                    value=value,
                    length=duration
                    )
Пример #14
0
async def download(t_file : str, download_loc : str, loop=None):
    '''Entry point for client, initializes `Peers` and `DownloadSession` according to 
    configureation present in `t_file` which is the .torrent file and saves the 
    downloaded file to `download_loc` directory'''

    torrent = Torrent(t_file)
    LOG.info('Torrent : {}'.format(torrent))

    torrent_writer = FileSaver(download_loc, torrent) 
    session = DownloadSession(torrent, torrent_writer.get_received_blocks_queue())

    tracker = Tracker(torrent) # implement Tracker class
    peer_info = await tracker.get_peers()

    seen_peers = set()
    peers = [Peer(session, host, port) for host, port in peer_info]
    seen_peers.update([str(p) for p in peers])
    LOG.info('Peers : {}'.format(seen_peers))

    asyncio.gather([peer.download() for peer in peers])
Пример #15
0
    def parse_peers(self, peers: bytes):
        self_addr = socket.gethostbyname(socket.gethostname())
        LOG.info('Self address in {}'.format(self_addr))

        def handle_bytes(peers_data):
            peers = []
            for i in range(0, len(peers_data, 6)):
                addr_bytes, port_bytes = (peers_data[i:i + 4],
                                          peers_data[i + 4:i + 6])
                ip_addr = str(ipaddress.IPv4Address(addr_bytes))
                if ip_addr == self_addr:
                    print('Skipping ', ip_addr)
                    continue
                port_bytes = struct.unpack('H>', port_bytes)[0]
                peers.append((ip_addr, port_bytes))
            return peers

        def handle_dict(peers):
            raise NotImplementedError

        handlers = {bytes: handle_bytes, dict: handle_dict}
        return handlers[type(peers)](peers)
Пример #16
0
    async def _download(self):
        try:
            reader, writer = await asyncio.wait_for(asyncio.open_connection(
                self.host, self.port),
                                                    timeout=10)
        except ConnectionError:
            LOG.error('Failed to connect to Peer {}'.format(self))
            return

        LOG.info('{} Sending handshake'.format(self))
        writer.write(self.handshake())
        await writer.drain()

        # TODO: Validate handshake
        handshake = await reader.read(
            68)  # Suspends here if there's nothing to be read

        await self.send_interested(writer)

        # TODO: use async iterator
        buf = b''
        while True:
            resp = await reader.read(
                REQUEST_SIZE)  # Suspends here if there's nothing to be read
            # LOG.info('{} Read from peer: {}'.format(self, resp[:8]))

            buf += resp

            #LOG.info('Buffer len({}) is {}'.format(len(buf), buf[:8]))

            if not buf and not resp:
                return

            while True:
                if len(buf) < 4:
                    # LOG.info('Buffer is too short')
                    break

                length = struct.unpack('>I', buf[0:4])[0]

                if not len(buf) >= length:
                    break

                def consume(buf):
                    buf = buf[4 + length:]
                    return buf

                def get_data(buf):
                    return buf[:4 + length]

                if length == 0:
                    LOG.info('[Message] Keep Alive')
                    buf = consume(buf)
                    data = get_data(buf)
                    LOG.info('[DATA]', data)
                    continue

                if len(buf) < 5:
                    LOG.info('Buffer is less than 5... breaking')
                    break

                msg_id = struct.unpack('>b', buf[4:5])[0]  # 5th byte is the ID

                if msg_id == 0:
                    LOG.info('[Message] CHOKE')
                    data = get_data(buf)
                    buf = consume(buf)
                    LOG.info('[DATA]', data)

                elif msg_id == 1:
                    data = get_data(buf)
                    buf = consume(buf)
                    LOG.info('[Message] UNCHOKE')
                    self.peer_choke = False

                elif msg_id == 2:
                    data = get_data(buf)
                    buf = consume(buf)
                    LOG.info('[Message] Interested')
                    pass

                elif msg_id == 3:
                    data = get_data(buf)
                    buf = consume(buf)
                    LOG.info('[Message] Not Interested')
                    pass

                elif msg_id == 4:
                    buf = buf[5:]
                    data = get_data(buf)
                    buf = consume(buf)
                    LOG.info('[Message] Have')
                    pass

                elif msg_id == 5:
                    bitfield = buf[5:5 + length - 1]
                    self.have_pieces = bitstring.BitArray(bitfield)
                    LOG.info('[Message] Bitfield: {}'.format(bitfield))

                    # buf = buf[5 + length - 1:]
                    buf = buf[4 + length:]
                    await self.send_interested(writer)

                elif msg_id == 7:
                    self.inflight_requests -= 1
                    data = get_data(buf)
                    buf = consume(buf)

                    l = struct.unpack('>I', data[:4])[0]
                    try:
                        parts = struct.unpack('>IbII' + str(l - 9) + 's',
                                              data[:length + 4])
                        piece_idx, begin, data = parts[2], parts[3], parts[4]
                        self.torrent_session.on_block_received(
                            piece_idx, begin, data)
                        # LOG.info('Got piece idx {} begin {}'.format(piece, begin))
                    except struct.error:
                        LOG.info('error decoding piece')
                        return None

                    # piece_index = buf[5]
                    # piece_begin = buf[6]
                    # block = buf[13: 13 + length]
                    # # buf = buf[13 + length:]
                    # buf = buf[4 + length:]
                    # LOG.info('Buffer is reduced to {}'.format(buf))
                    # LOG.info('Got piece idx {} begin {}'.format(piece_index, piece_begin))
                    # LOG.info('Block has len {}'.format(len(block)))
                    # LOG.info('Got this piece: {}'.format(block))

                    # TODO: delegate to torrent session
                    # with open(self.torrent_session.torrent.info[b'info'][b'name'].decode(), 'wb') as f:
                    #     f.write(block)
                    # continue
                else:
                    LOG.info('unknown ID {}'.format(msg_id))
                    if msg_id == 159:
                        exit(1)

                await self.request_a_piece(writer)
Пример #17
0
    async def _download(self):
        try:
            reader, writer = await asyncio.wait_for(asyncio.open_connection(
                self.host, self.port),
                                                    timeout=10)
        except TimeoutError:
            LOG.error('Failed to connect to peer {}'.format(self.host))
            return

        LOG.info('{} sending handshake'.format(self))
        writer.write(self.handshake())
        await writer.drain()

        hndshake = await reader.read(68)

        await self.interested(writer)

        buf = b''
        while True:
            resp = await reader.read(REQUEST_SIZE)

            buf += resp

            if not buf and not resp:
                return

            while True:

                if len(buf) < 4:
                    break

                length = struct.unpack('>I', buf[0:4])[0]

                if not len(buf) >= length:
                    break

                def consume(buf):
                    buf = buf[4 + length:]
                    return buf

                def get_data(buf):
                    return buf[:4 + length]

                if length == 0:
                    LOG.info('[Message] keep alive')
                    buf = consume(buf)
                    data = get_data(buf)
                    LOG.info('[Data]', data)
                    continue

                if len(buf) < 5:
                    LOG.info('Buffer is less than 5 bytes...breaking')
                    break

                msg_id = struct.unpack('>b', buf[4:5])[0]

                if msg_id == 0:
                    LOG.info('[Message] CHOKE')
                    data = get_data(buf)
                    buf = consume(buf)
                    LOG.info('[DATA] {}'.format(data))
                elif msg_id == 1:
                    LOG.info('[Message] UNCHOKE')
                    data = get_data(buf)
                    buf = consume(buf)
                    self.peer_choke = False
                elif msg_id == 2:
                    LOG.info('[Message] INTERESTED')
                    data = get_data(buf)
                    buf = consume(buf)
                elif msg_id == 3:
                    LOG.info('[Message] UNINTERESTED')
                    data = get_data(buf)
                    buf = consume(buf)
                elif msg_id == 4:
                    buf = buf[5:]
                    data = get_data(buf)
                    buf = consume(buf)
                    LOG.info('[Message] HAVE')

                elif msg_id == 5:
                    bitfield = buf[5:4 + length]
                    self.have_pieces = bitstring.BitArray(bitfield)
                    LOG.info('[Message] Bitfield {}'.format(bitfield))

                    buf = buf[4 + length:]
                    await self.interested(writer)
                elif msg_id == 7:
                    self.inflight_requests -= 1
                    data = get_data(buf)
                    buf = consume(buf)

                    l = struct.unpack('>I', data[:4])[0]
                    try:
                        parts = struct.unpack('>IbII' + str(l - 9) + 's',
                                              data[:length + 4])
                        piece_idx, begin, data = parts[2], parts[3], parts[4]
                        self.session.on_block_received(piece_idx, begin, data)
                    except struct.error:
                        LOG.error("Error decoding piece")
                        return None
                else:
                    LOG.info('Unknown message id {}'.format(msg_id))
                    if msg_id == 159:
                        exit(1)

                await self.request_a_piece(writer)
def check_network(network, Error):
    """
        return True if and Error has been detected
        otherwise return Error
    """

    controller = network.get('controller')
    if not controller:
        LOG.error('A controller must be specified')
        Error = True
    if not network.get('destinations'):
        LOG.warning(
            'Destinations should be configured to specify requirements')

    ips = {}
    routerids = {}
    routerospf = []
    hosts = []
    for router in network.get('routers'):
        if router.get('ospf6').get('enable') and\
        not router.get('ospf6').get('router-id') :
            LOG.error('If ospf is enable router %s must have a router-id ' %
                      router.get('name'))
            Error = True

        if not router.get('ospf6').get('enable'):
            hosts.append(router.get('name'))

        if router.get('ospf6').get('enable') and\
        routerids.get(router.get('ospf6').get('router-id') ) :
            LOG.error('routerid should be unique %s' %
                      router.get('ospf6').get('router-id'))
            LOG.info('already found here %s ' %
                     routerids.get(router.get('ospf6').get('router-id')))
            Error = True
        else:
            routerids[router.get('ospf6').get('router-id')] = router.get(
                'name')
            routerospf.append(router.get('name'))

        if not router.get('loopback-addr'):
            LOG.error('router %s have a loopback-addr' % router.get('name'))
            Error = True
        else:
            if ips.get(router.get('loopback-addr')):
                LOG.error('loopback-addr must be unique %s' %
                          router.get('loopback-addr'))
                LOG.info('already found here %s ' %
                         ips.get(router.get('loopback-addr')))
                Error = True
            else:
                ips[router.get('loopback-addr')] = router.get('name')

    ospfconfig = {}
    host_link = {}
    ospf_link = []
    link_name_map = {}
    for link in network.get('link'):
        link_name_map[link.get('name')] = link

    for link in network.get('ospf-links'):
        l = link_name_map[link.get('name')]
        ospf_link.append(link.get('name'))

        for intf in [link.get('src'), link.get('dest')]:
            area = intf.get('ospf').get('area')
            if not ospfconfig or ospfconfig.get(area):

                ospfconfig[area] = {
                    'area': intf.get('ospf').get('area'),
                    'hello-interval': intf.get('ospf').get('hello-interval'),
                    'dead-interval': intf.get('ospf').get('dead-interval'),
                }
            else:
                if ospfconfig.get(area).get('hello-interval') != intf.get(
                        'ospf').get('hello-interval'):
                    LOG.error(
                        'hello-interval should be the same for the same area %s'
                        % area)
                    Error = True
                if ospfconfig.get(area).get('hello-interval') != intf.get(
                        'ospf').get('hello-interval'):
                    LOG.error(
                        'hello-interval should be the same for the same area %s'
                        % area)
                    Error = True
            if l.get('src').get('name') not in routerospf:
                LOG.error(
                    'To enable ospf on a link both router must have ospf enable %s'
                    % l.get('src').get('name'))
                Error = True
            if l.get('dest').get('name') not in routerospf:
                LOG.error(
                    'To enable ospf on a link both router must have ospf enable %s'
                    % l.get('dest').get('name'))
                Error = True

    for link in network.get('link'):
        src = link.get('src')
        dest = link.get('dest')
        if not src.get('name'):
            LOG.error('Link must have a src ')
            Error = True
            continue
        if not dest.get('name'):
            LOG.error('Link must have a dest ')
            Error = True
            continue
        if not src.get('ip'):
            LOG.error('Link must have an ip address')
            Error = True
            continue
        if not dest.get('ip'):
            LOG.error('Link must have an ip address')
            Error = True
            continue

        if ips.get(src.get('ip')):
            LOG.error('ip address should be different %s than loopback-addr' %
                      src.get('ip'))
            LOG.info('already found %s' % ips.get(src.get('ip')))
            Error = True

        if ips.get(dest.get('ip')):
            LOG.error('ip address should be different %s' % dest.get('ip'))
            LOG.info('already found %s' % ips.get(dest.get('ip')))
            Error = True

        if link.get('name') not in ospf_link:
            # checking static link
            if src.get('name') in hosts and src.get('name') in host_link:
                LOG.error(
                    'A router without ospf can only have one static link')
                Error = True

            else:
                if src.get('name') in hosts and dest.get('name') in hosts:
                    LOG.error(
                        'Cannot have a static link between two router without ospf'
                    )
                    Error = True
                else:
                    host_link[src.get('name')] = dest.get('name')

            if dest.get('name') in hosts and dest.get('name') in host_link:
                LOG.error(
                    'A router without ospf can only have one static link')
                Error = True
            else:
                if src.get('name') in hosts and dest.get('name') in hosts:
                    LOG.error(
                        'Cannot have a static link between two router without ospf'
                    )
                    Error = True
                else:
                    host_link[dest.get('name')] = src.get('name')

    return Error
        },
    ]

    prompt = SimulationCLI(BW, TIME, checker=Check, autoBW=False)
    prompt.netconf_th('%s/data/netconf-topo.xml' % path)
    # time.sleep(5)
    prompt.do_start_network('Starting topo')
    prompt.netconf_th('%s/data/requirement-0.xml' % path)
    # time.sleep(5)
    prompt.do_apply_new_requirement('Apply Requirement')
    time.sleep(5)
    prompt.do_show_requirements('display')
    prompt.do_enable_auto_schedule('Start checking')
    time.sleep(30)  # wait for the check is done
    prompt.do_halt_auto_schedule('stop checking')
    prompt.do_show_requirements('display')
    LOG.info('No requirement should be running')
    time.sleep(5)
    prompt.do_next_timestamp('11:00')
    prompt.do_show_requirements('display')
    time.sleep(5)
    prompt.do_enable_auto_schedule('Start checking')
    time.sleep(30)  # wait for the check is done
    prompt.do_halt_auto_schedule('stop checking')
    prompt.do_show_requirements('display')
    LOG.info('bw requirement from A to B should not be running')

    prompt.start()

    prompt.netconf_th('%s/data/restore.xml' % path)
Пример #20
0
    ]

    prompt = SimulationCLI(BW,TIME, checker=Check)
    prompt.netconf_th('%s/data/netconf-topo.xml' % path)
    # time.sleep(5)
    prompt.do_start_network('Starting topo')
    prompt.netconf_th('%s/data/requirement-0.xml' % path)
    # time.sleep(5)
    prompt.do_apply_new_requirement('Apply Requirement')
    time.sleep(5)
    prompt.do_show_requirements('display')
    prompt.do_enable_auto_schedule('Start checking')
    time.sleep(30) # wait for the check is done
    prompt.do_halt_auto_schedule('stop checking')
    prompt.do_show_requirements('display')
    LOG.info('No requirement should be running')
    time.sleep(5)
    prompt.do_next_timestamp('11:00')
    prompt.do_show_requirements('display')
    time.sleep(5)
    prompt.do_enable_auto_schedule('Start checking')
    time.sleep(30) # wait for the check is done
    prompt.do_halt_auto_schedule('stop checking')
    prompt.do_show_requirements('display')
    LOG.info('bw requirement from A to B should not be running')
    prompt.do_next_timestamp('11:30 BW overload')
    LOG.info('11:30 BW overload')
    time.sleep(15)
    prompt.do_ping6('A B') # should  ping (testing if SNMP server can still be reached)
    time.sleep(5)
    prompt.do_enable_auto_schedule('Start checking')
    # time.sleep(5)
    prompt.do_apply_new_requirement('Apply Requirement')
    time.sleep(5)
    prompt.do_show_requirements('display')
    prompt.do_enable_auto_schedule('Start checking')
    time.sleep(30)  # wait for the check is done
    prompt.do_halt_auto_schedule('stop checking')
    time.sleep(5)
    prompt.do_next_timestamp('11:00')
    prompt.do_show_requirements('display')
    time.sleep(5)
    prompt.do_enable_auto_schedule('Start checking')
    time.sleep(30)  # wait for the check is done
    prompt.do_halt_auto_schedule('stop checking')
    prompt.do_show_requirements('display')
    LOG.info('back up requirement from A to B should not be running')
    prompt.do_link('A B down')
    LOG.info('Set link A B down')
    time.sleep(15)
    prompt.do_ping6(
        'A B')  # should  ping (testing if SNMP server can still be reached)
    time.sleep(5)
    prompt.do_enable_auto_schedule('Start checking')
    time.sleep(40)  # wait for the check is done
    prompt.do_halt_auto_schedule('stop checking')
    prompt.do_show_requirements('display')
    LOG.info('back up requirement from A to B should be running')

    prompt.do_link('A B up')
    LOG.info('Set link A B up')
    time.sleep(15)
def complete_path(data, Requirement, destRouter):
    """
        @API
        function called by the NMCore to
        perform path expansion of :Requirement

        :data is the network config from ConfD Agent
    """

    routers = []
    for r in data.get('routers'):
        if r.get('ospf6').get('enable'):
            routers.append(r.get('name'))
    ospf_link = []
    for link in data.get('ospf-links'):
        ospf_link.append(link.get('name'))
    edges = []
    for link in data.get('link'):
        if link.get('name') in ospf_link:
            src = link.get('src')
            dest = link.get('dest')
            if link.get('bidirectional'):
                edges.append((src.get('name'), dest.get('name'),
                              int(link.get('cost')), 'bidirectional'))
                edges.append((dest.get('name'), src.get('name'),
                              int(link.get('cost')), 'bidirectional'))

            else:
                edges.append((src.get('name'), dest.get('name'),
                              int(src.get('cost')), 'unidirectional'))
                edges.append((dest.get('name'), src.get('name'),
                              int(dest.get('cost')), 'unidirectional'))

    for i in range(len(Requirement) - 1):
        # remove edges already in the Requirement
        if Requirement[i] != '*' and Requirement[i + 1] != '*':
            edge = find_edge(Requirement[i], Requirement[i + 1], edges)
            index = edges.index(edge)
            del edges[index]
            edge = find_edge(Requirement[i + 1], Requirement[i], edges)
            index = edges.index(edge)
            del edges[index]

    # build graph
    graph = Graph()
    for router in routers:
        graph.add_node(router)

    for edge in edges:
        if edge[3] == 'unidirectional':
            graph.add_edge_unidirectional(edge[0], edge[1], edge[2])
        else:
            graph.add_edge(edge[0], edge[1], edge[2])

    try:
        Simple_path_req = get_simple_path_req(Requirement, destRouter, graph)
    except KeyError:
        LOG.critical('Could not solve path expansion for ' + str(Requirement))
        return False

    LOG.info('Success path expansion...')
    LOG.info('Path : ' + str(Simple_path_req) + '  for :' + str(destRouter))
    return Simple_path_req
Пример #23
0
    def build(self, *args, **kwargs):

        if not self.config:
            LOG.error(' not received config')
        else:
            LOG.info(' SUCCESS received config')
            conf = self.config

        fibbing_ctrl = conf.get('fibbing-controller')
        ospf_links = conf.get('link-ospf')
        hosts_link = conf.get('hosts-link')
        ctrl_links = fibbing_ctrl.get('links')
        ospf_routers = conf.get('routers')
        # get private network
        private_net = fibbing_ctrl.get('controller-config').get(
            'private-ip-prefix')
        base_net = fibbing_ctrl.get('controller-config').get('base-net-perfix')

        # mapping router name, iprouter obj
        routers = {}

        # configure ospf routers
        for o_r in ospf_routers:
            name = o_r.get('name')
            routers[name] = self.addRouter(
                name,
                routerid=str(o_r.get('ospf').get('router-id')),
                private_net=private_net)

        # configure ospf links
        for ol in ospf_links:
            src = ol.get('src')
            dest = ol.get('dest')
            src_router = routers.get(src.get('name'))
            dest_router = routers.get(dest.get('name'))
            src_intfName = src.get('intf')
            dest_intfName = dest.get('intf')

            src_cost = int(ol.get('cost'))
            dest_cost = int(ol.get('cost'))
            if not ol.get('bidirectional'):
                src_cost = int(ol.get('src').get('cost'))
                dest_cost = int(ol.get('dest').get('cost'))

            src_params = {
                'cost':
                src_cost,
                'bw':
                int(ol.get('bw')),
                'ip':
                src.get('ip'),
                PRIVATE_IP_KEY:
                src.get('private-ip'),
                'prefixLen':
                int(src.get('ip').split('/')[1]),
                'area':
                src.get('ospf').get('area'),
                'hello-interval':
                src.get('ospf').get('hello-interval'),
                'dead-interval':
                src.get('ospf').get('dead-interval'),
                'secondary-ips': [],
                'min_ls_arrival':
                int(src.get('ospf').get('lsa').get('min_ls_arrival')),
                'min_ls_interval':
                int(src.get('ospf').get('lsa').get('min_ls_interval')),
                'odelay':
                int(src.get('ospf').get('throttle').get('delay')),
                'initial_holdtime':
                int(src.get('ospf').get('throttle').get('initial_holdtime')),
                'max_holdtime':
                int(src.get('ospf').get('throttle').get('max_holdtime'))
            }

            dest_params = {
                'cost':
                dest_cost,
                'bw':
                int(ol.get('bw')),
                'ip':
                dest.get('ip'),
                PRIVATE_IP_KEY:
                dest.get('private-ip'),
                'prefixLen':
                int(dest.get('ip').split('/')[1]),
                'area':
                dest.get('ospf').get('area'),
                'hello-interval':
                dest.get('ospf').get('hello-interval'),
                'dead-interval':
                str(dest.get('ospf').get('dead-interval')),
                'secondary-ips': [],
                'min_ls_arrival':
                int(dest.get('ospf').get('lsa').get('min_ls_arrival')),
                'min_ls_interval':
                int(dest.get('ospf').get('lsa').get('min_ls_interval')),
                'odelay':
                int(dest.get('ospf').get('throttle').get('delay')),
                'initial_holdtime':
                int(dest.get('ospf').get('throttle').get('initial_holdtime')),
                'max_holdtime':
                int(dest.get('ospf').get('throttle').get('max_holdtime'))
            }

            self.addLink(routers[src.get('name')],
                         routers[dest.get('name')],
                         intfName1=src_intfName,
                         intfName2=dest_intfName,
                         params1=src_params,
                         params2=dest_params)

        # configure destinations link
        for d in hosts_link:
            host = d.get('host')
            router = d.get('router')
            routerip = router.get('ip')
            host_obj = self.addHost(host.get('name'),
                                    ip=str(host.get('ip')),
                                    defaultRoute='via ' +
                                    str(routerip.split('/')[0]))
            #
            host_intfname = host.get('intf')
            router_intfname = router.get('intf')
            host_params = {
                'ip': str(host.get('ip')),
                'cost': 1000,
                'bw': int(d.get('bw')),
                'prefixLen': int(host.get('ip').split('/')[1])
            }

            router_params = {
                'ip': str(router.get('ip')),
                'cost': 1000,
                'bw': int(d.get('bw')),
                'prefixLen': int(router.get('ip').split('/')[1]),
                'Host': True
            }

            self.addLink(host_obj,
                         routers[router.get('name')],
                         intfName1=host_intfname,
                         intfName2=router_intfname,
                         params1=host_params,
                         params2=router_params)

        # Get controller configurations
        controller_o_conf = fibbing_ctrl.get('controller-config').get('ospf')
        th_initial_holdtime = controller_o_conf.get('throttle').get(
            'initial_holdtime')
        th_delay = controller_o_conf.get('throttle').get('delay')
        th_max_holdtime = controller_o_conf.get('throttle').get('max_holdtime')
        min_ls_arrival = controller_o_conf.get('lsa').get('min_ls_arrival')
        min_ls_interval = controller_o_conf.get('lsa').get('min_ls_interval')
        hello = controller_o_conf.get('hello-interval')
        dead = controller_o_conf.get('dead-interval')
        area = controller_o_conf.get('area')

        defaults = {
            CFG_KEY: {
                'base_net': base_net,
                'controller_prefixlen': 24,
                'debug': int(_lib.DEBUG_FLAG),
                'private_net': private_net,
                'draw_graph': 0,
                'private_ips': 'private_ip_binding.json',
                'area': str(area),
                'hello_interval': str(hello),
                'dead_interval': str(dead),
                'min_ls_arrival': int(min_ls_arrival),
                'min_ls_interval': int(min_ls_interval),
                'delay': int(th_delay),
                'initial_holdtime': int(th_initial_holdtime),
                'max_holdtime': int(th_max_holdtime)
            }
        }

        ctrl_map = {}
        for c_l in ctrl_links:
            router = c_l.get('router')
            controller = c_l.get('controller')
            if not ctrl_map.get(controller.get('name')):
                # configure the fibbing controllers
                CFG_path = '/tmp/%s.cfg' % controller.get('name')
                ctrl = self.addController(controller.get('name'),
                                          cfg_path=None,
                                          **defaults)
                ctrl_map[controller.get('name')] = ctrl
            ctrl_ip = controller.get('ip')
            rc = routers[router.get('name')]
            ctrl_intfname = controller.get('intf')
            rc_intfname = router.get('intf')
            ctrl_params = {'bw': int(c_l.get('bw')), 'ip': str(ctrl_ip)}

            rc_params = {
                'bw': int(c_l.get('bw')),
                'ip': str(router.get('ip')),
                'area': str(area),
                'hello-interval': str(hello),
                'dead-interval': str(dead),
                'min_ls_arrival': int(min_ls_arrival),
                'min_ls_interval': int(min_ls_interval),
                'delay': int(th_delay),
                'initial_holdtime': int(th_initial_holdtime),
                'max_holdtime': int(th_max_holdtime)
            }

            # default cost=100000
            ctrl = ctrl_map[controller.get('name')]
            self.addLink(ctrl,
                         rc,
                         cost=10000,
                         intfName1=ctrl_intfname,
                         intfName2=rc_intfname,
                         params1=ctrl_params,
                         params2=rc_params)
Пример #24
0
 def tcpdump_router_th(self, router):
     Router = self.network[router]
     pcapFile = 'tcpdump_%s.pcap' % router
     Router.cmd('tcpdump -c %d -w %s' % (1000, pcapFile))
     LOG.info('tcpdump finish ')
Пример #25
0
        },
        {
            "links": [
                {
                    "from": "A",
                    "to": "B",
                    "bw": False
                },
            ]
        },
    ]

    #
    prompt = SimulationCLI(BW, TIME, checker=Check)

    LOG.info('sending topo config')
    prompt.netconf_th('%s/data/netconf-topo.xml' % path)
    # Starting the network
    prompt.do_start_network('Test')
    LOG.info('sending requirement-0 config')
    prompt.netconf_th('%s/data/requirement-0.xml' % path)
    # starting the controller with some requirements
    prompt.do_start_controller('Starting controller')
    time.sleep(5)

    # open manual CLI
    prompt.start()

    # # stopping the simulation
    # restoring the random restart
    prompt.netconf_th('%s/data/restore.xml' % path)