blob: aaa8a4de3efcf2d335ef9784090074b48d0504ba [file] [log] [blame]
Stephane Barbarie35595062018-02-08 08:34:39 -05001package grpc
2
3import (
4 "net"
5
6 "context"
7 "github.com/opencord/voltha/ponsim/v2/common"
8 "github.com/opencord/voltha/ponsim/v2/core"
9 "github.com/opencord/voltha/ponsim/v2/grpc/nbi"
10 "github.com/opencord/voltha/ponsim/v2/grpc/sbi"
11 "github.com/opencord/voltha/protos/go/bal"
12 "github.com/opencord/voltha/protos/go/ponsim"
13 "github.com/opencord/voltha/protos/go/voltha"
14 "google.golang.org/grpc"
15 "google.golang.org/grpc/credentials"
16 "strconv"
17 "strings"
18)
19
20type GrpcServer struct {
21 gs *grpc.Server
22 address string
23 port int32
24 secure bool
25 services []func(*grpc.Server)
26
27 *GrpcSecurity
28}
29
30/*
31Instantiate a GRPC server data structure
32*/
33func NewGrpcServer(
34 address string,
35 port int32,
36 certs *GrpcSecurity,
37 secure bool,
38) *GrpcServer {
39 server := &GrpcServer{
40 address: address,
41 port: port,
42 secure: secure,
43 GrpcSecurity: certs,
44 }
45 return server
46}
47
48/*
49Start prepares the GRPC server and starts servicing requests
50*/
51func (s *GrpcServer) Start(ctx context.Context) {
52 host := strings.Join([]string{
53 s.address,
54 strconv.Itoa(int(s.port)),
55 }, ":")
56
57 lis, err := net.Listen("tcp", host)
58 if err != nil {
59 common.Logger().Fatalf("failed to listen: %v", err)
60 }
61
62 if s.secure {
63 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
64 if err != nil {
65 common.Logger().Fatalf("could not load TLS keys: %s", err)
66 }
67 s.gs = grpc.NewServer(grpc.Creds(creds))
68
69 } else {
70 common.Logger().Println("In DEFAULT\n")
71 s.gs = grpc.NewServer()
72 }
73
74 // Register all required services
75 for _, service := range s.services {
76 service(s.gs)
77 }
78
79 if err := s.gs.Serve(lis); err != nil {
80 common.Logger().Fatalf("failed to serve: %v\n", err)
81 }
82}
83
84/*
85Stop servicing GRPC requests
86*/
87func (s *GrpcServer) Stop() {
88 s.gs.Stop()
89}
90
91/*
92AddService appends a generic service request function
93*/
94func (s *GrpcServer) AddService(
95 registerFunction func(*grpc.Server, interface{}),
96 handler interface{},
97) {
98 s.services = append(s.services, func(gs *grpc.Server) { registerFunction(gs, handler) })
99}
100
101/*
102AddPonSimService appends service request functions for PonSim devices
103*/
104func (s *GrpcServer) AddPonSimService(device core.PonSimInterface) {
105 s.services = append(
106 s.services,
107 func(gs *grpc.Server) {
108 voltha.RegisterPonSimServer(gs, nbi.NewPonSimHandler(device))
109 },
110 )
111}
112
113/*
114AddCommonService appends service request functions common to all PonSim devices
115*/
116func (s *GrpcServer) AddCommonService(device core.PonSimInterface) {
117 s.services = append(
118 s.services,
119 func(gs *grpc.Server) {
120 ponsim.RegisterPonSimCommonServer(gs, sbi.NewPonSimCommonHandler(device))
121 },
122 )
123}
124
125/*
126AddOltService appends service request functions specific to OLT devices
127*/
128func (s *GrpcServer) AddOltService(device core.PonSimInterface) {
129 s.services = append(
130 s.services,
131 func(gs *grpc.Server) {
132 ponsim.RegisterPonSimOltServer(
133 gs,
134 sbi.NewPonSimOltHandler(device.(*core.PonSimOltDevice)),
135 )
136 },
137 )
138}
139
140/*
141AddXPonService appends service request functions specific to XPonSim
142*/
143func (s *GrpcServer) AddXPonService() {
144 s.services = append(
145 s.services,
146 func(gs *grpc.Server) {
147 voltha.RegisterXPonSimServer(gs, nbi.NewXPonSimHandler())
148 },
149 )
150}
151
152/*
153AddBalService appends service request functions specific to BAL
154*/
155func (s *GrpcServer) AddBalService() {
156 s.services = append(
157 s.services,
158 func(gs *grpc.Server) {
159 bal.RegisterBalServer(gs, nbi.NewBalHandler())
160 },
161 )
162}