bbfdm/tools/generate_dm_excel.py
2023-04-18 11:30:46 +00:00

271 lines
8.5 KiB
Python
Executable file

#!/usr/bin/python3
# Copyright (C) 2021 iopsys Software Solutions AB
# Author: Amin Ben Ramdhane <amin.benramdhane@pivasoftware.com>
from collections import OrderedDict
import os
import sys
import json
import argparse
import xlwt
import bbf_common as bbf
LIST_DM = []
def getprotocols(value):
if isinstance(value, dict):
for obj, val in value.items():
if obj == "protocols" and isinstance(val, list):
if len(val) == 2:
return "CWMP+USP"
elif val[0] == "usp":
return "USP"
else:
return "CWMP"
return "CWMP+USP"
def is_param_obj_command_event_supported(dmobject):
for value in bbf.LIST_SUPPORTED_DM:
obj = json.loads(value)
param = bbf.get_option_value(obj, "param", None)
if param is None:
continue
if param == dmobject:
bbf.LIST_SUPPORTED_DM.remove(value)
return "Yes"
return "No"
def add_data_to_list_dm(obj, supported, protocols, types, version):
rootdm = bbf.get_root_node()
if (rootdm):
if (obj.startswith(rootdm)):
LIST_DM.append(obj + "," + protocols + "," + supported + "," + types + "," + version)
else:
LIST_DM.append(obj + "," + protocols + "," + supported + "," + types + "," + version)
def parse_standard_object(dmobject, value):
hasobj = bbf.obj_has_child(value)
hasparam = bbf.obj_has_param(value)
supported = is_param_obj_command_event_supported(dmobject)
version = bbf.get_option_value(value, "version", "2.0")
add_data_to_list_dm(dmobject, supported, getprotocols(value), "object", version)
if hasparam:
if isinstance(value, dict):
for k, v in value.items():
if k == "mapping":
continue
if isinstance(v, dict):
for k1, v1 in v.items():
if k1 == "type" and v1 != "object":
supported = is_param_obj_command_event_supported(dmobject + k)
version = bbf.get_option_value(v, "version", "2.0")
add_data_to_list_dm(dmobject + k, supported, getprotocols(v), "operate" if "()" in k else "event" if "!" in k else "parameter", version)
break
if hasobj:
if isinstance(value, dict):
for k, v in value.items():
if isinstance(v, dict):
for k1, v1 in v.items():
if k1 == "type" and v1 == "object":
parse_standard_object(k, v)
def parse_dynamic_object(dm_name_list):
if isinstance(dm_name_list, list) is False:
return None
for value in bbf.LIST_SUPPORTED_DM:
obj = json.loads(value)
param = bbf.get_option_value(obj, "param", None)
p_type = bbf.get_option_value(obj, "type", None)
version = bbf.get_option_value(obj, "version", "2.0")
if param is None or p_type is None:
continue
for dm in dm_name_list:
JSON_FILE = bbf.ARRAY_JSON_FILES.get(dm, None)
if JSON_FILE is None:
continue
if dm == "tr181" and ".Services." in param:
continue
if dm == "tr104" and ".Services." not in param:
continue
if dm == "tr135" and ".Services." not in param:
continue
dmType = "object" if p_type == "DMT_OBJ" else "parameter"
add_data_to_list_dm(param, "Yes", "CWMP+USP", dmType, version)
def parse_object_tree(dm_name_list):
if isinstance(dm_name_list, list) is False:
return None
for dm in dm_name_list:
JSON_FILE = bbf.ARRAY_JSON_FILES.get(dm, None)
if JSON_FILE is not None:
file = open(JSON_FILE, "r", encoding='utf-8')
data = json.loads(file.read(), object_pairs_hook=OrderedDict)
for obj, value in data.items():
if obj is None:
print(f'!!!! {dm} : Wrong JSON Data model format!')
continue
parse_standard_object(obj, value)
else:
print(f"!!!! {dm} : Data Model doesn't exist")
parse_dynamic_object(dm_name_list)
def generate_excel_file(output_file):
bbf.remove_file(output_file)
LIST_DM.sort(reverse=False)
wb = xlwt.Workbook(style_compression=2)
sheet = wb.add_sheet('CWMP-USP')
xlwt.add_palette_colour("custom_colour_yellow", 0x10)
xlwt.add_palette_colour("custom_colour_green", 0x20)
xlwt.add_palette_colour("custom_colour_grey", 0x30)
wb.set_colour_RGB(0x10, 255, 255, 153)
wb.set_colour_RGB(0x20, 102, 205, 170)
wb.set_colour_RGB(0x30, 153, 153, 153)
style_title = xlwt.easyxf(
'pattern: pattern solid, fore_colour custom_colour_grey;' + 'font: bold 1, color black;' + 'alignment: horizontal center;')
sheet.write(0, 0, 'OBJ/PARAM/OPERATE', style_title)
sheet.write(0, 1, 'Protocols', style_title)
sheet.write(0, 2, 'Version', style_title)
sheet.write(0, 3, 'Supported', style_title)
i = 0
for value in LIST_DM:
param = value.split(",")
i += 1
if param[3] == "object":
style_name = xlwt.easyxf(
'pattern: pattern solid, fore_colour custom_colour_yellow')
style = xlwt.easyxf(
'pattern: pattern solid, fore_colour custom_colour_yellow;' + 'alignment: horizontal center;')
elif param[3] == "operate" or param[3] == "event":
style_name = xlwt.easyxf(
'pattern: pattern solid, fore_colour custom_colour_green')
style = xlwt.easyxf(
'pattern: pattern solid, fore_colour custom_colour_green;' + 'alignment: horizontal center;')
else:
style_name = None
style = xlwt.easyxf('alignment: horizontal center;')
if style_name is not None:
sheet.write(i, 0, param[0], style_name)
else:
sheet.write(i, 0, param[0])
sheet.write(i, 1, param[1], style)
sheet.write(i, 2, param[4], style)
sheet.write(i, 3, param[2], style)
sheet.col(0).width = 1300*20
sheet.col(1).width = 175*20
sheet.col(2).width = 175*20
sheet.col(3).width = 175*20
wb.save(output_file)
def generate_excel(dm_name_list, output_file="datamodel.xml"):
print("Generating BBF Data Models in Excel format...")
bbf.fill_list_supported_dm()
parse_object_tree(dm_name_list)
generate_excel_file(output_file)
if os.path.isfile(output_file):
print(f' - Excel file generated: {output_file}')
else:
print(f' - Error in excel file generation {output_file}')
### main ###
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Script to generate list of supported and non-supported parameter in xls format',
epilog='Part of BBF-tools, refer Readme for more examples'
)
parser.add_argument(
'-d', '--datamodel',
action = 'append',
metavar='tr181',
choices= ['tr181', 'tr104'],
required= True,
)
parser.add_argument(
'-r', '--remote-dm',
action='append',
metavar = 'git^https://dev.iopsys.eu/bbf/stunc.git^devel',
help= 'Includes OBJ/PARAM defined under remote repositories defined as bbf plugin'
)
parser.add_argument(
'-v', '--vendor-list',
metavar='iopsys',
action = 'append',
help='Generate data model tree with vendor extension OBJ/PARAM'
)
parser.add_argument(
'-p', '--vendor-prefix',
default = 'X_IOPSYS_EU_',
metavar = 'X_IOPSYS_EU_',
help = 'Generate data model tree using provided vendor prefix for vendor defined objects'
)
parser.add_argument(
'-o', '--output',
default = "datamodel.xls",
metavar = "supported_datamodel.xls",
help = 'Generate the output file with given name'
)
args = parser.parse_args()
plugins = []
if isinstance(args.remote_dm, list) is True:
for f in args.remote_dm:
x = f.split('^')
r = {}
r["proto"] = x[0]
if len(x) > 1:
r["repo"] = x[1]
if len(x) == 3:
r["version"] = x[2]
plugins.append(r)
bbf.generate_supported_dm(args.vendor_prefix, args.vendor_list, plugins)
bbf.clean_supported_dm_list()
generate_excel(args.datamodel, args.output)
print(f'Datamodel generation completed, aritifacts available in {args.output}')
sys.exit(bbf.BBF_ERROR_CODE)