Commit 6c9ff158 authored by Simon Pastor's avatar Simon Pastor
Browse files

http log used for rnis only

parent d6942582
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ go 1.12
require (
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-ctrl-engine-model v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metric-store v0.0.0-20200306214341-11d08c83c4d6
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-model v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-redis v0.0.0
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-rnis-notification-client v0.0.0
@@ -17,6 +18,7 @@ require (
replace (
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-ctrl-engine-model => ../../go-packages/meep-ctrl-engine-model
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger => ../../go-packages/meep-logger
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metric-store => ../../go-packages/meep-metric-store
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-model => ../../go-packages/meep-model
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-redis => ../../go-packages/meep-redis
	github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-rnis-notification-client => ../../go-packages/meep-rnis-notification-client
+2 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ github.com/gorilla/handlers v1.4.0/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/
github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/influxdata/influxdb1-client v0.0.0-20190809212627-fc22c7df067e h1:txQltCyjXAqVVSZDArPEhUTg35hKwVIuXwtQo7eAMNQ=
github.com/influxdata/influxdb1-client v0.0.0-20190809212627-fc22c7df067e/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mattomatic/dijkstra v0.0.0-20130617153013-6f6d134eb237/go.mod h1:UOnLAUmVG5paym8pD3C4B9BQylUDC2vXFJJpT7JrlEA=
+11 −7
Original line number Diff line number Diff line
@@ -17,8 +17,6 @@
package sbi

import (
	//"strings"

	ceModel "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-ctrl-engine-model"
	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
	mod "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-model"
@@ -31,13 +29,14 @@ type RnisSbi struct {
	activeModel          *mod.Model
	updateUeEcgiInfoCB   func(string, string, string, string)
	updateAppEcgiInfoCB  func(string, string, string, string)
	updateScenarioNameCB func(string)
	cleanUpCB            func()
}

var sbi *RnisSbi

// Init - RNI Service SBI initialization
func Init(updateUeEcgiInfo func(string, string, string, string), updateAppEcgiInfo func(string, string, string, string), cleanUp func()) (err error) {
func Init(updateUeEcgiInfo func(string, string, string, string), updateAppEcgiInfo func(string, string, string, string), updateScenarioName func(string), cleanUp func()) (err error) {

	// Create new SBI instance
	sbi = new(RnisSbi)
@@ -51,6 +50,7 @@ func Init(updateUeEcgiInfo func(string, string, string, string), updateAppEcgiIn

	sbi.updateUeEcgiInfoCB = updateUeEcgiInfo
	sbi.updateAppEcgiInfoCB = updateAppEcgiInfo
	sbi.updateScenarioNameCB = updateScenarioName
	sbi.cleanUpCB = cleanUp

	return nil
@@ -88,6 +88,10 @@ func eventHandler(channel string, payload string) {
func processActiveScenarioUpdate() {
	log.Debug("processActiveScenarioUpdate")

	// Update scenario Name that needs to be accessed by the NBI
	scenarioName := sbi.activeModel.GetScenarioName()
	sbi.updateScenarioNameCB(scenarioName)

	// Update UE info
	ueNameList := sbi.activeModel.GetNodeNames("UE")
	for _, name := range ueNameList {
+130 −5
Original line number Diff line number Diff line
@@ -24,23 +24,148 @@
package server

import (
	"bytes"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strconv"
	"strings"
	"time"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
	ms "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-metric-store"
)

var nextUniqueId int32 = 1
var redisDBAddr string = "meep-redis-master:6379"
var influxDBAddr string = "http://meep-influxdb:8086"
var scenarioName string = ""
var metricStore *ms.MetricStore

const DirectionRX = "RX"
const DirectionTX = "TX"

func InitLogger() (err error) {

	//currentStoreName located in NBI of RNIS populated by SBI upon new activation
	scenarioName = currentStoreName
	metricStore, err = ms.NewMetricStore(scenarioName, influxDBAddr, redisDBAddr)
	if err != nil {
		log.Error("Failed connection to Redis: ", err)
		return err
	}
	return nil
}

func LogTx(url string, method string, body string, resp *http.Response, startTime time.Time) {

	uniqueId := nextUniqueId
	nextUniqueId++

	responseBodyString := ""

	if resp.Body != nil {
		responseData, _ := ioutil.ReadAll(resp.Body)
		responseBodyString = string(responseData)
	}

	var metric ms.HttpMetric
	metric.LoggerName = log.GetComponentName()
	metric.Direction = DirectionTX
	metric.Id = uniqueId
	metric.Url = url
	metric.Endpoint = url //reusing the url info
	metric.Method = method
	metric.Body = body
	metric.RespBody = responseBodyString
	metric.RespCode = strconv.Itoa(resp.StatusCode)
	metric.ProcTime = strconv.Itoa(int(time.Since(startTime) / time.Microsecond))

	if currentStoreName != scenarioName {
		scenarioName = currentStoreName
		err := metricStore.SetStore(scenarioName)
		if err != nil {
			log.Error("Failed to set the store: ", err)
		}
	}

	err := metricStore.SetHttpMetric(metric)
	if err != nil {
		log.Error("Failed to set http metric: ", err)
	}

}

func Logger(inner http.Handler, name string) http.Handler {

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		start := time.Now()

		inner.ServeHTTP(w, r)
		//use a recorder to record/intercept the response
		rr := httptest.NewRecorder()

		//consume the body and store it locally
		rawBody, _ := ioutil.ReadAll(r.Body)

		// Restore the io.ReadCloser to it's original state to be consumed in ServeHTTP
		r.Body = ioutil.NopCloser(bytes.NewBuffer(rawBody))

		inner.ServeHTTP(rr, r)

		endpoint := strings.Split(r.RequestURI, "?")

		uniqueId := nextUniqueId
		nextUniqueId++

		procTime := strconv.Itoa(int(time.Since(start) / time.Microsecond))

		log.Debug(
			r.Method, " ",
			r.RequestURI, " ",
			name, " ",
			time.Since(start),
			"fields [id: ", uniqueId,
			" url: ", r.RequestURI,
			" endpoint: ", endpoint[0],
			" method: ", r.Method,
			" body: ", string(rawBody),
			" resp_body: ", rr.Body.String(),
			" resp_code: ", int32(rr.Code),
			" proc_time: ", procTime,
			"] tags [name: ", log.GetComponentName(),
			" direction: ", DirectionRX,
		)

		if currentStoreName != scenarioName {
			scenarioName = currentStoreName
			err := metricStore.SetStore(scenarioName)
			if err != nil {
				log.Error("Failed to set the store: ", err)
			}
		}

		var metric ms.HttpMetric
		metric.LoggerName = log.GetComponentName()
		metric.Direction = DirectionRX
		metric.Id = uniqueId
		metric.Url = r.RequestURI
		metric.Endpoint = endpoint[0]
		metric.Method = r.Method
		metric.Body = string(rawBody)
		metric.RespBody = rr.Body.String()
		metric.RespCode = strconv.Itoa(rr.Code)
		metric.ProcTime = procTime

		err := metricStore.SetHttpMetric(metric)
		if err != nil {
			log.Error("Failed to set http metric: ", err)
		}

		// copy everything from response recorder
		// to actual response writer
		for k, v := range rr.Result().Header {
			w.Header()[k] = v
		}
		w.WriteHeader(rr.Code)

		//writting deletes the content of the body, so log had to be done before that
		_, _ = rr.Body.WriteTo(w)
	})
}
+31 −4
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import (
	"time"

	sbi "github.com/InterDigitalInc/AdvantEDGE/go-apps/meep-rnis/sbi"
	//ceModel "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-ctrl-engine-model"
	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
	redis "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-redis"
	clientNotif "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-rnis-notification-client"
@@ -45,6 +44,7 @@ const cellChangeSubscriptionType = "cell_change"

var ccSubscriptionMap = map[int]*CellChangeSubscription{}
var subscriptionExpiryMap = map[int][]int{}
var currentStoreName = ""

var RNIS_DB = 5

@@ -87,7 +87,7 @@ func Init() (err error) {
	}()

	//sbi is the sole responsible of updating the userInfo, zoneInfo and apInfo structures
	return sbi.Init(updateUeEcgiInfo, updateAppEcgiInfo, cleanUp)
	return sbi.Init(updateUeEcgiInfo, updateAppEcgiInfo, updateStoreName, cleanUp)
}

// reInit - finds the value already in the DB to repopulate local stored info
@@ -349,31 +349,54 @@ func checkNotificationRegisteredSubscriptions(appId string, assocId *AssociateId
}

func sendCcNotification(notifyUrl string, ctx context.Context, subscriptionId string, notification clientNotif.CellChangeNotification) {

	startTime := time.Now()

	client, err := createClient(notifyUrl)
	if err != nil {
		log.Error(err)
		return
	}

	_, err = client.NotificationsApi.PostCellChangeNotification(ctx, subscriptionId, notification)
	resp, err := client.NotificationsApi.PostCellChangeNotification(ctx, subscriptionId, notification)
	if err != nil {
		log.Error(err)
		return
	}
	defer resp.Body.Close()

	jsonNotif, err := json.Marshal(notification)
	if err != nil {
		log.Error(err.Error())
	}

	LogTx(notifyUrl, "POST", string(jsonNotif), resp, startTime)
}

func sendExpiryNotification(notifyUrl string, ctx context.Context, subscriptionId string, notification clientNotif.ExpiryNotification) {

	startTime := time.Now()

	client, err := createClient(notifyUrl)
	if err != nil {
		log.Error(err)
		return
	}

	_, err = client.NotificationsApi.PostExpiryNotification(ctx, subscriptionId, notification)
	resp, err := client.NotificationsApi.PostExpiryNotification(ctx, subscriptionId, notification)
	if err != nil {
		log.Error(err)
		return
	}
	defer resp.Body.Close()

	jsonNotif, err := json.Marshal(notification)
	if err != nil {
		log.Error(err.Error())
	}

	LogTx(notifyUrl, "POST", string(jsonNotif), resp, startTime)

}

func cellChangeSubscriptionsGET(w http.ResponseWriter, r *http.Request) {
@@ -687,5 +710,9 @@ func cleanUp() {

	ccSubscriptionMap = map[int]*CellChangeSubscription{}
	subscriptionExpiryMap = map[int][]int{}
	currentStoreName = ""
}

func updateStoreName(storeName string) {
	currentStoreName = storeName
}
Loading