Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batfish agent #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ MANIFEST
.venv*/
.conda*/
.python-version

batfish-data/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.10.7

COPY / /tmp/alto
RUN rm -rf /tmp/alto/.git && \
pip install redis geoip2 && \
pip install redis geoip2 pybatfish && \
pip install /tmp/alto && \
rm -rf /tmp/alto

Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ required packages:
$ pip3 install .
$ pip3 install redis
$ gunicorn -b 0.0.0.0:8000 --reload --preload --capture-output --error-logfile /tmp/openalto-error.log --access-logfile /tmp/openalto-access.log alto.server.northbound.wsgi -D
$ python3 -m alto.agent.manage --pid /tmp start -c etc/batfish.json -D batfish
$ python3 -m alto.agent.manage --pid /tmp start -c etc/lg-agent.json -D cernlg
$ python3 -m alto.agent.manage --pid /tmp start -c etc/cric-agent.json -D cric
$ python3 -m alto.agent.manage --pid /tmp start -c etc/geoip-delegate-agent.json -D geoip
Expand Down
14 changes: 14 additions & 0 deletions docker-batfish/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM batfish/allinone:latest

COPY . /tmp/alto
COPY ./docker-batfish/start.sh start.sh
RUN chmod +x start.sh
RUN apt-get install -y python3 python3-pip
RUN rm -rf /tmp/alto/.git && \
pip install redis geoip2 pybatfish && \
pip install /tmp/alto && \
rm -rf /tmp/alto

EXPOSE 8000

ENTRYPOINT [ "./start.sh" ]
13 changes: 13 additions & 0 deletions docker-batfish/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Start batfish-agent
python3 -m alto.agent.manage --pid /tmp start -c /etc/batfish-agent.json -D batfish &

# Start batfish-server
./wrapper.sh &

# Wait for any process to exit
wait -n

# Exit with status of process that exited first
exit $?
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
version: '2.2'
services:
gateway:
image: nginx
Expand Down Expand Up @@ -31,6 +32,15 @@ services:
entrypoint: python
command: ["-m", "alto.agent.manage", "--pid", "/tmp", "start", "-c", "/etc/cric-agent.json", "-D", "cric"]
network_mode: "service:alto-frontend"
alto-batfish-server-agent:
build:
context: .
dockerfile: docker-batfish/Dockerfile
volumes:
- ./etc/batfish-data:/data
- ./etc/batfish-agent.json:/etc/batfish-agent.json
- ./etc/alto.conf:/opt/alto/etc/alto.conf
network_mode: "service:alto-frontend"
alto-db:
image: redis
network_mode: "service:alto-frontend"
Expand Down
5 changes: 5 additions & 0 deletions etc/batfish-agent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"namespace": "default",
"agent_class": "alto.agent.batfish.BatfishAgent",
"refresh_interval": 300
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the agent JSON file doesn't include any config of network control plane snapshot data.

1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ install_requires =
dataclasses; python_version<="3.6"
importlib-metadata; python_version<"3.8"
requests
pybatfish
pytricia
Django
djangorestframework
Expand Down
45 changes: 45 additions & 0 deletions src/alto/agent/batfish.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import logging
from pybatfish.client.session import Session
from pybatfish.datamodel import *
from pybatfish.datamodel.answer import *
from pybatfish.datamodel.flow import *
import time

from alto.server.components.datasource import DBInfo, DataSourceAgent
from alto.server.components.db import ForwardingRule, Match, Action

class BatfishAgent(DataSourceAgent):
"""
Class of data source agent for looking glass server.
"""

def __init__(self, dbinfo: DBInfo, name: str, namespace='default', **cfg):
super().__init__(dbinfo, name, namespace)

self.refresh_interval = cfg.get('refresh_interval', None)

logging.info("Loading databases")
self.db = [ self.request_db(t) for t in ['forwarding', 'endpoint']]

self.bf = Session(host="localhost")

self.bf.init_snapshot('/data/live', name='live', overwrite=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is /data/live a hardcoded path? Should it be configured by the agent JSON file?

self.bf.q.initIssues().answer()

def update(self):
fib_trans = self.db[0].new_transaction()
results = self.bf.q.routes().answer().frame()
logging.info("RESULTS*****************************************" + str(results))
for index in results.index:
pkt_match = Match(results["Network"][index])
action = Action(results["Next_Hop_IP"][index])
rule = ForwardingRule(pkt_match, action)
fib_trans.add_rule(results["Node"][index], rule)
fib_trans.commit()

def run(self):
if self.refresh_interval is None:
self.refresh_interval = 60
while True:
self.update()
time.sleep(self.refresh_interval)