-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnmic.go
76 lines (66 loc) · 1.67 KB
/
gnmic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gnmic
import (
"context"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/orb-community/orb-gnmic/config"
"github.com/orb-community/orb-gnmic/runner"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
type RunnerInfo struct {
Policy config.Policy
Instance runner.Runner
}
type Gnmic struct {
logger *zap.Logger
conf *config.Config
stat config.Status
policies map[string]RunnerInfo
policiesDir string
ctx context.Context
cancelFunction context.CancelFunc
router *gin.Engine
capabilities []byte
}
func New(logger *zap.Logger, c *config.Config) (Gnmic, error) {
return Gnmic{logger: logger, conf: c, policies: make(map[string]RunnerInfo)}, nil
}
func (o *Gnmic) Start(ctx context.Context, cancelFunc context.CancelFunc) error {
o.stat.StartTime = time.Now()
o.ctx = context.WithValue(ctx, "routine", "orbGnmicRoutine")
o.cancelFunction = cancelFunc
var err error
o.policiesDir, err = os.MkdirTemp("", "policies")
if err != nil {
return err
}
o.capabilities, err = runner.GetCapabilities()
if err != nil {
return err
}
s := struct {
Version string
}{}
err = yaml.Unmarshal(o.capabilities, &s)
if err != nil {
return err
}
o.stat.Version = s.Version
if err = o.startServer(); err != nil {
return err
}
return nil
}
func (o *Gnmic) Stop(ctx context.Context) {
o.logger.Info("routine call for stop orb-gnmic", zap.Any("routine", ctx.Value("routine")))
defer os.RemoveAll(o.policiesDir)
o.cancelFunction()
}
func (o *Gnmic) RestartRunner(ctx context.Context, name string, reason string) error {
return nil
}
func (o *Gnmic) RestartAll(ctx context.Context, reason string) error {
return nil
}