From 472c68c897523927e4ca7f14d88763dee5589364 Mon Sep 17 00:00:00 2001 From: Tripon Alexandru-Ionut Date: Wed, 10 Apr 2019 12:27:52 +0300 Subject: [PATCH] Added DispatcherHosts to LoaderS --- config/config_defaults.go | 13 +++++- config/config_json_test.go | 28 ++++++++++++ config/config_test.go | 31 ++++++++++++++ loaders/loader_test.go | 88 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 1 deletion(-) diff --git a/config/config_defaults.go b/config/config_defaults.go index ed03dda2b..93c50f2fb 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -660,7 +660,7 @@ const CGRATES_CFG_JSON = ` }, { "type": "*dispatchers", // data source type - "file_name": "DispatcherProfiles.csv", // file name in the tp_in_dir + "file_name": "DispatcherProfiles.csv", // file name in the tp_in_dir "fields": [ {"tag": "Tenant", "field_id": "Tenant", "type": "*composed", "value": "~0", "mandatory": true}, {"tag": "ID", "field_id": "ID", "type": "*composed", "value": "~1", "mandatory": true}, @@ -677,6 +677,17 @@ const CGRATES_CFG_JSON = ` {"tag": "Weight", "field_id": "Weight", "type": "*composed", "value": "~12"}, ], }, + { + "type": "*dispatcher_hosts", // data source type + "file_name": "DispatcherHosts.csv", // file name in the tp_in_dir + "fields": [ + {"tag": "Tenant", "field_id": "Tenant", "type": "*composed", "value": "~0", "mandatory": true}, + {"tag": "ID", "field_id": "ID", "type": "*composed", "value": "~1", "mandatory": true}, + {"tag": "Address", "field_id": "Address", "type": "*composed", "value": "~2"}, + {"tag": "Transport", "field_id": "Transport", "type": "*composed", "value": "~3"}, + {"tag": "TLS", "field_id": "TLS", "type": "*composed", "value": "~4"}, + ], + }, ], }, ], diff --git a/config/config_json_test.go b/config/config_json_test.go index faf7b104b..bacbdf7ce 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -1293,6 +1293,34 @@ func TestDfLoaderJsonCfg(t *testing.T) { Value: utils.StringPointer("~12")}, }, }, + { + Type: utils.StringPointer(utils.MetaDispatcherHosts), + File_name: utils.StringPointer(utils.DispatcherHostsCsv), + Fields: &[]*FcTemplateJsonCfg{ + {Tag: utils.StringPointer(utils.Tenant), + Field_id: utils.StringPointer(utils.Tenant), + Type: utils.StringPointer(utils.META_COMPOSED), + Value: utils.StringPointer("~0"), + Mandatory: utils.BoolPointer(true)}, + {Tag: utils.StringPointer(utils.ID), + Field_id: utils.StringPointer(utils.ID), + Type: utils.StringPointer(utils.META_COMPOSED), + Value: utils.StringPointer("~1"), + Mandatory: utils.BoolPointer(true)}, + {Tag: utils.StringPointer("Address"), + Field_id: utils.StringPointer("Address"), + Type: utils.StringPointer(utils.META_COMPOSED), + Value: utils.StringPointer("~2")}, + {Tag: utils.StringPointer("Transport"), + Field_id: utils.StringPointer("Transport"), + Type: utils.StringPointer(utils.META_COMPOSED), + Value: utils.StringPointer("~3")}, + {Tag: utils.StringPointer("TLS"), + Field_id: utils.StringPointer("TLS"), + Type: utils.StringPointer(utils.META_COMPOSED), + Value: utils.StringPointer("~4")}, + }, + }, }, }, } diff --git a/config/config_test.go b/config/config_test.go index b3b702a5d..c64174f9a 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -1464,6 +1464,37 @@ func TestCgrLoaderCfgITDefaults(t *testing.T) { }, }, }, + { + Type: utils.MetaDispatcherHosts, + Filename: utils.DispatcherHostsCsv, + Fields: []*FCTemplate{ + {Tag: "Tenant", + FieldId: "Tenant", + Type: utils.META_COMPOSED, + Value: NewRSRParsersMustCompile("~0", true, utils.INFIELD_SEP), + Mandatory: true}, + {Tag: "ID", + FieldId: "ID", + Type: utils.META_COMPOSED, + Value: NewRSRParsersMustCompile("~1", true, utils.INFIELD_SEP), + Mandatory: true}, + {Tag: "Address", + FieldId: "Address", + Type: utils.META_COMPOSED, + Value: NewRSRParsersMustCompile("~2", true, utils.INFIELD_SEP), + }, + {Tag: "Transport", + FieldId: "Transport", + Type: utils.META_COMPOSED, + Value: NewRSRParsersMustCompile("~3", true, utils.INFIELD_SEP), + }, + {Tag: "TLS", + FieldId: "TLS", + Type: utils.META_COMPOSED, + Value: NewRSRParsersMustCompile("~4", true, utils.INFIELD_SEP), + }, + }, + }, }, }, } diff --git a/loaders/loader_test.go b/loaders/loader_test.go index d905c2369..604b2a1f3 100644 --- a/loaders/loader_test.go +++ b/loaders/loader_test.go @@ -1186,3 +1186,91 @@ cgrates.org,EVENT1,,,,,,ALL,,10,,, } } + +func TestLoaderProcessDispatcheHosts(t *testing.T) { + dipatcherHostCSV := ` +#Tenant[0],ID[1],Address[2],Transport[3],TLS[4] +cgrates.org,ALL,127.0.0.1:6012,*json,false +` + data, _ := engine.NewMapStorage() + ldr := &Loader{ + ldrID: "TestLoaderProcessContent", + bufLoaderData: make(map[string][]LoaderData), + dm: engine.NewDataManager(data), + timezone: "UTC", + } + ldr.dataTpls = map[string][]*config.FCTemplate{ + utils.MetaDispatcherHosts: []*config.FCTemplate{ + &config.FCTemplate{ + Tag: "Tenant", + FieldId: "Tenant", + Type: utils.META_COMPOSED, + Value: config.NewRSRParsersMustCompile("~0", true, utils.INFIELD_SEP), + Mandatory: true, + }, + &config.FCTemplate{ + Tag: "ID", + FieldId: "ID", + Type: utils.META_COMPOSED, + Value: config.NewRSRParsersMustCompile("~1", true, utils.INFIELD_SEP), + Mandatory: true, + }, + &config.FCTemplate{ + Tag: "Address", + FieldId: "Address", + Type: utils.META_COMPOSED, + Value: config.NewRSRParsersMustCompile("~2", true, utils.INFIELD_SEP), + }, + &config.FCTemplate{ + Tag: "Transport", + FieldId: "Transport", + Type: utils.META_COMPOSED, + Value: config.NewRSRParsersMustCompile("~3", true, utils.INFIELD_SEP), + }, + &config.FCTemplate{ + Tag: "TLS", + FieldId: "TLS", + Type: utils.META_COMPOSED, + Value: config.NewRSRParsersMustCompile("~4", true, utils.INFIELD_SEP), + }, + }, + } + rdr := ioutil.NopCloser(strings.NewReader(dipatcherHostCSV)) + csvRdr := csv.NewReader(rdr) + csvRdr.Comment = '#' + ldr.rdrs = map[string]map[string]*openedCSVFile{ + utils.MetaDispatcherHosts: map[string]*openedCSVFile{ + utils.DispatcherProfilesCsv: &openedCSVFile{ + fileName: utils.DispatcherProfilesCsv, + rdr: rdr, + csvRdr: csvRdr, + }, + }, + } + if err := ldr.processContent(utils.MetaDispatcherHosts); err != nil { + t.Error(err) + } + if len(ldr.bufLoaderData) != 0 { + t.Errorf("wrong buffer content: %+v", ldr.bufLoaderData) + } + eDispHost := &engine.DispatcherHost{ + Tenant: "cgrates.org", + ID: "ALL", + Conns: []*config.RemoteHost{ + &config.RemoteHost{ + Address: "127.0.0.1:6012", + Transport: utils.MetaJSONrpc, + }, + }, + } + + rcv, err := ldr.dm.GetDispatcherHost("cgrates.org", "ALL", + true, false, utils.NonTransactional) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(eDispHost, rcv) { + t.Errorf("expecting: %+v, received: %+v", utils.ToJSON(eDispHost), utils.ToJSON(rcv)) + } + +}