func (c *AppRuleController) insertOrUpdateTrafficFltrRec(appRule models.AppTrafficRule,
	appTrafficRuleRec *models.AppTrafficRuleRec, appInstanceId string) error {
	for _, filter := range appRule.AppTrafficFilter {
		trafficFilterRec := &models.TrafficFilterRec{
			TrafficFilterId: util.GenerateUniqueId(),
			AppTrafficRuleRec:  appTrafficRuleRec,
			Qci: filter.Qci,
			Dscp: filter.Dscp,
			Tc: filter.Tc,
		}
		err :=c.insertOrUpdateTrafficFltrChildRecs(filter, trafficFilterRec, appInstanceId)
		if err != nil {
			log.Error("failed to insert child records")
			return err
		}
		err = c.Db.InsertOrUpdateData(trafficFilterRec, "traffic_filter_id")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save traffic filter record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
Insert or update traffic filter record
package controllers
import (
	"encoding/json"
	"errors"
	"github.com/astaxie/beego"
	log "github.com/sirupsen/logrus"
	"mecm-apprulemgr/models"
	"mecm-apprulemgr/util"
	"net/http"
	"strings"
	"unsafe"
)
const (
	appdRule                        = "appd_rule_rec"
	appdRuleId                      = "appd_rule_id"
	failedToMarshal                 = "failed to marshal request"
	lastInsertIdNotSupported        = "LastInsertId is not supported by this driver"
	toDatabase                      = "to database."
	operation                       = "] Operation ["
	resource                        = " Resource ["
	applicationJson                 = "application/json"
)
type AppRuleController struct {
	beego.Controller
	Db Database
}
func (c *AppRuleController) HealthCheck() {
	_, _ = c.Ctx.ResponseWriter.Write([]byte("ok"))
}
func (c *AppRuleController) CreateAppRuleConfig() {
	log.Info("Application Rule Config create request received.")
	c.HandleAppRuleConfig(util.Post)
}
func (c *AppRuleController) UpdateAppRuleConfig() {
	log.Info("Application Rule Config update request received.")
	c.HandleAppRuleConfig(util.Put)
}
func (c *AppRuleController) DeleteAppRuleConfig() {
	log.Info("Application Rule Config delete request received.")
	code, err := c.ValidateRequest([]string{util.MecmTenantRole, util.MecmAdminRole}, true)
	if err != nil {
		c.handleLoggingForError(code, err.Error(), "")
		return
	}
	appInstanceId := c.Ctx.Input.Param(util.AppInstanceId)
	restClient, err := createRestClient(util.CreateAppdRuleUrl(appInstanceId), util.Delete, nil)
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, err.Error(), appInstanceId)
		return
	}
	appRuleFacade := createAppRuleFacade(restClient, appInstanceId)
	response, err := appRuleFacade.handleAppRuleRequest()
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, err.Error(), appInstanceId)
		return
	}
	tenantId := c.Ctx.Input.Param(util.TenantId)
	appdRuleRecord := &models.AppdRuleRec{
		AppdRuleId: tenantId+appInstanceId,
	}
	if response.code == http.StatusOK {
		err = c.Db.DeleteData(appdRuleRecord, appdRuleId)
		if err != nil {
			c.handleLoggingForError(util.InternalServerError, "Failed to delete app rule record for id"+
				tenantId+appInstanceId+toDatabase, appInstanceId)
			return
		}
	}
	rule := &models.StaleAppdRule{AppdRuleId: tenantId + appInstanceId, TenantId: tenantId, AppInstanceId: appInstanceId}
	err = c.Db.InsertOrUpdateData(rule, appdRuleId)
	if err != nil && err.Error() != lastInsertIdNotSupported {
		c.handleLoggingForError(util.InternalServerError, "Failed to save app rule record for id"+
			rule.AppdRuleId+toDatabase, appInstanceId)
		return
	}
	progressModelBytes, err := json.Marshal(response.progressModel)
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, util.MarshalProgressModelError, appInstanceId)
		return
	}
	c.WriteResponse(progressModelBytes, response.code)
}
func (c *AppRuleController) GetAppRuleConfig() {
	log.Info("Application Rule Config get request received.")
	code, err := c.ValidateRequest([]string{util.MecmTenantRole, util.MecmAdminRole, util.MecmGuestRole}, true)
	if err != nil {
		c.handleLoggingForError(code, err.Error(), "")
		return
	}
	appInstanceId := c.Ctx.Input.Param(util.AppInstanceId)
	restClient, err := createRestClient(util.CreateAppdRuleUrl(appInstanceId), util.Get, nil)
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, err.Error(), appInstanceId)
		return
	}
	appRuleFacade := createAppRuleFacade(restClient, appInstanceId)
	response, err := appRuleFacade.handleAppRuleGetRequest()
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, err.Error(), appInstanceId)
		return
	}
	if response.code == http.StatusOK {
		appRuleModelBytes, err := json.Marshal(response.appdrule)
		if err != nil {
			c.handleLoggingForError(util.InternalServerError, util.MarshalAppRuleModelError, appInstanceId)
			return
		}
		c.WriteResponse(appRuleModelBytes, response.code)
		return
	}
	progressModelBytes, err := json.Marshal(response.progressModel)
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, util.MarshalProgressModelError, appInstanceId)
		return
	}
	c.WriteResponse(progressModelBytes, response.code)
}
func (c *AppRuleController) handleLoggingForError(code int, errMsg string, appInstanceId string) {
	c.WriteErrorResponse(errMsg, code, appInstanceId)
	clientIp := c.Ctx.Input.IP()
	log.Info("Response message for ClientIP [" + clientIp + operation + c.Ctx.Request.Method + "]" +
		resource + c.Ctx.Input.URL() + "] Result [Failure: " + errMsg + ".]")
}
func (c *AppRuleController) WriteErrorResponse(errMsg string, code int, appInstanceId string) {
	progressModel := util.CreateOperationProgressModel(appInstanceId, util.Failure, errMsg)
	progressModelBytes, err := json.Marshal(progressModel)
	if err != nil {
		log.Error(util.MarshalProgressModelError)
		return
	}
	log.Error(errMsg)
	c.WriteResponse(progressModelBytes, code)
}
func (c *AppRuleController) WriteResponse(msg []byte, code int) {
	c.Ctx.Output.SetStatus(code)
	err := c.Ctx.Output.Body(msg)
	if err != nil {
		log.Error(util.FailedToWriteRes)
	}
}
func (c *AppRuleController) displayReceivedMsg(clientIp string) {
	log.Info("Received message from ClientIP [" + clientIp + operation + c.Ctx.Request.Method + "]" +
		resource + c.Ctx.Input.URL() + "]")
}
func (c *AppRuleController) validateApiParams() error {
	appInstanceId := c.Ctx.Input.Param(util.AppInstanceId)
	if appInstanceId != "" {
		err := util.ValidateUUID(appInstanceId)
		if err != nil {
			return errors.New(util.AppInstanceIdInvalid)
		}
	}
	tenantId := c.Ctx.Input.Param(util.TenantId)
	err := util.ValidateUUID(tenantId)
	if err != nil {
		return errors.New(util.TenantIdInvalid)
	}
	return nil
}
func (c *AppRuleController) ValidateAppRuleModel() (*models.AppdRule, error) {
	if len(c.Ctx.Input.RequestBody) > util.RequestBodyLength {
		return nil, errors.New(util.RequestBodyTooLarge)
	}
	var appRuleConfig *models.AppdRule
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &appRuleConfig); err != nil {
		log.Error("failed to unmarshal")
		return nil, errors.New(util.UnMarshalAppRuleModelError)
	}
	err := util.ValidateRestBody(appRuleConfig)
	if err != nil {
		log.Error("failed to validate rest body")
		return nil, err
	}
	return appRuleConfig, nil
}
func (c *AppRuleController) ValidateRequest(allowedRoles []string, isAppInstanceAvailable bool) (int, error) {
	clientIp := c.Ctx.Input.IP()
	err := util.ValidateIpv4Address(clientIp)
	if err != nil {
		return util.BadRequest, errors.New(util.ClientIpaddressInvalid)
	}
	c.displayReceivedMsg(clientIp)
	accessToken := c.Ctx.Request.Header.Get(util.AccessToken)
	tenantId := c.Ctx.Input.Param(util.TenantId)
	err = util.ValidateAccessToken(accessToken, allowedRoles, tenantId)
	if err != nil {
		if err.Error() == util.Forbidden {
			return util.StatusForbidden, errors.New(util.Forbidden)
		} else {
			return util.StatusUnauthorized, errors.New(util.AuthorizationFailed)
		}
	}
	bKey := *(*[]byte)(unsafe.Pointer(&accessToken))
	util.ClearByteArray(bKey)
	err = c.validateApiParams()
	if err != nil {
		return util.BadRequest, err
	}
	return 0, nil
}
func (c *AppRuleController) HandleAppRuleConfig(method string) {
	code, err := c.ValidateRequest([]string{util.MecmTenantRole, util.MecmAdminRole}, true)
	if err != nil {
		c.handleLoggingForError(code, err.Error(), "")
		return
	}
	appInstanceId := c.Ctx.Input.Param(util.AppInstanceId)
	err = util.ValidateUUID(appInstanceId)
	if err != nil {
		c.handleLoggingForError(code, err.Error(), "")
		return
	}
	appRuleConfig, err := c.ValidateAppRuleModel()
	if err != nil {
		c.handleLoggingForError(util.BadRequest, err.Error(), appInstanceId)
		return
	}
	restClient, err := createRestClient(util.CreateAppdRuleUrl(appInstanceId), method, appRuleConfig)
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, err.Error(), appInstanceId)
		return
	}
	appRuleFacade := createAppRuleFacade(restClient, appInstanceId)
	response, err := appRuleFacade.handleAppRuleRequest()
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, err.Error(), appInstanceId)
		return
	}
	origin := c.Ctx.Request.Header.Get("origin")
	originVar, err := util.ValidateName(origin, util.NameRegex)
	if err != nil || !originVar {
		c.handleLoggingForError(util.BadRequest, err.Error(), appInstanceId)
		return
	}
	syncStatus := true
	if origin == "MEPM" {
		syncStatus = false
	}
	tenantId := c.Ctx.Input.Param(util.TenantId)
	err = util.ValidateUUID(tenantId)
	if err != nil {
		c.handleLoggingForError(util.BadRequest, err.Error(), appInstanceId)
		return
	}
	appdRuleRecord := &models.AppdRuleRec{
		AppdRuleId: tenantId+appInstanceId,
	}
	_ = c.Db.DeleteData(appdRuleRecord, appdRuleId)
	appdRuleRec := &models.AppdRuleRec{
		AppdRuleId: tenantId + appInstanceId,
		TenantId: tenantId,
		AppInstanceId: appInstanceId,
		AppName:  appRuleConfig.AppName,
		AppSupportMp1: appRuleConfig.AppSupportMp1,
		SyncStatus: syncStatus,
		Origin     : origin,
	}
	err = c.Db.InsertOrUpdateData(appdRuleRec, "appd_rule_id")
	if err != nil && err.Error() != lastInsertIdNotSupported {
		c.handleLoggingForError(util.InternalServerError, "Failed to save appd rule record for id"+
			appdRuleRec.AppdRuleId+toDatabase, appInstanceId)
		return
	}
	err = c.InsertOrUpdateAppTrafficRuleRec(appRuleConfig, appdRuleRec, appInstanceId)
	if err != nil {
		log.Error("failed to insert app traffic rule record")
		return
	}
	err = c.insertOrUpdateAppDnsRuleRec(appRuleConfig, appdRuleRec, appInstanceId)
	if err != nil {
		log.Error("failed to insert dns rule record")
		return
	}
	progressModelBytes, err := json.Marshal(response.progressModel)
	if err != nil {
		c.handleLoggingForError(util.InternalServerError, util.MarshalProgressModelError, appInstanceId)
		return
	}
	c.WriteResponse(progressModelBytes, response.code)
}
func (c *AppRuleController) SynchronizeUpdatedRecords() {
	log.Info("Sync app config request received.")
	var syncUpdatedRulesRecords models.SyncUpdatedRulesRecords
	clientIp := c.Ctx.Input.IP()
	code, err := c.ValidateRequest([]string{util.MecmTenantRole, util.MecmAdminRole}, false)
	if err != nil {
		c.HandleLoggingForSyncError(clientIp, code, err.Error())
		return
	}
	appdRulesSync := c.getAppdRuleSyncInfo()
	syncUpdatedRulesRecords.AppdRuleUpdatedRecs = append(syncUpdatedRulesRecords.AppdRuleUpdatedRecs, appdRulesSync...)
	syncUpdatedRulesRecs := c.GetSyncUpdatedRulesRecs(syncUpdatedRulesRecords)
	c.SendSyncUpdatedRulesRecs(syncUpdatedRulesRecs, appdRulesSync, clientIp)
}
func (c *AppRuleController) SynchronizeDeletedRecords() {
	log.Info("Sync deleted app rule records request received.")
	var staleRules []models.StaleAppdRule
	var syncDeletedRulesRecords models.SyncDeletedRulesRecords
	clientIp := c.Ctx.Input.IP()
	err := util.ValidateIpv4Address(clientIp)
	if err != nil {
		c.WriteSyncErrorResponse("invalid ip address", util.BadRequest)
		return
	}
	code, err := c.ValidateRequest([]string{util.MecmTenantRole, util.MecmAdminRole}, false)
	if err != nil {
		c.HandleLoggingForSyncError(clientIp, code, err.Error())
		return
	}
	_, _ = c.Db.QueryTable("stale_appd_rule", &staleRules, "tenant_id", c.Ctx.Input.Param(util.TenantId))
	syncDeletedRulesRecords.AppdRuleDeletedRecs = append(syncDeletedRulesRecords.AppdRuleDeletedRecs, staleRules...)
	appRuleModelBytes, err := json.Marshal(syncDeletedRulesRecords)
	if err != nil {
		c.WriteSyncErrorResponse(failedToMarshal, util.BadRequest)
		return
	}
	c.Ctx.ResponseWriter.Header().Set("Content-Type", applicationJson)
	c.Ctx.ResponseWriter.Header().Set("Accept", applicationJson)
	_, _ = c.Ctx.ResponseWriter.Write(appRuleModelBytes)
	for _, staleRule := range staleRules {
		err = c.Db.DeleteData(&staleRule, appdRuleId)
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.HandleLoggingForSyncError(clientIp, util.InternalServerError, "Failed to delete stale data in " +
				"database with error: ." + err.Error())
			return
		}
	}
}
func (c *AppRuleController) WriteSyncErrorResponse(errMsg string, code int) {
	log.Error(errMsg)
	c.writeSyncResponse(errMsg, code)
}
func (c *AppRuleController) writeSyncResponse(msg string, code int) {
	c.Data["json"] = msg
	c.Ctx.ResponseWriter.WriteHeader(code)
	c.ServeJSON()
}
func (c *AppRuleController) HandleLoggingForSyncError(clientIp string, code int, errMsg string) {
	c.WriteSyncErrorResponse(errMsg, code)
	log.Info("Response message for ClientIP [" + clientIp + operation + c.Ctx.Request.Method + "]" +
		resource + c.Ctx.Input.URL() + "] Result [Failure: " + errMsg + ".]")
}
func (c *AppRuleController) InsertSrcAddressRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, srcAddress := range filter.SrcAddress {
		srcAddressRec := &models.SrcAddress{
			SrcAddress: srcAddress,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(srcAddressRec, "src_address")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save src address record record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertSrcPortRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, srcPort := range filter.SrcPort {
		srcPortRec := &models.SrcPort{
			SrcPort: srcPort,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(srcPortRec, "src_port")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save src port record record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertDstAddressRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, dstAddress := range filter.DstAddress {
		dstAddressRec := &models.DstAddress{
			DstAddress: dstAddress,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(dstAddressRec, "dst_address")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save dst address record record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertDstPortRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, dstPort := range filter.DstPort {
		dstPortRec := &models.DstPort{
			DstPort: dstPort,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(dstPortRec, "dst_port")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save dst port record record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertProtocolRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, protocol := range filter.Protocol {
		protocolRec := &models.Protocol{
			Protocol: protocol,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(protocolRec, "protocol")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save protocol record record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertTagRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, tag := range filter.Tag {
		tagRec := &models.Tag{
			Tag: tag,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(tagRec, "tag")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save tag record record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertSrcTunnelAddressRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, srcTunnelAddr := range filter.SrcTunnelAddress {
		srcTunnelAddrRec := &models.SrcTunnelAddress{
			SrcTunnelAddress: srcTunnelAddr,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(srcTunnelAddrRec, "src_tunnel_address")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save src tunnel address record record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertDstTunnelAddressRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, dstTunnelAddress := range filter.DstTunnelAddress {
		dstTunnelAddressRec := &models.DstTunnelAddress{
			DstTunnelAddress: dstTunnelAddress,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(dstTunnelAddressRec, "dst_tunnel_address")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save dst tunnel address record record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertSrcTunnelPortRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, srcTunnelPort := range filter.SrcTunnelPort {
		srcTunnelPortRec := &models.SrcTunnelPort{
			SrcTunnelPort: srcTunnelPort,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(srcTunnelPortRec, "src_tunnel_port")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save src tunnel port record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertDstTunnelPortRec(filter models.TrafficFilter, trafficFilterRec *models.TrafficFilterRec,
	appInstanceId string) error {
	for _, dstTunnelPort := range filter.DstTunnelPort {
		dstTunnelPortRec := &models.DstTunnelPort{
			DstTunnelPort: dstTunnelPort,
			TrafficFilterRec: trafficFilterRec,
		}
		err := c.Db.InsertOrUpdateData(dstTunnelPortRec, "dst_tunnel_port")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save dst tunnel port record for id"+
				filter.TrafficFilterId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) InsertOrUpdateAppTrafficRuleRec(appRuleConfig *models.AppdRule,
	appdRuleRec *models.AppdRuleRec, appInstanceId string) error {
	for _, appRule := range appRuleConfig.AppTrafficRule {
		appTrafficRuleRec := &models.AppTrafficRuleRec{
			TrafficRuleId: appRule.TrafficRuleId,
			FilterType: appRule.FilterType,
			Priority: appRule.Priority,
			Action:  appRule.Action,
			AppdRule: appdRuleRec,
		}
		err := c.insertOrUpdateTrafficFltrRec(appRule, appTrafficRuleRec, appInstanceId)
		if err != nil {
			return err
		}
		err = c.insertOrUpdateDstInterfaceRec(appRule, appTrafficRuleRec, appInstanceId)
		if err != nil {
			return err
		}
		err = c.Db.InsertOrUpdateData(appTrafficRuleRec, "traffic_rule_id")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save traffic rule record for id"+
				appRule.TrafficRuleId+toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertOrUpdateAppDnsRuleRec(appRuleConfig *models.AppdRule,
	appdRuleRec *models.AppdRuleRec, appInstanceId string) error {
	for _, appDnsRule := range appRuleConfig.AppDnsRule {
		appDnsRuleRec := &models.AppDnsRuleRec{
			DnsRuleId:appDnsRule.DnsRuleId,
			DomainName:appDnsRule.DomainName,
			IpAddressType: appDnsRule.IpAddressType,
			IpAddress     : appDnsRule.IpAddress,
			TTL      :appDnsRule.TTL,
			AppdRule: appdRuleRec,
		}
		err := c.Db.InsertOrUpdateData(appDnsRuleRec, "dns_rule_id")
		if err != nil && err.Error() != lastInsertIdNotSupported {
			c.handleLoggingForError(util.InternalServerError, "Failed to save dns rule record for id"+
				appDnsRule.DnsRuleId +toDatabase, appInstanceId)
			return err
		}
	}
	return nil
}
func (c *AppRuleController) insertOrUpdateTrafficFltrChildRecs(filter models.TrafficFilter,
	trafficFilterRec *models.TrafficFilterRec, appInstanceId string) error {
	err := c.InsertSrcAddressRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertSrcPortRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertDstAddressRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertDstPortRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertProtocolRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertTagRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertSrcTunnelAddressRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertDstTunnelAddressRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertSrcTunnelPortRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	err = c.insertDstTunnelPortRec(filter, trafficFilterRec, appInstanceId)
	if err != nil {
		return err
	}
	return nil
}