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