Fetch RIC active alarms via O1
[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
37 type O1Agent struct {
38         rmrReady  bool
39         nbiClient *nbi.Nbi
40         sigChan   chan os.Signal
41 }
42
43 func (o O1Agent) Consume(rp *xapp.RMRParams) (err error) {
44         xapp.Logger.Debug("Message received!")
45         return nil
46 }
47
48 func (o *O1Agent) ConfigChangeHandler(f string) {
49         xapp.Logger.Debug("Config changed!")
50 }
51
52 func (o *O1Agent) StatusCB() bool {
53         if !o.rmrReady {
54                 xapp.Logger.Info("RMR not ready yet!")
55         }
56         return true
57 }
58
59 func (o *O1Agent) Run() {
60         xapp.Logger.SetMdc("o1agent", fmt.Sprintf("%s:%s", Version, Hash))
61         xapp.SetReadyCB(func(d interface{}) { o.rmrReady = true }, true)
62         xapp.AddConfigChangeListener(o.ConfigChangeHandler)
63         xapp.Resource.InjectStatusCb(o.StatusCB)
64
65         signal.Notify(o.sigChan, syscall.SIGINT, syscall.SIGTERM)
66         go o.Sighandler()
67
68         xapp.Run(o)
69 }
70
71 func (o *O1Agent) Sighandler() {
72         xapp.Logger.Info("Signal handler installed!")
73
74         <-o.sigChan
75         o.nbiClient.Stop()
76         os.Exit(1)
77 }
78
79 func NewO1Agent() *O1Agent {
80         appmgrAddr := viper.GetString("sbi.appmgrAddr")
81         alertmgrAddr := viper.GetString("sbi.alertmgrAddr")
82         timeout := viper.GetInt("sbi.timeout")
83
84         sbiClient := sbi.NewSBIClient(appmgrAddr, alertmgrAddr, timeout)
85
86         return &O1Agent{
87                 rmrReady:  false,
88                 nbiClient: nbi.NewNbi(sbiClient),
89                 sigChan:   make(chan os.Signal, 1),
90         }
91 }
92
93 func main() {
94         o1Agent := NewO1Agent()
95
96         if ok := o1Agent.nbiClient.Start(); !ok {
97                 xapp.Logger.Error("NBI initialization failed!")
98                 return
99         }
100
101         o1Agent.Run()
102 }