Using xapp-frame v0.8.1
[ric-plt/o1.git] / agent / cmd / o1agent.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
20 package main
21
22 import (
23         "fmt"
24         "os"
25         "os/signal"
26         "syscall"
27
28         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
29         "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/nbi"
30         "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi"
31         "github.com/spf13/viper"
32 )
33
34 var Version string
35 var Hash string
36 var osExit = os.Exit
37
38 type O1Agent struct {
39         rmrReady  bool
40         nbiClient *nbi.Nbi
41         sigChan   chan os.Signal
42 }
43
44 func (o O1Agent) Consume(rp *xapp.RMRParams) (err error) {
45         xapp.Logger.Debug("Message received!")
46         return nil
47 }
48
49 func (o *O1Agent) ConfigChangeHandler(f string) {
50         xapp.Logger.Debug("Config changed!")
51 }
52
53 func (o *O1Agent) StatusCB() bool {
54         if !o.rmrReady {
55                 xapp.Logger.Info("RMR not ready yet!")
56         }
57         return true
58 }
59
60 func (o *O1Agent) Run() {
61         xapp.Logger.SetFormat(0)
62         xapp.Logger.SetMdc("o1agent", fmt.Sprintf("%s:%s", Version, Hash))
63         xapp.SetReadyCB(func(d interface{}) { o.rmrReady = true }, true)
64         xapp.AddConfigChangeListener(o.ConfigChangeHandler)
65         xapp.Resource.InjectStatusCb(o.StatusCB)
66
67         signal.Notify(o.sigChan, syscall.SIGINT, syscall.SIGTERM)
68         go o.Sighandler()
69
70         xapp.Run(o)
71 }
72
73 func (o *O1Agent) Sighandler() {
74         xapp.Logger.Info("Signal handler installed!")
75
76         <-o.sigChan
77         o.nbiClient.Stop()
78         osExit(1)
79 }
80
81 func NewO1Agent() *O1Agent {
82         appmgrAddr := viper.GetString("sbi.appmgrAddr")
83         alertmgrAddr := viper.GetString("sbi.alertmgrAddr")
84         timeout := viper.GetInt("sbi.timeout")
85
86         sbiClient := sbi.NewSBIClient(appmgrAddr, alertmgrAddr, timeout)
87
88         return &O1Agent{
89                 rmrReady:  false,
90                 nbiClient: nbi.NewNbi(sbiClient),
91                 sigChan:   make(chan os.Signal, 1),
92         }
93 }
94
95 func main() {
96         o1Agent := NewO1Agent()
97
98         if ok := o1Agent.nbiClient.Start(); !ok {
99                 xapp.Logger.Error("NBI initialization failed!")
100                 return
101         }
102
103         o1Agent.Run()
104 }