def test_parse_protocol_list_data(stc): input_string = '''{ "EnableProperty": "Left.CreateProtocolMix.EnableBgp", "ParentTagName": "ttBgpRouterConfig", "ClassName": "BgpRouterConfig", "PropertyValueDict": { "IpVersion": ["BLAH", "IPV4"], "EnableBfd": ["False", "True"] }, "StmPropertyModifierDict": { "AsNum": { "Start": ["0", "10"], "Step": ["0", "1"] }, "DutAsNum": { "Start": ["0", "20"], "Step": "1" } } }''' expected_prop_val = {} expected_prop_val["className"] = "BgpRouterConfig" expected_prop_val["tagName"] = "ttBgpRouterConfig" expected_prop_val["propertyValueList"] = {} expected_prop_val["propertyValueList"]["IpVersion"] = "IPV4" expected_prop_val["propertyValueList"]["EnableBfd"] = "True" expected_prop_mod1 = {} expected_prop_mod1["className"] = "BgpRouterConfig" expected_prop_mod1["tagName"] = "ttBgpRouterConfig.AsNum" expected_prop_mod1["parentTagName"] = "ttBgpRouterConfig" expected_prop_mod1["propertyName"] = "AsNum" expected_prop_mod1["propertyValueList"] = {} expected_prop_mod1["propertyValueList"]["Start"] = "10" expected_prop_mod1["propertyValueList"]["Step"] = "1" expected_prop_mod2 = {} expected_prop_mod2["className"] = "BgpRouterConfig" expected_prop_mod2["tagName"] = "ttBgpRouterConfig.DutAsNum" expected_prop_mod2["parentTagName"] = "ttBgpRouterConfig" expected_prop_mod2["propertyName"] = "DutAsNum" expected_prop_mod2["propertyValueList"] = {} expected_prop_mod2["propertyValueList"]["Start"] = "20" expected_prop_mod2["propertyValueList"]["Step"] = "1" err_str, input_table_data = json_utils.load_json(input_string) assert err_str == "" # Call parse on the second row res = process_util.parse_protocol_data(input_table_data, 1) assert res["protocolSrcTag"] == "ttBgpRouterConfig" assert len(res["propertyValueList"]) == 1 assert cmp(res["propertyValueList"][0], expected_prop_val) == 0 assert len(res["stmPropertyModifierList"]) == 2 assert expected_prop_mod1 in res["stmPropertyModifierList"] assert expected_prop_mod2 in res["stmPropertyModifierList"]
def config_table_data(input_dict): plLogger = PLLogger.GetLogger('methodology') plLogger.LogDebug('begin.txml_processing_functions.config_table_data') output_dict = {} err_msg = "" json_data_list = [] plLogger.LogDebug('input_dict: ' + str(input_dict)) # Validate the input_dict res = validate_input_dict(input_dict) if res != "": return output_dict, res # Loop through each row in the txml table weight_list = input_dict["input"]["customDict"]["Weight"] for row_idx, weight in enumerate(weight_list): json_data = {} json_data["weight"] = float(weight) enable_vlan = input_dict["input"]["customDict"]["EnableVlan"][row_idx] if ast.literal_eval(enable_vlan): json_data["baseTemplateFile"] = "Dual_Vlan.xml" else: json_data["baseTemplateFile"] = "Dual_NoVlan.xml" json_data["protocolTemplateFile"] = "AllRouters.xml" json_data["deviceTag"] = "ttEmulatedDevice" json_data["useBlock"] = False enable_bfd = False bfd_protocol = {} json_data["interfaceList"] = [] json_data["protocolList"] = [] # Loop through the interfaceDict in the input_dict and # add each interface to the interfaceList in the output_dict for interface in input_dict["input"]["interfaceDict"]: # Don't add the VlanIf if EnableVlan is False if interface["ClassName"] == "VlanIf" and \ ast.literal_eval(enable_vlan) is False: continue else: json_data["interfaceList"].append( process_util.parse_interface_data(interface, row_idx)) # Loop through the protocolDict in the input_dict and # add each protocol to the protocolList in the output_dict for protocol in input_dict["input"]["protocolDict"]: # Add the protocol to the protocolList if it is enabled if "EnableProperty" in protocol.keys(): if ast.literal_eval(protocol["EnableProperty"][row_idx]): if "EnableBfd" in protocol["PropertyValueDict"].keys(): enable_bfd |= ast.literal_eval( protocol["PropertyValueDict"]["EnableBfd"][row_idx]) json_data["protocolList"].append( process_util.parse_protocol_data(protocol, row_idx)) # BfdRouterConfig will be handled after all other protocols are parsed if protocol["ClassName"] == "BfdRouterConfig": bfd_protocol = protocol # Bfd is added to the protocolList if bfd is enabled in at least one protocol and # the BfdRouterConfig information is defined in input_dict if enable_bfd is True and len(bfd_protocol) != 0: json_data["protocolList"].append( process_util.parse_protocol_data(bfd_protocol, row_idx)) json_data_list.append(json_data) # f = open('out.txt', 'w') # f.write(json.dumps(json_data_list)) # f.write(str(json_data_list)) output_dict["TableData"] = json.dumps(json_data_list) plLogger.LogDebug('output_dict: ' + str(output_dict)) plLogger.LogDebug('end.txml_processing_functions.config_table_data') return output_dict, err_msg