From 8c281a16dddd20189a46a5d3c6f0485284e837ee Mon Sep 17 00:00:00 2001 From: TeoV Date: Fri, 11 Jan 2019 06:31:56 -0500 Subject: [PATCH] Add dispatcher and tp dispatcher in migrator --- apier/v1/tpchargers.go | 4 +- apier/v1/tpdispatchers.go | 87 ++++++++++++++++++++++++++++++++++++ migrator/dispatchers.go | 81 ++++++++++++++++++++++++++++++++++ migrator/tp_dispatchers.go | 90 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 apier/v1/tpdispatchers.go create mode 100644 migrator/dispatchers.go create mode 100644 migrator/tp_dispatchers.go diff --git a/apier/v1/tpchargers.go b/apier/v1/tpchargers.go index 7d11c8861..968f2c7cc 100755 --- a/apier/v1/tpchargers.go +++ b/apier/v1/tpchargers.go @@ -55,7 +55,7 @@ type AttrGetTPChargerIds struct { utils.Paginator } -// Queries Resource identities on specific tariff plan. +// Queries Charger identities on specific tariff plan. func (self *ApierV1) GetTPChargerIDs(attrs *AttrGetTPChargerIds, reply *[]string) error { if missing := utils.MissingStructFields(attrs, []string{"TPid"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) @@ -72,7 +72,7 @@ func (self *ApierV1) GetTPChargerIDs(attrs *AttrGetTPChargerIds, reply *[]string return nil } -// Removes specific Resource on Tariff plan +// Removes specific ChargerProfile on Tariff plan func (self *ApierV1) RemTPCharger(attrs *utils.TPTntID, reply *string) error { if missing := utils.MissingStructFields(attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing return utils.NewErrMandatoryIeMissing(missing...) diff --git a/apier/v1/tpdispatchers.go b/apier/v1/tpdispatchers.go new file mode 100644 index 000000000..455de6f70 --- /dev/null +++ b/apier/v1/tpdispatchers.go @@ -0,0 +1,87 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package v1 + +import ( + "github.com/cgrates/cgrates/utils" +) + +//SetTPDispatcher creates a new DispatcherProfile within a tariff plan +func (self *ApierV1) SetTPDispatcher(attr *utils.TPDispatcherProfile, reply *string) error { + if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + if err := self.StorDb.SetTPDispatchers([]*utils.TPDispatcherProfile{attr}); err != nil { + return utils.APIErrorHandler(err) + } + *reply = utils.OK + return nil +} + +//GetTPCharger queries specific DispatcherProfile on Tariff plan +func (self *ApierV1) GetTPDispatcher(attr *utils.TPTntID, reply *utils.TPDispatcherProfile) error { + if missing := utils.MissingStructFields(attr, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing + return utils.NewErrMandatoryIeMissing(missing...) + } + if rls, err := self.StorDb.GetTPDispatchers(attr.TPid, attr.Tenant, attr.ID); err != nil { + if err.Error() != utils.ErrNotFound.Error() { + err = utils.NewErrServerError(err) + } + return err + } else { + *reply = *rls[0] + } + return nil +} + +type AttrGetTPDispatcherIds struct { + TPid string // Tariff plan id + utils.Paginator +} + +//GetTPDispatcherIDs queries dispatcher identities on specific tariff plan. +func (self *ApierV1) GetTPDispatcherIDs(attrs *AttrGetTPDispatcherIds, reply *[]string) error { + if missing := utils.MissingStructFields(attrs, []string{"TPid"}); len(missing) != 0 { //Params missing + return utils.NewErrMandatoryIeMissing(missing...) + } + if ids, err := self.StorDb.GetTpTableIds(attrs.TPid, utils.TBLTPDispatchers, utils.TPDistinctIds{"id"}, + nil, &attrs.Paginator); err != nil { + if err.Error() != utils.ErrNotFound.Error() { + err = utils.NewErrServerError(err) + } + return err + } else { + *reply = ids + } + return nil +} + +//RemTPCharger removes specific DispatcherProfile on Tariff plan +func (self *ApierV1) RemTPDispatcher(attrs *utils.TPTntID, reply *string) error { + if missing := utils.MissingStructFields(attrs, []string{"TPid", "Tenant", "ID"}); len(missing) != 0 { //Params missing + return utils.NewErrMandatoryIeMissing(missing...) + } + if err := self.StorDb.RemTpData(utils.TBLTPDispatchers, attrs.TPid, + map[string]string{"tenant": attrs.Tenant, "id": attrs.ID}); err != nil { + return utils.NewErrServerError(err) + } else { + *reply = utils.OK + } + return nil +} diff --git a/migrator/dispatchers.go b/migrator/dispatchers.go new file mode 100644 index 000000000..7d4abb73b --- /dev/null +++ b/migrator/dispatchers.go @@ -0,0 +1,81 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package migrator + +import ( + "fmt" + "strings" + + "github.com/cgrates/cgrates/config" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func (m *Migrator) migrateCurrentDispatcher() (err error) { + var ids []string + tenant := config.CgrConfig().GeneralCfg().DefaultTenant + ids, err = m.dmIN.DataManager().DataDB().GetKeysForPrefix(utils.DispatcherProfilePrefix) + if err != nil { + return err + } + for _, id := range ids { + idg := strings.TrimPrefix(id, utils.DispatcherProfilePrefix+tenant+":") + dpp, err := m.dmIN.DataManager().GetDispatcherProfile(tenant, idg, false, false, utils.NonTransactional) + if err != nil { + return err + } + if dpp != nil { + if m.dryRun != true { + if err := m.dmOut.DataManager().SetDispatcherProfile(dpp, true); err != nil { + return err + } + m.stats[utils.Dispatchers] += 1 + } + } + } + return +} + +func (m *Migrator) migrateDispatchers() (err error) { + var vrs engine.Versions + current := engine.CurrentDataDBVersions() + vrs, err = m.dmOut.DataManager().DataDB().GetVersions("") + if err != nil { + return utils.NewCGRError(utils.Migrator, + utils.ServerErrorCaps, + err.Error(), + fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error())) + } else if len(vrs) == 0 { + return utils.NewCGRError(utils.Migrator, + utils.MandatoryIEMissingCaps, + utils.UndefinedVersion, + "version number is not defined for DispatcherProfile model") + } + switch vrs[utils.Dispatchers] { + case current[utils.Dispatchers]: + if m.sameDataDB { + return + } + if err := m.migrateCurrentDispatcher(); err != nil { + return err + } + return + } + return +} diff --git a/migrator/tp_dispatchers.go b/migrator/tp_dispatchers.go new file mode 100644 index 000000000..85fac4c8a --- /dev/null +++ b/migrator/tp_dispatchers.go @@ -0,0 +1,90 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package migrator + +import ( + "fmt" + + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +func (m *Migrator) migrateCurrentTPDispatchers() (err error) { + tpids, err := m.storDBIn.StorDB().GetTpIds(utils.TBLTPDispatchers) + if err != nil { + return err + } + + for _, tpid := range tpids { + ids, err := m.storDBIn.StorDB().GetTpTableIds(tpid, utils.TBLTPDispatchers, + utils.TPDistinctIds{"id"}, map[string]string{}, nil) + if err != nil { + return err + } + for _, id := range ids { + dispatchers, err := m.storDBIn.StorDB().GetTPDispatchers(tpid, "", id) + if err != nil { + return err + } + if dispatchers != nil { + if m.dryRun != true { + if err := m.storDBOut.StorDB().SetTPDispatchers(dispatchers); err != nil { + return err + } + for _, dispatcher := range dispatchers { + if err := m.storDBIn.StorDB().RemTpData(utils.TBLTPDispatchers, dispatcher.TPid, + map[string]string{"id": dispatcher.ID}); err != nil { + return err + } + } + m.stats[utils.TpDispatchers] += 1 + } + } + } + } + return +} + +func (m *Migrator) migrateTPDispatchers() (err error) { + var vrs engine.Versions + current := engine.CurrentStorDBVersions() + vrs, err = m.storDBOut.StorDB().GetVersions("") + if err != nil { + return utils.NewCGRError(utils.Migrator, + utils.ServerErrorCaps, + err.Error(), + fmt.Sprintf("error: <%s> when querying oldDataDB for versions", err.Error())) + } else if len(vrs) == 0 { + return utils.NewCGRError(utils.Migrator, + utils.MandatoryIEMissingCaps, + utils.UndefinedVersion, + "version number is not defined for TPDispatcher model") + } + switch vrs[utils.TpDispatchers] { + case current[utils.TpDispatchers]: + if m.sameStorDB { + return + } + if err := m.migrateCurrentTPDispatchers(); err != nil { + return err + } + return + } + return +}