blob: 8fe78738046ebfdd5bd5beb7fd9e2cfea3811803 [file] [log] [blame]
Juha Hyttinen4896a402019-11-08 11:29:34 +02001/*
2==================================================================================
Abukar Mohamedbf6780b2020-03-19 13:08:53 +00003 Copyright (c) 2020 AT&T Intellectual Property.
4 Copyright (c) 2020 Nokia
Juha Hyttinen4896a402019-11-08 11:29:34 +02005
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17==================================================================================
18*/
Mohamed Abukar2e78e422019-06-02 11:45:52 +030019package main
20
Mohamed Abukar96be5092020-02-04 19:31:51 +020021import (
22 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
23 "net/http"
24)
Mohamed Abukar2e78e422019-06-02 11:45:52 +030025
Mohamed Abukar96be5092020-02-04 19:31:51 +020026// This could be defined in types.go
27type ExampleXapp struct {
Abukar Mohamedbf6780b2020-03-19 13:08:53 +000028 msgChan chan *xapp.RMRParams
29 stats map[string]xapp.Counter
30 rmrReady bool
31 waitForSdl bool
Mohamed Abukar2e78e422019-06-02 11:45:52 +030032}
33
Mohamed Abukar96be5092020-02-04 19:31:51 +020034func (e *ExampleXapp) handleRICIndication(ranName string, r *xapp.RMRParams) {
35 // Just update metrics and store RMR message payload to SDL
36 e.stats["E2APIndicationsRx"].Inc()
Mohamed Abukar2e78e422019-06-02 11:45:52 +030037
Mohamed Abukar96be5092020-02-04 19:31:51 +020038 xapp.Sdl.Store("myKey", r.Payload)
39}
40
41func (e *ExampleXapp) handleRICExampleMessage(ranName string, r *xapp.RMRParams) {
42 // Just update metrics and echo the message back (update the message type)
43 e.stats["RICExampleMessageRx"].Inc()
44
45 r.Mtype = r.Mtype + 1
46 if ok := xapp.Rmr.SendMsg(r); !ok {
47 xapp.Logger.Info("Rmr.SendMsg failed ...")
48 }
49}
50
51func (e *ExampleXapp) messageLoop() {
52 for {
53 msg := <-e.msgChan
54 id := xapp.Rmr.GetRicMessageName(msg.Mtype)
55 defer xapp.Rmr.Free(msg.Mbuf)
56
57 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)
58
59 switch id {
60 case "RIC_INDICATION":
61 e.handleRICIndication(msg.Meid.RanName, msg)
62 case "RIC_EXAMPLE_MESSAGE":
63 e.handleRICExampleMessage(msg.Meid.RanName, msg)
64 default:
65 xapp.Logger.Info("Unknown Message Type '%d', discarding", msg.Mtype)
66 }
67 }
68}
69
70func (e *ExampleXapp) Consume(rp *xapp.RMRParams) (err error) {
71 e.msgChan <- rp
72 return
73}
74
75func (u *ExampleXapp) TestRestHandler(w http.ResponseWriter, r *http.Request) {
76 xapp.Logger.Info("TestRestHandler called!")
77}
78
79func (u *ExampleXapp) ConfigChangeHandler(f string) {
80 xapp.Logger.Info("Config file changed, do something meaningful!")
81}
82
83func (u *ExampleXapp) StatusCB() bool {
84 xapp.Logger.Info("Status callback called, do something meaningful!")
85 return true
86}
87
88func (e *ExampleXapp) Run() {
89 // Set MDC (read: name visible in the logs)
90 xapp.Logger.SetMdc("example-xapp", "0.1.2")
91
92 // Register various callback functions for application management
93 xapp.SetReadyCB(func(d interface{}) { e.rmrReady = true }, true)
94 xapp.AddConfigChangeListener(e.ConfigChangeHandler)
95 xapp.Resource.InjectStatusCb(e.StatusCB)
96
97 // Inject own REST handler for testing purpose
98 xapp.Resource.InjectRoute("/ric/v1/testing", e.TestRestHandler, "POST")
99
100 go e.messageLoop()
Abukar Mohamedbf6780b2020-03-19 13:08:53 +0000101 xapp.RunWithParams(e, e.waitForSdl)
Mohamed Abukar96be5092020-02-04 19:31:51 +0200102}
103
104func GetMetricsOpts() []xapp.CounterOpts {
105 return []xapp.CounterOpts{
106 {Name: "RICIndicationsRx", Help: "The total number of RIC inidcation events received"},
107 {Name: "RICExampleMessageRx", Help: "The total number of RIC example messages received"},
108 }
109}
110
Abukar Mohamedbf6780b2020-03-19 13:08:53 +0000111func NewExampleXapp(rmrReady bool) *ExampleXapp {
Mohamed Abukar96be5092020-02-04 19:31:51 +0200112 metrics := GetMetricsOpts()
113 return &ExampleXapp{
Abukar Mohamedbf6780b2020-03-19 13:08:53 +0000114 msgChan: make(chan *xapp.RMRParams),
115 stats: xapp.Metric.RegisterCounterGroup(metrics, "ExampleXapp"),
116 rmrReady: rmrReady,
117 waitForSdl: xapp.Config.GetBool("db.waitForSdl"),
Mohamed Abukar96be5092020-02-04 19:31:51 +0200118 }
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300119}
120
121func main() {
Abukar Mohamedbf6780b2020-03-19 13:08:53 +0000122 NewExampleXapp(true).Run()
Mohamed Abukar2e78e422019-06-02 11:45:52 +0300123}