0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 09:41:54 +00:00

YANG: CBOR -> JSON basic converter

This commit is contained in:
Maria Matejka 2023-11-15 13:56:37 +01:00
parent fe52f98a92
commit ce6fa363b2
6 changed files with 61 additions and 41 deletions

14
yang/JSON_CBOR.py Normal file
View File

@ -0,0 +1,14 @@
from yangson import DataModel
import cbor
import json
class Message:
def __init__(self, filename, _type):
self.filename = filename
with open(self.filename, "rb") as sf:
self.raw = { "json": json, "cbor": cbor }[_type].load(sf)
self.dm = DataModel.from_file('yang-library.json')
self.data = self.dm.from_raw(self.raw)
self.data.validate()

5
yang/cbor-json-yang Executable file
View File

@ -0,0 +1,5 @@
python3 -m venv venv
. venv/bin/activate
pip3 install yangson cbor
python3 cbor-json-yang.py "$@"

13
yang/cbor-json-yang.py Normal file
View File

@ -0,0 +1,13 @@
import cbor
import json
import sys
import JSON_CBOR
#print(sys.argv[1])
msg = JSON_CBOR.Message(sys.argv[1], "cbor")
with open(sys.argv[2], "w") as of:
json.dump(msg.raw, of, indent=2)
print("OK")

View File

@ -3,4 +3,3 @@ python3 -m venv venv
pip3 install yangson cbor
python3 json-cbor-yang.py "$@"
#python3 json2cbor.py "$@"

View File

@ -1,23 +1,12 @@
from yangson import DataModel
import cbor
import json
import sys
class Message:
def __init__(self, filename):
self.filename = filename
with open(self.filename) as sf:
self.raw = json.load(sf)
self.dm = DataModel.from_file('yang-library.json')
self.data = self.dm.from_raw(self.raw)
self.data.validate()
import JSON_CBOR
#print(sys.argv[1])
msg = Message(sys.argv[1])
#print(msg, msg.raw)
#print(cbor.dumps(msg.raw))
msg = JSON_CBOR.Message(sys.argv[1], "json")
with open(sys.argv[2], "wb") as of:
cbor.dump(msg.raw, of)

View File

@ -1,31 +1,31 @@
{
"show_memory:message": {
"header": "BIRD memory usage",
"body": {
"routing_tables": {
"effective": 324,
"overhead": 48
},
"route_attributes": {
"effective": 324,
"overhead": 48
},
"protocols": {
"effective": 324,
"overhead": 48
},
"current_config": {
"effective": 324,
"overhead": 48
},
"standby_memory": {
"effective": 324,
"overhead": 48
},
"total": {
"effective": 324,
"overhead": 48
}
}
"header": "BIRD memory usage",
"body": {
"routing_tables": {
"effective": 324,
"overhead": 48
},
"route_attributes": {
"effective": 324,
"overhead": 48
},
"protocols": {
"effective": 324,
"overhead": 48
},
"current_config": {
"effective": 324,
"overhead": 48
},
"standby_memory": {
"effective": 324,
"overhead": 48
},
"total": {
"effective": 324,
"overhead": 48
}
}
}
}