Juha Hyttinen | 4896a40 | 2019-11-08 11:29:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
Abukar Mohamed | bf6780b | 2020-03-19 13:08:53 +0000 | [diff] [blame] | 3 | Copyright (c) 2020 AT&T Intellectual Property. |
| 4 | Copyright (c) 2020 Nokia |
Juha Hyttinen | 4896a40 | 2019-11-08 11:29:34 +0200 | [diff] [blame] | 5 | |
| 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 Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 19 | package main |
| 20 | |
Mohamed Abukar | 96be509 | 2020-02-04 19:31:51 +0200 | [diff] [blame] | 21 | import ( |
| 22 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
| 23 | "net/http" |
| 24 | ) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 25 | |
Mohamed Abukar | 96be509 | 2020-02-04 19:31:51 +0200 | [diff] [blame] | 26 | // This could be defined in types.go |
| 27 | type ExampleXapp struct { |
Abukar Mohamed | bf6780b | 2020-03-19 13:08:53 +0000 | [diff] [blame] | 28 | msgChan chan *xapp.RMRParams |
| 29 | stats map[string]xapp.Counter |
| 30 | rmrReady bool |
| 31 | waitForSdl bool |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 32 | } |
| 33 | |
Mohamed Abukar | 96be509 | 2020-02-04 19:31:51 +0200 | [diff] [blame] | 34 | func (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 Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 37 | |
Mohamed Abukar | 96be509 | 2020-02-04 19:31:51 +0200 | [diff] [blame] | 38 | xapp.Sdl.Store("myKey", r.Payload) |
| 39 | } |
| 40 | |
| 41 | func (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 | |
| 51 | func (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 | |
| 70 | func (e *ExampleXapp) Consume(rp *xapp.RMRParams) (err error) { |
| 71 | e.msgChan <- rp |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | func (u *ExampleXapp) TestRestHandler(w http.ResponseWriter, r *http.Request) { |
| 76 | xapp.Logger.Info("TestRestHandler called!") |
| 77 | } |
| 78 | |
| 79 | func (u *ExampleXapp) ConfigChangeHandler(f string) { |
| 80 | xapp.Logger.Info("Config file changed, do something meaningful!") |
| 81 | } |
| 82 | |
| 83 | func (u *ExampleXapp) StatusCB() bool { |
| 84 | xapp.Logger.Info("Status callback called, do something meaningful!") |
| 85 | return true |
| 86 | } |
| 87 | |
| 88 | func (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 Mohamed | bf6780b | 2020-03-19 13:08:53 +0000 | [diff] [blame] | 101 | xapp.RunWithParams(e, e.waitForSdl) |
Mohamed Abukar | 96be509 | 2020-02-04 19:31:51 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | func 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 Mohamed | bf6780b | 2020-03-19 13:08:53 +0000 | [diff] [blame] | 111 | func NewExampleXapp(rmrReady bool) *ExampleXapp { |
Mohamed Abukar | 96be509 | 2020-02-04 19:31:51 +0200 | [diff] [blame] | 112 | metrics := GetMetricsOpts() |
| 113 | return &ExampleXapp{ |
Abukar Mohamed | bf6780b | 2020-03-19 13:08:53 +0000 | [diff] [blame] | 114 | msgChan: make(chan *xapp.RMRParams), |
| 115 | stats: xapp.Metric.RegisterCounterGroup(metrics, "ExampleXapp"), |
| 116 | rmrReady: rmrReady, |
| 117 | waitForSdl: xapp.Config.GetBool("db.waitForSdl"), |
Mohamed Abukar | 96be509 | 2020-02-04 19:31:51 +0200 | [diff] [blame] | 118 | } |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | func main() { |
Abukar Mohamed | bf6780b | 2020-03-19 13:08:53 +0000 | [diff] [blame] | 122 | NewExampleXapp(true).Run() |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 123 | } |