func createMitigationScopeParameterValue(session *xorm.Session, mitigationScope MitigationScope, mitigationScopeId int64) (err error) {
	newFqdnList := []*db_models.ParameterValue{}
	for _, v := range mitigationScope.FQDN.List() {
		if v == "" {
			continue
		}
		newFqdn := db_models.CreateFqdnParam(v)
		newFqdn.MitigationScopeId = mitigationScopeId
		newFqdnList = append(newFqdnList, newFqdn)
	}
	if len(newFqdnList) > 0 {
		_, err = session.Insert(newFqdnList)
		if err != nil {
			session.Rollback()
			log.Printf("FQDN insert err: %s", err)
			return
		}
	}
	newUriList := []*db_models.ParameterValue{}
	for _, v := range mitigationScope.URI.List() {
		if v == "" {
			continue
		}
		newUri := db_models.CreateUriParam(v)
		newUri.MitigationScopeId = mitigationScopeId
		newUriList = append(newUriList, newUri)
	}
	if len(newUriList) > 0 {
		_, err = session.Insert(newUriList)
		if err != nil {
			session.Rollback()
			log.Printf("URI insert err: %s", err)
			return
		}
	}
	newAliasNameList := []*db_models.ParameterValue{}
	for _, v := range mitigationScope.AliasName.List() {
		if v == "" {
			continue
		}
		newAliasName := db_models.CreateAliasNameParam(v)
		newAliasName.MitigationScopeId = mitigationScopeId
		newAliasNameList = append(newAliasNameList, newAliasName)
	}
	if len(newAliasNameList) > 0 {
		_, err = session.Insert(newAliasNameList)
		if err != nil {
			session.Rollback()
			log.Printf("AliasName insert err: %s", err)
			return
		}
	}
	newTargetProtocolList := []*db_models.ParameterValue{}
	for _, v := range mitigationScope.TargetProtocol.List() {
		newTargetProtocol := db_models.CreateTargetProtocolParam(v)
		newTargetProtocol.MitigationScopeId = mitigationScopeId
		newTargetProtocolList = append(newTargetProtocolList, newTargetProtocol)
	}
	if len(newTargetProtocolList) > 0 {
		_, err = session.Insert(newTargetProtocolList)
		if err != nil {
			session.Rollback()
			log.Printf("TargetProtocol insert err: %s", err)
			return
		}
	}
	return
}
/*
Stores Set<string> and Set<int> related to a MitigationScope to the ParameterValue table in the DB.

Parameter:
session Session information
mitigationScope Mitigation Scope
mitigation_scope_id id of the parent MitigationScope
return:
err error
package models
import (
	"time"
	"strconv"
	"github.com/go-xorm/xorm"
	log "github.com/sirupsen/logrus"
	"github.com/nttdots/go-dots/libcoap"
	"github.com/nttdots/go-dots/dots_server/db_models"
	"github.com/nttdots/go-dots/dots_common/messages"
	"github.com/nttdots/go-dots/dots_common/types/data"
	"github.com/nttdots/go-dots/dots_server/db_models/data"
)
func CreateMitigationScope(mitigationScope MitigationScope, customer Customer, isIfMatchOption bool) (newMitigationScope db_models.MitigationScope, err error) {
	engine, err := ConnectDB()
	if err != nil {
		log.Errorf("database connect error: %s", err)
		return
	}
	log.Debugf("CreateMitigationScope mitigationScope=%+v\n", mitigationScope)
	dbMitigationScope := new(db_models.MitigationScope)
	clientIdentifier := mitigationScope.ClientIdentifier
	clientDomainIdentifier := mitigationScope.ClientDomainIdentifier
	_, err = engine.Where("customer_id = ? AND client_identifier = ? AND mitigation_id = ?", customer.Id, clientIdentifier, mitigationScope.MitigationId).Desc("id").Get(dbMitigationScope)
	if err != nil {
		log.Errorf("mitigation_scope select error: %s", err)
		return
	}
	if dbMitigationScope.Id != 0 {
		currentTime := time.Now()
		remainingLifetime := dbMitigationScope.Lifetime - int(currentTime.Sub(dbMitigationScope.Updated).Seconds())
		if remainingLifetime > 0 || dbMitigationScope.Lifetime == int(messages.INDEFINITE_LIFETIME){
			mitigationScope.MitigationScopeId = dbMitigationScope.Id
			err = UpdateMitigationScope(mitigationScope, customer, isIfMatchOption)
			return
		}
	}
	session := engine.NewSession()
	err = session.Begin()
	if err != nil {
		session.Rollback()
		return
	}
	if mitigationScope.Status == 0 { mitigationScope.Status = InProgress }
	newMitigationScope = db_models.MitigationScope{
		CustomerId:       customer.Id,
		ClientIdentifier: clientIdentifier,
		ClientDomainIdentifier: clientDomainIdentifier,
		MitigationId:     mitigationScope.MitigationId,
		Lifetime:         mitigationScope.Lifetime,
		Status:           mitigationScope.Status,
		TriggerMitigation: mitigationScope.TriggerMitigation,
	}
	_, err = session.Insert(&newMitigationScope)
	if err != nil {
		session.Rollback()
		log.Errorf("mitigationScope insert err: %s", err)
		return
	}
	if err = session.Commit(); err != nil {
		session.Rollback()
		log.Errorf("mitigationScope commit err: %s", err)
		return
	}
	session.Close()
	session = engine.NewSession()
	defer session.Close()
	err = session.Begin()
	if err != nil {
		session.Rollback()
		return
	}
	err = createMitigationScopeParameterValue(session, mitigationScope, newMitigationScope.Id)
	if err != nil {
		return
	}
	err = createMitigationScopePrefix(session, mitigationScope, newMitigationScope.Id)
	if err != nil {
		return
	}
	err = createMitigationScopePortRange(session, mitigationScope, newMitigationScope.Id)
	if err != nil {
		return
	}
	if !isIfMatchOption {
		err = createMitigationScopeCallHome(session, mitigationScope, newMitigationScope.Id)
		if err != nil {
			return
		}
	}
	if !isIfMatchOption {
		err = createControlFiltering(session, mitigationScope, newMitigationScope.Id)
		if err != nil {
			return
		}
	}
	err = session.Commit()
	AddActiveMitigationRequest(newMitigationScope.Id, newMitigationScope.Lifetime, newMitigationScope.Created)
	return
}
func UpdateMitigationScopeStatus(mitigationScopeId int64, status int) (err error) {
	engine, err := ConnectDB()
	if err != nil {
		log.Errorf("database connect error: %s", err)
		return
	}
	session := engine.NewSession()
	defer session.Close()
	err = session.Begin()
	if err != nil {
		session.Rollback()
		return
	}
	updMitigationScope := db_models.MitigationScope{ Status: status }
	_, err = session.Id(mitigationScopeId).Cols("status").Update(&updMitigationScope)
	if err != nil {
		session.Rollback()
		log.Errorf("mitigationScope status update err: %s", err)
		return
	}
	err = session.Commit()
	return
}
func UpdateMitigationScope(mitigationScope MitigationScope, customer Customer, isIfMatchOption bool) (err error) {
	engine, err := ConnectDB()
	if err != nil {
		log.Errorf("database connect error: %s", err)
		return
	}
	session := engine.NewSession()
	defer session.Close()
	err = session.Begin()
	if err != nil {
		session.Rollback()
		return
	}
	dbMitigationScope := new(db_models.MitigationScope)
	clientIdentifier := mitigationScope.ClientIdentifier
	if mitigationScope.MitigationScopeId == 0 {
		_, err = engine.Where("customer_id = ? AND client_identifier = ? AND mitigation_id = ?", customer.Id, clientIdentifier, mitigationScope.MitigationId).Desc("id").Get(dbMitigationScope)
	} else {
		_, err = engine.Where("id = ?", mitigationScope.MitigationScopeId).Get(dbMitigationScope)
	}
	if err != nil {
		return
	}
	if dbMitigationScope.Id == 0 {
		log.Errorf("mitigation_scope update data exist err: %s", err)
		return
	}
	updMitigationScope := db_models.MitigationScope{
		Lifetime: mitigationScope.Lifetime,
		Status:   mitigationScope.Status,
		AttackStatus: mitigationScope.AttackStatus,
		TriggerMitigation: mitigationScope.TriggerMitigation,
	}
	_, err = session.Id(dbMitigationScope.Id).Update(&updMitigationScope)
	if err != nil {
		session.Rollback()
		log.Errorf("mitigationScope update err: %s", err)
		return
	}
	_, err = session.Id(dbMitigationScope.Id).Cols("trigger-mitigation").Update(&updMitigationScope)
	if err != nil {
		session.Rollback()
		log.Errorf("mitigationScope update err: %s", err)
		return
	}
	if mitigationScope.Status != Terminated {
		err = db_models.DeleteMitigationScopeParameterValue(session, dbMitigationScope.Id)
		if err != nil {
			session.Rollback()
			log.Errorf("ParameterValue record delete err(MitigationScope.id:%d): %s", dbMitigationScope.Id, err)
			return
		}
		err = db_models.DeleteMitigationScopePrefix(session, dbMitigationScope.Id)
		if err != nil {
			session.Rollback()
			log.Errorf("Prefix record delete err(MitigationScope.id:%d): %s", dbMitigationScope.Id, err)
			return
		}
		err = db_models.DeleteMitigationScopePortRange(session, dbMitigationScope.Id)
		if err != nil {
			session.Rollback()
			log.Errorf("PortRange record delete err(MitigationScope.id:%d): %s", dbMitigationScope.Id, err)
			return
		}
		err = db_models.DeleteMitigationScopeICMPTypeRange(session, dbMitigationScope.Id)
		if err != nil {
			session.Rollback()
			log.Errorf("ICMPTypeRange record delete err(MitigationScope.id:%d): %s", dbMitigationScope.Id, err)
			return
		}
		err = db_models.DeleteControlFiltering(session, dbMitigationScope.Id)
		if err != nil {
			session.Rollback()
			log.Errorf("ControlFilteringParameter record delete err(MitigationScope.id:%d): %s", dbMitigationScope.Id, err)
			return
		}
		err = createMitigationScopeParameterValue(session, mitigationScope, dbMitigationScope.Id)
		if err != nil {
			return
		}
		err = createMitigationScopePrefix(session, mitigationScope, dbMitigationScope.Id)
		if err != nil {
			return
		}
		err = createMitigationScopePortRange(session, mitigationScope, dbMitigationScope.Id)
		if err != nil {
			return
		}
		err = createMitigationScopeCallHome(session, mitigationScope, dbMitigationScope.Id)
		if err != nil {
			return
		}
		err = createControlFiltering(session, mitigationScope, dbMitigationScope.Id)
		if err != nil {
			return
		}
		if isIfMatchOption {
			err = UpdateTelemetryTotalAttackTraffic(engine, session, mitigationScope.MitigationScopeId, mitigationScope.TelemetryTotalAttackTraffic)
			if err != nil {
				session.Rollback()
				return
			}
			err = UpdateTelemetryAttackDetail(engine, session, mitigationScope.MitigationScopeId, mitigationScope.TelemetryAttackDetail)
			if err != nil {
				session.Rollback()
				return
			}
		}
	}
	err = session.Commit()
	AddActiveMitigationRequest(dbMitigationScope.Id, updMitigationScope.Lifetime, updMitigationScope.Updated)
	return
}