X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=examples%2Fexample-xapp.go;h=714cd37cbd0f652cd50f53aa9161da7637f7d79e;hb=3895a8c8775ef96652e6473414fdd7366c59f404;hp=c23ebc1e7f3e2be9d699acbf2bb02a34a9ab5845;hpb=4896a40dd504821403f835a1292d8897c2b63ee7;p=ric-plt%2Fxapp-frame.git diff --git a/examples/example-xapp.go b/examples/example-xapp.go index c23ebc1..714cd37 100755 --- a/examples/example-xapp.go +++ b/examples/example-xapp.go @@ -18,19 +18,106 @@ */ package main -import "gitlabe1.ext.net.nokia.com/ric_dev/nokia-xapps/xapp/pkg/xapp" +import ( + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" + "net/http" +) -type MessageCounter struct { +// This could be defined in types.go +type ExampleXapp struct { + msgChan chan *xapp.RMRParams + stats map[string]xapp.Counter + appReady bool + rmrReady bool } -func (m MessageCounter) Consume(mtype, len int, payload []byte) (err error) { - xapp.Logger.Debug("Message received - type=%d len=%d", mtype, len) +func (e *ExampleXapp) handleRICIndication(ranName string, r *xapp.RMRParams) { + // Just update metrics and store RMR message payload to SDL + e.stats["E2APIndicationsRx"].Inc() - xapp.Sdl.Store("myKey", payload) - xapp.Rmr.Send(10005, len, payload) - return nil + xapp.Sdl.Store("myKey", r.Payload) +} + +func (e *ExampleXapp) handleRICExampleMessage(ranName string, r *xapp.RMRParams) { + // Just update metrics and echo the message back (update the message type) + e.stats["RICExampleMessageRx"].Inc() + + r.Mtype = r.Mtype + 1 + if ok := xapp.Rmr.SendMsg(r); !ok { + xapp.Logger.Info("Rmr.SendMsg failed ...") + } +} + +func (e *ExampleXapp) messageLoop() { + for { + msg := <-e.msgChan + id := xapp.Rmr.GetRicMessageName(msg.Mtype) + defer xapp.Rmr.Free(msg.Mbuf) + + xapp.Logger.Info("Message received: name=%s meid=%s subId=%d txid=%s len=%d", id, msg.Meid.RanName, msg.SubId, msg.Xid, msg.PayloadLen) + + switch id { + case "RIC_INDICATION": + e.handleRICIndication(msg.Meid.RanName, msg) + case "RIC_EXAMPLE_MESSAGE": + e.handleRICExampleMessage(msg.Meid.RanName, msg) + default: + xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype) + } + } +} + +func (e *ExampleXapp) Consume(rp *xapp.RMRParams) (err error) { + e.msgChan <- rp + return +} + +func (u *ExampleXapp) TestRestHandler(w http.ResponseWriter, r *http.Request) { + xapp.Logger.Info("TestRestHandler called!") +} + +func (u *ExampleXapp) ConfigChangeHandler(f string) { + xapp.Logger.Info("Config file changed, do something meaningful!") +} + +func (u *ExampleXapp) StatusCB() bool { + xapp.Logger.Info("Status callback called, do something meaningful!") + return true +} + +func (e *ExampleXapp) Run() { + // Set MDC (read: name visible in the logs) + xapp.Logger.SetMdc("example-xapp", "0.1.2") + + // Register various callback functions for application management + xapp.SetReadyCB(func(d interface{}) { e.rmrReady = true }, true) + xapp.AddConfigChangeListener(e.ConfigChangeHandler) + xapp.Resource.InjectStatusCb(e.StatusCB) + + // Inject own REST handler for testing purpose + xapp.Resource.InjectRoute("/ric/v1/testing", e.TestRestHandler, "POST") + + go e.messageLoop() + xapp.Run(e) +} + +func GetMetricsOpts() []xapp.CounterOpts { + return []xapp.CounterOpts{ + {Name: "RICIndicationsRx", Help: "The total number of RIC inidcation events received"}, + {Name: "RICExampleMessageRx", Help: "The total number of RIC example messages received"}, + } +} + +func NewExampleXapp(appReady, rmrReady bool) *ExampleXapp { + metrics := GetMetricsOpts() + return &ExampleXapp{ + msgChan: make(chan *xapp.RMRParams), + stats: xapp.Metric.RegisterCounterGroup(metrics, "ExampleXapp"), + rmrReady: rmrReady, + appReady: appReady, + } } func main() { - xapp.Run(MessageCounter{}) + NewExampleXapp(true, false).Run() }