What Happens If I Make a Mistake?

This notebook contains an interactive introduction to the OFTR language.

ZOF Codec

For the first step, we are going to show how to use zof.codec. This is a tool for translating OpenFlow messages from YAML to binary and back again.

First, import zof.codec.


In [2]:
import zof.codec

Let's test zof.codec to make sure it is working.


In [3]:
'type: FEATURES_REQUEST'.encode('openflow')


Out[3]:
b'\x04\x05\x00\x08\x00\x00\x00\x00'

The output shows a binary OpenFlow version 1.3 (0x04) message. We can decode this using decode.


In [4]:
print(b'\x04\x05\x00\x08\x00\x00\x00\x00'.decode('openflow'))


---
type:            FEATURES_REQUEST
xid:             0x00000000
version:         0x04
msg:             
...


In [5]:
import zof.codec

def dump(s):
    try:
        print(s.encode('openflow').decode('openflow'))
    except Exception as ex:
        print(ex)

dump('''
type: HELLO
version: 1
''')


---
type:            HELLO
xid:             0x00000000
version:         0x01
msg:             
  versions:        [  ]
...


In [6]:
dump('''
type: FLOW_MOD
msg:
  command: ADD
  table_id: 0
  buffer_id: 7
  match:
    - field: ETH_DST
      value: 00:00:00:00:00:01
''')


---
type:            FLOW_MOD
xid:             0x00000000
version:         0x04
msg:             
  cookie:          0x0000000000000000
  cookie_mask:     0x0000000000000000
  table_id:        0x00
  command:         ADD
  idle_timeout:    0x0000
  hard_timeout:    0x0000
  priority:        0x0000
  buffer_id:       0x00000007
  out_port:        ANY
  out_group:       ANY
  flags:           [  ]
  match:           
    - field:           ETH_DST
      value:           '00:00:00:00:00:01'
  instructions:    
...


In [7]:
dump('''
  type: ROLE_request
  msg:
    role: ROLE_MASTER
    generation_id: 0x10
''')


YAML:2:9: error: unknown value "ROLE_request" Did you mean "ROLE_REQUEST"?
  type: ROLE_request
        ^~~~~~~~~~~~


In [8]:
dump('''
  type: ROLE_REQUEST
  msg:
    role: ROLE_MASTER
    generation: 0x10
''')


YAML:4:5: error: missing required key 'generation_id'
    role: ROLE_MASTER
    ^


In [9]:
dump('''
  type: ROLE_REQUEST
  msg:
    role: ROLE_MASTER
    generation_id: 0x10
    extra: 1
''')


YAML:6:12: error: unknown key 'extra'
    extra: 1
           ^


In [10]:
dump('''
  type: ROLE_REQUEST
  msg:
    role: 1000
    generation_id: 0x10
    extra: 1
''')


YAML:6:12: error: unknown key 'extra'
    extra: 1
           ^


In [11]:
dump('''
{
  "type": "ROLE_REQUEST",
   "msg": {
      role: ROLE_MASTER,
      generation_id: "0x10"
    }
}
''')


---
type:            ROLE_REQUEST
xid:             0x00000000
version:         0x04
msg:             
  role:            ROLE_MASTER
  generation_id:   0x0000000000000010
...


In [ ]: