Skip to content

Instantly share code, notes, and snippets.

@fno2010
Last active November 1, 2017 17:55
Show Gist options
  • Save fno2010/e3c61ded2510f3a0866595f587369a2f to your computer and use it in GitHub Desktop.
Save fno2010/e3c61ded2510f3a0866595f587369a2f to your computer and use it in GitHub Desktop.
Quickly remove all filtered flows: ./check-flow.sh --filter <flow_name_head> | ./remove-flow.sh
#!/bin/bash
FILTER=false
while true; do
case "$1" in
--filter)
FILTER=true
FILTER_HEAD=$2
shift 2
;;
*)
CONTROLLER=${1:-localhost}
shift
break
;;
esac
done
if [[ "$FILTER" == "false" ]]; then
curl -u admin:admin -H 'Content-Type: application/json' http://$CONTROLLER:8181/restconf/operational/opendaylight-inventory:nodes | jq '.nodes | .node | .[] | ."flow-node-inventory:table" | .[] | .flow' | ack '"id"'
else
if [[ -n $FILTER_HEAD ]]; then
curl -u admin:admin -H 'Content-Type: application/json' http://$CONTROLLER:8181/restconf/operational/opendaylight-inventory:nodes | ./uf-filter.py -H $FILTER_HEAD
else
curl -u admin:admin -H 'Content-Type: application/json' http://$CONTROLLER:8181/restconf/operational/opendaylight-inventory:nodes | ./uf-filter.py
fi
fi
#!/bin/bash
echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<input xmlns="urn:opendaylight:flow:service">
<barrier>false</barrier>
<node xmlns:inv="urn:opendaylight:inventory">/inv:nodes/inv:node[inv:id="'$1'"]</node>
<cookie>'$3'</cookie>
<table_id>'$2'</table_id>
</input>'
#!/bin/bash
CONTROLLER=${1:-localhost}
while read line
do
echo "Removing flows about $line"
curl -v -u admin:admin -X POST -d "$(./generate-input.sh $line)" -H 'Content-Type: application/xml' http://$CONTROLLER:8181/restconf/operations/sal-flow:remove-flow
done < "${2:-/dev/stdin}"
#!/usr/bin/env python
import sys
import json
def filtered_flow(nodes, head='#UF'):
for n in nodes:
node_id = n['id']
for t in n.get('flow-node-inventory:table', []):
table_id = t['id']
cookies = []
for f in t.get('flow', []):
if f['id'].startswith(head) and f['cookie'] not in cookies:
cookies.append(f['cookie'])
for cookie in cookies:
sys.stdout.write("%s %d %s\n" % (node_id, table_id, cookie))
def parse_argument():
import argparse
parser = argparse.ArgumentParser(description='ODL Flow Filter.')
parser.add_argument('-H', '--head', dest='head',
type=str, default='',
help='Filter the flow whose name starts with <head>.')
parser.add_argument('data', nargs='?')
return parser.parse_args(sys.argv[1:])
if '__main__' == __name__:
args = parse_argument()
if args.data:
raw_data = json.load(open(args.data))
else:
raw_data = json.load(sys.stdin)
filtered_flow(raw_data['nodes']['node'], head=args.head)
@fno2010
Copy link
Author

fno2010 commented Jun 26, 2017

Download all the scripts into the same directory and run the following command on the controller host:

# Remove all flows
./check-flow.sh --filter '' | ./remove-flow.sh

# Remove all flows which name starting with '#UF' (which should be undefined flows)
./check-flow.sh --filter '#UF' | ./remove-flow.sh

If scripts are not on the controller host, you should pass the IP address of the controller host from the command-line argument:

CONTROLLER_IP=10.0.0.100 # For example
./check-flow.sh --filter '#UF' $CONTROLLER_IP | ./remove-flow.sh $CONTROLLER_IP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment