func GetTelemetryConfiguration(teleSetupId int64) (telemetryConfiguration *TelemetryConfiguration, err error) {
	engine, err := ConnectDB()
	if err != nil {
		log.Errorf("Database connect error: %s", err)
		return nil, err
	}
	dbTelemetryConfiguration, err := db_models.GetTelemetryConfigurationByTeleSetupId(engine, teleSetupId)
	if err != nil {
		log.Error("Get telemetry_configuration by teleSetupId err: %+v", err)
		return nil, err
	}
	telemetryConfiguration = &TelemetryConfiguration{}
	telemetryConfiguration.MeasurementInterval       = ConvertMesurementIntervalToInt(dbTelemetryConfiguration.MeasurementInterval)
	telemetryConfiguration.MeasurementSample         = ConvertMesurementSampleToInt(dbTelemetryConfiguration.MeasurementSample)
	telemetryConfiguration.LowPercentile             = dbTelemetryConfiguration.LowPercentile
	telemetryConfiguration.MidPercentile             = dbTelemetryConfiguration.MidPercentile
	telemetryConfiguration.HighPercentile            = dbTelemetryConfiguration.HighPercentile
	telemetryConfiguration.ServerOriginatedTelemetry = dbTelemetryConfiguration.ServerOriginatedTelemetry
	telemetryConfiguration.TelemetryNotifyInterval   = dbTelemetryConfiguration.TelemetryNotifyInterval
	dbUnitConfigurationList := []db_models.UnitConfiguration{}
	err = engine.Where("tele_config_id = ?", dbTelemetryConfiguration.Id).OrderBy("id ASC").Find(&dbUnitConfigurationList)
	if err != nil {
		return
	}
	for _, v := range dbUnitConfigurationList {
		unitConfig := UnitConfig{}
		unitConfig.Unit       = ConvertUnitToInt(v.Unit)
		unitConfig.UnitStatus = v.UnitStatus
		telemetryConfiguration.UnitConfigList = append(telemetryConfiguration.UnitConfigList, unitConfig)
	}
	return
}
Get telemetry configuration
package models
import (
	"time"
	"encoding/json"
	"github.com/go-xorm/xorm"
	"github.com/nttdots/go-dots/dots_common/messages"
	"github.com/nttdots/go-dots/dots_server/db_models"
	log "github.com/sirupsen/logrus"
	dots_config "github.com/nttdots/go-dots/dots_server/config"
	types "github.com/nttdots/go-dots/dots_common/types/data"
	db_models_data "github.com/nttdots/go-dots/dots_server/db_models/data"
)
var DefaultTsid = -1
type SetupType string
const (
	TELEMETRY_CONFIGURATION SetupType = "TELEMETRY_CONFIGURATION"
	PIPE                    SetupType = "PIPE"
	BASELINE                SetupType = "BASELINE"
)
type TelemetryType string
const (
	TELEMETRY       TelemetryType = "TELEMETRY"
	TELEMETRY_SETUP TelemetryType = "TELEMETRY_SETUP"
)
type PrefixType string
const (
	TARGET_PREFIX PrefixType = "TARGET_PREFIX"
	SOURCE_PREFIX PrefixType = "SOURCE_PREFIX"
)
type ParameterType string
const (
	TARGET_PROTOCOL ParameterType = "TARGET_PROTOCOL"
	TARGET_FQDN     ParameterType = "FQDN"
	TARGET_URI      ParameterType = "URI"
	ALIAS_NAME      ParameterType = "ALIAS_NAME"
)
type trafficType string
const (
	TOTAL_TRAFFIC_NORMAL trafficType = "TOTAL_TRAFFIC_NORMAL"
	TOTAL_ATTACK_TRAFFIC trafficType = "TOTAL_ATTACK_TRAFFIC"
	TOTAL_TRAFFIC        trafficType = "TOTAL_TRAFFIC"
)
func CreateTelemetryConfiguration(customerId int, cuid string, cdid string, tsid int, telemetryConfiguration *TelemetryConfiguration) (err error) {
	engine, err := ConnectDB()
	if err != nil {
		log.Errorf("Database connect error: %s", err)
		return err
	}
	session := engine.NewSession()
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return err
	}
	currentSetupList, err := GetTelemetrySetupByCuidAndSetupType(customerId, cuid, string(TELEMETRY_CONFIGURATION))
	if err != nil {
		log.Errorf("Get telemetry setup with setup type is telemetry-configuration err: %+v", err)
		return err
	}
	for _, currentSetup := range currentSetupList {
		err = DeleteTelemetrySetupTelemetryConfiguration(engine, session, currentSetup.Id)
		if err != nil {
			session.Rollback()
			return err
		}
	}
	err = CreateTelemetrySetupTelemetryConfiguration(session, customerId, cuid, cdid, tsid, telemetryConfiguration)
	if err != nil {
		session.Rollback()
		return err
	}
	err = session.Commit()
	return nil
}
func UpdateTelemetryConfiguration(customerId int, cuid string, cdid string, tsid int, telemetryConfiguration *TelemetryConfiguration) (err error) {
	engine, err := ConnectDB()
	if err != nil {
		log.Errorf("Database connect error: %s", err)
		return err
	}
	session := engine.NewSession()
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return err
	}
	currentSetup, err := GetTelemetrySetupByCuidAndSetupType(customerId, cuid, string(TELEMETRY_CONFIGURATION))
	if err != nil {
		log.Errorf("Get telemetry setup with setup type is telemetry-configuration err: %+v", err)
		return err
	}
	if currentSetup[0].Id == 0 {
		log.Debugf("telemetry_setup update data exist err: %s", err)
		return
	}
	updateTelemetryConfiguration, err := db_models.GetTelemetryConfigurationByTeleSetupId(engine, currentSetup[0].Id)
	if err != nil {
		log.Errorf("Get telemetry configuration err: %+v", err)
		return err
	}
	if updateTelemetryConfiguration.Id == 0 {
		log.Debugf("telemetry_configuration update data exist err: %s", err)
		return
	}
	updateTelemetryConfiguration.MeasurementInterval = ConvertMesurementIntervalToString(telemetryConfiguration.MeasurementInterval)
	updateTelemetryConfiguration.MeasurementSample = ConvertMesurementSampleToString(telemetryConfiguration.MeasurementSample)
	updateTelemetryConfiguration.LowPercentile = telemetryConfiguration.LowPercentile
	updateTelemetryConfiguration.MidPercentile = telemetryConfiguration.MidPercentile
	updateTelemetryConfiguration.HighPercentile = telemetryConfiguration.HighPercentile
	updateTelemetryConfiguration.ServerOriginatedTelemetry = telemetryConfiguration.ServerOriginatedTelemetry
	updateTelemetryConfiguration.TelemetryNotifyInterval = telemetryConfiguration.TelemetryNotifyInterval
	_, err = session.Id(updateTelemetryConfiguration.Id).Update(updateTelemetryConfiguration)
	if err != nil {
		log.Errorf("telemetry_configuration update err: %s", err)
		session.Rollback()
		return err
	}
	_, err = session.Id(updateTelemetryConfiguration.Id).Cols("server_originated_telemetry").Update(&updateTelemetryConfiguration)
	if err != nil {
		session.Rollback()
		log.Errorf("telemetry_configuration update err: %s", err)
		return
	}
	_, err = session.Id(updateTelemetryConfiguration.Id).Cols("telemetry_notify_interval").Update(&updateTelemetryConfiguration)
	if err != nil {
		session.Rollback()
		log.Errorf("telemetry_configuration update err: %s", err)
		return
	}
	err = db_models.DeleteUnitConfigurationByTeleConfigId(session, updateTelemetryConfiguration.Id)
	if err != nil {
		log.Errorf("Delete unit configuration err: %+v", err)
		session.Rollback()
		return err
	}
	err = RegisterUnitConfiguration(session, updateTelemetryConfiguration.Id, telemetryConfiguration.UnitConfigList)
	if err != nil {
		return err
	}
	err = session.Commit()
	return nil
}
func CreateTotalPipeCapacity(customerId int, cuid string, cdid string, tsid int, pipeList []TotalPipeCapacity, isPresent bool) (isConflict bool, err error) {
	isConflict = false
	engine, err := ConnectDB()
	if err != nil {
		log.Errorf("Database connect error: %s", err)
		return isConflict, err
	}
	session := engine.NewSession()
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return isConflict, err
	}
	currentSetupList, err := GetTelemetrySetupByCustomerIdAndSetupType(customerId, string(PIPE))
	if err != nil {
		log.Errorf("Get telemetry setup with setup type is 'pipe' err: %+v", err)
		return isConflict, err
	}
	for _, currentSetup := range currentSetupList {
		currentPipeList, err := db_models.GetTotalPipeCapacityByTeleSetupId(engine, currentSetup.Id)
		if err != nil {
			log.Errorf("Get total-pipe-capacity err: %+v", err)
			return isConflict, err
		}
		lenCurrentPipeList := len(currentPipeList)
		for _, pipe := range pipeList {
			for _, currentPipe := range currentPipeList {
				if (pipe.LinkId == currentPipe.LinkId && pipe.Capacity == 0) || currentSetup.Tsid == DefaultTsid {
					log.Debugf("[Capacity is 0] The request capacity is 0. DOTS server will delete total_pipe_capacity id = %+v", currentPipe.Id)
					err = db_models.DeleteTotalPipeCapacityById(session, currentPipe.Id)
					if err != nil {
						log.Errorf("Delete total pipe capacity err: %+v", err)
						session.Rollback()
						return isConflict, err
					}
					lenCurrentPipeList--
				} else if pipe.LinkId == currentPipe.LinkId && ConvertUnitToString(pipe.Unit) == currentPipe.Unit {
					if currentSetup.Cuid == cuid && currentSetup.Tsid < tsid {
						log.Debugf("[Overlap] Overlapping link_id = %+v and unit_id = %+v. DOTS server will delete total_pipe_capacity id = %+v", 
						currentPipe.LinkId, currentPipe.Unit, currentPipe.Id)
						err = db_models.DeleteTotalPipeCapacityById(session, currentPipe.Id)
						if err != nil {
							log.Errorf("Delete total pipe capacity err: %+v", err)
							session.Rollback()
							return isConflict, err
						}
						lenCurrentPipeList--
					} else if currentSetup.Cuid != cuid {
						isConflict = true
						return isConflict, nil
					}
				}
			}
		}
		if lenCurrentPipeList == 0 {
			err = db_models.DeleteTelemetrySetupById(session, currentSetup.Id)
			if err != nil {
				log.Errorf("Delete telemetry setup error: %s", err)
				session.Rollback()
				return isConflict, err
			}
		}
	}
	if !isPresent {
		log.Debug("Create total pipe capacity")
		err = CreateTelemetrySetupPipe(session, customerId, cuid, cdid, tsid, pipeList)
		if err != nil {
			session.Rollback()
			return isConflict, err
		}
	} else {
		log.Debug("Update total pipe capacity")
		err = UpdateTotalPipeCapacity(session, customerId, cuid, cdid, tsid, pipeList)
		if err != nil {
			session.Rollback()
			return isConflict, err
		}
	}
	err = session.Commit()
	return isConflict, err
}
func UpdateTotalPipeCapacity(session *xorm.Session, customerId int, cuid string, cdid string, tsid int, pipeList []TotalPipeCapacity) error {
	currentSetup, err := GetTelemetrySetupByTsidAndSetupType(customerId, cuid, tsid, string(PIPE))
	if err != nil {
		log.Errorf("Get telemetry setup with setup type is 'pipe' err: %+v", err)
		return err
	}
	if currentSetup.Id == 0 {
		log.Debugf("telemetry setup update data exist err: %s", err)
		return nil
	}
	currentPipeList, err := db_models.GetTotalPipeCapacityByTeleSetupId(engine, currentSetup.Id)
	if err != nil {
		log.Errorf("Get total pipe capacity err: %+v", err)
		return err
	}
	for _, currentPipe := range currentPipeList {
		err = db_models.DeleteTotalPipeCapacityById(session, currentPipe.Id)
		if err != nil {
			log.Errorf("Delete total pipe capacity err: %+v", err)
			return err
		}
	}
	err = RegisterTotalPipecapacity(session, currentSetup.Id, pipeList)
	if err != nil {
		return err
	}
	return nil
}
func CreateBaseline(customerId int, cuid string, cdid string, tsid int, baselineList []Baseline, isPresent bool) (isConflict bool, err error) {
	isConflict = false
	engine, err := ConnectDB()
	if err != nil {
		log.Errorf("Database connect error: %s", err)
		return isConflict, err
	}
	session := engine.NewSession()
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return isConflict, err
	}
	currentSetupList, err := GetTelemetrySetupByCustomerIdAndSetupType(customerId, string(BASELINE))
	if err != nil {
		log.Errorf("Get telemetry setup with setup type is 'baseline' err: %+v", err)
		return isConflict, err
	}
	for _, currentSetup := range currentSetupList {
		currentBaselineList, err := GetBaselineByTeleSetupId(customerId, currentSetup.Cuid, currentSetup.Id)
		if err != nil {
			log.Errorf("Get baseline by teleSetupId err: %+v", err)
			return isConflict, err
		}
		lenCurrentBaselineList := len(currentBaselineList)
		for _, baseline := range baselineList {
			for _, currentBaseline := range currentBaselineList {
				if currentSetup.Cuid == cuid && currentSetup.Tsid == tsid {
					continue
				}
				isOverlap := CheckOverlapTargetList(baseline.TargetList, currentBaseline.TargetList)
				if (isOverlap && currentSetup.Cuid == cuid && currentSetup.Tsid < tsid) || currentSetup.Tsid == DefaultTsid {
					log.Debugf("DOTS server will delete baseline id = %+v", currentBaseline.Id)
					err = DeleteBaseline(session, currentBaseline.Id)
					if err != nil {
						session.Rollback()
						return isConflict, err
					}
					lenCurrentBaselineList--
				} else if isOverlap && currentSetup.Cuid != cuid  {
					isConflict = true
					return isConflict, nil
				}
			}
		}
		if lenCurrentBaselineList == 0 {
			err = db_models.DeleteTelemetrySetupById(session, currentSetup.Id)
			if err != nil {
				log.Errorf("Delete telemetry setup error: %s", err)
				session.Rollback()
				return isConflict, err
			}
		}
	}
	if !isPresent {
		log.Debug("Create baseline")
		err = CreateTelemetrySetupBaseline(session, customerId, cuid, cdid, tsid, baselineList)
		if err != nil {
			session.Rollback()
			return isConflict, err
		}
	} else {
		log.Debug("Update baseline")
		err = UpdateBaseline(session, customerId, cuid, cdid, tsid, baselineList)
		if err != nil {
			session.Rollback()
			return isConflict, err
		}
	}
	err = session.Commit()
	return isConflict, err
}
func UpdateBaseline(session *xorm.Session, customerId int, cuid string, cdid string, tsid int, baselineList []Baseline) error {
	currentSetup, err := GetTelemetrySetupByTsidAndSetupType(customerId, cuid, tsid, string(BASELINE))
	if err != nil {
		log.Errorf("Get telemetry setup with setup type is 'baseline' err: %+v", err)
		return err
	}
	if currentSetup.Id == 0 {
		log.Debugf("telemetry setup update data exist err: %s", err)
		return nil
	}
	currentBaselineList, err := db_models.GetBaselineByTeleSetupId(engine, currentSetup.Id)
	if err != nil {
		log.Errorf("Get baseline by teleSetupId err: %+v", err)
		return err
	}
	for _, currentBaseline := range currentBaselineList {
		err = DeleteBaseline(session, currentBaseline.Id)
		if err != nil {
			return err
		}
	}
	err = createBaseline(session, currentSetup.Id, baselineList)
	if err != nil {
		return err
	}
	return nil
}
func CreateTelemetrySetupTelemetryConfiguration(session *xorm.Session, customerId int, cuid string, cdid string, tsid int, telemetryConfiguration *TelemetryConfiguration) error {
	newTelemetrySetup, err := RegisterTelemetrySetup(session, customerId, cuid, cdid, tsid, string(TELEMETRY_CONFIGURATION))
	if err != nil {
		return err
	}
	err = createTelemetryConfiguration(session, newTelemetrySetup.Id, telemetryConfiguration)
	if err != nil {
		return err
	}
	return nil
}
func CreateTelemetrySetupPipe(session *xorm.Session, customerId int, cuid string, cdid string, tsid int, pipeList []TotalPipeCapacity) error {
	newTelemetrySetup, err := RegisterTelemetrySetup(session, customerId, cuid, cdid, tsid, string(PIPE))
	if err != nil {
		return err
	}
	err = RegisterTotalPipecapacity(session, newTelemetrySetup.Id, pipeList)
	if err != nil {
		return err
	}
	return nil
}
func CreateTelemetrySetupBaseline(session *xorm.Session, customerId int, cuid string, cdid string, tsid int, baselineList []Baseline) error {
	newTelemetrySetup, err := RegisterTelemetrySetup(session, customerId, cuid, cdid, tsid, string(BASELINE))
	if err != nil {
		return err
	}
	err = createBaseline(session, newTelemetrySetup.Id, baselineList)
	if err != nil {
		return err
	}
	return nil
}
func createTelemetryConfiguration(session *xorm.Session, teleSetupId int64, telemetryConfiguration *TelemetryConfiguration) error {
	newTelemetryConfiguration, err := RegisterTelemetryConfiguration(session, teleSetupId, telemetryConfiguration)
	if err != nil {
		return err
	}
	err = RegisterUnitConfiguration(session, newTelemetryConfiguration.Id, telemetryConfiguration.UnitConfigList)
	if err != nil {
		return err
	}
	return nil
}
func createBaseline(session *xorm.Session, teleSetupId int64, baselines []Baseline)  error {
	for _, baseline := range baselines {
		newBaseline, err := RegisterBaseline(session, teleSetupId, baseline)
		if err != nil {
			return err
		}
		err = RegisterTelemetryPrefix(session, string(TELEMETRY_SETUP), newBaseline.Id, string(TARGET_PREFIX), baseline.TargetPrefix)
		if err != nil {
			return err
		}
		err = RegisterTelemetryPortRange(session, string(TELEMETRY_SETUP), newBaseline.Id, string(TARGET_PREFIX), baseline.TargetPortRange)
		if err != nil {
			return err
		}
		err = CreateTelemetryParameterValue(session, string(TELEMETRY_SETUP), newBaseline.Id, baseline.TargetProtocol, baseline.FQDN, baseline.URI, baseline.AliasName)
		if err != nil {
			return err
		}
		err = RegisterTraffic(session, string(TELEMETRY_SETUP), string(TARGET_PREFIX), newBaseline.Id, string(TOTAL_TRAFFIC_NORMAL), baseline.TotalTrafficNormal)
		if err != nil {
			return err
		}
		err = RegisterTrafficPerProtocol(session, string(TELEMETRY_SETUP), newBaseline.Id, string(TOTAL_TRAFFIC_NORMAL), baseline.TotalTrafficNormalPerProtocol)
		if err != nil {
			return err
		}
		err = RegisterTrafficPerPort(session, string(TELEMETRY_SETUP), newBaseline.Id, string(TOTAL_TRAFFIC_NORMAL), baseline.TotalTrafficNormalPerPort)
		if err != nil {
			return err
		}
		err = RegisterTotalConnectionCapacity(session, newBaseline.Id, baseline.TotalConnectionCapacity)
		if err != nil {
			return err
		}
		err = RegisterTotalConnectionCapacityPerPort(session, newBaseline.Id, baseline.TotalConnectionCapacityPerPort)
		if err != nil {
			return err
		}
	}
	return nil
}
func RegisterTelemetrySetup(session *xorm.Session, customerId int, cuid string, cdid string, tsid int, setupType string) (*db_models.TelemetrySetup, error) {
	newTelemetrySetup := db_models.TelemetrySetup{
		CustomerId: customerId,
		Cuid:       cuid,
		Cdid:       cdid,
		Tsid:       tsid,
		SetupType:  setupType,
	}
	_, err := session.Insert(&newTelemetrySetup)
	if err != nil {
		log.Errorf("telemetry setup insert err: %s", err)
		return nil, err
	}
	return &newTelemetrySetup , nil
}
func RegisterTelemetryConfiguration(session *xorm.Session, teleSetupId int64, telemetryConfiguration *TelemetryConfiguration) (*db_models.TelemetryConfiguration, error) {
	newTelemetryConfiguration := db_models.TelemetryConfiguration{
		TeleSetupId:               teleSetupId,
		MeasurementInterval:       ConvertMesurementIntervalToString(telemetryConfiguration.MeasurementInterval),
		MeasurementSample:         ConvertMesurementSampleToString(telemetryConfiguration.MeasurementSample),
		LowPercentile:             telemetryConfiguration.LowPercentile,
		MidPercentile:             telemetryConfiguration.MidPercentile,
		HighPercentile:            telemetryConfiguration.HighPercentile,
		ServerOriginatedTelemetry: telemetryConfiguration.ServerOriginatedTelemetry,
		TelemetryNotifyInterval:   telemetryConfiguration.TelemetryNotifyInterval,
	}
	_, err := session.Insert(&newTelemetryConfiguration)
	if err != nil {
		log.Errorf("telemetry configuration insert err: %s", err)
		return nil, err
	}
	return &newTelemetryConfiguration, nil
}
func RegisterTotalPipecapacity(session *xorm.Session, teleSetupId int64, pipeList []TotalPipeCapacity) error {
	newTotalPipeCapacityList := []db_models.TotalPipeCapacity{}
	for _, pipe := range pipeList {
		if pipe.Capacity > 0 {
			newTotalPipeCapacity := db_models.TotalPipeCapacity{
				TeleSetupId: teleSetupId,
				LinkId:      pipe.LinkId,
				Capacity:    pipe.Capacity,
				Unit:        ConvertUnitToString(pipe.Unit),
			}
			newTotalPipeCapacityList = append(newTotalPipeCapacityList, newTotalPipeCapacity)
		}
	}
	if len(newTotalPipeCapacityList) > 0 {
		_, err := session.Insert(&newTotalPipeCapacityList)
		if err != nil {
			log.Errorf("total pipe capacity insert err: %s", err)
			return err
		}
	} else {
		err := db_models.DeleteTelemetrySetupById(session, teleSetupId)
		if err != nil {
			log.Errorf("Delete telemetry setup error: %s", err)
			return err
		}
	}
	return nil
}
func RegisterBaseline(session *xorm.Session, teleSetupId int64, baseline Baseline) (*db_models.Baseline, error) {
	newBaseline := db_models.Baseline{
		TeleSetupId: teleSetupId,
		BaselineId:  baseline.BaselineId,
	}
	_, err := session.Insert(&newBaseline)
	if err != nil {
		log.Errorf("baseline insert err: %s", err)
		return nil, err
	}
	return &newBaseline, nil
}
func RegisterUnitConfiguration(session *xorm.Session, tConID int64, unitConfigList []UnitConfig) (err error) {
	newUnitConfigList := []db_models.UnitConfiguration{}
	for _, config := range unitConfigList {
		unit := ConvertUnitToString(config.Unit)
		newUnitConfig := db_models.CreateUnitConfiguration(tConID, unit, config.UnitStatus)
		newUnitConfigList = append(newUnitConfigList, *newUnitConfig)
	}
	if len(newUnitConfigList) > 0 {
		_, err = session.Insert(&newUnitConfigList)
		if err != nil {
			log.Errorf("unit configuration insert err: %s", err)
			return err
		}
	}
	return
}
func RegisterTelemetryPrefix(session *xorm.Session, tType string, typeId int64, prefixType string, prefixs []Prefix) error {
	newTelemetryPrefixList := []db_models.TelemetryPrefix{}
	for _, prefix := range prefixs {
		newTelemetryPrefix := db_models.TelemetryPrefix{
			Type:       tType,
			TypeId:     typeId,
			PrefixType: prefixType,
			Addr:       prefix.Addr,
			PrefixLen:  prefix.PrefixLen,
		}
		newTelemetryPrefixList = append(newTelemetryPrefixList, newTelemetryPrefix)
	}
	if len(newTelemetryPrefixList) > 0 {
		_, err := session.Insert(&newTelemetryPrefixList)
		if err != nil {
			log.Errorf("telemetry prefix insert err: %s", err)
			return err
		}
	}
	return nil
}
func RegisterTelemetryPortRange(session *xorm.Session, tType string, typeId int64, prefixType string, portRanges []PortRange) error {
	newTelemetryPortRangeList := []db_models.TelemetryPortRange{}
	for _, portRange := range portRanges {
		newTelemetryPortRange := db_models.TelemetryPortRange{
			Type:       tType,
			TypeId:     typeId,
			PrefixType: prefixType,
			LowerPort:  portRange.LowerPort,
			UpperPort:  portRange.UpperPort,
		}
		newTelemetryPortRangeList = append(newTelemetryPortRangeList, newTelemetryPortRange)
	}
	if len(newTelemetryPortRangeList) > 0 {
		_, err := session.Insert(&newTelemetryPortRangeList)
		if err != nil {
			log.Errorf("telemetry port range insert err: %s", err)
			return err
		}
	}
	return nil
}
func CreateTelemetryParameterValue(session *xorm.Session, tType string, typeId int64, protocols SetInt, fqdns SetString, uris SetString, aliasNames SetString) error {
	err := RegisterTelemetryParameterIntValue(session, tType, typeId, string(TARGET_PROTOCOL), protocols)
	if err != nil {
		return err
	}
	err = RegisterTelemetryParameterStringValue(session, tType, typeId, string(TARGET_FQDN), fqdns)
	if err != nil {
		return err
	}
	err = RegisterTelemetryParameterStringValue(session, tType, typeId, string(TARGET_URI), uris)
	if err != nil {
		return err
	}
	err = RegisterTelemetryParameterStringValue(session, tType, typeId, string(ALIAS_NAME), aliasNames)
	if err != nil {
		return err
	}
	return nil
}
func RegisterTelemetryParameterStringValue(session *xorm.Session, tType string, typeId int64, parameterType string, stringValues SetString) error {
	newTeleParameterList := []db_models.TelemetryParameterValue{}
	for _, stringValue := range stringValues.List() {
		if stringValue == "" {
			continue
		}
		newTeleParameter := db_models.TelemetryParameterValue{
			Type:          tType,
			TypeId:        typeId,
			ParameterType: parameterType,
			StringValue:   stringValue,
		}
		newTeleParameterList = append(newTeleParameterList, newTeleParameter)
	}
	if len(newTeleParameterList) > 0 {
		_, err := session.Insert(&newTeleParameterList)
		if err != nil {
			log.Errorf("telemetry parameter value insert err: %s", err)
			return err
		}
	}
	return nil
}
func RegisterTelemetryParameterIntValue(session *xorm.Session, tType string, typeId int64, parameterType string, intValues SetInt) error {
	newTeleParameterList := []db_models.TelemetryParameterValue{}
	for _, intValue := range intValues.List() {
		newTeleParameter := db_models.TelemetryParameterValue{
			Type:          tType,
			TypeId:        typeId,
			ParameterType: parameterType,
			IntValue:      intValue,
		}
		newTeleParameterList = append(newTeleParameterList, newTeleParameter)
	}
	if len(newTeleParameterList) > 0 {
		_, err := session.Insert(&newTeleParameterList)
		if err != nil {
			log.Errorf("telemetry parameter value insert err: %s", err)
			return err
		}
	}
	return nil
}
func RegisterTraffic(session *xorm.Session, tType string, prefixType string, typeId int64, trafficType string, traffics []Traffic) error {
	newTrafficList := []db_models.Traffic{}
	for _, vTraffic := range traffics {
		newTraffic := db_models.Traffic{
			Type:            tType,
			PrefixType:      prefixType,
			TypeId:          typeId,
			TrafficType:     trafficType,
			Unit:            ConvertUnitToString(vTraffic.Unit),
			LowPercentileG:  vTraffic.LowPercentileG,
			MidPercentileG:  vTraffic.MidPercentileG,
			HighPercentileG: vTraffic.HighPercentileG,
			PeakG:           vTraffic.PeakG,
		}
		newTrafficList = append(newTrafficList, newTraffic)
	}
	if len(newTrafficList) > 0 {
		_, err := session.Insert(&newTrafficList)
		if err != nil {
			log.Errorf("traffic insert err: %s", err)
			return err
		}
	}
	return nil
}
func RegisterTrafficPerProtocol(session *xorm.Session, tType string, typeId int64, trafficType string, traffics []TrafficPerProtocol) error {
	newTrafficList := []db_models.TrafficPerProtocol{}
	for _, vTraffic := range traffics {
		newTraffic := db_models.TrafficPerProtocol{
			Type:            tType,
			TypeId:          typeId,
			TrafficType:     trafficType,
			Unit:            ConvertUnitToString(vTraffic.Unit),
			Protocol:        vTraffic.Protocol,
			LowPercentileG:  vTraffic.LowPercentileG,
			MidPercentileG:  vTraffic.MidPercentileG,
			HighPercentileG: vTraffic.HighPercentileG,
			PeakG:           vTraffic.PeakG,
		}
		newTrafficList = append(newTrafficList, newTraffic)
	}
	if len(newTrafficList) > 0 {
		_, err := session.Insert(&newTrafficList)
		if err != nil {
			log.Errorf("traffic per protocol insert err: %s", err)
			return err
		}
	}
	return nil
}
func RegisterTrafficPerPort(session *xorm.Session, tType string, typeId int64, trafficType string, traffics []TrafficPerPort) error {
	newTrafficList := []db_models.TrafficPerPort{}
	for _, vTraffic := range traffics {
		newTraffic := db_models.TrafficPerPort{
			Type:            tType,
			TypeId:          typeId,
			TrafficType:     trafficType,
			Unit:            ConvertUnitToString(vTraffic.Unit),
			Port:            vTraffic.Port,
			LowPercentileG:  vTraffic.LowPercentileG,
			MidPercentileG:  vTraffic.MidPercentileG,
			HighPercentileG: vTraffic.HighPercentileG,
			PeakG:           vTraffic.PeakG,
		}
		newTrafficList = append(newTrafficList, newTraffic)
	}
	if len(newTrafficList) > 0 {
		_, err := session.Insert(&newTrafficList)
		if err != nil {
			log.Errorf("traffic per port insert err: %s", err)
			return err
		}
	}
	return nil
}
func RegisterTotalConnectionCapacity(session *xorm.Session, teleBaselineId int64, tccs []TotalConnectionCapacity) error {
	newTccList := []db_models.TotalConnectionCapacity{}
	for _, vTcc := range tccs {
		newTcc  := db_models.TotalConnectionCapacity {
			TeleBaselineId:         teleBaselineId,
			Protocol:               vTcc.Protocol,
			Connection:             vTcc.Connection,
			ConnectionClient:       vTcc.ConnectionClient,
			Embryonic:              vTcc.Embryonic,
			EmbryonicClient:        vTcc.EmbryonicClient,
			ConnectionPs:           vTcc.ConnectionPs,
			ConnectionClientPs:     vTcc.ConnectionClientPs,
			RequestPs:              vTcc.RequestPs,
			RequestClientPs:        vTcc.RequestClientPs,
			PartialRequestPs:       vTcc.PartialRequestPs,
			PartialRequestClientPs: vTcc.PartialRequestClientPs,
		}
		newTccList = append(newTccList, newTcc)
	}
	if len(newTccList) > 0 {
		_, err := session.Insert(&newTccList)
		if err != nil {
			log.Errorf("total connection capacity insert err: %s", err)
			return err
		}
	}
	return nil
}
func RegisterTotalConnectionCapacityPerPort(session *xorm.Session, teleBaselineId int64, tccs []TotalConnectionCapacityPerPort) error {
	newTccList := []db_models.TotalConnectionCapacityPerPort{}
	for _, vTcc := range tccs {
		newTcc  := db_models.TotalConnectionCapacityPerPort {
			TeleBaselineId:         teleBaselineId,
			Protocol:               vTcc.Protocol,
			Port:                   vTcc.Port,
			Connection:             vTcc.Connection,
			ConnectionClient:       vTcc.ConnectionClient,
			Embryonic:              vTcc.Embryonic,
			EmbryonicClient:        vTcc.EmbryonicClient,
			ConnectionPs:           vTcc.ConnectionPs,
			ConnectionClientPs:     vTcc.ConnectionClientPs,
			RequestPs:              vTcc.RequestPs,
			RequestClientPs:        vTcc.RequestClientPs,
			PartialRequestPs:       vTcc.PartialRequestPs,
			PartialRequestClientPs: vTcc.PartialRequestClientPs,
		}
		newTccList = append(newTccList, newTcc)
	}
	if len(newTccList) > 0 {
		_, err := session.Insert(&newTccList)
		if err != nil {
			log.Errorf("total connection capacity per port insert err: %s", err)
			return err
		}
	}
	return nil
}