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
}
Registed baseline to DB
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
}