Carmelo Cascone | b34d8e1 | 2020-09-28 16:16:59 -0700 | [diff] [blame^] | 1 | #! /usr/bin/env python3 |
Ray Milkey | 43ee97f | 2015-09-04 15:14:40 -0700 | [diff] [blame] | 2 | import requests |
| 3 | |
| 4 | from requests.auth import HTTPBasicAuth |
| 5 | import sys |
| 6 | |
| 7 | |
| 8 | |
| 9 | if len(sys.argv) != 6: |
Carmelo Cascone | b34d8e1 | 2020-09-28 16:16:59 -0700 | [diff] [blame^] | 10 | print("usage: create-flow onos-node name device in-port out-port") |
Ray Milkey | 43ee97f | 2015-09-04 15:14:40 -0700 | [diff] [blame] | 11 | sys.exit(1) |
| 12 | |
| 13 | node = sys.argv[1] |
| 14 | name = sys.argv[2] |
| 15 | device = sys.argv[3] |
| 16 | inPort = sys.argv[4] |
| 17 | outPort = sys.argv[5] |
| 18 | |
| 19 | flowJsonTemplate = \ |
| 20 | '{{' + \ |
| 21 | '"priority": 1,' + \ |
| 22 | '"isPermanent": true,' + \ |
| 23 | '"treatment": {{' + \ |
| 24 | '"instructions": [' + \ |
| 25 | '{{' + \ |
| 26 | '"type": "OUTPUT",' + \ |
| 27 | '"port": {}' + \ |
| 28 | '}}' + \ |
| 29 | ']' + \ |
| 30 | '}},' + \ |
| 31 | '"selector": {{' + \ |
| 32 | '"criteria": [' + \ |
| 33 | '{{' + \ |
| 34 | '"type": "IN_PORT",' + \ |
| 35 | '"port": {}' + \ |
| 36 | '}}' + \ |
| 37 | ']' + \ |
| 38 | '}}' + \ |
| 39 | '}}' |
| 40 | |
| 41 | flowJson = flowJsonTemplate.format(inPort, outPort) |
Ray Milkey | ead5c91 | 2016-11-04 09:45:03 +0100 | [diff] [blame] | 42 | payload = {'appId': 'org.onosproject.cli'} |
| 43 | flowRequest = requests.post('http://' + node + ':8181/onos/v1/flows/' + device, |
Ray Milkey | 43ee97f | 2015-09-04 15:14:40 -0700 | [diff] [blame] | 44 | auth=HTTPBasicAuth('onos', 'rocks'), |
Ray Milkey | ead5c91 | 2016-11-04 09:45:03 +0100 | [diff] [blame] | 45 | data=flowJson, |
| 46 | params=payload) |
Ray Milkey | 43ee97f | 2015-09-04 15:14:40 -0700 | [diff] [blame] | 47 | |
Ray Milkey | ead5c91 | 2016-11-04 09:45:03 +0100 | [diff] [blame] | 48 | if flowRequest.status_code != 201: |
Carmelo Cascone | b34d8e1 | 2020-09-28 16:16:59 -0700 | [diff] [blame^] | 49 | print(flowRequest.text) |
Ray Milkey | 43ee97f | 2015-09-04 15:14:40 -0700 | [diff] [blame] | 50 | sys.exit(1) |
| 51 | |
Ray Milkey | ead5c91 | 2016-11-04 09:45:03 +0100 | [diff] [blame] | 52 | location = flowRequest.headers["location"] |
Carmelo Cascone | b34d8e1 | 2020-09-28 16:16:59 -0700 | [diff] [blame^] | 53 | print("@stc " + name + "Location=" + location) |
Ray Milkey | 43ee97f | 2015-09-04 15:14:40 -0700 | [diff] [blame] | 54 | sys.exit(0) |
| 55 | |
| 56 | |
| 57 | |