Add models
[ric-plt/xapp-frame.git] / examples / example-xapp.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
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 */
19 package main
20
21 import (
22         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
23         "net/http"
24 )
25
26 // This could be defined in types.go
27 type ExampleXapp struct {
28         msgChan  chan *xapp.RMRParams
29         stats    map[string]xapp.Counter
30         appReady bool
31         rmrReady bool
32 }
33
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()
37
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()
101         xapp.Run(e)
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
111 func NewExampleXapp(appReady, rmrReady bool) *ExampleXapp {
112         metrics := GetMetricsOpts()
113         return &ExampleXapp{
114                 msgChan:  make(chan *xapp.RMRParams),
115                 stats:    xapp.Metric.RegisterCounterGroup(metrics, "ExampleXapp"),
116                 rmrReady: rmrReady,
117                 appReady: appReady,
118         }
119 }
120
121 func main() {
122         NewExampleXapp(true, false).Run()
123 }