class StarosParser_2 #parser for staros ECS configuration def ecs_config_parser(config_string_array) out_ans = {} parsed_config = {"config" => {"active-charging service" => []}} config_string_array.shift # {"config" => {"active-charging service" => [{"ECS-SVC" => {"root" => [], "host-pool"=>{"1b1.nekurims.top"=>["ip 185.59.101.182/32"]}, "ruledef"=>{"VKONTAKTE_CDN_b"=>[]}}}}]} flag_inter_ecs_name = 0 current_ecs_name = "" current_section = "root" current_section_name = "" section_flag = 0 section_deep = 0 ecs_number = 0 for aaa in config_string_array if section_deep == 0 && aaa[0..22] == "active-charging service" #&& flag_inter_ecs_name == 0 ecs_name_regexp = /active-charging service (\S+)/ current_ecs_name = aaa.match(ecs_name_regexp)[1] #p current_ecs_name parsed_config["config"]["active-charging service"].push({current_ecs_name => {"root" => [], "host-pool" => {}, "port-map" => {}, "ruledef" => {}, "access-ruledef" => {}, "group-of-ruledefs" => {}, "packet-filter" => {}, "edr-format" => {}, "xheader-format" => {}, "charging-action" => {}, "rulebase" => {}, "fw-and-nat policy" => {} }}) flag_inter_ecs_name = 1 section_deep = 1 current_section = "root" else if aaa == "exit" && section_deep == 2 section_flag = 0 section_deep = section_deep - 1 current_section = "root" elsif aaa[0..9] == "host-pool " && section_flag == 0 && section_deep == 1 current_section = "host-pool" ecs_hostpool_regexp = /host-pool\s+(.+)/ current_section_name = aaa.match(ecs_hostpool_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..8] == "port-map " && section_flag == 0 && section_deep == 1 current_section = "port-map" ecs_portmap_regexp = /port-map\s+(.+)/ current_section_name = aaa.match(ecs_portmap_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..7] == "ruledef " && section_flag == 0 && section_deep == 1 current_section = "ruledef" ecs_ruldef_regexp = /ruledef\s+(.+)/ current_section_name = aaa.match(ecs_ruldef_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..14] == "access-ruledef " && section_flag == 0 && section_deep == 1 current_section = "access-ruledef" ecs_accruldef_regexp = /access-ruledef\s+(.+)/ current_section_name = aaa.match(ecs_accruldef_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..17] == "group-of-ruledefs " && section_flag == 0 && section_deep == 1 current_section = "group-of-ruledefs" ecs_grruldef_regexp = /group-of-ruledefs\s+(.+)/ current_section_name = aaa.match(ecs_grruldef_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..13] == "packet-filter " && section_flag == 0 && section_deep == 1 current_section = "packet-filter" ecs_packfil_regexp = /packet-filter\s+(.+)/ current_section_name = aaa.match(ecs_packfil_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..10] == "edr-format " && section_flag == 0 && section_deep == 1 current_section = "edr-format" ecs_edrformat_regexp = /edr-format\s+(.+)/ current_section_name = aaa.match(ecs_edrformat_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..14] == "xheader-format " && section_flag == 0 && section_deep == 1 current_section = "xheader-format" ecs_xheader_regexp = /xheader-format\s+(.+)/ current_section_name = aaa.match(ecs_xheader_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..15] == "charging-action " && section_flag == 0 && section_deep == 1 current_section = "charging-action" ecs_xheader_regexp = /charging-action\s+(.+)/ current_section_name = aaa.match(ecs_xheader_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..8] == "rulebase " && section_flag == 0 && section_deep == 1 current_section = "rulebase" ecs_xheader_regexp = /rulebase\s+(.+)/ current_section_name = aaa.match(ecs_xheader_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif aaa[0..17] == "fw-and-nat policy " && section_flag == 0 && section_deep == 1 current_section = "fw-and-nat policy" ecs_xheader_regexp = /fw-and-nat policy\s+(.+)/ current_section_name = aaa.match(ecs_xheader_regexp)[1] parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name] = [] section_flag = 1 section_deep = section_deep + 1 elsif section_flag == 1 && current_section != "root" && current_section != "end" && section_deep == 2 parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name][current_section][current_section_name].push(aaa) elsif current_section == "root" && section_deep == 1 && aaa != "end" parsed_config["config"]["active-charging service"][ecs_number][current_ecs_name]["root"].push(aaa) elsif aaa == "end" section_flag = 0 section_deep = 0 current_section = "end" p "end" else p aaa end end end parsed_config end end