func NewTelemetryTopTalker(ttRequest messages.TelemetryTopTalker) (talkerList []TelemetryTopTalker, err error) {
	talkerList = []TelemetryTopTalker{}
	for _, v := range ttRequest.Talker {
		talker := TelemetryTopTalker{}
		if v.SpoofedStatus != nil {
			talker.SpoofedStatus = *v.SpoofedStatus
		} else {
			talker.SpoofedStatus = false
		}
		talker.SourcePrefix, err = NewPrefix(*v.SourcePrefix)
		if err != nil {
			errMsg := fmt.Sprintf("%+v: %+v", ValidationError, err)
			log.Error("%+v", errMsg)
			return nil, errors.New(errMsg)
		}
		for _, portRange := range v.SourcePortRange {
			if portRange.LowerPort == nil {
				errMsg := "Missing required 'lower-port' attribute"
				log.Error(errMsg)
				return nil, errors.New(errMsg)
			}
			lowerPort := *portRange.LowerPort
			upperPort := *portRange.LowerPort
			if portRange.UpperPort != nil && *portRange.LowerPort > *portRange.UpperPort {
				errMsg := "'upper-port' MUST greater than 'lower-port'"
				log.Error(errMsg)
				return nil, errors.New(errMsg)
			} else if portRange.UpperPort != nil && *portRange.LowerPort <= *portRange.UpperPort {
				upperPort = *portRange.UpperPort
			}
			talker.SourcePortRange = append(talker.SourcePortRange, PortRange{LowerPort: lowerPort, UpperPort: upperPort})
		}
		for _, icmpTypeRange := range v.SourceIcmpTypeRange {
			if icmpTypeRange.LowerType == nil {
				errMsg := "Missing required 'lower-type' attribute"
				log.Error(errMsg)
				return nil, errors.New(errMsg)
			}
			lowerType := *icmpTypeRange.LowerType
			upperType := *icmpTypeRange.LowerType
			if icmpTypeRange.UpperType != nil && *icmpTypeRange.LowerType > *icmpTypeRange.UpperType {
				errMsg := "'upper-type' MUST greater than 'lower-type'"
				log.Error(errMsg)
				return nil, errors.New(errMsg)
			} else if icmpTypeRange.UpperType != nil && *icmpTypeRange.LowerType < *icmpTypeRange.UpperType {
				upperType = *icmpTypeRange.UpperType
			}
			talker.SourceIcmpTypeRange = append(talker.SourceIcmpTypeRange, ICMPTypeRange{LowerType: lowerType, UpperType: upperType})
		}
		if v.TotalAttackTraffic != nil {
			talker.TotalAttackTraffic, err = NewTelemetryTotalAttackTraffic(v.TotalAttackTraffic)
			if err != nil {
				return nil, err
			}
		}
		if v.TotalAttackConnection != nil {
			tac := TelemetryTotalAttackConnection{}
			if v.TotalAttackConnection.LowPercentileC != nil{
				tac.LowPercentileC = NewConnectionPercentile(*v.TotalAttackConnection.LowPercentileC)
			}
			if v.TotalAttackConnection.MidPercentileC != nil{
				tac.MidPercentileC = NewConnectionPercentile(*v.TotalAttackConnection.MidPercentileC)
			}
			if v.TotalAttackConnection.HighPercentileC != nil{
				tac.HighPercentileC = NewConnectionPercentile(*v.TotalAttackConnection.HighPercentileC)
			}
			if v.TotalAttackConnection.PeakC != nil{
				tac.PeakC = NewConnectionPercentile(*v.TotalAttackConnection.PeakC)
			}
			talker.TotalAttackConnection = tac
		}
		talkerList = append (talkerList, talker)
	}
	return
}
New telemetry top talker
package models
import (
	"fmt"
	"errors"
	"github.com/nttdots/go-dots/dots_common/messages"
	log "github.com/sirupsen/logrus"
	types "github.com/nttdots/go-dots/dots_common/types/data"
)
type TelemetryPreMitigation struct {
	Cuid                       string
	Cdid                       string
	Tmid                       int
	Targets                    Targets
	TotalTraffic               []Traffic
	TotalTrafficProtocol       []TrafficPerProtocol
	TotalTrafficPort           []TrafficPerPort
	TotalAttackTraffic         []Traffic
	TotalAttackTrafficProtocol []TrafficPerProtocol
	TotalAttackTrafficPort     []TrafficPerPort
	TotalAttackConnection      TotalAttackConnection
	TotalAttackConnectionPort  TotalAttackConnectionPort
	AttackDetail               []AttackDetail
}
type UriFilteringTelemetryPreMitigation struct {
	Cuid       string
	Cdid       string
	Tmid       int
	TargetList []Target
}
type Targets struct {
	TargetPrefix    []Prefix
	TargetPortRange []PortRange
	TargetProtocol  SetInt
	FQDN            SetString
	URI             SetString
	AliasName       SetString
	TargetList      []Target
}
type TotalAttackConnection struct {
	LowPercentileL  []ConnectionProtocolPercentile
	MidPercentileL  []ConnectionProtocolPercentile
	HighPercentileL []ConnectionProtocolPercentile
	PeakL           []ConnectionProtocolPercentile
}
type TotalAttackConnectionPort struct {
	LowPercentileL  []ConnectionProtocolPortPercentile
	MidPercentileL  []ConnectionProtocolPortPercentile
	HighPercentileL []ConnectionProtocolPortPercentile
	PeakL           []ConnectionProtocolPortPercentile
}
type AttackDetail struct {
	VendorId       int
	AttackId       int
	AttackName     string
	AttackSeverity int
	StartTime      int
	EndTime        int
	SourceCount    SourceCount
	TopTalker      []TopTalker
}
type ConnectionProtocolPercentile struct {
	Protocol         int
	Connection       int
	Embryonic        int
	ConnectionPs     int
	RequestPs        int
	PartialRequestPs int
}
type ConnectionProtocolPortPercentile struct {
	Protocol         int
	Port             int
	Connection       int
	Embryonic        int
	ConnectionPs     int
	RequestPs        int
	PartialRequestPs int
}
type SourceCount struct {
	LowPercentileG  int
	MidPercentileG  int
	HighPercentileG int
	PeakG           int
}
type TopTalker struct {
	SpoofedStatus         bool
	SourcePrefix          Prefix
	SourcePortRange       []PortRange
	SourceIcmpTypeRange   []ICMPTypeRange
	TotalAttackTraffic    []Traffic
	TotalAttackConnection TotalAttackConnection
}
type TelemetryTotalAttackConnection struct {
	LowPercentileC  ConnectionPercentile
	MidPercentileC  ConnectionPercentile
	HighPercentileC ConnectionPercentile
	PeakC           ConnectionPercentile
}
type ConnectionPercentile struct {
	Connection       int
	Embryonic        int
	ConnectionPs     int
	RequestPs        int
	PartialRequestPs int
}
type TelemetryAttackDetail struct {
	VendorId       int
	AttackId       int
	AttackName     string
	AttackSeverity int
	StartTime      int
	EndTime        int
	SourceCount    SourceCount
	TopTalker      []TelemetryTopTalker
}
type TelemetryTopTalker struct {
	SpoofedStatus         bool
	SourcePrefix          Prefix
	SourcePortRange       []PortRange
	SourceIcmpTypeRange   []ICMPTypeRange
	TotalAttackTraffic    []Traffic
	TotalAttackConnection TelemetryTotalAttackConnection
}
type AttackSeverity int
const (
	None AttackSeverity = iota + 1
	Low
	Medium
	High
	Unknown
)
type QueryType int
const (
	TargetPrefix QueryType = iota + 1
	TargetPort
	TargetProtocol
	TargetFqdn
	TargetUri
	TargetAlias
	Mid
	SourcePrefix
	SourcePort
	SourceIcmpType
	Content
)
func NewTelemetryPreMitigation(customer *Customer, cuid string, dataRequest messages.PreOrOngoingMitigation, aliases types.Aliases) (preMitigation *TelemetryPreMitigation, err error) {
	preMitigation = &TelemetryPreMitigation{}
	preMitigation.Targets, err = NewTarget(customer, cuid, dataRequest.Target, aliases)
	if err != nil {
		return
	}
	preMitigation.TotalTraffic = NewTraffic(dataRequest.TotalTraffic)
	preMitigation.TotalTrafficProtocol = NewTrafficPerProtocol(dataRequest.TotalTrafficProtocol)
	preMitigation.TotalTrafficPort = NewTrafficPerPort(dataRequest.TotalTrafficPort)
	preMitigation.TotalAttackTraffic = NewTraffic(dataRequest.TotalAttackTraffic)
	preMitigation.TotalAttackTrafficProtocol = NewTrafficPerProtocol(dataRequest.TotalAttackTrafficProtocol)
	preMitigation.TotalAttackTrafficPort = NewTrafficPerPort(dataRequest.TotalAttackTrafficPort)
	if dataRequest.TotalAttackConnection != nil {
		preMitigation.TotalAttackConnection = NewTotalAttackConnection(*dataRequest.TotalAttackConnection)
	}
	if dataRequest.TotalAttackConnectionPort != nil {
		preMitigation.TotalAttackConnectionPort = NewTotalAttackConnectionPerPort(*dataRequest.TotalAttackConnectionPort)
	}
	preMitigation.AttackDetail, err = NewAttackDetail(dataRequest.AttackDetail)
	if err != nil {
		return
	}
	return
}
func NewTarget(customer *Customer, cuid string, targetRequest *messages.Target, aliases types.Aliases) (target Targets, err error) {
	target = Targets{make([]Prefix, 0),make([]PortRange, 0),NewSetInt(),NewSetString(),NewSetString(),NewSetString(), make([]Target, 0)}
	target.TargetPrefix, err = NewTelemetryPrefix(targetRequest.TargetPrefix)
	if err != nil {
		return
	}
	target.TargetPortRange = NewTargetPortRange(targetRequest.TargetPortRange)
	target.TargetProtocol.AddList(targetRequest.TargetProtocol)
	target.FQDN.AddList(targetRequest.FQDN)
	target.URI.AddList(targetRequest.URI)
	target.AliasName.AddList(targetRequest.AliasName)
	target.TargetList, err = GetTelemetryTargetList(target.TargetPrefix, target.FQDN, target.URI)
	if err != nil {
		log.Errorf ("Failed to get telemetry target list. Error: %+v", err)
		return
	}
	aliasTargetList, err := GetAliasDataAsTargetList(aliases)
	if err != nil {
		log.Errorf ("Failed to get alias data as target list. Error: %+v", err)
		return
	}
	target.TargetList = append(target.TargetList, aliasTargetList...)
	return
}
func GetAliasDataAsTargetList(aliases types.Aliases) (targetList []Target, err error) {
	var fqdnList, uriList SetString
	prefixList := []Prefix{}
	for _, alias :=  range aliases.Alias {
		for _, prefix := range alias.TargetPrefix {
			targetPrefix, err := NewPrefix(prefix.String())
			if err != nil {
				return nil, err
			}
			prefixList = append(prefixList, targetPrefix)
		}
		fqdnList.AddList(alias.TargetFQDN)
		uriList.AddList(alias.TargetURI)
	}
	targetList, err = GetTelemetryTargetList(prefixList, fqdnList, uriList)
	if err != nil {
		return nil, err
	}
	return targetList, nil
}
func NewTotalAttackConnection(tacRequest messages.TotalAttackConnection) (tac TotalAttackConnection) {
	tac = TotalAttackConnection{}
	if tacRequest.LowPercentileL != nil {
		tac.LowPercentileL = NewConnectionProtocolPercentile(tacRequest.LowPercentileL)
	}
	if tacRequest.MidPercentileL != nil {
		tac.MidPercentileL = NewConnectionProtocolPercentile(tacRequest.MidPercentileL)
	}
	if tacRequest.HighPercentileL != nil {
		tac.HighPercentileL = NewConnectionProtocolPercentile(tacRequest.HighPercentileL)
	}
	if tacRequest.PeakL != nil {
		tac.PeakL = NewConnectionProtocolPercentile(tacRequest.PeakL)
	}
	return
}
func NewTotalAttackConnectionPerPort(tacRequest messages.TotalAttackConnectionPort) (tac TotalAttackConnectionPort) {
	tac = TotalAttackConnectionPort{}
	if tacRequest.LowPercentileL != nil {
		tac.LowPercentileL = NewConnectionProtocolPortPercentile(tacRequest.LowPercentileL)
	}
	if tacRequest.MidPercentileL != nil {
		tac.MidPercentileL = NewConnectionProtocolPortPercentile(tacRequest.MidPercentileL)
	}
	if tacRequest.HighPercentileL != nil {
		tac.HighPercentileL = NewConnectionProtocolPortPercentile(tacRequest.HighPercentileL)
	}
	if tacRequest.PeakL != nil {
		tac.PeakL = NewConnectionProtocolPortPercentile(tacRequest.PeakL)
	}
	return
}
func NewAttackDetail(adRequests []messages.AttackDetail) (attackDetailList []AttackDetail, err error) {
	attackDetailList = []AttackDetail{}
	for _, adRequest := range adRequests {
		attackDetail := AttackDetail{}
		if adRequest.VendorId != nil {
			attackDetail.VendorId = int(*adRequest.VendorId)
		}
		if adRequest.AttackId != nil {
			attackDetail.AttackId = int(*adRequest.AttackId)
		}
		if adRequest.AttackName != nil {
			attackDetail.AttackName = *adRequest.AttackName
		}
		if adRequest.AttackSeverity != nil {
			attackDetail.AttackSeverity = int(*adRequest.AttackSeverity)
		} else {
			attackDetail.AttackSeverity = int(None)
		}
		if adRequest.StartTime != nil {
			attackDetail.StartTime = int(*adRequest.StartTime)
		}
		if adRequest.EndTime != nil {
			attackDetail.EndTime = int(*adRequest.EndTime)
		}
		if adRequest.TopTalKer != nil {
			attackDetail.TopTalker, err = NewTopTalker(*adRequest.TopTalKer)
			if err != nil {
				return
			}
		}
		attackDetailList = append(attackDetailList, attackDetail)
	}
	return
}
func NewConnectionProtocolPercentile(cppRequest []messages.ConnectionProtocolPercentile) (cppList []ConnectionProtocolPercentile) {
	cppList = []ConnectionProtocolPercentile{}
	for _, v := range cppRequest {
		cpp := ConnectionProtocolPercentile{}
		cpp.Protocol = int(*v.Protocol)
		if v.Connection != nil {
			cpp.Connection = int(*v.Connection)
		}
		if v.Embryonic != nil {
			cpp.Embryonic = int(*v.Embryonic)
		}
		if v.ConnectionPs != nil {
			cpp.ConnectionPs = int(*v.ConnectionPs)
		}
		if v.RequestPs != nil {
			cpp.RequestPs = int(*v.RequestPs)
		}
		if v.PartialRequestPs != nil {
			cpp.PartialRequestPs = int(*v.PartialRequestPs)
		}
		cppList = append(cppList, cpp)
	}
	return
}
func NewConnectionProtocolPortPercentile(cppRequest []messages.ConnectionProtocolPortPercentile) (cppList []ConnectionProtocolPortPercentile) {
	cppList = []ConnectionProtocolPortPercentile{}
	for _, v := range cppRequest {
		cpp := ConnectionProtocolPortPercentile{}
		cpp.Protocol = int(*v.Protocol)
		cpp.Port = *v.Port
		if v.Connection != nil {
			cpp.Connection = int(*v.Connection)
		}
		if v.Embryonic != nil {
			cpp.Embryonic = int(*v.Embryonic)
		}
		if v.ConnectionPs != nil {
			cpp.ConnectionPs = int(*v.ConnectionPs)
		}
		if v.RequestPs != nil {
			cpp.RequestPs = int(*v.RequestPs)
		}
		if v.PartialRequestPs != nil {
			cpp.PartialRequestPs = int(*v.PartialRequestPs)
		}
		cppList = append(cppList, cpp)
	}
	return
}
func NewSourceCount(scRequest messages.SourceCount) (sourceCount SourceCount) {
	sourceCount = SourceCount{}
	if scRequest.LowPercentileG != nil {
		sourceCount.LowPercentileG = int(*scRequest.LowPercentileG)
	}
	if scRequest.MidPercentileG != nil {
		sourceCount.MidPercentileG = int(*scRequest.MidPercentileG)
	}
	if scRequest.HighPercentileG != nil {
		sourceCount.HighPercentileG = int(*scRequest.HighPercentileG)
	}
	if scRequest.PeakG != nil {
		sourceCount.PeakG = int(*scRequest.PeakG)
	}
	return
}
func NewTopTalker(ttRequest messages.TopTalker) (talkerList []TopTalker, err error) {
	talkerList = []TopTalker{}
	for _, v := range ttRequest.Talker {
		talker := TopTalker{}
		if v.SpoofedStatus != nil {
			talker.SpoofedStatus = *v.SpoofedStatus
		} else {
			talker.SpoofedStatus = false
		}
		talker.SourcePrefix, err = NewPrefix(*v.SourcePrefix)
		if err != nil {
			errMsg := fmt.Sprintf("%+v: %+v", ValidationError, err)
			log.Error("%+v", errMsg)
			return nil, errors.New(errMsg)
		}
		for _, portRange := range v.SourcePortRange {
			lowerPort := portRange.LowerPort
			upperPort := portRange.LowerPort
			if portRange.UpperPort != nil {
				upperPort = portRange.UpperPort
			}
			talker.SourcePortRange = append(talker.SourcePortRange, PortRange{LowerPort: *lowerPort, UpperPort: *upperPort})
		}
		for _, typeRange := range v.SourceIcmpTypeRange {
			lowerType := typeRange.LowerType
			upperType := typeRange.LowerType
			if typeRange.UpperType != nil {
				upperType = typeRange.UpperType
			}
			talker.SourceIcmpTypeRange = append(talker.SourceIcmpTypeRange, ICMPTypeRange{LowerType: *lowerType, UpperType: *upperType})
		}
		if v.TotalAttackTraffic != nil {
			talker.TotalAttackTraffic = NewTraffic(v.TotalAttackTraffic)
		}
		if v.TotalAttackConnection != nil {
			talker.TotalAttackConnection = NewTotalAttackConnection(*v.TotalAttackConnection)
		}
		talkerList = append (talkerList, talker)
	}
	return
}
func NewTelemetryTotalAttackTraffic(teleTraffics []messages.TelemetryTraffic) (trafficList []Traffic, err error) {
	trafficList = make([]Traffic, len(teleTraffics))
	for k, v := range teleTraffics {
		traffic := Traffic{}
		_, errMsg := ValidateUnit(v.Unit)
		if errMsg != "" {
			log.Errorf(errMsg)
			return nil, errors.New(errMsg)
		}
		traffic.Unit = *v.Unit
		if v.LowPercentileG != nil {
			traffic.LowPercentileG = int(*v.LowPercentileG)
		}
		if v.MidPercentileG != nil {
			traffic.MidPercentileG = int(*v.MidPercentileG)
		}
		if v.HighPercentileG != nil {
			traffic.HighPercentileG = int(*v.HighPercentileG)
		}
		if v.PeakG != nil {
			traffic.PeakG = int(*v.PeakG)
		}
		trafficList[k] = traffic
	}
	return trafficList, nil
}
func NewTelemetryAttackDetail(adRequests []messages.TelemetryAttackDetail) (attackDetailList []TelemetryAttackDetail, err error) {
	attackDetailList = []TelemetryAttackDetail{}
	for _, adRequest := range adRequests {
		attackDetail := TelemetryAttackDetail{}
		if adRequest.VendorId != nil {
			attackDetail.VendorId = int(*adRequest.VendorId)
		} else {
			errMsg := "Missing required 'vendor-id' attribute"
			log.Error(errMsg)
			return nil, errors.New(errMsg)
		}
		if adRequest.AttackId != nil {
			attackDetail.AttackId = int(*adRequest.AttackId)
		} else {
			errMsg := "Missing required 'attack-id' attribute"
			log.Error(errMsg)
			return nil, errors.New(errMsg)
		}
		if adRequest.AttackName != nil {
			attackDetail.AttackName = *adRequest.AttackName
		}
		if adRequest.AttackSeverity != nil {
			if adRequest.AttackSeverity != nil && *adRequest.AttackSeverity != int(None) && *adRequest.AttackSeverity != int(Low) && *adRequest.AttackSeverity != int(Medium) &&
			*adRequest.AttackSeverity != int(High) && *adRequest.AttackSeverity != int(Unknown) {
				errMsg := fmt.Sprintf("Invalid 'attack-severity' value %+v. Expected values include 1:None, 2:Low, 3:Medium, 4:High, 5:Unknown", *adRequest.AttackSeverity)
				log.Error(errMsg)
				return nil, errors.New(errMsg)
			}
			attackDetail.AttackSeverity = int(*adRequest.AttackSeverity)
		} else {
			attackDetail.AttackSeverity = int(None)
		}
		if adRequest.StartTime != nil {
			attackDetail.StartTime = int(*adRequest.StartTime)
		}
		if adRequest.EndTime != nil {
			attackDetail.EndTime = int(*adRequest.EndTime)
		}
		if adRequest.SourceCount != nil {
			attackDetail.SourceCount = NewTelemetrySourceCount(*adRequest.SourceCount)
		}
		if adRequest.TopTalKer != nil {
			attackDetail.TopTalker, err = NewTelemetryTopTalker(*adRequest.TopTalKer)
			if err != nil {
				return
			}
		}
		attackDetailList = append(attackDetailList, attackDetail)
	}
	return attackDetailList, nil
}
func NewTelemetrySourceCount(scRequest messages.TelemetrySourceCount) (sourceCount SourceCount) {
	sourceCount = SourceCount{}
	if scRequest.LowPercentileG != nil {
		sourceCount.LowPercentileG = int(*scRequest.LowPercentileG)
	}
	if scRequest.MidPercentileG != nil {
		sourceCount.MidPercentileG = int(*scRequest.MidPercentileG)
	}
	if scRequest.HighPercentileG != nil {
		sourceCount.HighPercentileG = int(*scRequest.HighPercentileG)
	}
	if scRequest.PeakG != nil {
		sourceCount.PeakG = int(*scRequest.PeakG)
	}
	return
}