Add dispatcher and tp dispatcher in migrator

This commit is contained in:
TeoV
2019-01-11 06:31:56 -05:00
committed by Dan Christian Bogos
parent be41b33e50
commit 8c281a16dd
4 changed files with 260 additions and 2 deletions

View File

@@ -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...)

87
apier/v1/tpdispatchers.go Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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
}

81
migrator/dispatchers.go Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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
}

View File

@@ -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 <http://www.gnu.org/licenses/>
*/
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
}