add missing implementation for proxy
[govpp.git] / proxy / server.go
1 package proxy
2
3 import (
4         "fmt"
5         "log"
6         "reflect"
7         "time"
8
9         "git.fd.io/govpp.git/api"
10 )
11
12 type StatsRequest struct {
13         StatsType string
14 }
15
16 type StatsResponse struct {
17         SysStats   *api.SystemStats
18         NodeStats  *api.NodeStats
19         IfaceStats *api.InterfaceStats
20         ErrStats   *api.ErrorStats
21         BufStats   *api.BufferStats
22 }
23
24 // StatsRPC is a RPC server for proxying client request to api.StatsProvider.
25 type StatsRPC struct {
26         stats api.StatsProvider
27 }
28
29 // NewStatsRPC returns new StatsRPC to be used as RPC server
30 // proxying request to given api.StatsProvider.
31 func NewStatsRPC(stats api.StatsProvider) *StatsRPC {
32         return &StatsRPC{stats: stats}
33 }
34
35 func (s *StatsRPC) GetStats(req StatsRequest, resp *StatsResponse) error {
36         log.Printf("StatsRPC.GetStats - REQ: %+v", req)
37
38         switch req.StatsType {
39         case "system":
40                 resp.SysStats = new(api.SystemStats)
41                 return s.stats.GetSystemStats(resp.SysStats)
42         case "node":
43                 resp.NodeStats = new(api.NodeStats)
44                 return s.stats.GetNodeStats(resp.NodeStats)
45         case "interface":
46                 resp.IfaceStats = new(api.InterfaceStats)
47                 return s.stats.GetInterfaceStats(resp.IfaceStats)
48         case "error":
49                 resp.ErrStats = new(api.ErrorStats)
50                 return s.stats.GetErrorStats(resp.ErrStats)
51         case "buffer":
52                 resp.BufStats = new(api.BufferStats)
53                 return s.stats.GetBufferStats(resp.BufStats)
54         default:
55                 return fmt.Errorf("unknown stats type: %s", req.StatsType)
56         }
57 }
58
59 type BinapiRequest struct {
60         Msg      api.Message
61         IsMulti  bool
62         ReplyMsg api.Message
63 }
64
65 type BinapiResponse struct {
66         Msg  api.Message
67         Msgs []api.Message
68 }
69
70 type BinapiCompatibilityRequest struct {
71         MsgName string
72         Crc     string
73 }
74
75 type BinapiCompatibilityResponse struct {
76 }
77
78 type BinapiTimeoutRequest struct {
79         Timeout time.Duration
80 }
81
82 type BinapiTimeoutResponse struct {
83 }
84
85 // BinapiRPC is a RPC server for proxying client request to api.Channel.
86 type BinapiRPC struct {
87         binapi api.Channel
88 }
89
90 // NewBinapiRPC returns new BinapiRPC to be used as RPC server
91 // proxying request to given api.Channel.
92 func NewBinapiRPC(binapi api.Channel) *BinapiRPC {
93         return &BinapiRPC{binapi: binapi}
94 }
95
96 func (s *BinapiRPC) Invoke(req BinapiRequest, resp *BinapiResponse) error {
97         log.Printf("BinapiRPC.Invoke - REQ: %#v", req)
98
99         if req.IsMulti {
100                 multi := s.binapi.SendMultiRequest(req.Msg)
101                 for {
102                         // create new message in response of type ReplyMsg
103                         msg := reflect.New(reflect.TypeOf(req.ReplyMsg).Elem()).Interface().(api.Message)
104
105                         stop, err := multi.ReceiveReply(msg)
106                         if err != nil {
107                                 return err
108                         } else if stop {
109                                 break
110                         }
111
112                         resp.Msgs = append(resp.Msgs, msg)
113                 }
114         } else {
115                 // create new message in response of type ReplyMsg
116                 resp.Msg = reflect.New(reflect.TypeOf(req.ReplyMsg).Elem()).Interface().(api.Message)
117
118                 err := s.binapi.SendRequest(req.Msg).ReceiveReply(resp.Msg)
119                 if err != nil {
120                         return err
121                 }
122         }
123
124         return nil
125 }
126
127 func (s *BinapiRPC) SetTimeout(req BinapiTimeoutRequest, _ *BinapiTimeoutResponse) error {
128         log.Printf("BinapiRPC.SetTimeout - REQ: %#v", req)
129         s.binapi.SetReplyTimeout(req.Timeout)
130         return nil
131 }
132
133 func (s *BinapiRPC) Compatibility(req BinapiCompatibilityRequest, _ *BinapiCompatibilityResponse) error {
134         log.Printf("BinapiRPC.Compatiblity - REQ: %#v", req)
135         if val, ok := api.GetRegisteredMessages()[req.MsgName+"_"+req.Crc]; ok {
136                 return s.binapi.CheckCompatiblity(val)
137         }
138         return fmt.Errorf("compatibility check failed for the message: %s", req.MsgName+"_"+req.Crc)
139 }