blob: 488d470904384a207eb6233773174ebdc7d7dfd8 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package grpc
17
18import (
19 "context"
20 "fmt"
Scott Bakerce767002019-10-23 13:30:24 -070021 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Baker2c1c4822019-10-16 11:02:41 -070022 "google.golang.org/grpc"
Scott Baker104b67d2019-10-29 15:56:27 -070023 "google.golang.org/grpc/codes"
Scott Baker2c1c4822019-10-16 11:02:41 -070024 "google.golang.org/grpc/credentials"
Scott Baker104b67d2019-10-29 15:56:27 -070025 "google.golang.org/grpc/status"
Scott Baker2c1c4822019-10-16 11:02:41 -070026 "net"
27)
28
29/*
30To add a GRPC server to your existing component simply follow these steps:
31
321. Create a server instance by passing the host and port where it should run and optionally add certificate information
33
34 e.g.
35 s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false)
36
372. Create a function that will register your service with the GRPC server
38
39 e.g.
40 f := func(gs *grpc.Server) {
41 voltha.RegisterVolthaReadOnlyServiceServer(
42 gs,
43 core.NewReadOnlyServiceHandler(s.root),
44 )
45 }
46
473. Add the service to the server
48
49 e.g.
50 s.server.AddService(f)
51
524. Start the server
53
54 s.server.Start(ctx)
55*/
56
Scott Baker104b67d2019-10-29 15:56:27 -070057// Interface allows probes to be attached to server
58// A probe must support the IsReady() method
59type ReadyProbe interface {
60 IsReady() bool
61}
62
Scott Baker2c1c4822019-10-16 11:02:41 -070063type GrpcServer struct {
64 gs *grpc.Server
65 address string
66 port int
67 secure bool
68 services []func(*grpc.Server)
Scott Baker104b67d2019-10-29 15:56:27 -070069 probe ReadyProbe // optional
Scott Baker2c1c4822019-10-16 11:02:41 -070070
71 *GrpcSecurity
72}
73
74/*
75Instantiate a GRPC server data structure
76*/
77func NewGrpcServer(
78 address string,
79 port int,
80 certs *GrpcSecurity,
81 secure bool,
82) *GrpcServer {
83 server := &GrpcServer{
84 address: address,
85 port: port,
86 secure: secure,
87 GrpcSecurity: certs,
88 }
89 return server
90}
91
92/*
93Start prepares the GRPC server and starts servicing requests
94*/
95func (s *GrpcServer) Start(ctx context.Context) {
96
97 host := fmt.Sprintf("%s:%d", s.address, s.port)
98
99 lis, err := net.Listen("tcp", host)
100 if err != nil {
101 log.Fatalf("failed to listen: %v", err)
102 }
103
104 if s.secure && s.GrpcSecurity != nil {
105 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
106 if err != nil {
107 log.Fatalf("could not load TLS keys: %s", err)
108 }
Scott Baker104b67d2019-10-29 15:56:27 -0700109 s.gs = grpc.NewServer(grpc.Creds(creds),
110 withServerUnaryInterceptor(s))
Scott Baker2c1c4822019-10-16 11:02:41 -0700111
112 } else {
113 log.Info("starting-insecure-grpc-server")
Scott Baker104b67d2019-10-29 15:56:27 -0700114 s.gs = grpc.NewServer(withServerUnaryInterceptor(s))
Scott Baker2c1c4822019-10-16 11:02:41 -0700115 }
116
117 // Register all required services
118 for _, service := range s.services {
119 service(s.gs)
120 }
121
122 if err := s.gs.Serve(lis); err != nil {
123 log.Fatalf("failed to serve: %v\n", err)
124 }
125}
126
Scott Baker104b67d2019-10-29 15:56:27 -0700127// Attach a readiness probe to the server.
128// If the probe returns NotReady, the server will return UNAVAILABLE
129func (s *GrpcServer) AttachReadyProbe(p ReadyProbe) {
130 s.probe = p
131}
132
133func withServerUnaryInterceptor(s *GrpcServer) grpc.ServerOption {
134 return grpc.UnaryInterceptor(mkServerInterceptor(s))
135}
136
137// Make a serverInterceptor for the given GrpcServer
138// This interceptor will check whether there is an attached probe,
139// and if that probe indicates NotReady, then an UNAVAILABLE
140// response will be returned.
141func mkServerInterceptor(s *GrpcServer) func(ctx context.Context,
142 req interface{},
143 info *grpc.UnaryServerInfo,
144 handler grpc.UnaryHandler) (interface{}, error) {
145
146 return func(ctx context.Context,
147 req interface{},
148 info *grpc.UnaryServerInfo,
149 handler grpc.UnaryHandler) (interface{}, error) {
150
151 if (s.probe != nil) && (!s.probe.IsReady()) {
152 log.Warnf("Grpc request received while not ready %v", req)
153 return nil, status.Error(codes.Unavailable, "system is not ready")
154 }
155
156 // Calls the handler
157 h, err := handler(ctx, req)
158
159 return h, err
160 }
161}
162
Scott Baker2c1c4822019-10-16 11:02:41 -0700163/*
164Stop servicing GRPC requests
165*/
166func (s *GrpcServer) Stop() {
167 if s.gs != nil {
168 s.gs.Stop()
169 }
170}
171
172/*
173AddService appends a generic service request function
174*/
175func (s *GrpcServer) AddService(
176 registerFunction func(*grpc.Server),
177) {
178 s.services = append(s.services, registerFunction)
179}